Need help for fast connection in station mode

electr0dave
Posts: 4
Joined: Mon May 15, 2017 9:40 pm

Need help for fast connection in station mode

Postby electr0dave » Mon May 15, 2017 9:44 pm

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 all

static 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
pratik
Posts: 361
Joined: Wed Jun 29, 2016 7:17 pm
Location: India
Contact:

Re: Need help for fast connection in station mode

Postby pratik » Thu May 18, 2017 12:45 pm

If you know the router Wi-Fi channel it is probably going to be much faster than usual. The long time duration is because when you specify connection to the AP, the ESP scans all channels to find the AP (takes ~2s) and then connects. The time is wasted in scanning and additional time is spent trying to connect.
You can connect faster to saved APs I think, if their channel has not changed. That's why you get lucky sometimes I guess, with short connection time.
Try not to save AP settings to flash and see if that helps (use current_config APIs, not default_config).
Regards,
Pratik Panda
Website: http://www.PratikPanda.com

Custom firmware, Knowledge base and freelancing (ESP8266/ESP32):
http://www.iot-bits.com

electr0dave
Posts: 4
Joined: Mon May 15, 2017 9:40 pm

Re: Need help for fast connection in station mode

Postby electr0dave » Thu May 18, 2017 10:12 pm

Thanks for the answer. I'll try to do this.

Do you know any way to know the lease time using the ESP8266?
When DHCP does offer, the message brings a lease time, but I can not access that value ...

User avatar
pratik
Posts: 361
Joined: Wed Jun 29, 2016 7:17 pm
Location: India
Contact:

Re: Need help for fast connection in station mode

Postby pratik » Fri May 19, 2017 5:33 pm

You may use the API wifi_softap_set_dhcps_lease_time() and wifi_softap_gset_dhcps_lease_time() to set and get the DHCP server lease time respectively. The default time is 120 min.
Regards,
Pratik Panda
Website: http://www.PratikPanda.com

Custom firmware, Knowledge base and freelancing (ESP8266/ESP32):
http://www.iot-bits.com

electr0dave
Posts: 4
Joined: Mon May 15, 2017 9:40 pm

Re: Need help for fast connection in station mode

Postby electr0dave » Mon May 22, 2017 4:52 pm

pratik wrote:You may use the API wifi_softap_set_dhcps_lease_time() and wifi_softap_gset_dhcps_lease_time() to set and get the DHCP server lease time respectively. The default time is 120 min.


But these APIs isn't just for softAP?
If I use wifi_softap_get_dhcps_lease_time(), in station mode, will I get the lease time of my IP?

What I'm looking for is something that should be called "wifi_station_get_dhcps_lease_time()" ...

electr0dave
Posts: 4
Joined: Mon May 15, 2017 9:40 pm

Re: Need help for fast connection in station mode

Postby electr0dave » Thu May 25, 2017 7:14 pm

This thread died? :cry: :cry:

User avatar
pratik
Posts: 361
Joined: Wed Jun 29, 2016 7:17 pm
Location: India
Contact:

Re: Need help for fast connection in station mode

Postby pratik » Sun May 28, 2017 11:07 pm

Nope, you probably cannot obtain the lease time of the router you are connected to. I guess that feature raises security issues as it is AP internal config.
Regards,
Pratik Panda
Website: http://www.PratikPanda.com

Custom firmware, Knowledge base and freelancing (ESP8266/ESP32):
http://www.iot-bits.com

Who is online

Users browsing this forum: No registered users and 4 guests