Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.

Tue, 11 Apr 2023 19:41:53 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Tue, 11 Apr 2023 19:41:53 +0200
changeset 23
2cc30d828d6e
parent 22
9c0bcc91fe1a
child 24
74609f70411e

Shorter delays during wifi connect timeout. Some code cleanup in the main state table. Report WiFi TX power during init. Removed SSID from WIFI_State because it wasn't used.

main/iotbalkon.c file | annotate | diff | comparison | revisions
main/task_wifi.c file | annotate | diff | comparison | revisions
main/task_wifi.h file | annotate | diff | comparison | revisions
--- a/main/iotbalkon.c	Tue Apr 11 11:11:13 2023 +0200
+++ b/main/iotbalkon.c	Tue Apr 11 19:41:53 2023 +0200
@@ -395,8 +395,8 @@
     /*
      * Main application loop.
      */
-    int State = State_Init;
-    int OldState = State_Init + 1;
+    int		State = State_Init;
+    int		OldState = State_Init + 1;
     uint8_t	ds_time = DS_Time;
 
 
@@ -435,10 +435,10 @@
 					    DisCounter = 0;
 					} else {
 					    /*
-					     * 30 seconds connection try.
+					     * 15 seconds connection try.
 					     */
 					    DisCounter++;
-					    vTaskDelay(1000 / portTICK_PERIOD_MS);
+					    vTaskDelay(500 / portTICK_PERIOD_MS);
 					    ESP_LOGI(TAG, "* Counter WiFi %d", DisCounter);
 					    if (DisCounter > 30) {
 						Alarm |= AL_NOWIFI;
@@ -543,7 +543,7 @@
       					gTimeNext = millis() + 1000;
 					break;
 
-	    case State_WorkDone:	if (gTimeInMillis > gTimeNext) {
+	    case State_WorkDone:	if (gTimeInMillis >= gTimeNext) {
 					    State = State_Disconnect_MQTT;
 					    request_mqtt(false);
 					}
@@ -556,20 +556,16 @@
 
 	    case State_Disconnect_Wifi:	request_WiFi(false);
       					// // Reset values for average current measurement.
-      					// HaveIP = false;
 					loopno = 0;
 					gLastTime = millis();
-					//vTaskDelay(10 / portTICK_PERIOD_MS);
 
 					/*
       					 *  If any output is on, use 6 10 seconds loops.
       					 *  If nothing on, do a deep sleep.
       					 */
 					if (Relay1 || Relay2 || Dimmer3 || Dimmer4) {
-        				    if (DS_Active != 0) {
-						DS_Active = 0;
-						nvsio_write_u8((char *)"ds_active", 0);
-					    }
+					    DS_Active = 0;
+					    nvsio_write_u8((char *)"ds_active", 0);
 
         				    // Active mode, 60 seconds loop
 					    ST_LOOPS = 6;
@@ -578,10 +574,10 @@
 					    State = State_Wait;
       					} else {
 					    ds_time = DS_TIME;
-        				//    if (solarVolts < 6) {
-          				//      // At night, increase the deep-sleep time.
-          				//	ds_time *= 4;
-        				//    }
+        				    if (solarVolts < 6) {
+						// At night, increase the deep-sleep time.
+          					ds_time *= 4;
+        				    }
 					    nvsio_write_u8((char *)"ds_active", 1);
 					    DS_Active = 1;
 					    nvsio_write_u32((char *)"ds_time", ds_time);
@@ -590,14 +586,14 @@
       					}
 					break;
 
-	    case State_Wait:		if (gTimeInMillis > gTimeNext) {
+	    case State_Wait:		if (gTimeInMillis >= gTimeNext) {
 					    State = State_Measure;
 					}
-					vTaskDelay(10 / portTICK_PERIOD_MS);
+					vTaskDelay(50 / portTICK_PERIOD_MS);
 					break;
 
-	    case State_Measure:		getVoltsCurrent();
-					gTimeNext = millis() + ST_INTERVAL;
+	    case State_Measure:		gTimeNext = millis() + ST_INTERVAL;
+					getVoltsCurrent();
 					if (loopno >= ST_LOOPS) {
 					    ESP_LOGI(TAG, "Enough loops, do connect");
         				    getLightValues();
@@ -614,7 +610,6 @@
 					esp_deep_sleep_start();
 					break;
 	}
-        vTaskDelay(10 / portTICK_PERIOD_MS);
     }
     // Not reached.
 }
--- a/main/task_wifi.c	Tue Apr 11 11:11:13 2023 +0200
+++ b/main/task_wifi.c	Tue Apr 11 19:41:53 2023 +0200
@@ -170,7 +170,6 @@
 
 static void init_wifi(void)
 {
-    ESP_LOGI(TAG, "initialise_wifi() start");
     ESP_ERROR_CHECK(esp_event_loop_create_default());
     xSemaphoreWiFi = xSemaphoreCreateMutex();
 
@@ -186,12 +185,10 @@
     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() );
+    ESP_ERROR_CHECK(esp_wifi_start());
 
     xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
     xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
-
-    ESP_LOGI(TAG, "initialise_wifi() done");
 }
 
 
@@ -202,13 +199,10 @@
 	.sta = {
 	    .ssid = ESP_WIFI_SSID,
 	    .password = ESP_WIFI_PASS,
+	    .threshold.authmode = WIFI_AUTH_WPA2_PSK,
 	},
     };
     // .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) );
@@ -230,15 +224,14 @@
 {
     uint64_t starttime = 0;
 
-    ESP_LOGI(TAG, "Start WiFi");
-    esp_log_level_set("wifi", ESP_LOG_WARNING);
+    ESP_LOGI(TAG, "Starting WiFi task");
+    esp_log_level_set("wifi", ESP_LOG_ERROR);
 
     /*
      * memory allocation of objects used by the task 
      */
     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
@@ -250,8 +243,9 @@
      */
     init_wifi();
     EventBits_t uxBits;
-
-    ESP_LOGI(TAG, "Startup completed, enter task loop");
+    int8_t tx_level;
+    ESP_ERROR_CHECK(esp_wifi_get_max_tx_power(&tx_level));
+    ESP_LOGI(TAG, "Startup completed, maximum transmit power %d dBm", tx_level / 4);
 
     for(;;) {
 
--- a/main/task_wifi.h	Tue Apr 11 11:11:13 2023 +0200
+++ b/main/task_wifi.h	Tue Apr 11 19:41:53 2023 +0200
@@ -13,7 +13,6 @@
 typedef struct {
     bool		STA_connected;		///< Connected to AP as station.
     bool		STA_online;		///< Connected and online.
-    char		STA_ssid[33];		///< Connected to this SSID.
     int8_t		STA_rssi;		///< Signal strength.
     char		STA_ip[17];		///< IP address.
     char		STA_nm[17];		///< IP netmask.

mercurial