Simple Iot device with the ESP8266

The ESP8266 is a really cheap Wifi module suitable for Diy Iot devices. There is some info about it out there but it's hard to get the whole picture of how to use it. Here's my take on it.

I connect the module to a 3.3 volt USB-serial adapter, as described in this link. Note that a simple CP2102 won't provide enough power, use another power source for the ESP8266. I used an Arduino at first.
The adapter is then attached to the PC and 'dmesg' is run:

dmesg usb 3-1: cp210x converter now attached to ttyUSB0

Then use a terminal program to connect. One simple solution is to use the one in the Arduino IDE. My module used a Baud rate of 9600 and with this setup it answered on AT command, for example "AT":


Native code
To build native code for the modules internal CPU a toolchain is needed. This can be installed in the computer with the instructions here.
I did this steps on my computer with Fedora 21:

>mkdir esp8266toolchain
>cd esp8266toolchain
>git clone
>cd esp-open-sdk

This failed a few times due to missing tools, they were installed with Yum and then the make-command was run again.

Install esptool
>tar zxvf esptool*
>sudo cp esptool/esptool /usr/bin
>sudo cp esptool/ /usr/bin

Python needs serial support:
>sudo yum install pyserial

First test
>git clone
>cd source-code-examples/blinky

Edit Makefile
XTENSA_TOOLS_ROOT ?= /opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin
XTENSA_TOOLS_ROOT ?= /run/media/patrik/1TB_2014/patrik/Program/esp8266toolchain/esp-open-sdk/xtensa-lx106-elf/bin

SDK_BASE ?= /opt/Espressif/ESP8266_SDK
SDK_BASE ?= /run/media/patrik/1TB_2014/patrik/Program/esp8266toolchain/esp-open-sdk/sdk

EXTRA_INCDIR = include /opt/Espressif/include
EXTRA_INCDIR = include /run/media/patrik/1TB_2014/patrik/Program/esp8266toolchain/esp-open-sdk/sdk/include

Next step is to prepare the Esp for uploading:
-Connect Gpio0 to ground
-Reset the Esp. I use a 1k resistor from the Esp:s Rst-pin to Vcc and a small button from Rst to ground. This way I can reset the module by pressing the button.

Then, compile and upload:
>make ESPPORT=/dev/ttyUSB0 flash

---Press the reset button--- --port /dev/ttyUSB0 write_flash 0x00000 firmware/0x00000.bin 0x40000 firmware/0x40000.bin
Erasing flash...
Writing at 0x00007400... (100 %)
Erasing flash...
Writing at 0x00065700... (100 %)


This code will toggle Gpio2 on and off. A Led connected to Gpio2 via a 100 ohm resistor to ground will flash nicely.

Connecting to a network
Now lets try to connect to a network. Code for this is in the Source_code_examples "basic_examples" directory. We create a copy of this folder:

>cp -R basic_example connect_to_network
>cd connect_to_network/

Configure the network settings:
>nano user/user_config.h

Add the correct values for your wireless network. Also adjust Makefile as in the previous example.
For some reason the code misses the line for connecting to the network. This is fixed by adding the row "wifi_station_connect();" below wifi_station_set_config(&stationConf);:

>nano user/user_main.c
When this is done, compile and upload:
>make ESPPORT=/dev/ttyUSB0 flash

The code compiles, uploads and starts at the module. If all went well the module now is connected to the specified network. This can be checked in many way. I use the Android app Fing, which now lists a new network device with vendor "Espressif".