Initial Setup to AP and reInit to STA does not work properly

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

Initial Setup to AP and reInit to STA does not work properly

Postby kyrpav » Sun Oct 10, 2021 9:32 am

I have created a small test example that esp wifi starts as AP if there are not credentials for ssid and pass in nvs.
Then from html you can set the ssid and pass to login and wifi is reinitialized to switch from AP to STA.

I a using esp-idf v4.4

As process the issues are:

After erase_flash ,flash and monitor the esp start to setup as AP.
At that moment before wifi_start i try to get wifi mode and the netif_config_t and i get

Code: Select all

W (660) WIFI_INIT: After reInit of mode
W (670) WIFI_INIT: wifi mode is null
W (670) WIFI_INIT: desc: ap
W (680) WIFI_INIT: key: WIFI_AP_DEF
After wifi start esp is AP but the ssid is not the same from what set in config. ssid of AP is ESP_C845D5 while in program was EspSSID , password works and i can login.

One more thing here i do not like the way wifi_ap_config_t is created but i can not use .ssid cause i get
"message": "C99 designator 'ssid' outside aggregate initializer"
so i put it immediately as you will see in code. And in order not to set all the params i set authmode outside.


After this normal login of laptop to esp happens and also normally with form i am passing the ssid and the pass to setup the wifi connection. Then wifi init happens again.

Code: Select all

I (850) CONNECTION: Processing...
I (860) SERVER: Starting server...

I (860) CONNECTION: Registered Endpoints...
I (11710) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
I (11710) wifi:station: f4:5c:89:c8:e2:77 join, AID=1, bgn, 20
W (11760) wifi:<ba-add>idx:4 (ifx:1, f4:5c:89:c8:e2:77), tid:6, ssn:0, winSize:64
I (11780) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2
At this time esp goes to setup as STA, i destroy previous netif and create default of STA.

Code: Select all

I (54950) wifi:flush txq
I (54950) wifi:stop sw txq
I (54950) wifi:lmac stop hw txq
W (54950) ON STA INIT: inside STA
W (54950) ON STA INIT: CRED ssid: MyWeb24, pwd: ComeAndGetIt
W (54950) ON STA INIT: netif is AP and we reset
W (54960) ON STA INIT: in config ssid: MyWeb24, pass: ComeAndGetIt
Then the same log happens after the end of sta setup for the wifi mode and the netif. Wifi mode is still null but netif is changed but wifi mode when esp starts on the last row is again softAP and not STA

Code: Select all


W (55000) WIFI_INIT: After reInit of mode
W (55010) WIFI_INIT: wifi mode is null
W (55010) WIFI_INIT: desc: sta
W (55020) WIFI_INIT: key: WIFI_STA_DEF
I (55020) wifi:mode : softAP (f0:08:d1:c8:45:d5)
Next is a guru error crash and a restart

Code: Select all

Guru Meditation Error: Core  0 panic'ed (InstrFetchProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x00000000  PS      : 0x00060830  A0      : 0x800e6154  A1      : 0x3ffc2210  
A2      : 0x3ffbfe78  A3      : 0x3ffcae44  A4      : 0x0000002a  A5      : 0x3ffcadb8  
A6      : 0x000000a5  A7      : 0x40084df0  A8      : 0x80149e1c  A9      : 0x3ffb23a8  
0x40084df0: task_yield_from_isr_wrapper at /Users/kyrpav/esp/esp-idf/components/esp_wifi/esp32/esp_adapter.c:243

A10     : 0x00000000  A11     : 0x3ffcae44  A12     : 0x0000002a  A13     : 0x3ffcadb8  
A14     : 0x3ffb3a98  A15     : 0x3ffb0b30  SAR     : 0x00000010  EXCCAUSE: 0x00000014  
EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  


Backtrace:0xfffffffd:0x3ffc22100x400e6151:0x3ffc2230 0x4010671d:0x3ffc2250 0x4010720b:0x3ffc2350 0x40092d9d:0x3ffc2370 0x40090be4:0x3ffc2390 0x4008b991:0x3ffc23c0 
0x400e6151: wifi_ap_receive at /Users/kyrpav/esp/esp-idf/components/esp_wifi/src/wifi_netif.c:52

0x4010671d: hostap_input at ??:?

0x4010720b: ap_rx_cb at ??:?

0x40092d9d: ppRxPkt at ??:?

0x40090be4: ppTask at ??:?

0x4008b991: vPortTaskWrapper at /Users/kyrpav/esp/esp-idf/components/freertos/port/xtensa/port.c:159
After reboot esp tries to setup as STA credentials still exist in nvs but, netif can not be identified either as STA either as AP

Code: Select all

W (20) ON STA INIT: inside STA
W (20) ON STA INIT: CRED ssid: MyWeb24, pwd: ComeAndGetIt
W (20) ON STA INIT: netif is not AP
W (30) ON STA INIT: netif is not STA also
W (30) ON STA INIT: in config ssid: MyWeb24, pass: ComeAndGetIt
Of course after these i get wifi mode null and no netif log cause it is null

Code: Select all

W (699) WIFI_INIT: After reInit of mode
W (699) WIFI_INIT: wifi mode is null
I (709) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
I (809) wifi:mode : softAP (f0:08:d1:c8:45:d5)
I am giving you the complete log and these 3 functions with the wifi init and the two setups for sta and ap.

As you see something is completely wrong. Can someone show me the proper fixes?

Thanx
sta-ap_log.c
(12.59 KiB) Downloaded 494 times
connectInit.cpp
(4.05 KiB) Downloaded 489 times

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

Re: Initial Setup to AP and reInit to STA does not work properly

Postby kyrpav » Thu Oct 14, 2021 7:17 am

After several tests and check of the ap examples and the order of the functions called.

I have this working with some questions remain though. So solution is to put an intermediate commonInit function in order to place something call from both functions before i set the wifi_config_t

Wifi starts as AP and i can see the proper ssid. After setting the pass and ssid it switches to sta. The remaining issue is that when in every case of the process from the main ap i try to read the wifi mode it is null even if i set it now separately in each specific function of connect as sta or connect as ap.
Of course then the normal esp32 log shows that connections is softap or sta so system is working.
Code is like

Code: Select all

void connectSTA(char *ssid, char *pass)
{
    ESP_LOGW(TAG6, "inside STA");
    if (ssid != NULL && pass != NULL)
    {
        ESP_LOGW(TAG6,"CRED ssid: %s, pwd: %s", ssid, pass);
        esp_netif_t *netifConf = NULL;
        netifConf = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
        if (netifConf == NULL)
        {
            ESP_LOGW(TAG6, "netif is not AP");
            netifConf = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
            if(netifConf==NULL){
                ESP_LOGW(TAG6, "netif is not STA also");
            }else{
                ESP_LOGW(TAG6, "netif is STA");
            }
        }
        else
        {   
            ESP_LOGW(TAG6, "netif is AP and we reset");
            esp_netif_destroy_default_wifi(netifConf);
            esp_netif_create_default_wifi_sta();
            commonWifiInit();
        }

        wifi_config_t wifi_configSta;
        memset(&wifi_configSta, 0, sizeof(wifi_configSta));
        strcpy((char *)wifi_configSta.sta.ssid, ssid);
        strcpy((char *)wifi_configSta.sta.password, pass);
        ESP_LOGW(TAG6, "in config ssid: %s, pass: %s", wifi_configSta.sta.ssid, wifi_configSta.sta.password);
        esp_wifi_set_mode(WIFI_MODE_STA);
        esp_wifi_set_config(WIFI_IF_STA, &wifi_configSta);
    }
    else
    {
        ESP_LOGW(TAG6, "STA connection unconfigured");
    }
}

void connectAP()
{
    ESP_LOGW(TAG7, "inside AP");

    esp_netif_create_default_wifi_ap();
    commonWifiInit();
 
    wifi_ap_config_t ap_config={
        "EspSSID3",
        "Polycom!",
        sizeof("EspSSID3"),  
        11,
        WIFI_AUTH_WPA_WPA2_PSK,
        0,
        10,
        100,
        WIFI_CIPHER_TYPE_TKIP,
        false
    };
    
    

    wifi_config_t wifi_config={
        .ap=ap_config
    };

    esp_wifi_set_mode(WIFI_MODE_AP);
    esp_wifi_set_config(WIFI_IF_AP, &wifi_config);

}

void commonWifiInit(){
        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_register(WIFI_EVENT, ESP_EVENT_ANY_ID, my_event_handler, NULL));
        ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, my_event_handler, NULL));
        ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
}

void wifiInit2(void *params)
{

    ESP_ERROR_CHECK(nvs_flash_init());
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());

    
    while (true)
    {
        if (xSemaphoreTake(wifiInitSemaphore, portMAX_DELAY))
        {

            nvs_handle_t nvs;
            nvs_open("nvsCreds", NVS_READWRITE, &nvs);

            size_t ssidLen, passLen;
            char *ssid = NULL, *pass = NULL;

            if (nvs_get_str(nvs, "ssid", NULL, &ssidLen) == ESP_OK)
            {
                if (ssidLen > 0)
                {
                    ssid = (char *)malloc(ssidLen);
                    nvs_get_str(nvs, "ssid", ssid, &ssidLen);
                }
                if (nvs_get_str(nvs, "pass", NULL, &passLen) == ESP_OK)
                {
                    if (passLen > 0)
                    {
                        pass = (char *)malloc(passLen);
                        nvs_get_str(nvs, "pass", pass, &passLen);
                    }
                }
            }

            if (ssid != NULL && pass != NULL)
            {
                connectSTA(ssid, pass);
            }
            else
            {
                connectAP();
            }

            
            
            
            
            ESP_LOGW(TAG5, "After reInit of mode");
            wifi_mode_t *wifiMode=NULL;
            esp_wifi_get_mode(wifiMode);
            if(wifiMode!=NULL){
                ESP_LOGW(TAG5, "key: %s", (char *)wifiMode);
            }else{
                ESP_LOGW(TAG5, "wifi mode is null");
            }
            esp_netif_t *apNetIf = NULL;
            while ((apNetIf = esp_netif_next(apNetIf)) != NULL)
            {
                ESP_LOGW(TAG5, "desc: %s", esp_netif_get_desc(apNetIf));
                ESP_LOGW(TAG5, "key: %s", esp_netif_get_ifkey(apNetIf));
                
            }
            
            ESP_ERROR_CHECK(esp_wifi_start());
            xSemaphoreGive(connectionSemaphore);
            if (ssid != NULL)
                free(ssid);
            if (pass != NULL)
                free(pass);
        }
    }
}
And the log is :
I (27) boot: ESP-IDF v4.4-dev-3042-g220590d599 2nd stage bootloader
I (27) boot: compile time 08:18:47
I (27) boot: chip revision: 1
I (32) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (39) boot.esp32: SPI Speed : 40MHz
I (43) boot.esp32: SPI Mode : DIO
I (48) boot.esp32: SPI Flash Size : 4MB
I (52) boot: Enabling RNG early entropy source...
I (58) boot: Partition Table:
I (61) boot: ## Label Usage Type ST Offset Length
I (69) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (76) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (84) boot: 2 factory factory app 00 00 00010000 00100000
I (91) boot: 3 spiffs Unknown data 01 82 00110000 00100000
I (99) boot: End of partition table
I (103) boot_comm: chip revision: 1, min. application chip revision: 0
I (110) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=18f70h (102256) map
I (156) esp_image: segment 1: paddr=00028f98 vaddr=3ffb0000 size=03834h ( 14388) load
I (161) esp_image: segment 2: paddr=0002c7d4 vaddr=40080000 size=03844h ( 14404) load
I (168) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=7e794h (518036) map
I (357) esp_image: segment 4: paddr=000ae7bc vaddr=40083844 size=110d8h ( 69848) load
I (386) esp_image: segment 5: paddr=000bf89c vaddr=50000000 size=00010h ( 16) load
I (397) boot: Loaded app from partition at offset 0x10000
I (397) boot: Disabling RNG early entropy source...
I (409) cpu_start: Pro cpu up.
I (409) cpu_start: Starting app cpu, entry point is 0x40081208
0x40081208: call_start_cpu1 at /Users/kyrpav/esp/esp-idf/components/esp_system/port/cpu_start.c:155

I (0) cpu_start: App cpu up.
I (423) cpu_start: Pro cpu start user code
I (423) cpu_start: cpu freq: 160000000
I (423) cpu_start: Application information:
I (427) cpu_start: Project name: main
I (432) cpu_start: App version: 7f852c0-dirty
I (438) cpu_start: Compile time: Oct 10 2021 08:17:25
I (444) cpu_start: ELF file SHA256: aa1017d0702ac43b...
I (450) cpu_start: ESP-IDF: v4.4-dev-3042-g220590d599
I (456) heap_init: Initializing. RAM available for dynamic allocation:
I (463) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (469) heap_init: At 3FFB7590 len 00028A70 (162 KiB): DRAM
I (476) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (482) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (488) heap_init: At 4009491C len 0000B6E4 (45 KiB): IRAM
I (496) spi_flash: detected chip: generic
I (499) spi_flash: flash io: dio
I (505) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
W (550) ON AP INIT: inside AP
I (560) wifi:wifi driver task: 3ffc2414, prio:23, stack:6656, core=0
I (560) system_api: Base MAC address is not set
I (560) system_api: read default base MAC address from EFUSE
I (570) wifi:wifi firmware version: d683787
I (570) wifi:wifi certification version: v7.0
I (570) wifi:config NVS flash: enabled
I (570) wifi:config nano formating: disabled
I (580) wifi:Init data frame dynamic rx buffer num: 32
I (580) wifi:Init management frame dynamic rx buffer num: 32
I (590) wifi:Init management short buffer num: 32
I (590) wifi:Init dynamic tx buffer num: 32
I (600) wifi:Init static rx buffer size: 1600
I (600) wifi:Init static rx buffer num: 10
I (600) wifi:Init dynamic rx buffer num: 32
I (610) wifi_init: rx ba win: 6
I (610) wifi_init: tcpip mbox: 32
I (620) wifi_init: udp mbox: 6
I (620) wifi_init: tcp mbox: 6
I (620) wifi_init: tcp tx win: 5744
I (630) wifi_init: tcp rx win: 5744
I (630) wifi_init: tcp mss: 1440
I (640) wifi_init: WiFi IRAM OP enabled
I (640) wifi_init: WiFi RX IRAM OP enabled
W (1380) WIFI_INIT: After reInit of mode
W (1380) WIFI_INIT: wifi mode is null
W (1380) WIFI_INIT: desc: ap
W (1380) WIFI_INIT: key: WIFI_AP_DEF
I (1380) phy_init: phy_version 4670,719f9f6,Feb 18 2021,17:07:07
W (1390) phy_init: failed to load RF calibration data (0x1102), falling back to full calibration
I (1550) wifi:mode : softAP (f0:08:d1:c8:45:d5)
I (1550) wifi:Total power save buffer number: 16
I (1550) wifi:Init max length of beacon: 752/752
I (1550) wifi:Init max length of beacon: 752/752
I (1550) CONNECTION: Processing...
I (1560) SERVER: Starting server...

I (1570) CONNECTION: Registered Endpoints...
I (15940) wifi:new:<11,0>, old:<11,2>, ap:<11,2>, sta:<255,255>, prof:11
I (15940) wifi:station: f4:5c:89:c8:e2:77 join, AID=1, bg, 20
I (15990) esp_netif_lwip: DHCP server assigned IP to a station, IP is: 192.168.4.2
I (22130) SERVER: url accessed is: /
I (22270) PATH: path requested is: /spiffs/index.html
I (22320) SERVER: url accessed is: /style.css
I (22480) PATH: path requested is: /spiffs/style.css
I (22500) SERVER: url accessed is: /scripts.js
I (22710) PATH: path requested is: /spiffs/scripts.js
I (26880) SERVER: url accessed is: /config-wifi.html
I (27010) PATH: path requested is: /spiffs/config-wifi.html
I (27080) SERVER: url accessed is: /style.css
I (27240) PATH: path requested is: /spiffs/style.css
I (40990) SERVER: url accessed is: /api/setwifi
I (40990) FORM: ssid is: MyWeb24, pwd is : ComeAndGetIt
I (41020) SERVER: url accessed is: /wifi-set.html
I (41180) PATH: path requested is: /spiffs/wifi-set.html
I (41240) SERVER: url accessed is: /style.css
I (41380) PATH: path requested is: /spiffs/style.css
I (42000) wifi:station: f4:5c:89:c8:e2:77 leave, AID = 1, bss_flags is 655411, bss:0x3ffcaf00
I (42000) wifi:new:<11,0>, old:<11,0>, ap:<11,2>, sta:<255,255>, prof:11
W (42000) httpd_txrx: httpd_sock_err: error in recv : 113
W (42010) httpd_txrx: httpd_sock_err: error in recv : 113
I (42080) wifi:flush txq
I (42080) wifi:stop sw txq
I (42080) wifi:lmac stop hw txq
W (42080) ON STA INIT: inside STA
W (42080) ON STA INIT: CRED ssid: MyWeb24, pwd: ComeAndGetIt
W (42080) ON STA INIT: netif is AP and we reset
I (42090) wifi_init: rx ba win: 6
I (42090) wifi_init: tcpip mbox: 32
I (42100) wifi_init: udp mbox: 6
I (42100) wifi_init: tcp mbox: 6
I (42100) wifi_init: tcp tx win: 5744
I (42110) wifi_init: tcp rx win: 5744
I (42110) wifi_init: tcp mss: 1440
I (42120) wifi_init: WiFi IRAM OP enabled
I (42120) wifi_init: WiFi RX IRAM OP enabled
W (42130) event: handler already registered, overwriting
W (42130) ON STA INIT: in config ssid: MyWeb24, pass: ComeAndGetIt
W (42140) WIFI_INIT: After reInit of mode
W (42140) WIFI_INIT: wifi mode is null
W (42150) WIFI_INIT: desc: sta
W (42150) WIFI_INIT: key: WIFI_STA_DEF
I (42160) wifi:mode : sta (f0:08:d1:c8:45:d4)
I (42160) wifi:enable tsf
I (42160) CONNECTION: connecting...
I (42170) CONNECTION: connecting...
E (42170) wifi:sta is connecting, return error
I (42420) wifi:new:<3,1>, old:<1,0>, ap:<255,255>, sta:<3,1>, prof:11
I (43160) wifi:state: init -> auth (b0)
I (43160) wifi:state: auth -> assoc (0)
I (43160) wifi:state: assoc -> run (10)
I (43170) wifi:connected with MyWeb24, aid = 24, channel 3, 40U, bssid = f8:aa:3f:6d:e8:46
I (43170) wifi:security: WPA2-PSK, phy: bgn, rssi: -44
I (43180) wifi:pm start, type: 1

I (43180) CONNECTION: connected...
I (43190) CONNECTION: connected...
I (43220) wifi:AP's beacon interval = 204800 us, DTIM period = 1
W (43830) wifi:<ba-add>idx:0 (ifx:0, f8:aa:3f:6d:e8:46), tid:0, ssn:0, winSize:64
I (46040) CONNECTION: got ip...
I (46040) esp_netif_handlers: sta ip: 192.168.1.10, mask: 255.255.255.0, gw: 192.168.1.1
I (95660) SERVER: url accessed is: /
I (95830) PATH: path requested is: /spiffs/index.html
I (96060) SERVER: url accessed is: /style.css
I (96300) PATH: path requested is: /spiffs/style.css
I (96330)
Also there is one more question how can i fix this part of the config:

wifi_ap_config_t ap_config={
"EspSSID3",
"Polycom!",
sizeof("EspSSID3"),
11,
WIFI_AUTH_WPA_WPA2_PSK,
0,
10,
100,
WIFI_CIPHER_TYPE_TKIP,
false
};

and use the .ssid= "something", .password="" etc
cause in this case i get
{
"resource": "/Users/kyrpav/workspace/esp32/tutorials/LearnEsp32/main/Lesson9/connect.cpp",
"owner": "cpp",
"severity": 8,
"message": "C99 designator 'ssid' outside aggregate initializer",
"startLineNumber": 105,
"startColumn": 5,
"endLineNumber": 105,
"endColumn": 5
}

Who is online

Users browsing this forum: Bing [Bot], Majestic-12 [Bot] and 85 guests