Adafruit Huzzah ESP8266
Wanting to try out the ESP8266, but reading about the difficulties connecting to and interfacing with the bare module, I opted for the Adafruit Huzzah ESP8266 breakout board.
The breakout board takes care of various things, making it easier to work with ESP8266:
- reset and user button
- onboard LED
- regulated output voltage
- breadboard friendly pinout
The breakout board costs about $10, making it an ideal, hassle-free, first experience with the ESP8266.
Connecting to the ESP8266
To connect to the ESP8266 and pass commands to the Lua interpreter, a console cable is required.
Ideally the console cable has four connectors:
- Power (V+)
- Ground (GND)
- Receive (RX)
- Transmit (TX)
In some cases however, only three connectors could be present on the console cable, omitting the power connection. In that case, the ESP8266 needs to be powered separately. Powering the ESP8266 from an external power supply can be done using a 3.5-16V power supply connected to the V+ and GND pins. When applying power, the onboard LEDs (red and blue) will briefly light up and turn off again.
Once the cable is connected and the ESP8266 powered on, a serial terminal can be used to access the interpreter. On Windows you could use PuTTY to do that, I’m using “screen” on OSX.
screen /dev/cu.usbserial-DA007GKI 9600
After connecting, press the reset button on the ESP8266 board and the following should appear:
8����������������c�C� NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4 lua: cannot open init.lua >
I was wondering about the error message displayed. It is however normal and expected. The file init.lua is a file that is executed when powered on. It can be used to have the ESP8266 automatically connect to the network every time it’s booted for example.
Let’s start with the “Hello World” of electronics: blinking an LED.
On the command line interpreter, type the following commands. The “–” are used to indicate a comment.
> gpio.mode(3, gpio.OUTPUT) -- define as output > while 1 do -- create infinite loop >> gpio.write(3, gpio.HIGH) -- turn LED off >> tmr.delay(1000000) -- wait 1 second >> gpio.write(3, gpio.LOW) -- turn LED on >> tmr.delay(1000000) -- wait 1 second >> end
The prompt won’t return due to the infinite loop. The only way to abort, is to use the reset button.
Let’s take this a step further and connect to the network using Wifi.
> wifi.setmode(wifi.STATION) > wifi.sta.config("
"," ") > wifi.sta.connect()
That’s all there is to it. To verify the ESP8266 is connected, it’s possible to request its IP address:
> print(wifi.sta.getip()) 192.168.0.234 255.255.255.0 192.168.0.1
Commands entered as above are lost when the ESP is restarted. To make them persistent, they need to be saved to a file.
The file that is attempted to be read every time the module is powered on, is called “init.lua”.
Using the following commands, code can be persisted:
> file.open(“init.lua”,”w”) > file.writeline([[-- your code here]]) > file.writeline([[-- your code here]]) > file.writeline([[-- your code here]]) > file.close()
If you make a mistake and your module is stuck in an infinite loop, the only way to stop it, is to flash the module. This will erase the init file and allow you to reprogram it.
The first thing you’ll need is a firmware file. There are different types of firmware available for the ESP. In this guide, I will cover NodeMCU.
Download the latest firmware file from https://github.com/nodemcu/nodemcu-firmware/releases. The latest version at the time of writing is 0.9.6.
The second thing you’ll need is a flashing application. I mostly came across “EspTool”, which is a Pyhton script.
You can download it using the following command:
git clone https://github.com/themadinventor/esptool.git
Make sure you have PySerial installed for EspTool to work properly:
curl https://pypi.python.org/packages/source/p/pyserial/pyserial-3.0.1.tar.gz -o pyserial-3.0.1.tar.gz
Extract the downloaded package and install it.
sudo python setup.py install
To be able to flash the ESP module, it needs to be put in programming mode. This is achieved by pressing the reset button while holding the GPIO0 button. You’ll know you are in programming mode when the red LED remains lit on.
On the command line, run EspTool and provide the correct serial interface and firmware file. The flashing process should take about a minute.
Fredericks-Mac-mini:esptool frederickvandenbosch$ sudo python esptool.py --port /dev/cu.usbserial-DA007GKI write_flash 0x00000 ../nodemcu_integer_0.9.6-dev_20150704.bin Connecting... Erasing flash... Took 2.22s to erase flash block Wrote 450560 bytes at 0x00000000 in 48.8 seconds (73.9 kbit/s)... Leaving...
If the prompt does not reappear, try pressing the reset button. You should now see the new version being reported.
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4 lua: cannot open init.lua >
Even though programming is possible via the command line interface, there are tools available to facilitate the process. One of those tools is ESPlorer, a Java application with syntax highlighting and many more features such as sending code or writing files on the ESP8266.
ESPlorer can be downloaded from http://esp8266.ru/esplorer/. Make sure Java is installed or the application won’t run.
Fredericks-Mac-mini:ESPlorer frederickvandenbosch$ java -jar "ESPlorer.jar"
Example: IoT Doorbell
As an example application, I created a simple, internet-connected doorbell. When the GPIO0 button is pressed, an event is sent using the IFTTT Maker channel, triggering a notification on my smartphone.
On IFTTT, create a new recipe and set up the necessary trigger and action.
Program the ESP8266 to trigger when the GPIO0 button is pressed, with the following code:
Keep in mind this is my first real ESP program and it’s probably far from perfect. For some reason the trigger keeps happening after the initial one, even though the state of the button is restored. As a workaround, I restart the ESP after triggering the notification. Will update the code when I manage to fix that glitch.
The result, is as expected. A notification appears on my phone within a few seconds of pressing the button.
And that’s it! First IoT project using the ESP8266 complete!