Speedup connection time to AP. [How-To]

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jomas
Posts: 59
Joined: Mon Dec 25, 2017 1:48 pm
Location: Netherlands

Speedup connection time to AP. [How-To]

Post by jomas » Mon Dec 25, 2017 4:15 pm

I needed a method to speedup the connection time to my AP in case of a "Power on reset". I could not find anything about this in this
forum so I implemented it my own way. It is based on the documentation on http://espressif.com/en/support/download/documents

Document:
"Espressif FAQ" see question: "How to speed up connection to an AP after ESP8266 powers on?"

Basically you need to execute the command:
WRITE_PERI_REG(0x600011f4, 1 << 16 | channel); // channel is the channel you want to connect to.
somewhere in your C source code. You can write a new function which implements this or you can include the code in some existing
code. I choose to implement it in 'esp.deepsleep()' which resides in the file modesp.c in the micropython source code.
Btw, if you look at the C code of 'esp_deepsleep', you can see there is also a second parameter to set the "deep_sleep_set_option".
This parameter is not described in the Micopython Documentation. However, it is needed to be set to '2' (no RF calibration at startup,
see "ESP8266 Low Power Solutions" on the Espressif website) to further speedup the connection time.

So here are the steps I did:

1. Download micropython source code.

2. Change function esp_deepsleep in file modesp.c (micropython/ports/esp8266/modesp.c) to:

Code: Select all

STATIC mp_obj_t esp_deepsleep(size_t n_args, const mp_obj_t *args) {
uint32_t sleep_us = n_args > 0 ? mp_obj_get_int(args[0]) : 0;
// prepare for RTC reset at wake up
rtc_prepare_deepsleep(sleep_us);
system_deep_sleep_set_option(n_args > 1 ? mp_obj_get_int(args[1]) : 0);
// Begin speedup code
if (n_args > 2){
	WRITE_PERI_REG(0x600011f4, 1 << 16 | mp_obj_get_int(args[2]));
}
// End speedup code
system_deep_sleep(sleep_us);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(esp_deepsleep_obj, 0, 3, esp_deepsleep); // !! nrof parameters changed from 2 to 3
3. Compile micropython and flash the esp8266 with the new firmware.

4. Start your esp8266 and connect to your AP. (You have to do this once so your SSID and password will be saved)

5. Then to use the new deepsleep function in your python program, start like this:

Code: Select all

import time
from esp import deepsleep
from machine import reset_cause

if reset_cause() == 6: # If python is started from power up (Cold start),
	deepsleep(1,2,8) # then set deep_sleep_set_option to '2', and channel number to 8 (in my case) and goto deepsleep for 1 uS

# Here, the code of your python program.
This method reduced the connection time to my AP from almost 5 seconds to 2 seconds.

Post Reply