This article is about using and programming a Wireless Module for IoT – ESP8266 for enhancing sensors (whether Temperature, Motion, Smoke etc) to send their telemetry data for aggregation and analysis for Internet of Things applications. One of the most widely available module/circuit for making a standard Hardware sensor talk over a network (or Internet) with a server is the Arduino based (or compatible) series of WiFi modules. So, let us go through the details about ESP8266 and its software, schematics, SDK, etc which will help you in understanding and actually developing a WiFi based embedded system.
Wireless Sensor Use-cases
As the number of sensors grow and spread over a bigger geographical area, it becomes more difficult and expensive to draw and maintain physical wires to/from each of them for monitoring. (By the way, the same holds true for Actuators too). So, learning how to program, use and interface a Wireless sensor with your hardware becomes key to a success for project deployment, management and usability.
Furthermore, with ESP8266 it has become extremely easy to integrate any sensor with WiFi these days and there are various applications that can benefit from wireless connectivity, for example:
- A network of Motion Sensors in an Office or Apartment
- Pollution monitoring sensors in a city
- Smart Metering for Electricity and Water, etc
One can connect to an ESP8266 through its serial interface pins, and then of course via Wifi, through a Light Weight IP stack. In addition, to communicate with sensors, one can make use of I2C or SPI bus capabilities of the GPIO pins.
The ESP8266 based WiFi
There are WiFi solutions based on the IC ESP8266 designed for the needs of a new connected world. Each wireless module is a self-contained WiFi based networking solution. ESP8266 has on-board processing and storage capabilities as well as GPIOs to integrate with other electronics like sensors (and actuators). A basic pin-out of the ESP8266 is given in the figure below:
VCC: 3.3 Volt, GND: Ground
CH_PD: Chip Enable, RST: Chip Reset (For both of them use 10K PULL-UP resistor to VCC)
TX: The serial-data transmit, RX: The serial-data receive (Connect to RX/TX Serial or USB-Serial FTDI converter)
GPIO-0/2: General purpose I/O pins (with GPIO-0 to control flashing a new firmware program)
A general schematic can be found here or advanced auto-reset scheme here.
Brands, Variations & Form Factors
The circuits packages are available in various form-factors and as SMD packages or normal circuit boards with varying amounts of memory (flash) from 0.5MB to 4MB or more. The wireless standards supported are usually the 802.11b/g/n type in this series of wireless modules.
The ESP wireless module can be programmed (for example with the Arduino IDE or the supplied tool-chain) and a C/C++ program etc can write/read from the socket API just like a normal Linux application. There is a Serial Interface is the used for writing to the ESP flash and once the image (or Sketch) is programmed, the ESP will boot and run the updated firmware (the embedded software). Caution: Programming your custom firmware program will delete/overwrite the manufacturer AT firmware and/or other functionality.
There are several way to program your device:
- An Arduino board can be connected to the ESP to program the image.
- Programming via a PC/Laptop running Arduino IDE via the RS232 serial port.
- If the serial port is unavailable on laptop, a USB-to-Serial TTY converter can be used.
- Or, any supported FTDI programmer. I have used an Elementz Engineers USB to TTL module for serial communication. The Windows drivers can be downloaded from the Silicon Labs product page and installed.
- Comprehensive step-by-step for FTDI + ESP8266 (by Martyn Currey)
When The Firmware version of the Wireless module is printed on the Serial console when we boot the device. For example, you may get something like following:
2nd boot version : 1.5
SPI Speed : 40MHz
SPI Mode : DIO
SPI Flash Size & Map: 8Mbit(512KB+512KB)
jump to run user1 @ 1000
Or, you can execute: AT+GMR and should see something like:
AT version:22.214.171.124(Jul 1 2016 20:04:45)
Ai-Thinker Technology Co. Ltd.
Dec 2 2016 14:21:16
The following information I get about the ESP device (REES52) that I used is necessary to re-image or update the firmware.
Flash size: 8Mbit: 512KB+512KB
Firmware update binaries from the OEM and address vectors for installation:
esp_init_data_default.bin 0xfc000 (optional)
blank.bin 0x7e000 & 0xfe000
- Steps for Image update for AI Thinker (click here)
- Detailed tutorial for updating with official firmware
The ESP hardware (depending on the brand) usually comes with a default firmware which is usually enough to setup the WiFi without changing the flash image. With the USB-to-Serial TTY (or a direct Serial connection) various commands like the following can be executed:
- AT+RST: Restart module.
- AT+GMR: View version info.
- AT+CWJAP: Connect to AP.
- AT+CWLAP: Lists available APs.
- etc… (refer the brand website or manual for the supported commands)
- One comprehensive AT command reference is here
Here is one cool Instructable on using AT commands with ESP device.
Working with GPIO in the Wireless Module
The general APIs provided in the Arduino libraries/IDE are digitalRead(), digitalWrite(), analogRead(), analogWrite() for creating a custom program that uses the general purpose I/O pins for its particular use-case. Remember, when you flash the ESP with a custom program (or firmware) the default firmware would be erased (over-written) which also means that the AT command-set of the ESP8266 would not be available (unless you re-flash the original or similar firmware again).
That brand that I used (REES52) has the chip/SDK version from AI Thinker based on original firmware/SoC from Espressif (The AT command-set from Espressif does not have ability to read/write GPIO from the default firmware image)
Links to some useful tutorials:
- Programming the on-board GPIO for blinking LED
- Accessing GPIO via WiFi by programming a sever on ESP or here
- GPIO access via UDP
Protocol Stack and RESTful API
The ESP wireless module comes integrated with TCP/IP protocol stack for communication over a network and it becomes extremely simple for any Hardware to send/receive wireless data and connect to the network or the cloud server. A socket/connect is a standard interface, and the ESP can also be programmed to communicate via HTTP and other higher layer protocols based on the server and IoT application architecture and requirements. Here are a few practical examples and libraries:
- Normal HTTP
- REST APIs with Secure HTTPS server
- Plain TCP/UDP/Server examples
- Opening a TCP server and talking via Telnet (Look for AT+CIPSERVER)
SDK, APIs and Libraries for Wireless Module
- ESP8266 Library for Arduino to control and program functionality
- GCC toolchain for ESP8266
- Espressif SDK, ESP Open SDK and Compiler on LUbuntu
- An ESP8266 library for Arduino: WeeESP8266
Books and Other Material
- Browsing the web using ESP8266
- Water flow sensor
- Webserver with ESP8266
- Another interesting take is controlling ESP8266 via Android Phone
- An HTTP IO server for ESP8266, that will allow to control ESP from a network.
- ESP to ESP communication without cloud/server middleman
- Door Status Notification:
- Facebook Chat with ESP8266:
- IOT Playground EasyIOT ESP8266 WiFi PIR motion sensor (REST APIs)
- EasyIOT WiFi PIR motion sensor (Arduino IDE)
- REES52 specifications
- Step-by-step Instructable for using ESP8266
- Some ESP WiFi practical notes at Hackaday.io
- Demo using ESP8266
- ElectroDragon’s list of references
- Basic communication via TCP (and telnet etc)
- eBook for ESP8266 by Neil Kolban
- Internet of things with ESP8266
- ESP Boot Sequence
- Another tutorial here
Some Practical Tips
- The ESP8266 usually uses 9600 or 115200 baud-rate, but try with other in case you are not able to get any meaningful data on the serial terminal (you can use Putty, or Arduino IDE’s Serial Monitor etc to connect to COM-1, COM-2 as applicable)
- The serial settings for my board were:
- Speed: 115200
- Data bits: 8
- Stop bits: 1
- Parity: None
- NL + CR are required for communicating via AT commands.
- The serial settings for my board were:
- For enabling a server on wireless module, you can use AT+CIPSERVER=1,80. The MUX mode should also be set to accept multiple connections with the command: AT+CIPMUX=1
- A new firmware can be programmed on the device only after booting it to flash mode. To do this GROUND the GPIO0 pin on ESP then pulse the Reset (to GROUND) and finally release the GPIO0 from Ground. This with move the ESP into programming mode and you can flash using flash utility or Arduino IDE etc. Refer another Short tutorial.
- If you see this error (espcomm_sync failed error: espcomm_open failed or espcomm_send_command: cant receive slip payload data) while programming, it usually means the device has not gone into FLASH mode. Re-validate if you are using DTR, or Arduino Uno to program or manually changing the mode. Some more info here.
- If you have solid BLUE light and ESP not running your program it could be a power supply issue. Read more here.
- Setting a Static IP for ESP8266
- ESP8266 low-power mode (ESP8266 power consumption tests)
Alternatives to ESP8266 Wifi module
There is a new module based on STM32F4 ARM Coretex M4 microcontroller called the EMW3165, which we shall explore in a different write-up separate from this one concentrating on ESP8266.
And finally, feel free to comment about your progress with ESP based wireless module and if this article was insightful. I plan to add more details in the same page, so you can bookmark, subscribe, comment and share this page. Enjoy making your new or existing hardware-solution wireless!