Broken Code with SDK 2.2 and newer

Inquisitor
Posts: 22
Joined: Thu Dec 14, 2017 10:53 am
Contact:

Broken Code with SDK 2.2 and newer

Postby Inquisitor » Mon Nov 29, 2021 12:52 am

The following code used to work great with SDK <= 2.1.0.
Can someone tell me what modifications I need to make to allow it to work with SDK > 2.2 and newer?

It is a simple Testing Web Server (only 230 lines). In old SDK (and current manual) espconn_regist_disconcb gets called and all works great. In 2.2 and newer it never gets called and fails after the 5th browser refresh.

Thanks.

Code: Select all

#include "user_interface.h"
#include "espconn.h"
#include "osapi.h"
#include "driver/uart.h"
#include "mem.h"

#define SSID "<your SSID>"
#define PW "<your Password>"
#define HOST "ws"
#define dbg(f, ...) os_printf(f, ## __VA_ARGS__)

static os_timer_t ptimer;

// This PAGE only sends a one time page.
const char* PAGE = "<!DOCTYPE html><html><head><title>Native Server Test</title> <meta name='viewport' content='width=device-width, initial-scale=1'></head><body>  <h1>Welcome to my Native Server Test</h1></body></html>";
// This PAGE sends back a page that keeps reloading, so we can check for errors and/or memory leaks.
// const char* PAGE = "<!DOCTYPE html><html><head><title>Native Server Test</title> <meta name='viewport' content='width=device-width, initial-scale=1'><script>window.addEventListener('load',()=>{ setTimeout(()=>{location.reload();},500);},false);</script></head><body>  <h1>Welcome to my Native Server Test</h1></body></html>";

ICACHE_FLASH_ATTR void printIfErr(s8 esp, const char* msg)
{
    if (esp)
        os_printf("ERR - %s = %d\n", msg, esp);
}

ICACHE_FLASH_ATTR void hack(const char* msg)
{
    static u32 last = 0;
    u32 now = system_get_time();
    if (msg)
        os_printf("%s=%uus ", msg, now - last);
    last = now;
}

ICACHE_FLASH_ATTR void onDisconnect(void* c)
{
    hack("onDisconnect");
}

ICACHE_FLASH_ATTR void doDisconnect(void* c)
{
    hack("doDisconnect");

    struct espconn* conn = (struct espconn*)c;
   
    // ******** THIS IS WHERE I THINK THE PROBLEM IS ******************
    // In SDK <= 2.1.0 espconn_disconnect
    //        * does disconnect
    //        * does call espconn_regist_disconcb callback
    //        * does allow new connection after the first 5
    // In SDK > 2.1.0 espconn_disconnect
    //        * does NOT disconnect
    //        * does NOT call espconn_regist_disconcb callback
    //        * does NOT allow new connection after the first 5
   
    printIfErr(espconn_disconnect(conn), "disconnect");
}

ICACHE_FLASH_ATTR void onSent(void* c)
{
    hack("onSent");

    struct espconn* conn = (struct espconn*)c;

    os_timer_disarm(&ptimer);
    os_timer_setfn(&ptimer, (os_timer_func_t *)doDisconnect, conn);
    os_timer_arm(&ptimer, 5, 0);
}

ICACHE_FLASH_ATTR void onReceive(void* c, char* buf, u16 length)
{
    hack("onReceive");

    struct espconn* conn = (struct espconn*)c;

    // Print out the request just to make sure its what we think it is.
    static char buffer[1024];
    os_memcpy(buffer, buf, length);
    *(buffer + length) = 0;
    // os_printf(buffer);
    os_sprintf(buffer, "HTTP/1.1 200 OK\r\n"
        "Server: InqPortal/5.0\r\n"
        "Content-Length: %d\r\n"
        "Content-type: text/html\r\n"
        "Pragma: no-cache\r\n\r\n%s", strlen(PAGE), PAGE);
    printIfErr(espconn_send(conn, (u8*)buffer, strlen(buffer)), "send");   
}

ICACHE_FLASH_ATTR void onConnection(void* c)
{
    hack(NULL);

    static u32 cnt = 0;
    os_printf("\n%3u ", ++cnt);   
   
    struct espconn* conn = (struct espconn*)c;
    espconn_regist_recvcb(conn, onReceive);
    espconn_regist_sentcb(conn, onSent);
    espconn_regist_disconcb(conn, onDisconnect);   
}

ICACHE_FLASH_ATTR void startServer()
{
    // Setup and start web server listener
    LOCAL struct espconn listen;
    LOCAL esp_tcp tcp;
    os_memset(&listen, 0, sizeof(struct espconn));
    listen.type = ESPCONN_TCP;
    listen.state = ESPCONN_NONE;   
    listen.proto.tcp = &tcp;
    listen.proto.tcp->local_port = 80;
   
    espconn_regist_connectcb(&listen, onConnection); 
       
    printIfErr(espconn_accept(&listen), "listen");   
}

ICACHE_FLASH_ATTR void chkAP(void* arg)
{
    if (wifi_station_get_connect_status() != STATION_GOT_IP)
    {
        os_printf(".");
        return;
    }
    os_timer_disarm(&ptimer);

    struct ip_info info;
    wifi_get_ip_info(STATION_IF, &info);
    // This is just cook-book Station connection stuff.
    dbg("\n\nSDK version: %s\n", system_get_sdk_version());

    os_printf("\nBrowse to (http://%s/index.html) or (http://" IPSTR
        "/index.html)\n",
        HOST, IP2STR(&(info.ip)));
}

ICACHE_FLASH_ATTR void startStation()
{
    os_printf("\nConnecting to your router");

    struct station_config sc;
    os_memset(&sc, 0, sizeof(struct station_config));
    sc.bssid_set = 0;
    os_memcpy(&sc.ssid, SSID, 32);
    os_memcpy(&sc.password, PW, 64);   
    wifi_station_set_config_current(&sc);

    wifi_station_set_hostname(HOST);

    wifi_station_connect();

    os_timer_disarm(&ptimer);
    os_timer_setfn(&ptimer, (os_timer_func_t *)chkAP, NULL);
    os_timer_arm(&ptimer, 1000, 1);
}

ICACHE_FLASH_ATTR void startSoftAP()
{   
    // Start up the communications Host/Station and Client/Soft
    // ssid - Not NULL terminated in code - Can be 32 characters coming in! 
    // Password length must be blank OR >= 8!  Truncated if >= 64 characters.

    // Address
    wifi_softap_dhcps_stop();

    struct ip_info info;
    os_memset(&info, 0, sizeof(struct ip_info));
    // Don't set Gateway since we can't offer Internet / DNS
    // We're hardcoding our InqPortal server to always be 10.10.10.10.
    IP4_ADDR(&info.ip, 10, 10, 10, 10);   
    IP4_ADDR(&info.gw, 10, 10, 10, 10);   
    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
   
    wifi_set_ip_info(SOFTAP_IF, &info);
   
    // Start up the DHCP server.
    wifi_softap_dhcps_start();
   
    struct softap_config cfg;
    os_memset((u8*)&cfg, 0, sizeof(struct softap_config));
   
    os_sprintf((char*)cfg.ssid, "%s-%X",
        "ESP8266", system_get_chip_id());
    cfg.ssid_len = strlen((char*)cfg.ssid);   
    // Password is null terminated (no length) specified.  Use this filling.
    os_sprintf((char*)cfg.password, "");
    cfg.channel = 1;
    cfg.authmode = AUTH_OPEN;
    cfg.ssid_hidden = 0;
    cfg.max_connection = 4;    // Note: default 4, max 4
    cfg.beacon_interval = 100; // Note: support 100 ~ 60000 ms, default 100   
   
    wifi_softap_set_config(&cfg);

    wifi_set_sleep_type(NONE_SLEEP_T);
}

ICACHE_FLASH_ATTR void user_init(void)
{
    uart_init(BIT_RATE_115200, BIT_RATE_115200);

    wifi_set_opmode(STATIONAP_MODE);

    startSoftAP();
    startStation();

    startServer();
   
   os_printf("Ready\n");
}

ICACHE_FLASH_ATTR uint32 user_rf_cal_sector_set(void)
{
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 5;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        case FLASH_SIZE_64M_MAP_1024_1024:
            rf_cal_sec = 2048 - 5;
            break;
        case FLASH_SIZE_128M_MAP_1024_1024:
            rf_cal_sec = 4096 - 5;
            break;
        default:
            rf_cal_sec = 0;
            break;
    }
    return rf_cal_sec;
}

3 lines of code = App/Web Server w/ GUI Admin, File Mngr, OTA, AP Mgr, Perf Metrics, WebSocket Comms, App API, All running on ESP8266... Even usable on ESP-01S
https://www.esp8266.com/viewtopic.php?f=11&t=23535
https://InqOnThat.com

AgentSmithers
Posts: 195
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby AgentSmithers » Thu Dec 09, 2021 4:59 pm

I recall a Wifi issue for myself with SDK 2.1 to 2.2 back when I upgraded. It had to do with the sleep function or configuration APis of the Wifi, (They might have just been added in that SDK version 2.2) I think in 2.1 there may have been left over Init Params that may jack with the 2.2 wifi Params but I don't have the technicals on it. From what I recall I was using pfalcon's github setup and when I moved from 2.1 to 2.2 it broke my station code. I remember what I did was I opened the PDF for the "ESP8266 Non-OS SDK
API Reference" for 2.2 and compared it to this below (version 2.0, look for 2.1 if you can, I couldn't find it online)
http://www.liot.io/media/liot_esp8266_e ... erence.pdf

In 2.2 I think if my memory serves me correctly they add a few new API commands for the 802.11 to adjust power savings to some extent. I would add those API's into your AP init code and toggle with those Params and see if it resolves your issue. If it does post back here so everyone else can see the result!

Also, do a full memory purge with esp_tool erase and reflash your 'esp_init_data_default' files to make sure everything is clean.

That should get you back and running.

Good Luck!

Inquisitor
Posts: 22
Joined: Thu Dec 14, 2017 10:53 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby Inquisitor » Sat Dec 11, 2021 5:00 am

Thank you for responding. I first posted this problem back in 2017 and you're the first to respond. This thread is a re-posting with as simple an example as I could create hoping someone would take pity on me. :?

AgentSmithers wrote:...
API Reference" for 2.2 and compared it to this below (version 2.0, look for 2.1 if you can, I couldn't find it online)
http://www.liot.io/media/liot_esp8266_e ... erence.pdf

In 2.2 I think if my memory serves me correctly they add a few new API commands for the 802.11 to adjust power savings to some extent. I would add those API's into your AP init code and toggle with those Params and see if it resolves your issue. If it does post back here so everyone else can see the result!


Last week in desperation, I did resort to looking for old documentation. I did not find any. Must have had the wrong search terms. So this document was a great find. Since your post, I have been scouring through the differences and trying various parameters. I would not have even tried things related to power settings. At this point, I'd try anything.

I think what bothers me the most is that no one else has this problem. I am beginning to think it has more to do with the Arduino implementation riding on top of the Espressif code base. I have tried...
  1. A totally NonOS version using VSCode/PlatformIO which is at 2.1.0. It works fine, showing the espconn_regist_disconcb() being called.
    good.png
  2. Unfortunately, I can't seem to find a way to make PlatformIO use a newer SDK. So, I have not found a way to test the above code with a true, unmodified SDK like 3.0+.
  3. I have tested it with the NonOS modified version in Arduino IDE that uses SDK 1.5 and again, it works fine the same as above.
  4. As soon as I move to the newer Arduino IDE versions that uses Espressif version 2.2 through 3.0 SDK, it serves up the web page five times and fails on the sixth request. The Browser shows a "This site can’t be reached The connection was reset."
    bad.png

Currently, I'm now focusing on what settings the Arduino version modifies that might cause this to fail. But again, I'm stuck with the lack of complaints on the Arduino forum. There differences seem to be that all their code is based on lwIP while the above code avoids even using that 3rd party library.

Thanks again!
VBR
3 lines of code = App/Web Server w/ GUI Admin, File Mngr, OTA, AP Mgr, Perf Metrics, WebSocket Comms, App API, All running on ESP8266... Even usable on ESP-01S
https://www.esp8266.com/viewtopic.php?f=11&t=23535
https://InqOnThat.com

AgentSmithers
Posts: 195
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby AgentSmithers » Tue Dec 14, 2021 5:02 am

Ah! I feel your pain, I used Pfalcons tool chain and had a hell of a time getting to Version 3.0+.
I have a VM premade with the environment setup in cent os (for this exact problem), Ill get it uploaded to Googledrive and you can download it and give it a run in Oracle box and move your files to it or from a share on your host OS. Its a complete setup of the latest SDK for NON-OS. Would that give you a hand?

Inquisitor
Posts: 22
Joined: Thu Dec 14, 2017 10:53 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby Inquisitor » Wed Dec 15, 2021 11:28 pm

AgentSmithers wrote:Ah! I feel your pain, I used Pfalcons tool chain and had a hell of a time getting to Version 3.0+.
I have a VM premade with the environment setup in cent os (for this exact problem), Ill get it uploaded to Googledrive and you can download it and give it a run in Oracle box and move your files to it or from a share on your host OS. Its a complete setup of the latest SDK for NON-OS. Would that give you a hand?


Sure! That would be very helpful. At the very least it would confirm whether my program above works on pure Espressif SDK > 2.1. It would also confirm my suspicions that it's the Arduino libraries riding on top of Espressif SDK > 2.1 causing the problem.

Thank you for your help.
3 lines of code = App/Web Server w/ GUI Admin, File Mngr, OTA, AP Mgr, Perf Metrics, WebSocket Comms, App API, All running on ESP8266... Even usable on ESP-01S
https://www.esp8266.com/viewtopic.php?f=11&t=23535
https://InqOnThat.com

AgentSmithers
Posts: 195
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby AgentSmithers » Thu Dec 16, 2021 6:02 am

Yeah not a problem! I'm packed with school finals and work but inbetween Ill prep the ova and get it uploaded for you and will post back here with the link. You can give it a go then. Hopefully it will help you narrow down the problem.

AgentSmithers
Posts: 195
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby AgentSmithers » Thu Dec 16, 2021 6:03 am

Prepping it Should only take me 2-3 days to get it done.

AgentSmithers
Posts: 195
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby AgentSmithers » Wed Dec 22, 2021 6:53 am

Link as promised!
Download OracleBox and you can import the OVA.
https://drive.google.com/file/d/1JrA750 ... sp=sharing
Then import you project folder and compile and your good to go!
Let me know if you need help but that is a fully setup dev. If you need a test project if your having issues let me know and Ill zip one up with a makefile and send that over too.

Inquisitor
Posts: 22
Joined: Thu Dec 14, 2017 10:53 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby Inquisitor » Wed Dec 22, 2021 6:41 pm

AgentSmithers wrote:Link as promised!
Download OracleBox and you can import the OVA.
https://drive.google.com/file/d/1JrA750 ... sp=sharing
Then import you project folder and compile and your good to go!
Let me know if you need help but that is a fully setup dev. If you need a test project if your having issues let me know and Ill zip one up with a makefile and send that over too.


Thank you for all your time. I hope I didn't take you away from festivities. :?
Later today, I'll go into town and download this at the library. If I remember correctly, these images are multi-gigabytes. I'm on a metered connection here at the house and with everyone home this time of year, my download is often below 1 Mbit and sometime it drops. :( I'll report back with progress.
3 lines of code = App/Web Server w/ GUI Admin, File Mngr, OTA, AP Mgr, Perf Metrics, WebSocket Comms, App API, All running on ESP8266... Even usable on ESP-01S
https://www.esp8266.com/viewtopic.php?f=11&t=23535
https://InqOnThat.com

AgentSmithers
Posts: 195
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Broken Code with SDK 2.2 and newer

Postby AgentSmithers » Thu Dec 23, 2021 8:06 am

Inquisitor wrote:
AgentSmithers wrote:Link as promised!
Download OracleBox and you can import the OVA.
https://drive.google.com/file/d/1JrA750 ... sp=sharing
Then import you project folder and compile and your good to go!
Let me know if you need help but that is a fully setup dev. If you need a test project if your having issues let me know and Ill zip one up with a makefile and send that over too.


Thank you for all your time. I hope I didn't take you away from festivities. :?
Later today, I'll go into town and download this at the library. If I remember correctly, these images are multi-gigabytes. I'm on a metered connection here at the house and with everyone home this time of year, my download is often below 1 Mbit and sometime it drops. :( I'll report back with progress.



LMAO, no your fine.. I should of packed up a thumbdrive and sent over a carrier pigeon lol. I'll keep watch for your reply if you need anything else. I still use this OVA today for alot of my programming so should work like a charm.

Who is online

Users browsing this forum: No registered users and 186 guests