9V battery powered, $2 Web Server with Arduino
I’ve been playing around with the ESP8266 Wi-Fi module for a while and has been fascinated by its versatility, the built in microcontroller and wireless antenna to be specific. The brain of these dev boards and modules is the 5mm ESP8266 chip. The price of the chip is a few dollar cents and dev boards start from $1. The chip has gone through a few generations and has now become reliable but luckily the price has stayed low.
I originally had an ESP8266 module with only 8 pins and it looked like this and cost $2:
This was not too reliable and only exposed GPIO (general purpose input and output) pins from the chip. I also had problems programming it from OS X possibly because of my FTDI converter. Unfortunately this module didn’t have a USB connector and an onboard voltage regulator.
Last month I came across and ordered from the brand new NodeMCU 1.0 development boards which is, again, based on the ESP8266 chip. This time it’s the latest 12-E generation:
This module has everything I wanted: wi-fi, microcontroller, voltage regulator and a micro USB connector for easy code uploads. This costs $4.50 but once you prototyped your project and ready to package it up you can just buy the brain, the 12-E module for 2$. That’s the little rectangle with the metal heat sink.
The onboard processor is 32 bit with SDIO, SPI, UART and 16 GPIO pins and it has a lot more power than an Arduino. The ESP8266:
– is running at 80MHz
– has 32KBytes of instruction RAM
– has 96KBytes of data RAM
– has 64KBytes boot ROM
Pretty powerful properties for a microcontroller. Compare this to the Arduino UNOs 16Mhz clock speed.
You might say at this point that the clear benefit of the Arduino UNO is that it can be programmed with the Arduino language from the Arduino IDE. The good news is that the Arduino IDE has been made compatible with the ESP8266 chip so you can upload your regular Arduino code onto the board.
Uploading sketches from the Arduino IDE
Before programming the board you need to install it from the Arduino Additional Board Manager. Here are the steps to follow:
– Install Arduino 1.6.5 from the Arduino website
– Start Arduino and open Preferences window
– Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field. You can add multiple URLs, separating them with commas
– Open Boards Manager from Tools > Board menu and install esp8266 platform (and don’t forget to select your ESP8266 board from Tools > Board menu after installation)
These steps were been taken from the original ESP8266 Arduino project github repository: https://github.com/esp8266/arduino
Once these steps are successfully completed you need to make sure that the chip on the board responsible for the USB serial communication is compatible with your computer. On my NodeMCU board it was a chip labelled as CP2102. For that I had to install the driver from a Chinese website:
This did the job and after installation the board appeared in my Arduino IDE on OS X Yosemite. I used the below settings in the Arduino IDE:
Now is the best time to run an LED blink test. The only thing you need to be aware is the the pin labels on the NodeMCU don’t correspond to the numbers in your Arduino sketch. Here are the numbers you need to use instead:
pin 0 = D3
pin 1 = TX
pin 2 = D4
pin 3 = RX
pin 4 = D2
pin 5 = D1
pin 9 = SD2
pin 10 = SD3
pin 12 = D6
pin 13 = D7
pin 14 = D5
pin 15 = D8
pin 16 = D0
If you wish to switch an LED on connected to D4 then your Arduino sketch will be:
A 9V battery powered $2 Web Server
The cool thing about the ESP8266 chip is that once it’s connected to your Wi-Fi modem you can connect to its IP address. You can also setup an HTTP server on the board which means that you can send HTML responds back if the IP address is entered into a browser from a device connected to the same Wi-Fi network.
Let’s build this web server and switch on an LED. First connect an LED to pin D2, then add this sketch to the Arduino IDE. I added a few comments to help you understand what’s happening in the background:
Modify your wifi details and upload the code to the board. For me it normally takes 2 – 5 attempts before the actual upload process starts and I’m not entirely sure why…
The server code is uploaded onto your board so you no longer need to be connected to your computer through the USB. Unplug it and try powering it from an external power source (minimum 6 maximum 15 volts). I connected the positive pole of a 9V battery to the VIN pin and the negative to GND. The board will power up and connect to your Wi-Fi in a few seconds then by navigating to the IP address of the board you will see the HTML page you added into the returnHTML function.
Notice how in line 91 we output the IP address of the board to the Serial port:
If you open up the Serial Monitor of your Arduino IDE you will see the IP address. This is what you need to enter into the browser to access your ESP8266 mini server and to see the HTML page.
Now as you click on the HTML buttons the LED will switch on and off connected to your server.
Alternatively you can add this $2 web server into a desk lamp. You just need to replace the LED with a 240V relay to switch the power supply instead. This means that your desk lamp is now a web server that can serve a responsive website to control itself. Your desk lamp is now part of the Internet of Things 🙂