You can chat about native SDK questions and issues here.

User avatar
By electr0dave
#65858 Hello.

I need to make a very low power application. I need to connect maybe, every 30min, to an AP and do the data uploads. The ESP8266 will be controlled by another MCU and so only when the other MCU gives power to ESP it starts up.

But I'm taking about 2.5s (or more) until I get IP.

Can you guys help me get in the way of connecting faster?

I can not have static IP because the final equipment will be mobile and may have to "jump" between AP's.


PS: I noticed that if I connect to the AP once and then reprogram the ESP with the init_user empty, in the terminal the information appears, after 0.8s, that I have IP (in the best cases) ... but it is not constant.



Thank you.


Code: Select allstatic void ICACHE_FLASH_ATTR wifi_check_ip(void *arg)
{
   struct ip_info ipConfig;

   os_printf("TIMER callback\r\n");

   //verifica o estado da ligação
   switch(wifi_station_get_connect_status())
   {
      case STATION_GOT_IP:

         os_timer_disarm(&tim0);

         wifi_get_ip_info(STATION_IF, &ipConfig);

         os_printf("IP:" IPSTR ", MASK:" IPSTR ", GATEWAY:" IPSTR,
                  IP2STR(&ipConfig.ip),
                  IP2STR(&ipConfig.netmask),
                  IP2STR(&ipConfig.gw));
                  os_printf("\n");

         wifi_set_sleep_type(LIGHT_SLEEP_T);

         os_timer_setfn(&tim0, (os_timer_func_t *)setup_wifi_st_mode, NULL);
         os_timer_arm(&tim0, 10000, 0);

         break;
   }
}

//setup do wifi em station mode
void setup_wifi_st_mode(void)
{
        os_timer_disarm(&tim0);

   if(wifi_get_opmode() != STATION_MODE)
   {
      os_printf("-> OpMode != STATION MODE\r\n");
      wifi_set_opmode(STATION_MODE);
   }
   else
      os_printf("-> OpMode == STATION MODE\r\n");

   struct station_config stconfig;
   os_printf("-> Read stored config\r\n");
   wifi_station_get_config(&stconfig);
   os_printf("SSID (flash): %s\r\n", stconfig.ssid);
   os_printf("PASSWORD (flash): %s\r\n", stconfig.password);

   //bad configuration in memory?
   if(os_strcmp(stconfig.ssid, WIFI_CLIENTSSID) != 0)
   {
      os_printf("-> Stored config is wrong!\r\n");
      //stop all
      wifi_station_disconnect();
      wifi_station_dhcpc_stop();

      //conigure connection
      os_memset(stconfig.ssid, 0, sizeof(stconfig.ssid));
      os_memset(stconfig.password, 0, sizeof(stconfig.password));
      os_sprintf(stconfig.ssid, "%s", WIFI_CLIENTSSID);
      os_sprintf(stconfig.password, "%s", WIFI_CLIENTPASSWORD);
      wifi_station_set_config(&stconfig);

      //init station
      wifi_station_connect();
      wifi_station_dhcpc_start();
      wifi_station_set_auto_connect(1);
   }
   else
      os_printf("-> Stored config is right!\r\n");

   //init timer to verify the connection
   //os_timer_disarm(&tim0);
   os_timer_setfn(&tim0, (os_timer_func_t *)wifi_check_ip, NULL);
   os_timer_arm(&tim0, IP_SCAN_MS, 1);   //repetição a 500mS

}


uint32 ICACHE_FLASH_ATTR 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 - 8;
            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;

        default:
            rf_cal_sec = 0;
            break;
    }

    return rf_cal_sec;
}

void ICACHE_FLASH_ATTR user_rf_pre_init(void)
{
   //No RF calibration after deep-sleep wake up; this reduces the current consumption
   system_phy_set_rfoption(2);
}

void ICACHE_FLASH_ATTR user_init(void)
{
    uart_init(115200, 115200);
    os_delay_us(100);

    os_printf("\r\nSDK version: %s \r\n", system_get_sdk_version());

    os_printf("GET auto connect: %d\r\n",wifi_station_get_auto_connect());

    wifi_set_sleep_type(LIGHT_SLEEP_T);

    os_printf("-> Setup WIFI in STATION mode\n");
    setup_wifi_st_mode();
}
User avatar
By rudy
#65942 What I think is that the ESP does a scan of all channels looking for the AP. I think that you can specify the channel number and this would probably eliminate the scanning part.

I know none of this for certain. I also don't do anything with the native SDK.
User avatar
By electr0dave
#65943 Yes this is a hypothesis, but I get the scandone message after ~ 200mS. It's not too bad. I think where's a lot of time is in DHCP ...

With the native SDK, before the user_init is called it starts scanning. I have no control in this process.
If I do wifi_set_channel (2) (channel 2) the results start to be worse ...