Wifi disconnects

gerardbos
Posts: 5
Joined: Wed Jun 13, 2018 1:06 pm

Wifi disconnects

Postby gerardbos » Thu Jan 28, 2021 7:53 pm

Hi, i'm working a project with a https webserver and some https client connections. This all seems to work fine on the v4.2 esp-idf release. But, when I'm trying to implement the OTA functionality (using the advanced_ota_https example as a basis), I consequently end up with disconnects of the wifi connection during the OTA:

Code: Select all

I (30) boot: ESP-IDF v4.2-238-g8cd16b60f 2nd stage bootloader
I (30) boot: compile time 20:25:42
I (30) boot: chip revision: 1
I (34) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (50) boot.esp32: SPI Speed      : 40MHz
I (50) boot.esp32: SPI Mode       : DIO
I (50) boot.esp32: SPI Flash Size : 4MB
I (55) boot: Enabling RNG early entropy source...
I (60) boot: Partition Table:
I (64) boot: ## Label            Usage          Type ST Offset   Length
I (71) boot:  0 nvs              WiFi data        01 02 00009000 00004000
I (79) boot:  1 otadata          OTA data         01 00 0000d000 00002000
I (86) boot:  2 phy_init         RF data          01 01 0000f000 00001000
I (94) boot:  3 factory          factory app      00 00 00010000 00100000
I (101) boot:  4 ota_0            OTA app          00 10 00110000 00100000
I (109) boot:  5 ota_1            OTA app          00 11 00210000 00100000
I (116) boot: End of partition table
I (121) boot: Defaulting to factory image
I (125) boot_comm: chip revision: 1, min. application chip revision: 0
I (132) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x22abc (142012) map
I (196) esp_image: segment 1: paddr=0x00032ae4 vaddr=0x3ffb0000 size=0x033e0 ( 13280) load
I (201) esp_image: segment 2: paddr=0x00035ecc vaddr=0x40080000 size=0x00404 (  1028) load
0x40080000: _WindowOverflow4 at /home/private/esp32/esp-idf/components/freertos/xtensa/xtensa_vectors.S:1730

I (203) esp_image: segment 3: paddr=0x000362d8 vaddr=0x40080404 size=0x09d40 ( 40256) load
I (229) esp_image: segment 4: paddr=0x00040020 vaddr=0x400d0020 size=0x9a930 (633136) map
0x400d0020: _stext at ??:?

I (470) esp_image: segment 5: paddr=0x000da958 vaddr=0x4008a144 size=0x0b560 ( 46432) load
0x4008a144: prvCopyDataFromQueue at /home/private/esp32/esp-idf/components/freertos/queue.c:1948

I (502) boot: Loaded app from partition at offset 0x10000
I (503) boot: Disabling RNG early entropy source...
I (503) cpu_start: Pro cpu up.
I (507) cpu_start: Application information:
I (511) cpu_start: Project name:     smarts
I (516) cpu_start: App version:      2.0.0-3-gbd14820-dirty
I (522) cpu_start: Compile time:     Jan 28 2021 20:25:37
I (529) cpu_start: ELF file SHA256:  05326fa7ce44f901...
I (535) cpu_start: ESP-IDF:          v4.2-238-g8cd16b60f
I (541) cpu_start: Starting app cpu, entry point is 0x400818cc
0x400818cc: call_start_cpu1 at /home/private/esp32/esp-idf/components/esp32/cpu_start.c:287

I (0) cpu_start: App cpu up.
I (551) heap_init: Initializing. RAM available for dynamic allocation:
I (558) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (564) heap_init: At 3FFB7AD8 len 00028528 (161 KiB): DRAM
I (570) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (576) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (583) heap_init: At 400956A4 len 0000A95C (42 KiB): IRAM
I (589) cpu_start: Pro cpu start user code
I (608) spi_flash: detected chip: generic
I (608) spi_flash: flash io: dio
I (608) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (691) main: Starting Smarts
W (691) serial: No serial configured
I (691) networking: Device hostname: smarts-0000000000
I (711) wifi:wifi driver task: 3ffc00f0, prio:23, stack:6656, core=0
I (711) system_api: Base MAC address is not set
I (711) system_api: read default base MAC address from EFUSE
I (731) wifi:wifi firmware version: 25f230e
I (731) wifi:wifi certification version: v7.0
I (731) wifi:config NVS flash: enabled
I (731) wifi:config nano formating: disabled
I (741) wifi:Init data frame dynamic rx buffer num: 32
I (741) wifi:Init management frame dynamic rx buffer num: 32
I (751) wifi:Init management short buffer num: 32
I (751) wifi:Init dynamic tx buffer num: 32
I (761) wifi:Init static rx buffer size: 1600
I (761) wifi:Init static rx buffer num: 10
I (761) wifi:Init dynamic rx buffer num: 32
I (771) wifi_init: rx ba win: 6
I (771) wifi_init: tcpip mbox: 32
I (781) wifi_init: udp mbox: 6
I (781) wifi_init: tcp mbox: 6
I (781) wifi_init: tcp tx win: 5744
I (791) wifi_init: tcp rx win: 5744
I (791) wifi_init: tcp mss: 1440
I (801) wifi_init: WiFi IRAM OP enabled
I (801) wifi_init: WiFi RX IRAM OP enabled
I (811) wifi: Setting up sta mode to SSID: myssid, with password: mypassword
I (941) phy: phy_version: 4500, 0cd6843, Sep 17 2020, 15:37:07, 0, 0
I (941) wifi:mode : sta (b4:e6:2d:8e:fe:35)
I (941) wifi: sta_start
I (941) webserver: Starting htttps server!
I (941) esp_https_server: Starting server
I (951) esp_https_server: Server listening on port 443
I (951) webserver: Register new GET endpoint /device
I (961) webserver: Register new PUT endpoint /config/erase
I (971) webserver: Register new GET endpoint /config
I (971) webserver: Register new PUT endpoint /config
I (981) webserver: Register new PUT endpoint /power/reset
I (981) switches: No configuration found
I (991) webserver: Register new GET endpoint /switches_state
I (2271) wifi:new:<11,0>, old:<1,0>, ap:<255,255>, sta:<11,0>, prof:1
I (3061) wifi:state: init -> auth (b0)
I (3071) wifi:state: auth -> assoc (0)
I (3071) wifi:state: assoc -> run (10)
I (3081) wifi:connected with myssid, aid = 2, channel 11, BW20, bssid = f4:92:bf:a0:bd:20
I (3091) wifi:security: WPA2-PSK, phy: bgn, rssi: -58
I (3161) wifi:pm start, type: 1

I (3161) wifi:AP's beacon interval = 102400 us, DTIM period = 3
I (5191) network: connected
I (5191) wifi: sta_got_ip: 192.168.1.81
I (5191) esp_netif_handlers: sta ip: 192.168.1.81, mask: 255.255.255.0, gw: 192.168.1.1
I (5191) ota: check URL: https://firmware.esp32.nl/latest
I (7331) ota: version available: 2.1.0
I (7341) ota: New version available (2.1.0)
I (7341) wifi:Set ps type: 0

I (9531) esp_https_ota: Starting OTA...
I (9531) esp_https_ota: Writing to partition subtype 16 at offset 0x110000
I (9541) ota: Running firmware version: 2.0.0-3-gbd14820-dirty
I (19311) wifi:state: run -> init (3c0)
I (19311) wifi:pm stop, total sleep time: 3268041 us / 16154179 us

I (19311) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (19321) network: disconnected
I (19321) wifi: wifi_disconnected
I (19331) wifi: retry connect to ap
E (19341) esp-tls-mbedtls: read error :-76:
E (19341) TRANS_SSL: esp_tls_conn_read error, errno=Software caused connection abort
W (19351) HTTP_CLIENT: esp_transport_read returned:-76 and errno:113 
E (19361) TRANS_SSL: ssl_poll_read select error 113, errno = Software caused connection abort, fd = 57
E (19361) ota: Complete data was not received.
I (19371) esp_image: segment 0: paddr=0x00110020 vaddr=0x3f400020 size=0x228bc (141500) map
I (19431) esp_image: segment 1: paddr=0x001328e4 vaddr=0x3ffb0000 size=0x033e0 ( 13280) 
I (19431) esp_image: segment 2: paddr=0x00135ccc vaddr=0x40080000 size=0x00404 (  1028) 
0x40080000: _WindowOverflow4 at /home/private/esp32/esp-idf/components/freertos/xtensa/xtensa_vectors.S:1730

I (19441) esp_image: segment 3: paddr=0x001360d8 vaddr=0x40080404 size=0x09f40 ( 40768) 
I (19461) esp_image: segment 4: paddr=0x00140020 vaddr=0x400d0020 size=0x9a560 (632160) map
0x400d0020: _stext at ??:?

I (19461) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (19471) wifi:state: init -> auth (b0)
I (19471) wifi:state: auth -> assoc (0)
I (19481) wifi:state: assoc -> run (10)
I (19491) wifi:connected with myssid, aid = 2, channel 11, BW20, bssid = f4:92:bf:a0:bd:20
I (19491) wifi:security: WPA2-PSK, phy: bgn, rssi: -57
I (19501) wifi:pm start, type: 0

I (19541) wifi:AP's beacon interval = 102400 us, DTIM period = 3
E (19691) esp_image: invalid segment length 0xffffffff
E (19691) ota: Image validation failed, image is corrupted
E (19691) ota: ESP_HTTPS_OTA upgrade failed 5379
I (19691) wifi:Set ps type: 1

I (20191) network: connected
I (20191) wifi: sta_got_ip: 192.168.1.81
I (20191) esp_netif_handlers: sta ip: 192.168.1.81, mask: 255.255.255.0, gw: 192.168.1.1
I've tried this over multiple devices (lolin lite), all have the same behavior. When I flash the advanced_ota_https onto the device, I don't have the problem, so I'm probably doing something wrong here.

But even when I remove everything but the bare essentials and the OTA part, I still get this problem.
Last edited by gerardbos on Fri Jan 29, 2021 11:00 am, edited 1 time in total.

WilliamR
Posts: 6
Joined: Wed Jan 13, 2021 1:03 am

Re: Wifi disconnects

Postby WilliamR » Fri Jan 29, 2021 5:21 am

Hi gerardbos,
Would you be able to provide some of your code? Especially the code which handles WiFi connectivity

When I was starting, to get wifi up and running, I personally found that the easiest way was to start with the example_connect function from https://github.com/espressif/esp-idf/bl ... /connect.c (referenced in a lot of other example projects). I then dug into the code for example_connect and began making edits as needed for my purposes. A simple example:

Code: Select all


#include "protocol_examples_common.h"

void app_main()
{
	ESP_ERROR_CHECK(example_connect());
}


To implement OTA, I found that it was simplest to start from the simple_ota_example, verify functionality and then proceed further

Just my two cents - this may not be applicable to your use case.

gerardbos
Posts: 5
Joined: Wed Jun 13, 2018 1:06 pm

Re: Wifi disconnects

Postby gerardbos » Fri Jan 29, 2021 10:58 am

Yes, I can provide a sample of the code, I tried to strip it down as much as possible, but it still contains a lot of files (as this is part of a much larger system). I've placed the code here:

https://cloud.gbmb.nl/index.php/s/FaisxGrzT6YtnpS

Due to that the code runs on different devices, an extra layer is added to the code to run the WiFi part. Due to this, the code of the WiFi connection setup is different that the examples, but it should work the same. But please verify it.

The thing that I don't understand is, the WiFi connects perfectly, I can retrieve data over it (getting the "latest" file, containing the new image version). Then I start the OTA, and it can connect, read the header data, etc, but during the transfer, the WiFi connection goes from run to init with code 3c0 (Is there somewhere I can find the meaning of these codes?).
OTA fails due to closed connection, WiFi is setup again, and the rest of the system just works as it is intended to. Same hold for when I don't do the OTA, WiFi works, no disconnects, I even have long lived HTTPS connections, and they stay connected for days.

Code: Select all

I (7177) network: connected
I (7177) wifi: sta_got_ip: 192.168.1.75
I (7177) esp_netif_handlers: sta ip: 192.168.1.75, mask: 255.255.255.0, gw: 192.168.1.1
I (7177) ota: check URL: https://firmware.esp32.com/latest
I (8867) wc: retreive response data
I (8867) wc: rec: 6, 2.1.0

I (8867) wc: rec: 0, 2.1.0

I (8867) ota: version available: 2.1.0
I (8877) ota: New version available (2.1.0)
I (8877) wifi:Set ps type: 0

I (10577) esp_https_ota: Starting OTA...
I (10577) esp_https_ota: Writing to partition subtype 16 at offset 0x110000
I (10587) ota: Running firmware version: 1
I (19317) wifi:state: run -> init (3c0)
I (19317) wifi:pm stop, total sleep time: 2626757 us / 14019959 us

I (19317) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (19327) network: disconnected
I (19327) wifi: wifi_disconnected
I (19327) wifi: retry connect to ap
E (19337) esp-tls-mbedtls: read error :-76:
E (19337) TRANS_SSL: esp_tls_conn_read error, errno=Software caused connection abort
W (19347) HTTP_CLIENT: esp_transport_read returned:-76 and errno:113 
E (19357) TRANS_SSL: ssl_poll_read select error 113, errno = Software caused connection abort, fd = 54
E (19367) ota: Complete data was not received.
I (19367) esp_image: segment 0: paddr=0x00110020 vaddr=0x3f400020 size=0x228bc (141500) map
I (19427) esp_image: segment 1: paddr=0x001328e4 vaddr=0x3ffb0000 size=0x033e0 ( 13280) 
I (19437) esp_image: segment 2: paddr=0x00135ccc vaddr=0x40080000 size=0x00404 (  1028) 
0x40080000: _WindowOverflow4 at /home/bosger/projects/private/esp32/esp-idf/components/freertos/xtensa/xtensa_vectors.S:1730

I (19437) esp_image: segment 3: paddr=0x001360d8 vaddr=0x40080404 size=0x09f40 ( 40768) 
I (19457) wifi:new:<11,0>, old:<11,0>, ap:<255,255>, sta:<11,0>, prof:1
I (19467) wifi:I (19467) esp_image: segment 4: paddr=0x00140020 vaddr=0x400d0020 size=0x9a560 (632160) map
0x400d0020: _stext at ??:?

state: init -> auth (b0)
I (19477) wifi:state: auth -> assoc (0)
I (19477) wifi:state: assoc -> run (10)
I (19487) wifi:connected with myssid, aid = 2, channel 11, BW20, bssid = f4:92:bf:a0:bd:20
I (19487) wifi:security: WPA2-PSK, phy: bgn, rssi: -63
I (19497) wifi:pm start, type: 0

I (19517) wifi:AP's beacon interval = 102400 us, DTIM period = 3
E (19687) esp_image: invalid segment length 0xffffffff
E (19697) ota: Image validation failed, image is corrupted
E (19697) ota: ESP_HTTPS_OTA upgrade failed 5379
I (19697) wifi:Set ps type: 1

I (20177) network: connected
I (20177) wifi: sta_got_ip: 192.168.1.75

gerardbos
Posts: 5
Joined: Wed Jun 13, 2018 1:06 pm

Re: Wifi disconnects

Postby gerardbos » Fri Jan 29, 2021 11:03 am

By the way, my WiFi code is based on the following example: https://github.com/espressif/esp-idf/tr ... ed/station

And even though it is reshuffled a bit, it works fine for everything, except the OTA update. I have the idea that the OTA update triggers something in the WiFi code, but I can't pinpoint it.

gerardbos
Posts: 5
Joined: Wed Jun 13, 2018 1:06 pm

Re: Wifi disconnects

Postby gerardbos » Mon Feb 01, 2021 3:50 pm

I finally found the issue, after extensive debugging and version compare between the different WiFi examples.

The difference is in the sta config:

Code: Select all

                wifi_config_t sta_config = {0};

                esp_netif_t * ap_if = esp_netif_create_default_wifi_sta();
                esp_netif_set_hostname(ap_if, wifi_hostname);

                memcpy(sta_config.sta.ssid, ssid, strlen(ssid) + 1); 
                memcpy(sta_config.sta.password, password, strlen(password) + 1); 
                sta_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
                // although my APs are capable, disable PMF for now, as it gives problems with huge data loads (ota)
                // When PMF is disabled, we can't connect to WPA3, so this can be disabled in the menuconfig as well
                sta_config.sta.pmf_cfg.capable = false;
                sta_config.sta.pmf_cfg.required = false;

                log(INFO, TAG, "Setting up sta mode to SSID: %s, with password: %s", sta_config.sta.ssid, sta_config.sta.password);
                err = esp_wifi_set_config(ESP_IF_WIFI_STA, &sta_config);
In the example I've used, the pmf_cfg.capable is true (which is not the case in the example_connect version).
As I have a pretty new AP in my home (UniFi 6 Lite), which should be able to support PMF but fails, disabling PMF solves the issue.

As for now, I don't know if the issue is because of the AP or because of the ESP32. If more research is needed, please let me know.

Who is online

Users browsing this forum: Google [Bot] and 126 guests