Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.

Tue, 11 Apr 2023 11:11:13 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 11 Apr 2023 11:11:13 +0200
changeset 22
9c0bcc91fe1a
parent 21
df8564c9701e
child 23
2cc30d828d6e

Decreased WiFi transmit level to 13. Level 15 is unstable, skip 14 just to be sure. It seems to be the bad designed antenna circuit on the ESP32-C3 Lolin v2.1.0 board. The board also benefits from a ground plane connected to the shield of the USB connector. The final implementation needs an external antenna anyway so the onboard antenna will go away. In the meanwhile, the wifi task is now more robust. Also, in testing mode there is no battery alarm, only when building for production.

main/iotbalkon.c file | annotate | diff | comparison | revisions
main/task_wifi.c file | annotate | diff | comparison | revisions
--- a/main/iotbalkon.c	Fri Apr 07 14:31:17 2023 +0200
+++ b/main/iotbalkon.c	Tue Apr 11 11:11:13 2023 +0200
@@ -388,7 +388,6 @@
     xTaskCreate(&task_out,      "task_out",         2560, NULL, 9, &xTaskOUT);
     // esp_log_level_set("MQTT_CLIENT", ESP_LOG_ERROR);
     xTaskCreate(&task_mqtt,     "task_mqtt",        4096, NULL, 5, &xTaskMQTT);
-     esp_log_level_set("wifi", ESP_LOG_ERROR);
     xTaskCreate(&task_wifi,     "task_wifi",        4096, NULL, 3, &xTaskWifi);
 
     vTaskDelay(10 / portTICK_PERIOD_MS);
@@ -521,12 +520,17 @@
 					    ESP_LOGI(TAG, "  Solar Volts: %.4fV Current %.4fmA Power %.4fmW", solarVolts, solarCurrent, solarPower);
 					    ESP_LOGI(TAG, "Battery Volts: %.4fV Current %.4fmA Power %.4fmW", batteryVolts, batteryCurrent, batteryPower);
 
-        				    /*   Check alarm conditions */
+#ifdef CONFIG_CODE_PRODUCTION
+					    /*   Check alarm conditions */
 					    if (batteryState <= 10) {
 					    	Alarm |= AL_ACCULOW;
 					    } else {
 					    	Alarm &= ~AL_ACCULOW;
 					    }
+#endif
+#ifdef CONFIG_CODE_TESTING
+					    Alarm &= ~AL_ACCULOW;
+#endif
       					}
 					getTempBaro();
 					vTaskDelay(2000 / portTICK_PERIOD_MS);
@@ -610,7 +614,7 @@
 					esp_deep_sleep_start();
 					break;
 	}
-        vTaskDelay(20 / portTICK_PERIOD_MS);
+        vTaskDelay(10 / portTICK_PERIOD_MS);
     }
     // Not reached.
 }
--- a/main/task_wifi.c	Fri Apr 07 14:31:17 2023 +0200
+++ b/main/task_wifi.c	Tue Apr 11 11:11:13 2023 +0200
@@ -14,15 +14,11 @@
 
 SemaphoreHandle_t       	xSemaphoreWiFi = NULL;		///< Semaphore WiFi task.
 EventGroupHandle_t		xEventGroupWifi;		///< Events WiFi task.
-wifi_config_t			wifi_Config = {			///< Current STA configuration.
-   .sta = {
-	.ssid = ESP_WIFI_SSID,
-        .password = ESP_WIFI_PASS,
-	.scan_method = WIFI_FAST_SCAN,
-   },
-};
+esp_event_handler_instance_t	instance_any_id;		///< WiFi event handler.
+esp_event_handler_instance_t	instance_got_ip;		///< IP event handler.
+
 WIFI_State			*wifi_state = NULL;		///< Public state for other tasks.
-esp_netif_t			*sta_netif = NULL;		///< Station interface
+esp_netif_t			*sta_netif = NULL;		///< Station interface.
 
 
 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1;		///< When set, means a client requested to disconnect from currently connected AP.
@@ -34,6 +30,10 @@
 const int TASK_WIFI_STA_CONNECTED = BIT7;			///< Indicate that we are connected to AP as station, flip of BIT6.
 
 
+static void init_wifi(void);
+void wifi_connect(void);
+
+
 /****************************************************************************/
 
 
@@ -72,7 +72,6 @@
 		break;
 
 	case WIFI_EVENT_STA_CONNECTED: {
-//		system_event_sta_connected_t* event = (wifi_event_sta_connected_t*) event_data;
                 wifi_ap_record_t ap_info;
 		esp_wifi_sta_get_ap_info(&ap_info);
 		ESP_LOGI(TAG, "Event STA connected rssi=%d", ap_info.rssi);
@@ -90,11 +89,26 @@
 
 	case WIFI_EVENT_STA_DISCONNECTED: {
 		wifi_event_sta_disconnected_t* disconnected = (wifi_event_sta_disconnected_t*) event_data;
+                ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason);
 
-                ESP_LOGI(TAG, "Event STA disconnected, reason: %d", disconnected->reason);
+		/*
+		 * If it's not a normal request to disconnect, make sure the mqtt
+		 * connection will be removed.
+		 */
 		if (disconnected->reason != 8)
 		    request_mqtt(false);
-                if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) {
+
+		/*
+		 * Error conditions.
+		 */
+		if (disconnected->reason == 2) {
+		    ESP_LOGW(TAG, "Auth Expire: try to recover");
+		    wifi_connect();
+		} else if (disconnected->reason == 39) {
+		    ESP_LOGW(TAG, "Timeout: try to recover");
+		    ESP_ERROR_CHECK(esp_wifi_connect());
+		}
+                if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
                     wifi_state->STA_connected = false;
                     wifi_state->STA_online = false;
                     xSemaphoreGive(xSemaphoreWiFi);
@@ -154,52 +168,96 @@
 }
 
 
+static void init_wifi(void)
+{
+    ESP_LOGI(TAG, "initialise_wifi() start");
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
+    xSemaphoreWiFi = xSemaphoreCreateMutex();
+
+    /* initialize the tcp stack */
+    ESP_ERROR_CHECK(esp_netif_init());
+    sta_netif = esp_netif_create_default_wifi_sta();
+    assert(sta_netif);
+
+    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_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_mode(WIFI_MODE_STA));
+    ESP_ERROR_CHECK( esp_wifi_start() );
+
+    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
+    xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
+
+    ESP_LOGI(TAG, "initialise_wifi() done");
+}
+
+
+void wifi_connect(void)
+{
+    ESP_LOGI(TAG, "wifi_connect() start");
+    wifi_config_t	wifi_Config = {		///< Current STA configuration.
+	.sta = {
+	    .ssid = ESP_WIFI_SSID,
+	    .password = ESP_WIFI_PASS,
+	},
+    };
+    // .threshold.authmode = WIFI_AUTH_WPA2_PSK,
+    // .pmf_cfg = {
+    //            .capable = true,
+    //            .required = false
+    // },
+
+    ESP_ERROR_CHECK(esp_wifi_disconnect());
+    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_Config) );
+
+    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
+    esp_err_t wifierror = esp_wifi_connect();
+
+    if (wifierror != ESP_OK) {
+	ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x %s", (int)wifierror, esp_err_to_name(wifierror));
+	xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
+    } else {
+	ESP_LOGI(TAG, "Connected Ok");
+    }
+    ESP_LOGI(TAG, "wifi_connect() done");
+}
+
 
 void task_wifi( void * pvParameters )
 {
-    ESP_LOGI(TAG, "Start WiFi");
+    uint64_t starttime = 0;
 
-    /* event handler and event group for the wifi driver */
-    xEventGroupWifi = xEventGroupCreate();
-    /* initialize the tcp stack */
-    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);
+    ESP_LOGI(TAG, "Start WiFi");
+    esp_log_level_set("wifi", ESP_LOG_WARNING);
 
     /*
      * memory allocation of objects used by the task 
      */
-    xSemaphoreWiFi = xSemaphoreCreateMutex();
     wifi_state = malloc(sizeof(WIFI_State));
     memset(wifi_state, 0x00, sizeof(WIFI_State));
     sprintf(wifi_state->STA_ssid, "%s", ESP_WIFI_SSID);
 
     /*
+     * event group for the wifi driver
+     */
+    xEventGroupWifi = xEventGroupCreate();
+
+    /*
      * 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_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_mode(WIFI_MODE_STA));
-    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config) );
-    ESP_ERROR_CHECK(esp_wifi_start());
-
-    //xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
-    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
-    xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
+    init_wifi();
     EventBits_t uxBits;
 
     ESP_LOGI(TAG, "Startup completed, enter task loop");
 
     for(;;) {
 
-	/* actions that can trigger: request a connection or a disconnection */
+	/*
+	 * Actions that can trigger: request a connection or a disconnection
+	 */
 	uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT,
 		                     pdFALSE, pdFALSE, portMAX_DELAY );
 
@@ -211,22 +269,17 @@
 	    ESP_ERROR_CHECK(esp_wifi_disconnect());
 	    xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY );
 
-	    /* finally: release the request bit */
+	    /*
+	     * Finally: release the request bit
+	     */
 	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
+	    ESP_LOGI(TAG, "Connection time %llu", (esp_timer_get_time() / 1000) - starttime);
 
 	} else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) {
 
-	    ESP_LOGI(TAG, "Request STA connect `%s' `%s'", wifi_Config.sta.ssid, wifi_Config.sta.password);
-	    xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
-	    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config));
-
-	    esp_err_t wifierror = esp_wifi_connect();
-	    if (wifierror != ESP_OK) {
-		ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror);
-		xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
-	    } else {
-		ESP_LOGI(TAG, "Connected Ok");
-	    }
+	    ESP_LOGI(TAG, "Request STA connect `%s' `%s'", ESP_WIFI_SSID, ESP_WIFI_PASS);
+	    starttime = esp_timer_get_time() / 1000;
+	    wifi_connect();
 
 	    /*
 	     * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted
@@ -240,26 +293,21 @@
 		/*
 		 * only save the config if the connection was successful!
 		 */
-		if (uxBits & TASK_WIFI_STA_CONNECTED) {
-		    /* save wifi config */
-		    //SaveStaConfig();
-		} else {
+		if (uxBits & TASK_WIFI_STA_FAILED) {
 		    ESP_LOGI(TAG, "No AP found");
 		    vTaskDelay(3000 / portTICK_PERIOD_MS);
 		    ESP_LOGW(TAG, "Connection failed");
 		    /* failed attempt to connect regardles of the reason */
-
-		    /* otherwise: reset the config */
-		    //memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t));
             	}
 	    }
 
-	    /* finally: release the request bit */
+	    /*
+	     * Finally: release the request bit
+	     */
             xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
 	}
 
     } /* for(;;) */
-    vTaskDelay(10 / portTICK_PERIOD_MS);
 }
 
 

mercurial