-->
Page 1 of 1

set channel in sleep mode, fail and exit

PostPosted: Sun May 07, 2023 2:51 am
by rcjk9590
Hello,

I have been scouring the internet and have came up empty handed. I have an ESP-01 where I made a project to read a temp sensor and post to my mqtt broker. The program worked great after some tweaking and now I am trying to include a light sleep. I am unable to do deep sleep since I don't have access the the required pin (tried on one and ended up pulling a pad :roll: ).

I have successfully deployed the light sleep and the current drops to uA but I have this error popup when I try to wake and re-initialize wifi.

After a few tries and an error (is it reporting what is stored in the buffer?) it reconnects, sends the temp and continues on. After about 3-5 cycles of this, it finally errors out and stops running all together. It appears that wifi drivers are still asleep? I am not sure if channel refers to trying to connect to the scanned channel found or something else. I am using channel 11 on my router. I tried adding de-init to no success. Did not find any information on needing to wake up things once timer was done.

For the wifi init I am using the example code. Here is everything else I got in regards to sleep:

Code: Select all printf("MQTT CONNECT\n");
    printf("MQTT SEND DATA\n");
    mqtt_app_start();
    printf("STOP MQTT\n");
    mqtt_disconnect();
    printf("STOP WIFI 2\n");
    ESP_ERROR_CHECK(esp_wifi_disconnect());
    ESP_ERROR_CHECK(esp_wifi_stop());
    // ESP_ERROR_CHECK(esp_wifi_deinit());

    printf("START TIMER\n");

    sleep_mode();
    printf("START wifi\n");
    ESP_ERROR_CHECK(esp_event_loop_delete_default());
    wifi_init_sta();

    mqtt_connect();


Sleep function:

Code: Select allvoid sleep_mode() {

  esp_sleep_enable_timer_wakeup(TIMER_WAKEUP_TIME_US);
  ESP_ERROR_CHECK(esp_light_sleep_start());
  printf("WAKEUP!\n");
  vTaskDelay(pdMS_TO_TICKS(1) / portTICK_RATE_MS);
}



Code: Select allI (2238) tcpip_adapter: sta ip: 192.168.7.133, mask: 255.255.255.0, gw: 192.168.7.1
I (2242) wifi station: got ip:192.168.7.133
I (2246) wifi station: connected to ap
I (2253) system_api: Base MAC address is not set, read default base MAC address from EFUSE
I (2268) MQTT_EXAMPLE: Other event id:7
MQTT CONNECT
MQTT SEND DATA
I (2296) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED
STOP MQTT
I (4061) MQTT_CLIENT: Client asked to disconnect
STOP WIFI 2
I (4213) wifi:state: 5 -> 0 (0)
START TIMER
WAKEUP!
START wifi
I (10531) system_api: Base MAC address is not set, read default base MAC address from EFUSE
I (10536) system_api: Base MAC address is not set, read default base MAC address from EFUSE
I (10549) wifi station: wifi_init_sta finished.
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
I (12593) wifi station: retry to connect to the AP
I (12596) wifi station: connect to the AP fail
set channel in sleep mode, fail and exit
E:RX 16b
rxbufbit:ffff
E:AC 1, 33, 52
CONF:b00b, PLCP:632da0, rf:50000000, rx:0,0, tx:10000
I (14652) wifi station: retry to connect to the AP
I (14655) wifi station: connect to the AP fail
I (14773) wifi:state: 0 -> 2 (b0)
I (14774) wifi:state: 2 -> 3 (0)
I (14776) wifi:state: 3 -> 5 (10)
I (14788) wifi:connected with Garden, aid = 5, channel 11, HT20, bssid = 68:72:51:7e:7b:c6
I (16534) tcpip_adapter: sta ip: 192.168.7.133, mask: 255.255.255.0, gw: 192.168.7.1
I (16538) wifi station: got ip:192.168.7.133
I (16542) wifi station: connected to ap
W (16549) event: handler already registered, overwriting
I (16558) MQTT_EXAMPLE: Other event id:7
MQTT CONNECT
MQTT SEND DATA
I (16592) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED
STOP MQTT
I (18356) MQTT_CLIENT: Client asked to disconnect
STOP WIFI 2
I (18437) wifi:state: 5 -> 0 (0)
START TIMER


Appreciate any guidance.

Re: set channel in sleep mode, fail and exit

PostPosted: Tue May 09, 2023 3:08 am
by rooppoorali
It seems that the error is related to the wifi channel, and it is failing to set the channel during the sleep mode. It looks like the wifi driver is not fully initialized after waking up from sleep mode, and it takes some time to reconnect to the wifi network.

One way to solve this issue is to add a delay before re-initializing the wifi. You can try to increase the delay time between the sleep mode and wifi initialization and see if it solves the problem. You can also try to add a loop to retry the wifi initialization until it succeeds.

Re: set channel in sleep mode, fail and exit

PostPosted: Fri May 12, 2023 1:02 pm
by rcjk9590
Thank you rooppoorali,

I tried a delay after leaving sleep mode but it may not have been long enough. I will revisit that first and see if I can find a delay that works. Good idea on the loop, I will try that as well :) I'll report back if I find a way, just busy at this time.

Re: set channel in sleep mode, fail and exit

PostPosted: Sun May 14, 2023 8:13 pm
by rcjk9590
Okay I had some time today to look at it and as it turns out, I do not think my delays are working at all. I did some debug with printf and the 10 second delays did not work. I fixed those syntax error and started with two delays working up until 30 second delay. In the menuconfig my port tick rate is 100 Hz or 10ms.

I have done the delay like this: vTaskDelay(1000 / 10); // 10 second delay

My flow in English is as follows:

1. Init Wifi
2. Mqtt connect
3. into while loop
a. Mqtt app start, send temp once
b. Mqtt disconnect
c. esp wifi disconnect
d. esp wifi stop
e. sleep function
f. vTaskdelay 30 seconds
g. clear the default loop event
h. wifi init
1. Inside wifi init added 30s delay before wifi start
i. mqtt connect

Here is the wifi code

Code: Select all  void wifi_init_sta(void) {
  s_wifi_event_group = xEventGroupCreate();

  tcpip_adapter_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_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
                                             &event_handler, NULL));
  ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
                                             &event_handler, NULL));

  wifi_config_t wifi_config = {
      .sta = {.ssid = EXAMPLE_ESP_WIFI_SSID, .password = EXAMPLE_ESP_WIFI_PASS},
  };

  /* Setting a password implies station will connect to all security modes
   * including WEP/WPA. However these modes are deprecated and not advisable to
   * be used. Incase your Access point doesn't support WPA2, these mode can be
   * enabled by commenting below line */

  if (strlen((char *)wifi_config.sta.password)) {
    wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
  }

  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
  ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  printf("start wait\n");
  vTaskDelay(60000 / 10);
  printf("wait done\n");
  ESP_ERROR_CHECK(esp_wifi_start());

  ESP_LOGI(TAG2, "wifi_init_sta finished.");

  /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or
   * connection failed for the maximum number of re-tries (WIFI_FAIL_BIT). The
   * bits are set by event_handler() (see above) */
  EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
                                         WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
                                         pdFALSE, pdFALSE, portMAX_DELAY);
  printf("MADE IT PAST EVENT BITS\n");
  /* xEventGroupWaitBits() returns the bits before the call returned, hence we
   * can test which event actually happened. */
  if (bits & WIFI_CONNECTED_BIT) {
    ESP_LOGI(TAG2, "connected to ap SSID:%s password:%s", EXAMPLE_ESP_WIFI_SSID,
             EXAMPLE_ESP_WIFI_PASS);
  } else if (bits & WIFI_FAIL_BIT) {
    ESP_LOGI(TAG2, "Failed to connect to SSID:%s, password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  } else {
    ESP_LOGE(TAG2, "UNEXPECTED EVENT");
  }

  ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP,
                                               &event_handler));
  ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID,
                                               &event_handler));
  vEventGroupDelete(s_wifi_event_group);
}



Here is the error message again. You can see the Start wait and wait done to where I tried two delays

Code: Select allMQTT CONNECT
MQTT SEND DATA
I (62330) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED
STOP MQTT
I (63204) MQTT_CLIENT: Client asked to disconnect
STOP WIFI 2
I (63261) wifi:state: 5 -> 0 (0)
START TIMER
WAKEUP!
start wait
Wait done
START wifi
I (154287) system_api: Base MAC address is not set, read default base MAC address from EFUSE
I (154293) system_api: Base MAC address is not set, read default base MAC address from EFUSE
start wait
wait done
I (214315) wifi station: wifi_init_sta finished.
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
I (216361) wifi station: retry to connect to the AP
I (216364) wifi station: connect to the AP fail
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
set channel in sleep mode, fail and exit
E:RX 6ce
rxbufbit:ffff
E:AC 1, 33, 0
CONF:b00b, PLCP:632da0, rf:50000000, rx:0,0, tx:10000
I (218410) wifi station: retry to connect to the AP
I (218415) wifi station: connect to the AP fail
I (218531) wifi:state: 0 -> 2 (b0)
I (218533) wifi:state: 2 -> 3 (0)
I (218535) wifi:state: 3 -> 5 (10)
I (218549) wifi:connected with Garden, aid = 5, channel 11, HT20, bssid = 68:72:51:7e:7b:c6
I (220313) tcpip_adapter: sta ip: 192.168.7.133, mask: 255.255.255.0, gw: 192.168.7.1
I (220319) wifi station: got ip:192.168.7.133



I have tried up to a minute delay with no success. Wonder if I am recovering from light sleep wrong?