Here is the code to init/stop wifi and enter sleep mode.
Code: Select all
static void oneshot_timer_callback2(void* arg){
ESP_ERROR_CHECK(esp_timer_delete(oneshot_timer2));
oneshot_timer2 = NULL;
ESP_LOGI("TIMER", "timer2 timeout");
s_retry_num = MAXIMUM_RETRY;
ESP_ERROR_CHECK(wifi_stop());
// need to wait until UART TX FIFO is empty:
ESP_LOGI("CALLBACKTMR", "enter sleep mode");
uart_wait_tx_idle_polling(0); //UART_PLM);
msgFlags.sleep = 1;
gpio_set_level(LED_G, LED_ON);
esp_sleep_enable_timer_wakeup(180*1000000);
/* Get timestamp before entering sleep */
int64_t t_before_us = esp_timer_get_time();
esp_err_t err = esp_light_sleep_start();
if (err == ESP_ERR_INVALID_STATE) {
ESP_LOGI("CALLBACKTMR", "ESP_ERR_INVALID_STATE");
}
/* Get timestamp after waking up from sleep */
int64_t t_after_us = esp_timer_get_time();
gpio_set_level(LED_G, LED_OFF);
ESP_LOGI("CALLBACKTMR", "wake up");
ESP_LOGI("CALLBACKTMR", "Returned from light sleep, t=%lld ms, slept for %lld ms\n",
t_after_us / 1000, (t_after_us - t_before_us) / 1000);
wifi_init_sta();
}
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_LOGI("event_handler", "Event - wifi_connect");
esp_err_t err = esp_wifi_connect();
ESP_LOGI("event_handler", "wifi connect result %i", err);
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < MAXIMUM_RETRY) {
gpio_set_level(LED_Y, LED_ON);
esp_wifi_connect();
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
else {
gpio_set_level(LED_Y, LED_OFF);
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(TAG,"connect to the AP fail");
}
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));
s_retry_num = 0;
gpio_set_level(LED_Y, LED_ON);
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}
esp_err_t wifi_stop(void){
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
vEventGroupDelete(s_wifi_event_group);
esp_err_t err = esp_wifi_stop();
if (err == ESP_ERR_WIFI_NOT_INIT) {
return ESP_ERR_WIFI_NOT_INIT;
}
ESP_ERROR_CHECK(esp_wifi_deinit());
ESP_ERROR_CHECK(esp_wifi_clear_default_wifi_driver_and_handlers(sta_netif));
esp_netif_destroy(sta_netif);
sta_netif = NULL;
ESP_LOGI("STOP", "Disconnected from %s", WIFI_SSID);
gpio_set_level(LED_Y, LED_OFF);
esp_event_loop_delete_default();
return ESP_OK;
}
void wifi_init_sta(){
s_retry_num = 0;
s_wifi_event_group = xEventGroupCreate();
esp_base_mac_addr_set(wifi_mac_addr);
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);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
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, &event_handler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
wifi_config_t wifi_config = {
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.channel = 11,
.pmf_cfg = {
.capable = true,
.required = false
},
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", WIFI_SSID, WIFI_PASS);
xTaskCreate(udp_server_task, "udp_server", 4096, (void*)AF_INET, 5, NULL);
msgFlags.wifiConnected = 1;
gpio_set_level(LED_Y, LED_ON);
}
else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", WIFI_SSID, WIFI_PASS);
msgFlags.wifiConnected = 0;
gpio_set_level(LED_Y, LED_OFF);
}
else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}
After trying to reconnect to wifi, it stops on the event handler event - wifi connect.[0;32mI (957) wifi_init_sta: Wifi Mac Address: 18 fe 34 e0 3b ed
I (977) wifi:wifi driver task: 3ffd8204, prio:23, stack:6656, core=0
I (997) wifi:wifi firmware version: 1865b55
I (997) wifi:wifi certification version: v7.0
I (997) wifi:config NVS flash: enabled
I (997) wifi:config nano formating: disabled
I (997) wifi:Init data frame dynamic rx buffer num: 32
I (1007) wifi:Init management frame dynamic rx buffer num: 32
I (1007) wifi:Init management short buffer num: 32
I (1017) wifi:Init dynamic tx buffer num: 32
I (1017) wifi:Init static rx buffer size: 1600
I (1017) wifi:Init static rx buffer num: 10
I (1027) wifi:Init dynamic rx buffer num: 32
[0;32mI (1027) wifi_init: rx ba win: 6
[0;32mI (1037) wifi_init: tcpip mbox: 32
[0;32mI (1037) wifi_init: udp mbox: 6
[0;32mI (1037) wifi_init: tcp mbox: 6
[0;32mI (1047) wifi_init: tcp tx win: 5744
[0;32mI (1047) wifi_init: tcp rx win: 5744
[0;32mI (1057) wifi_init: tcp mss: 1440
[0;32mI (1057) wifi_init: WiFi IRAM OP enabled
[0;32mI (1067) wifi_init: WiFi RX IRAM OP enabled
[0;32mI (1157) phy: phy_version: 4500, 0cd6843, Sep 17 2020, 15:37:07, 0, 0
I (1157) wifi:mode : sta (18:fe:34:e0:3b:ed)
[0;32mI (1167) Hub1: wifi_init_sta finished.
[0;32mI (1167) event_handler: Event - wifi_connect
[0;32mI (1167) event_handler: wifi connect result 0
I (1287) wifi<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1
I (1297) wifi:state: init -> auth (b0)
I (1307) wifi:state: auth -> assoc (0)
I (1307) wifi:state: assoc -> run (10)
I (1327) wifi:connected with WS003, aid = 4, channel 11, BW20, bssid = e0:b9:e5:d3:26:c1
I (1327) wifi:security: WPA2-PSK, phy: bgn, rssi: -84
I (1337) wifi:pm start, type: 1
I (1427) wifi:AP's beacon interval = 102400 us, DTIM period = 1
[0;32mI (1957) esp_netif_handlers: sta ip: 192.168.0.42, mask: 255.255.255.0, gw: 192.168.0.1
[0;32mI (1957) Hub1: got ip:192.168.0.42
[0;32mI (1957) Hub1: connected to ap SSID:WS003
[0;32mI (1967) Hub1: UDP_server_task created
[0;32mI (1967) uart: queue free spaces: 24
[0;32mI (1967) Hub1: Socket created
[0;32mI (1977) Hub1: Socket bound, port 30300
[0;32mI (1987) Hub1: Waiting for data
[0;32mI (1987) uart: queue free spaces: 24
[0;32mI (1987) Hub1: uart2 event task
[0;32mI (1987) TX_TASK: tx event task
[0;32mI (1987) Hub1: Time is not set yet. Connecting to WiFi and getting time over NTP.
[0;32mI (2007) Hub1: Initializing SNTP
[0;32mI (2007) Hub1: Waiting for system time to be set... (1/10)
[0;32mI (2317) Hub1: Notification of a time synchronization event
[0;32mI (2317) Hub1: The current date/time is: 17/05/21 08:35:04
[0;32mI (2317) CHECK ALARM: Task checkAlarms created
[0;32mI (142317) TIMER: timer2 timeout
I (142317) wifi:state: run -> init (0)
I (142317) wifi:pm stop, total sleep time: 126652578 us / 140979752 us
I (142317) wifi<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
W (142327) wifi:hmac tx: stop, discard
I (142357) wifi:flush txq
I (142357) wifi:stop sw txq
I (142357) wifi:lmac stop hw txq
I (142357) wifi:Deinit lldesc rx mblock:10
[0;32mI (142367) STOP: Disconnected from WS003
[0;32mI (142367) CALLBACKTMR: enter sleep mode
I (142427) wifi:wifi firmware version: 1865b55
I (142427) wifi:wifi certification version: v7.0
I (142427) wifi:config NVS flash: enabled
I (142427) wifi:config nano formating: disabled
I (142427) wifi:Init data frame dynamic rx buffer num: 32
I (142437) wifi:Init management frame dynamic rx buffer num: 32
I (142447) wifi:Init management short buffer num: 32
I (142447) wifi:Init dynamic tx buffer num: 32
I (142447) wifi:Init static rx buffer size: 1600
I (142457) wifi:Init static rx buffer num: 10
I (142457) wifi:Init dynamic rx buffer num: 32
[0;32mI (142467) wifi_init: rx ba win: 6
[0;32mI (142467) wifi_init: tcpip mbox: 32
[0;32mI (142467) wifi_init: udp mbox: 6
[0;32mI (142477) wifi_init: tcp mbox: 6
[0;32mI (142477) wifi_init: tcp tx win: 5744
[0;32mI (142487) wifi_init: tcp rx win: 5744
[0;32mI (142487) wifi_init: tcp mss: 1440
[0;32mI (142497) wifi_init: WiFi IRAM OP enabled
[0;32mI (142497) wifi_init: WiFi RX IRAM OP enabled
I (142517) wifi:mode : sta (18:fe:34:e0:3b:ed)
[0;32mI (142517) Hub1: wifi_init_sta finished.
[0;32mI (142517) event_handler: Event - wifi_connect
[0;32mI (142517) event_handler: wifi connect result 0
it never gets to "I (1287) wifi<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1" as it is doing on the first connect on power up.
Any suggestions would be greatly appreciated.