# How to work with ESP8266 micros on Linux I have a an `ESP12f` module that came with a cheap ten USD programmer built around the SiLabs `CP210x` USB UART. I would like to program the microcontroller to run in a low power mode which based on some research will affect how I build the development environment. Apparently the standard Espressif development toolchain includes a bloated WiFi core. I believe the whole point of the `ESP8266` microcontroller is WiFi so it seems that this is a bit of an issue for those who want long term battery power and connectivity. More than likely the low power mode will run at less than tens of microamps and during times of WiFi usage we will use hundreds of milliamps. This means that over the lifetime of a battery nearly 100% of the power will be consumed while WiFi is active. ## Espressif IoT Development Framework Assuming one wants to make use of the chip manufacturer's software development kit, one would need to download and install the `ESP-IDF` build system. I am not providing details on how to do this. I did some research and one may be able to perform intermittent communication within 170ms assuming they have previously connected to the same WiFi access-point. I believe the initial connection took 1,500ms. Some people state that this can actually take between 6 to 10 seconds when using the ESP-nonOS-sdk. ## ESP8266 meSDK Victor's `pvvx` repositories contain some cool projects, including `meSDK`, a minimal ESP8266 we can boot, load a program from flash, connect to wifi, send, and then power down in less than 100ms. The WiFi connect time is only part of this, so to compare to the `ESP-IDF` previously mentioned fastest possible WiFi connect we would compare against just that part, which is less than 20ms in this case, almost ten times faster. To accomplish this kind of low power usage we need to make use of some tricks as well as Victor's `meSDK` and a separate server with a WiFi monitor-mode capable adapter. We do this so that the ESP8266 does not need to connect to an access-point, perform a DHCP request, etc. ``` #git clone https://github.com/pvvx/MinEspSDKLib : "alternative repos"; git clone https://github.com/cnlohr/MinEspSDKLib git clone https://github.com/cnlohr/tplink-raw-wifi # Arch removed python2 repositories, install setuptools and pip so we can install pyserial curl -o python2-setuptools.tar.gz -L https://github.com/pypa/setuptools/archive/v44.1.1.tar.gz tar xzvf python2-setuptools.tar.gz cd setuptools-44.1.1/ python2 bootstrap.py sudo python2 setup.py install cd ../ curl -o python2-pip.tar.gz -L https://github.com/pypa/pip/archive/20.3.4.tar.gz tar xzvf python2-pip-tar.gz cd pip-20.3.4/ sudo python2 setup.py install cd ../ sudo python2 -m pip install pyserial # we should be able to run this old (python2) version of esptool.py cd MinEspSDKLib python2 esptool.py -h : "check that we can communicate with a connected programmer sudo python2 esptool.py --port /dev/ttyUSB0 flash_id # we need esp-open-sdk sudo pacman -Syu --needed gcc git make ncurses flex bison gperf guile expat git clone --recursive https://github.com/pfalcon/esp-open-sdk.git # on line 193 in crosstools-NG/configure.ac it checks for bash 3.1, or 4, edit to also check for 5 ## |$EGREP '^GNU bash, version (3\.[1-9]|4)') ## to ## |$EGREP '^GNU bash, version (3\.[1-9]|4|5)') # another problem with expat (just use the packaged libexpat that comes with your distro) # there is a problem with the old one that crosstools-NG requires (2_1_0) you will get an error: ./ct-ng build [INFO ] Performing some trivial sanity checks [INFO ] Build started 20231115.093714 [INFO ] Building environment variables [INFO ] ================================================================= [INFO ] Retrieving needed toolchain components' tarballs [ERROR] [ERROR] >> [ERROR] >> Build failed in step 'Retrieving needed toolchain components' tarballs' [ERROR] >> called in step '(top-level)' [ERROR] >> [ERROR] >> Error happened in: do_isl_get[scripts/build/companion_libs/121-isl.sh@16] [ERROR] >> called from: do_companion_libs_get[scripts/build/companion_libs.sh@15] [ERROR] >> called from: main[scripts/crosstool-NG.sh@591] [ERROR] >> [ERROR] >> For more info on this error, look at the file: 'build.log' [ERROR] >> There is a list of known issues, some with workarounds, in: [ERROR] >> 'share/doc/crosstool-ng/crosstool-ng-1.22.0-60-g37b07f6f-dirty/B - Known issues.txt' [ERROR] [ERROR] (elapsed: 4:41.38) [04:41] / make[2]: *** [ct-ng:152: build] Error 1 make[1]: *** [../Makefile:139: _toolchain] Error 2 make[1]: Leaving directory '/home/jb6113/esp8266-dev-env/esp-open-sdk/crosstool-NG' make: *** [Makefile:131: crosstool-NG/.built] Error 2 edit the crosstool-NG/config/companion_libs/expat.in file to reflect the packaged libexpat # the isl is also broken change the download location in "scripts/build/companion_libs/121-isl.sh" line 17 ftp://gcc.gnu.org/pub/gcc/infrastructure # http://isl.gforge.inria.fr # The cross compile gcc is version 4.8.5, and the gcc compiler for that is newer, it will complain gcc/reload1.c error use of an operand of type 'bool' in 'operator++' is forbidden in c++17 fix by setting 'spill_indirect_levels' to value 1 whenever it is incremented in the while loop in the source # there is an issue with building dbg with python3, but building with python2 is fine cd /usr/bin ln -sf python2 python build, then later put it back ln -sf python3 python # Github user ChrisMacGregor ran into all these issues and documented them to build esp-open-sdk on ubuntu Xtensa toolchain is built, to use it: export PATH=/home/jb6113/esp8266-dev-env/esp-open-sdk/xtensa-lx106-elf/bin:$PATH Espressif ESP8266 SDK is installed, its libraries and headers are merged with the toolchain ``` # cnlohr's MeSDK cnlohr already did what I want to do, so use his repository as a starting point. After modifying the Makefile to match appropriate paths one can compile and flash a ESP8266 binary that turns on, sends a packet, then goes to sleep. # esptool The esptool that is found with esp-open-sdk uses python2, but my python2 env is having problems, `hashlib` is not working correctly, instead I will try to use the newer esptool and test if that works okay. ``` curl -L -o esptool-v4.6.2.zip https://github.com/espressif/esptool/releases/download/v4.6.2/esptool-v4.6.2-linux-amd64.zip or, just install with package manager, I did not really like how espressif switched from esptool.py to binary, I am sure it is fine, I just did not have time to look through it. sudo pacman -Syu esptool ``` # openwrt installed the openwrt developer requirements on a debian computer ``` sudo apt install build-essential clang flex bison g++ gawk gcc-multilib g++-multilib gettext git libncurses-dev libssl-dev python3-distutils rsync unzip zlib1g-dev file wget ``` follow the openwrt helloworld examples We are going to target two devices. The Google WiFi Router, NLS-1304-25 using a Qualcomm IPQ4019 arm cortex a7 neon vfpv4. The other, a TP-Link TGR1900 using a Qualcomm IPQ8064 SoC. # ESP8266ex The `ESP8266` chip does not really exist as part that is available to consumers, but everyone calls the `ESP8266ex` by that name (without the *ex*). # Solutions Table Our solution connects to WiFi within 30ms. MRDIY has a solution using ESPNOW to connect to a hub within 130ms. The trigboard by Kevin Durah connects to WiFi within 1500ms. John Mueller's solution connects to WiFi within 1000ms.