Programming for ESP8266 (Part 1)
ESP8266 is a game-changer chip. It provides WIFI functionality for as low as 5 $ when all competitors at the time we offering their chips for 30 $ or more. It is very small in size. In addition it has serial connectivity, Real Time Clock(RTC), General Programmable Inputs and Outputs (GPIO) and Analog to Digital Converter (ADC). But that is not the thing that makes a lot of people in the ameteur Internet Of Things world so excited. It is the possibility to program this chip and use your imagination to build your very own smart Thing.
In this article we will help you get started in setting up a development environment and creating your first Hello Real World application for ESP8266.
In part 1 of the article we will prepare the development environment and buy the needed hardware.
In order to set up a development environment we will use Vagrant together with Virtualbox. Vagrant will create the virtual machine for us. It will also provide the needed settings, toolchains, etc and Virtualbox will run this machine for us. For simplicity the instructions below are for Ubuntu 14.04 but you can apply them to other distributions or Operating Systems(OS).
Go to the Vagrant’s download page and choose the latest version. I am using 64bit version therefore I have chosen to download this URL: https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.deb
You can run the following commands to install Vagrant on your machine
wget -c https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2_x86_64.deb
sudo dpkg -i vagrant_1.7.2_x86_64.deb
Once you are ready you should go to the next step which is installing VirtualBox.
Go to the VirtualBox’s download page and get find the package for your distribution/OS. In my case I downloaded Virtualbox from this URL: http://download.virtualbox.org/virtualbox/4.3.26/virtualbox-4.3_4.3.26-98988~Ubuntu~raring_amd64.deb.
The commands to install it on your computer are given below.
wget -c 'http://download.virtualbox.org/virtualbox/4.3.26/virtualbox-4.3_4.3.26-98988~Ubuntu~raring_amd64.deb'
sudo dpkg -i 'virtualbox-4.3_4.3.26-98988~Ubuntu~raring_amd64.deb'
For our purposes Virtualbox should be able to access the USB devices. Usually if you running as a non-root user that is not the case. In order to check if your Virtualbox has access to the USB devices, please run the following command:
VBoxManage list usbhost
If you do not get an output similar to this one:
Host USB Devices:
VendorId: 0x046d (046D)
Current State: Busy
Then you have to add your current user to the vboxusers group. This can be done with the following command:
sudo useradd -G vboxusers $USERNAME
To test if you are now able to access the USB devices from your virtualbox you should open a new terminal and from there run
VBoxManage list usbhost
This time you should see a list of available USB devices.
Now that we have Vagrant and Virtualbox we can use a pre-made template for creating our Development virtual machine.
Go to the directory where you do your development. And from there run the following command to download the new virtual machine template.
git clone https://github.com/slaff/esp8266.dev.box.git
There are two important files in that directory. The first is called Vagrantfile and it describes how our new virtual machine will be created. The second important file is called vm-bootstrap.sh and this one describes to download, install and seupt the software that we will be using. In vagrant terms this is known as “provisioning”.
In order to create our development box we need to run the following command:
When you run vagrant up for the first time it will take more than an hour. Vagrant will download the base box, then it will download the esp-open-sdk and try to create a compiler toolchain. And finally it will try to download and compile NodeMCU, Micro Python and Frankenstein-ESP8266 platforms for our programming pleasure.
While we are waiting for our Dev box to be generated we can buy the needed hardware.
There are a lot of ESP8266 modules out there. You can choose the whichever you want. The development box that we are building should work for most of them.
For this article I have chosen to use Olimex’es ESP8266 Evaluation Board. Mainly due to its ease of use and due to the fact that we can start using it almost immediately without the need to solder something. Please note that you might need to buy also 5V adapter like SY0605E, and USB-Serial-Cable-F to connect ESP8266-EVB to your computer.
The Olimex ESP8266-EVB module has in addition a push button and power relay that we will use later on. If you have already bought ESP8266 module you should be able to apply our examples also to your module.
That is for part 1. Make sure that you have all needed hardware and software set up before you move to part 2 of this article.
In part 2 of this article we will compile a simple Hello Real World application for the ESP8266. We will use the C programming language so brush up your C knowledge and get ready for the second part.