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":

>AT
OK

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:

>cd
>mkdir esp8266toolchain
>cd esp8266toolchain
>git clone https://github.com/pfalcon/esp-open-sdk.git
>cd esp-open-sdk
>make STANDALONE=y

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

Install esptool
>wget https://github.com/esp8266/esp8266-wiki/raw/master/deb/src/esptool_0.0.2.orig.tar.gz
>tar zxvf esptool*
>sudo cp esptool/esptool /usr/bin
>sudo cp esptool/esptool.py /usr/bin

Python needs serial support:
>sudo yum install pyserial

First test
>git clone https://github.com/esp8266/source-code-examples.git
>cd source-code-examples/blinky
>pwd
/run/media/patrik/1TB_2014/patrik/Program/esp8266toolchain/esp-open-sdk/source-code-examples/blinky

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

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

EXTRA_INCDIR = include /opt/Espressif/include
to
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---

esptool.py --port /dev/ttyUSB0 write_flash 0x00000 firmware/0x00000.bin 0x40000 firmware/0x40000.bin
Connecting...
Erasing flash...
Writing at 0x00007400... (100 %)
Erasing flash...
Writing at 0x00065700... (100 %)

Leaving...

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
wifi_station_set_config(&stationConf);
wifi_station_connect();
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".