I’ve always loved to play with Arduino’s, but also thought it was underequiped. There must be at least something that connects it to a network and should have at least a radio on board. Because of that I got really excited about Jeenodes, which had a RF12B radio on board (really good for low-energy, battery operated stuff), and was a couple of factors smaller than an Arduino at the same time.
But that was back-then. Theres a new kid on the block that offers a very nice set of features, and doesn’t only just have a blip-radio built-in, but has a complete 802.1b/g/n Wifi stack, including a radio and antenna on board. Initially it was released as a component that could be used as a shield for an arduino, in which the arduino would provide computing and the esp8266 would provide wifi networking. The thing about the esp8266 is that it actually has a very powerful 32-bit CPU on board that can double as application processor. Unfortunately, when it was just release, it didn’t come with an SDK and it wasn’t possible to load your own firmware into the chip.
This has changed though, an SDK has been release and chip can now be reprogrammed. This is excellent, because it wouldn’t need an Arduino anymore and can run completely autonomously. Which is not a bad idea in the first place, as the esp8266’s 32-bit MCU, has a lot of horsepower compared to your average Atmega.
Oh, and it’s tiny! It so tiny, I’d consider it cute! (check the size compared to a dollar coin in the picture on the right)
Now the thing is, this little board doesn’t have a USB port built in, and requires certain pins to be shorted to be programmed and doesn’t nicely fit in a breadboard. However, it does fit in one of these great development boards that you can find all over the internet. I bought mine from banggood for less than 9 euros, together with 3 esp8266 boards for 3 euros each to play with.
Getting it hooked up
The first thing I wanted to do was to hook it to my computer, to see if I could connect to it. It is supposed to be easy, because out of the box they speak some sort of AT language (just like modems in the days) if you connect through a serial connection. This means that you could just fire up a serial terminal and start speaking AT-commands to it. E.g. the board should respond to an AT command without any parameters with OK.
The tough part about the development board is that it came with absolutely no description or schematics, and it seems very hard to find on the internet as well. Fortunately one of my colleagues had some knowledge about this and told me how the dip switches should be set to get the esp8266 online (if you want to know, it’s set in the picture above). After setting the dip-switches, is was a matter of switching the devevelopment board on (black lever on the bottom-right) and it should start getting online.
The development board has a ch340g USB-to-serial chip. My PC automatically recognized it, but it may be because I’ve been playing with arduino’s and ftdi chips for quite sometime already and have installed some drivers in the process. So, it may need a certain driver to be installed, which may or may not come with Windows.
One my computer connected to the board, an additional serial port was installed (COM10), which I could connect to with putty. The baud rate to be use is 9600 baud. I read somewhere on the internet that it should be higher, but 9600 is really the default speed.
Install firmware toolchain
Once there’s a link between the computer and the esp8266, it should also be possible to write new firmware to it. This requires a specifc toolchain though.
Because I didn’t want to install this toolchain on my PC (which I try to keep relatively clean), I have setup a virtual machine for it using Virtualbox. The nice thing about Virtualbox is that is allows you to redirect USB equipment that’s connected to the host PC to the virtual machine. My virtual machine is a 64-bit installation of Ubuntu 14.04 (server) after a bare install, I followed the steps below to get the toolchain installed:
#Install needed dependencies
sudo apt-get install git autoconf build-essential
sudo apt-get install gperf bison flex texinfo libtool libncurses5-dev wget gawk libc6-dev-amd64 python-serial libexpat-dev unzip
sudo mkdir /opt/Espressif
sudo chown jpenninkhof /opt/Espressif/
# Install the Xtensa crosstool-NG
git clone -b lx106 git://github.com/jcmvbkbc/crosstool-NG.git
./bootstrap && ./configure --prefix=`pwd` && make && make install
echo 'PATH=/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin:$PATH' >> ~/.profile
sudo cp ct-ng.comp /etc/bash_completion.d/
# Setting up the Espressif SDK
wget -O esp_iot_sdk_v1.1.1_15_06_05.zip https://github.com/esp8266/esp8266-wiki/raw/master/sdk/esp_iot_sdk_v1.1.1_15_06_05.zip
mv esp_iot_sdk_v1.1.1/* ESP8266_SDK
mv License ESP8266_SDK/
mv release_note.txt ESP8266_SDK/
rm -rf esp_iot_sdk_v1.1.1
sed -i -e 's/xt-ar/xtensa-lx106-elf-ar/' -e 's/xt-xcc/xtensa-lx106-elf-gcc/' -e 's/xt-objcopy/xtensa-lx106-elf-objcopy/' Makefile
mv examples/IoT_Demo .
# Installing Xtensa libraries and headers
wget -O lib/libc.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a
wget -O lib/libhal.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a
wget -O include.tgz https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz
tar -xvzf include.tgz
# Installing the ESP image tool
wget -O esptool_0.0.2-1_i386.deb https://github.com/esp8266/esp8266-wiki/raw/master/deb/esptool_0.0.2-1_i386.deb
sudo dpkg -i esptool_0.0.2-1_i386.deb
# Installing the ESP upload tool
git clone https://github.com/themadinventor/esptool esptool-py
sudo ln -s $PWD/esptool-py/esptool.py crosstool-NG/builds/xtensa-lx106-elf/bin/
This process takes a while btw. Some steps take almost an hour to complete.
Build the first “sketch”
The first sketch everyone should start with, is to get one of the GPIOs to alternate between high and low. On my development board, this means that one of the leds would start to blink. Exactly this, is what one of the examples sketches does. Pulling it from github and compiling it is simple:
git clone https://github.com/esp8266/source-code-examples.git
Two files were built: firmware/0x00000.bin and firmware/0x40000.bin.
Before you can upload anything, you need to have access to the serial port to do so. The serial port device (/dev/ttyUSB0) has the group of dialout. All you need to do grant yourself access to the serial port is to add yourself to the dialout group:
sudo adduser jpenninkhof dialout
After getting access to the serial port, the sketch that has just been built can be uploaded to the esp8266 chip. To get the board in “upload mode”, the board should be booted while GPIO0 is grounded. On the development board this can be very easily achieved by keeping the GPIO0 pushbutton (top-leftmost button in picture above) pressed, while pressing the reset-button (bottom-left-most button the picture above). Shortly after the reset-button has been released, the GPIO0 button can also be released again. The sketch can then be uploaded using:
make ESPPORT=/dev/ttyUSB0 flash
After the upload had completed, it started toggling GPIO2 and the led connected to GPIO2 started to blink:
After successfully loading my first sketch, I am now ready to take over the world 🙂
Schematics of the development board here