Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.

Wed, 19 May 2021 14:35:48 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 19 May 2021 14:35:48 +0200
changeset 94
87aa80b8e452
parent 93
24cb415af787
child 95
c02e12e9c884

Changed HTTP tasks startup code to prevent a race condition during startup. Upgraded the WiFi code to support the new esp_netif api. Removed the AP mode, WiFi is now STA (station) only.

main/task_http.c file | annotate | diff | comparison | revisions
main/task_wifi.c file | annotate | diff | comparison | revisions
main/task_wifi.h file | annotate | diff | comparison | revisions
sdkconfig file | annotate | diff | comparison | revisions
--- a/main/task_http.c	Tue May 18 16:52:23 2021 +0200
+++ b/main/task_http.c	Wed May 19 14:35:48 2021 +0200
@@ -552,8 +552,7 @@
     struct netconn	*conn, *newconn;
     static err_t	err;
 
-    ESP_LOGI(TAG, "Start http server_task");
-    client_queue = xQueueCreate(client_queue_size,sizeof(struct netconn*));
+    ESP_LOGI(TAG, "Start HTTP/web server");
 
     conn = netconn_new(NETCONN_TCP);
     netconn_bind(conn,NULL,80);
@@ -569,7 +568,7 @@
 	vTaskDelay(5 / portTICK_PERIOD_MS);
     } while (err == ERR_OK);
 
-    ESP_LOGE(TAG, "Stopping http server_task");
+    ESP_LOGE(TAG, "Stopping HTTP/web server");
     netconn_close(conn);
     netconn_delete(conn);
     vTaskDelete(NULL);
@@ -586,7 +585,7 @@
 
     ESP_LOGI(TAG, "Start Queue task");
     for(;;) {
-	xQueueReceive(client_queue, &conn, portMAX_DELAY);
+	xQueueReceive(client_queue, &(conn), portMAX_DELAY);
 	if (!conn)
 	    continue;
 	http_serve(conn);
@@ -602,8 +601,14 @@
 {
     ESP_LOGI(TAG, "Start HTTP/Websocket server");
 
+    client_queue = xQueueCreate(client_queue_size,sizeof(struct netconn*));
+    if (client_queue == 0) {
+        ESP_LOGE(TAG, "Failed to create client_queue");
+        return;
+    }
+
     ws_server_start();
-    xTaskCreate(&task_HTTPserver, "HTTPserver", 4000, NULL, 9, &xTaskHTTP);
-    xTaskCreate(&task_Queue,      "Queue",      5000, NULL, 6, &xTaskQueue);
+    xTaskCreate(&task_HTTPserver, "HTTPserver", 5000, NULL, 9, &xTaskHTTP);
+    xTaskCreate(&task_Queue,      "Queue",      6000, NULL, 6, &xTaskQueue);
 }
 
--- a/main/task_wifi.c	Tue May 18 16:52:23 2021 +0200
+++ b/main/task_wifi.c	Wed May 19 14:35:48 2021 +0200
@@ -1,7 +1,7 @@
 /**
  * @file task_wifi.c
  * @brief WiFi task. Connects to a known Access Point. If we know more then
- *        one AP, try to connect all of them until it succeeds (Not yet written).
+ *        one AP, try to connect all of them until it succeeds.
  */
 
 
@@ -17,6 +17,8 @@
 wifi_ap_record_t		*accessp_records;		///< [MAX_AP_NUM] records array with scan results.
 wifi_config_t			*task_wifi_ConfigSTA = NULL;	///< Current STA configuration.
 WIFI_State			*wifi_state = NULL;		///< Public state for other tasks.
+esp_netif_t			*sta_netif = NULL;		///< Station interface
+
 
 wifi_scan_config_t scan_config = {				///< WiFi scanner configuration.
 	.ssid = 0,
@@ -43,7 +45,6 @@
 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2;			///< When set, means a client requested to connect to an access point.
 
 const int TASK_WIFI_HAS_IP = BIT3;				///< Indicate that we have an IP address
-const int TASK_WIFI_AP_STARTED = BIT4;				///< Indicate that the SoftAP is started
 const int TASK_WIFI_STA_FAILED = BIT5;				///< Indicate that we could not get a connection to AP as station.
 const int TASK_WIFI_STA_DISCONNECTED = BIT6;			///< Indicate that we are disconnected from an ap station.
 const int TASK_WIFI_STA_CONNECTED = BIT7;			///< Indicate that we are connected to AP as station, flip of BIT6.
@@ -156,7 +157,7 @@
 
 	case WIFI_EVENT_STA_START:
 		// Set the configured hostname for the dhcp client.
-		ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname));   
+		ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, config.hostname));
 		esp_wifi_connect();
 		break;
 
@@ -236,55 +237,6 @@
 		}
 		break;
 	}
-    	case WIFI_EVENT_AP_START:
-    		xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    wifi_state->AP_active = true;
-		    wifi_state->AP_clients = 0;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_START");
-                }
-		break;
-
-	case WIFI_EVENT_AP_STOP:
-		xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    wifi_state->AP_active = false;
-		    wifi_state->AP_clients = 0;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STOP");
-                }
-		break;
-
-    	case WIFI_EVENT_AP_STACONNECTED: {
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    wifi_state->AP_clients++;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STACONNECTED");
-                }
-		wifi_event_ap_staconnected_t* staconnected = (wifi_event_ap_staconnected_t*) event_data;
-            	ESP_LOGI(TAG, "Event AP connected, mac:" MACSTR ", aid:%d, conns:%d",
-                       MAC2STR(staconnected->mac), staconnected->aid, wifi_state->AP_clients);
-		break;
-	}
-    	case WIFI_EVENT_AP_STADISCONNECTED: {
-		if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
-		    if (wifi_state->AP_clients > 0)
-		    	wifi_state->AP_clients--;
-		    else
-		    	wifi_state->AP_clients = 0;
-		    xSemaphoreGive(xSemaphoreWiFi);
-		} else {
-                    ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STADISCONNECTED");
-                }
-		wifi_event_ap_stadisconnected_t* stadisconnected = (wifi_event_ap_stadisconnected_t*) event_data;
-            	ESP_LOGI(TAG, "Event AP disconnected, mac:" MACSTR ", aid:%d, conns:%d",
-                       MAC2STR(stadisconnected->mac), stadisconnected->aid, wifi_state->AP_clients);
-		break;
-	}
 	default:
 		ESP_LOGW(TAG, "Unknown WiFi event %d", event_id);
         	break;
@@ -302,9 +254,9 @@
 		ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
                 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
                     wifi_state->STA_online = true;
-                    snprintf(wifi_state->STA_ip, 16, "%s", ip4addr_ntoa(&event->ip_info.ip));
-                    snprintf(wifi_state->STA_nm, 16, "%s", ip4addr_ntoa(&event->ip_info.netmask));
-                    snprintf(wifi_state->STA_gw, 16, "%s", ip4addr_ntoa(&event->ip_info.gw));
+                    snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip));
+                    snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask));
+                    snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw));
                     xSemaphoreGive(xSemaphoreWiFi);
                 } else {
                     ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP");
@@ -347,7 +299,7 @@
                 break;
 
 	case IP_EVENT_AP_STAIPASSIGNED:
-		ESP_LOGD(TAG, "IP_EVENT_AP_STAIPASSIGNED");
+		ESP_LOGI(TAG, "IP_EVENT_AP_STAIPASSIGNED");
 		break;
 
         default:
@@ -385,7 +337,7 @@
      * Initialize NVS
      */
     ret = nvs_flash_init();
-    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
+    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
 	ESP_ERROR_CHECK(nvs_flash_erase());
 	ret = nvs_flash_init();
     }
@@ -394,8 +346,10 @@
     /* event handler and event group for the wifi driver */
     xEventGroupWifi = xEventGroupCreate();
     /* initialize the tcp stack */
-    tcpip_adapter_init();
+    ESP_ERROR_CHECK(esp_netif_init());
     ESP_ERROR_CHECK(esp_event_loop_create_default());
+    sta_netif = esp_netif_create_default_wifi_sta();
+    assert(sta_netif);
 
     /*
      * memory allocation of objects used by the task 
@@ -406,70 +360,26 @@
 
     xSemaphoreWiFi = xSemaphoreCreateMutex();
     wifi_state = malloc(sizeof(WIFI_State));
-    wifi_state->AP_clients = 0;
-    wifi_state->AP_active = false;
     wifi_state->STA_connected = false;
     wifi_state->STA_online = false;
     wifi_state->STA_rssi = 0;
 
     /*
-     * start the softAP access point
-     * stop DHCP server
-     */
-    ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
-
-    /*
-     * Assign a static IP to the AP network interface
-     */
-    tcpip_adapter_ip_info_t info;
-    memset(&info, 0x00, sizeof(info));
-    IP4_ADDR(&info.ip, 192, 168, 1, 1);
-    IP4_ADDR(&info.gw, 192, 168, 1, 1);
-    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
-    ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
-
-    /* start dhcp server */
-    ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
-    ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1");
-
-    /* start dhcp client */
-    ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
-
-    /*
-     * init wifi as station + access point
+     * init wifi as station
      */
     wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
     ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
 
-    ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL) );
-    ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID /*IP_EVENT_STA_GOT_IP*/, &got_ip_event_handler, NULL) );
+    esp_event_handler_instance_t instance_any_id;
+    esp_event_handler_instance_t instance_got_ip;
+    ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) );
+    ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) );
 
     ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
-    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));
-    ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap_bandwidth));
+    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
 
-    /* configure the softAP and start it */
-    wifi_config_t ap_config = {
-	.ap = {
-	    .ssid_len = 0,
-	    .channel = config.ap_channel,
-	    .authmode = WIFI_AUTH_WPA2_PSK,
-	    .ssid_hidden = config.ap_ssid_hidden,
-	    .max_connection = AP_MAX_CONNECTIONS,
-	    .beacon_interval = 100,
-	},
-    };
-    memcpy(ap_config.ap.ssid, config.ap_ssid , sizeof(config.ap_ssid));
-    memcpy(ap_config.ap.password, config.ap_pwd, sizeof(config.ap_pwd));
-    ret = esp_wifi_set_config(WIFI_IF_AP, &ap_config);
-    if (ret != ESP_OK) {
-	ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret);
-    }
     ESP_ERROR_CHECK(esp_wifi_start());
 
-    ESP_LOGI(TAG, "AP start ssid:`%s' pwd:`%s' channel:%d, hidden:%s", 
-		    ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no");
-
     /*
      * try to get access to previously saved wifi
      */
@@ -481,7 +391,6 @@
      * Wait for access point to start
      */
     xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
-    xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY );
     EventBits_t uxBits;
 
     for(;;) {
@@ -653,14 +562,14 @@
 			wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */;
 			if (wconfig) {
 
-			    tcpip_adapter_ip_info_t ip_info;
-			    ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
+			    esp_netif_ip_info_t ip_info;
+			    ESP_ERROR_CHECK(esp_netif_get_ip_info(sta_netif, &ip_info));
 			    char ip[IP4ADDR_STRLEN_MAX];
 			    char gw[IP4ADDR_STRLEN_MAX];
 			    char netmask[IP4ADDR_STRLEN_MAX];
-			    strcpy(ip, ip4addr_ntoa(&ip_info.ip));
-			    strcpy(netmask, ip4addr_ntoa(&ip_info.netmask));
-			    strcpy(gw, ip4addr_ntoa(&ip_info.gw));
+			    sprintf(ip, IPSTR, IP2STR(&ip_info.ip));
+			    sprintf(netmask, IPSTR, IP2STR(&ip_info.netmask));
+			    sprintf(gw, IPSTR, IP2STR(&ip_info.gw));
 			    TFT_setFont(DEFAULT_FONT, NULL);
 			    _fg = TFT_WHITE;
 			    TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 40);
--- a/main/task_wifi.h	Tue May 18 16:52:23 2021 +0200
+++ b/main/task_wifi.h	Wed May 19 14:35:48 2021 +0200
@@ -17,18 +17,11 @@
  */
 #define MAX_AP_NUM 			10
 
-/** 
- * @brief Defines access point's maximum number of clients. 
- */
-#define AP_MAX_CONNECTIONS 		4
-
 
 /**
  * @brief Structure containing the information of the driver task.
  */
 typedef struct {
-    bool		AP_active;		///< Is the AP active.
-    uint8_t		AP_clients;		///< Connected AP clients.
     bool		STA_connected;		///< Connected to AP as station.
     bool		STA_online;		///< Connected and online.
     char		STA_ssid[33];		///< Connected to this SSID.
--- a/sdkconfig	Tue May 18 16:52:23 2021 +0200
+++ b/sdkconfig	Wed May 19 14:35:48 2021 +0200
@@ -121,8 +121,10 @@
 #
 # BrewBoard Configuration
 #
-# CONFIG_TEMP_SENSORS_ONEWIRE is not set
-CONFIG_TEMP_SENSORS_SIMULATOR=y
+CONFIG_TEMP_SENSORS_ONEWIRE=y
+# CONFIG_TEMP_SENSORS_SIMULATOR is not set
+CONFIG_ONE_WIRE_MLT=27
+CONFIG_ONE_WIRE_HLT=26
 CONFIG_SSR_MLT_GPIO=32
 CONFIG_SSR_HLT_GPIO=33
 CONFIG_SSR_PUMP_GPIO=12
@@ -141,10 +143,11 @@
 # CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE is not set
 # CONFIG_COMPILER_CXX_EXCEPTIONS is not set
 # CONFIG_COMPILER_CXX_RTTI is not set
-CONFIG_COMPILER_STACK_CHECK_MODE_NONE=y
-# CONFIG_COMPILER_STACK_CHECK_MODE_NORM is not set
+# CONFIG_COMPILER_STACK_CHECK_MODE_NONE is not set
+CONFIG_COMPILER_STACK_CHECK_MODE_NORM=y
 # CONFIG_COMPILER_STACK_CHECK_MODE_STRONG is not set
 # CONFIG_COMPILER_STACK_CHECK_MODE_ALL is not set
+CONFIG_COMPILER_STACK_CHECK=y
 # CONFIG_COMPILER_WARN_WRITE_STRINGS is not set
 # CONFIG_COMPILER_DISABLE_GCC8_WARNINGS is not set
 # end of Compiler options
@@ -399,7 +402,7 @@
 CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL=120
 CONFIG_ESP_NETIF_TCPIP_LWIP=y
 # CONFIG_ESP_NETIF_LOOPBACK is not set
-CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y
+# CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER is not set
 # end of ESP NETIF Adapter
 
 #
@@ -1057,10 +1060,11 @@
 # CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set
 # CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set
 # CONFIG_CXX_EXCEPTIONS is not set
-CONFIG_STACK_CHECK_NONE=y
-# CONFIG_STACK_CHECK_NORM is not set
+# CONFIG_STACK_CHECK_NONE is not set
+CONFIG_STACK_CHECK_NORM=y
 # CONFIG_STACK_CHECK_STRONG is not set
 # CONFIG_STACK_CHECK_ALL is not set
+CONFIG_STACK_CHECK=y
 # CONFIG_WARN_WRITE_STRINGS is not set
 # CONFIG_DISABLE_GCC8_WARNINGS is not set
 # CONFIG_ESP32_APPTRACE_DEST_TRAX is not set

mercurial