Two things make the current Internet of Things (IoT) craze possible – one is the crazy-cheap microcontroller chips flooding the market, like the Atmel ATMEGA328P found in our Arduino Uno board. The other is the new range of powerful, low-cost Wi-Fi controller chips appearing everywhere.
These chips, that also include their own microcontroller inside, are cutting the cost of incorporating Wi-Fi into new gadgets by a factor of ten, providing enormous savings. And the chip doing most of the price damage is Espressif Systems’ ESP8266.
Get the code
Download this month’s source code from here.
Unzip the file, copy the SoftwareSerial subfolder to the /libraries subfolder of your Arduino IDE and restart it. Build the circuit, load the ESPWIFI.ino code, flash it to your Arduino Uno, press the Serial Monitor button and you should see the return code.
Wi-Fi on a chip
It’ll work as a software-based access point (use it as a client or a server), it supports 802.11b/g/n standards and it even has a 32-bit microcontroller CPU on-board you can program to do other things.
However, the simplest way to use it is part of a low-cost module, the most common being the ‘ESP-01’.
This tiny 8-pin module is smaller than an SD card and can be made to work with almost any microcontroller, including the Arduino series. You’ll find it on eBay for under $5 including shipping.
How it works
The most complicated thing about it is it’s designed to work on a 3.3VDC voltage supply whereas most Arduino boards run off 5VDC. Feed 5VDC straight into the ESP8266 and its pretty much toast.
The solution is to use a voltage regulator chip. We’re using a three-pin module based on the AMS1117-3.3 regulator. It’s smaller than a microSD card, sells for around $1 on eBay and works perfectly, regulating the incoming voltage to a rock-steady 3.3VDC output.
The ESP8266 only has two data pins that matter – TX (transmit) and RX (receive). Now the ESP8255 being ‘5V-intolerant’ suggests putting 5VDC on its pins blows it up.
However, we’ve been using the layout shown for several weeks without an issue. The key is ensuring the voltage rail to the module is 3.3VDC.
We’ll get cracking with a simple Serial I/O app that allows you to communicate with the ESP8266 chip over the Arduino IDE (Integrated Development Environment) Serial Monitor. The Serial Monitor is Arduino’s way of allowing you to code feedback from your projects.
We’ve built the project on a DIY prototype shield plugged into a bog-standard Arduino Uno board here – you can follow the overlay diagram for details.
But there’s a problem – the Arduino only has one serial port (technically known as a Universal Asynchronous Receiver/Transmitter or UART for short) and we need two. We need one for the ESP8266 to communicate with the Arduino Uno and we need another for the Uno to talk to the Arduino IDE.
The trick is to incorporate a ‘soft’ UART using the SoftwareSerial library. A serial port is typically just two pins, one to transmit (TX) our data to a serial device and another to receive (RX) data back again.
Once the two serial ports are up, the app simply monitors the two devices – the ESP8266, to see if there’s any data ready to receive; and the Serial Monitor data buffer, checking for any code you’ve typed in. It then processes the data and sends it in the appropriate direction.
Open the Serial Monitor window and you should see an initialisation string, including the device maker data. But the important bit is the ‘OK’ acknowledgement at the end, indicating we’re good to go.
We’ve got a number of projects in mind for the ESP8266 and one of them involves ‘AT+CWLAP’ (List Access Points). Type it into the Serial Monitor code line, press the Send button and the ESP8266 will list all available Wi-Fi access points (APs) within its signal radius.
And the list has a bucketload of information – from the AP encryption, to its SSID or network name, its received signal strength indication (RSSI) measured in dBm (decibels referenced to one milliwatt of received power), along with its MAC address and Wi-Fi channel. The SSID and RSSI data alone give you the makings of a simple Wi-Fi Sniffer.
Station, AP or both
This little chip is extremely clever and can act as a Wi-Fi client and AP at the same time.
You start by first running ‘AT+CWMODE?’ to find the current mode. Station or ‘client’ is Mode 1, AP is Mode 2 and both, Mode 3. You then set the mode by issuing ‘AT+CWMODE=’ where is, well, the mode number.
For example, ‘AT+CWMODE=3’ sets up the chip to operate as both a client and AP. A more complete AT command set can be found here.
Because the ESP8266 has its own firmware, it gets updated from time to time, as noted by the first chips that arrived with a 57600bps default serial rate, which became 9600bps on later versions. Buy now and you should get the 9600bps version, which just means use the ‘wifi.begin(9600)’ codeline as-is.
The ESP-01 module has a red power LED and a blue data LED, the latter flashes when it’s transmitting data. A popular mistake to avoid is to ditch the AMS1117 and just power the ESP-01 with the Arduino Uno’s 3.3VDC pin instead.
This method isn’t recommended as the ESP8266 has a peak current draw in excess of 150mA when it’s cracking on, well above the Uno’s 50mA limit.
If you’re seeing the command responses on the Arduino IDE Serial Monitor, you’re on the right track. Next time, we’ll continue by showing how to hook into your home wireless network.