summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2022-07-22 12:50:12 -0300
committerSebastian <sebasjm@gmail.com>2022-07-22 12:50:12 -0300
commit637b75cf74fccedb63c4a98c00c692fb5beafd73 (patch)
treee18b34b9801fcc26c123db345693df4f5b7741db
parenteb18c56078a047a2aef35a8f74b31ceebdb7928c (diff)
downloadmch2022-637b75cf74fccedb63c4a98c00c692fb5beafd73.tar.gz
mch2022-637b75cf74fccedb63c4a98c00c692fb5beafd73.tar.bz2
mch2022-637b75cf74fccedb63c4a98c00c692fb5beafd73.zip
just getting started, wifi and http client working
-rw-r--r--main/include/main.h1
-rw-r--r--main/main.c113
2 files changed, 106 insertions, 8 deletions
diff --git a/main/include/main.h b/main/include/main.h
index 14e997f..bb46bf3 100644
--- a/main/include/main.h
+++ b/main/include/main.h
@@ -34,6 +34,7 @@
#include "esp_netif.h"
#include "esp_tls.h"
#include "esp_crt_bundle.h"
+#include "esp_wifi.h"
#include "esp_http_client.h"
diff --git a/main/main.c b/main/main.c
index 6d6a535..8d8d4c9 100644
--- a/main/main.c
+++ b/main/main.c
@@ -151,12 +151,8 @@ esp_err_t _http_event_handler(esp_http_client_event_t *evt)
return ESP_OK;
}
-#define MAX_HTTP_OUTPUT_BUFFER 2048
-void app_main()
+void initialize_things(void)
{
-
- ESP_LOGI(TAG, "Starting app!");
-
// Initialize the screen, the I2C and the SPI busses.
bsp_init();
@@ -173,16 +169,115 @@ void app_main()
nvs_flash_init();
// Initialize WiFi. This doesn't connect to Wifi yet.
- wifi_init();
+ // wifi_init();
+ // Set the background to an initial color
int hue = esp_random() & 255;
pax_col_t col = pax_col_hsv(hue, 255 /*saturation*/, 255 /*brighness*/);
pax_background(&buf, col);
+}
+
+#define MAX_HTTP_OUTPUT_BUFFER 2048
+
+/* Set the SSID and Password via project configuration, or can set directly here */
+#define DEFAULT_SSID "MCH2022-open"
+#define DEFAULT_PWD ""
+
+#if CONFIG_EXAMPLE_WIFI_ALL_CHANNEL_SCAN
+#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
+#elif CONFIG_EXAMPLE_WIFI_FAST_SCAN
+#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
+#else
+#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
+#endif /*CONFIG_EXAMPLE_SCAN_METHOD*/
+
+#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
+#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
+#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
+#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
+#else
+#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
+#endif /*CONFIG_EXAMPLE_SORT_METHOD*/
+
+#if CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD
+#define DEFAULT_RSSI CONFIG_EXAMPLE_FAST_SCAN_MINIMUM_SIGNAL
+#if CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_OPEN
+#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
+#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WEP
+#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
+#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA
+#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
+#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA2
+#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
+#else
+#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
+#endif
+#else
+#define DEFAULT_RSSI -127
+#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
+#endif
+/*CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD*/
+
+static void event_handler(void *arg, esp_event_base_t event_base,
+ int32_t event_id, void *event_data)
+{
+ if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
+ {
+ esp_wifi_connect();
+ }
+ else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
+ {
+ esp_wifi_connect();
+ }
+ else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
+ {
+ ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
+ ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
+ }
+}
+
+/* Initialize Wi-Fi as sta and set scan method */
+static void fast_scan(void)
+{
+ ESP_ERROR_CHECK(esp_netif_init());
+ ESP_ERROR_CHECK(esp_event_loop_create_default());
+
+ wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
+ ESP_ERROR_CHECK(esp_wifi_init(&cfg));
+
+ ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
+ ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL));
+
+ // Initialize default station as network interface instance (esp-netif)
+ esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
+ assert(sta_netif);
+
+ // Initialize and start WiFi
+ wifi_config_t wifi_config = {
+ .sta = {
+ .ssid = DEFAULT_SSID,
+ .password = DEFAULT_PWD,
+ .scan_method = DEFAULT_SCAN_METHOD,
+ .sort_method = DEFAULT_SORT_METHOD,
+ .threshold.rssi = DEFAULT_RSSI,
+ .threshold.authmode = DEFAULT_AUTHMODE,
+ },
+ };
+ ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
+ ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
+ ESP_ERROR_CHECK(esp_wifi_start());
+}
+
+void app_main()
+{
+
+ ESP_LOGI(TAG, "Starting app!");
+ initialize_things();
+ fast_scan();
// This text is shown on screen.
- char *text = "GNU Taler Merchant!";
+ char *text = "Just starting!";
- char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
// Pick the font (Saira is the only one that looks nice in this size).
const pax_font_t *font = pax_font_saira_condensed;
{
@@ -211,6 +306,8 @@ void app_main()
while (1)
{
+ char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
+
// POST
// const char *post_data = "{\"order\":{\"summary\":\"s\",\"amount\":\"ARS:1\"}}";
// esp_http_client_set_url(client, "https://merchant-backend.taler.ar/instances/mch2022/private/orders");