WiFi and the Internet Of Things

The Internet Of Things (IOT) is going to be (or rather already is) the biggest hype in technology in recent years. It is predicted to be a multi-Billion (with a capital B) dollar business with 30 billion devices connected by 2020. If you do some rough estimates, this is about 20 million IOT devices manufactured and deployed every day!

A popular IOT device that is catching fast momentum is the ESP8266 module. In this project, you'll learn how to work with the ESP8266 WiFi module using the Arduino IDE.

The ESP8266 WiFi module has both a microcontroller and a WiFi interface built-in. Hence there is no need for the Arduino board, but we still will use the Arduino IDE for programming the ESP8266 module. There are many different versions of the ESP8266 board. This tutorial shows the pinouts for the ESP8266-01 and the ESP8266-12 modules. The documentation on the basic WiFi functions is available in the WiFi library. The documentation on using the ESP8266 and additional WiFi functions are available in GitHub.

This is an advanced project and requires many skills, however, the instructions in this tutorial should be simple enough to quickly get you going. At the end, you'll have a WiFi web server implemented on this tiny chip and you can browse to it from anywhere in the world to control things.

Parts needed:
  • ESP8266-01 or ESP8266-12 WiFi module
  • Either the CP2102 USB-to-serial adapter (easier to work with) or the Arduino Uno.
  • Must use Arduino IDE version 1.6.7 or greater
  • Mini-hook jumper wires or Dupont male-to-female jumper wires for connecting the WiFi module.






If you are using the CP2102 USB-to-serial adapter to program the ESP8266 WiFi module, then click here now.
ESP8266-01 ESP8266-12 Arduino Dupont male-female jumper wires Mini grabber hook jumper wires
1 In order to program the ESP8266 WiFi module, you need to add the ESP8266 Board to the Arduino IDE program. If this has already been done, then skip this step and go to step 2.
  • Start the Arduino program (must use version 1.6.7 or greater).
  • From the Arduino menu, select File | Preferences
  • In the Additional Board Manager URLs field, copy and paste the following:
     http://arduino.esp8266.com/stable/package_esp8266com_index.json
  • Click OK
Adding ESP8266 board
  • From the Arduino menu, select Tools | Board | Boards Manager
  • Find esp8266 by ESP8266 Community (usually at the very end) and click on it to select it.
  • Select the version you want from the drop-down box. Normally, you'll want the newest version (largest number).
  • Click the Install button.
  • After installation, exit and re-launch the Arduino IDE program.
Here are more detailed instructions if you run into trouble.
Adding ESP8266 board
2 Connections for the ESP8266-01 to the Arduino Uno

You need to use either the Dupoint male-to-female jumper wires or the mini-hook jumper wires to make the connections.
 
ESP8266-01Arduino Uno
VCC3.3V (Do not connect to 5V or you will destroy the IC)
GNDGND
TXTX pin 1
RXRX pin 0
CHPD3.3V (hint: solder a wire between VCC and CHPD on the board to make it permanent)
D0GND for programming; no connection for running
D2no connection
ESP8266-01 pinouts ESP8266-01 connections to Arduino

Connections for the ESP8266-12 to the Arduino Uno

You need to use the mini-hook jumper wires or solder the wires to make the connections.
 
ESP8266-12Arduino Uno
VCC3.3V (Do not connect to 5V or you will destroy the IC)
GNDGND
TXTX pin 1
RXRX pin 0
CHPD3.3V (hint: solder a wire between VCC and CHPD on the board to make it permanent)
D0GND for programming; no connection for running
D15GND (via a 10K resistor if you want to use D15 for I/O)
D2no connection
ESP8266-01 pinouts ESP8266-12 connections to Arduino
ESP8266-01 connections to Arduino ESP8266-12 connections to Arduino
3 Program your Arduino Uno with the BareMinimum example.
  • Select the BareMinimum Sketch from the Arduino v1.6.7 IDE menu
       File | Examples | Basics | BareMinimum
  • Select the Arduino board from the Arduino IDE menu
       Tools | Board | Arduino/Genuino Uno
  • Select the correct port number from the Arduino IDE menu
       Tools | Port | [port #].
  • Connect your Arduino to your computer and click Upload.
4 Get the Blink example.
  • Select the Blink Sketch from the Arduino IDE menu
       File | Examples | Basics | Blink
  • The ESP8266-01 has a built-in blue LED connected to pin 1.
    Change the digital pin 13 to pin 1 if you're using the ESP8266-01. There are three places in the code that you need to change.
  • The ESP8266-12 has a built-in blue LED connected to pin 2.
    Change the digital pin 13 to pin 2 if you're using the ESP8266-12. There are three places in the code that you need to change.

    The built-in LED on both boards is active low, meaning that it turns on when you send a LOW signal and turns off when you send a HIGH signal.
5 Programming the ESP8266 module with the Blink example.
  • Select the ESP8266 board from the Arduino v1.6.7 IDE menu
       Tools | Board | Generic ESP8266 Module
  • Reset the ESP8266 module by disconnecting and connecting the wire to VCC.
    You should see the blue LED on the board flash once.
  • Click the Upload button as you normally would. The blue LED on the WiFi module will flash as the program is being uploaded onto the module.

  • If you get the orange warning message "espcomm_sync failed" then reset the module and upload it again.

  • After the program is uploaded successfully, the Blink program will run, and you should see the blue LED blink.
  • If you want to restart the program, you need to first disconnect D0 from GND, and then reset the module.
Selecting Generic ESP8266 Module board
6 Now that you are able to program the ESP8266 module, you can run (almost) all of the programs on the ESP8266 module just like on the Arduino. The difference is in the I/O port usage.
(Note that pin 1 on the ESP8266-01 module is also used by the serial monitor so it will interfere with the outputs to the monitor if you are using the monitor.)

Since the ESP8266 has a built-in WiFi capability, the obvious thing to do next is to write a WiFi webserver program. Here is our first Hello World WiFi program.
  • Download and install the Wifi_RobotsForFun library.
  • Open the HelloWorldWeb example under
    Examples | WiFi_RobotsForFun | HelloWorldWeb
  • (Instead of installing the library, you can simply copy this second version of the program to the Arduino IDE editor.)
  • Change the place in the code where it says "your ssid" and "your password" to your WiFi's ssid and password.

  • Open up the serial monitor. Set the baud rate (bottom right corner) to 115200.
  • Select the ESP8266 board from the Arduino v1.6.7 IDE menu
       Tools | Board | Generic ESP8266 Module
  • Reset the ESP8266 module by disconnecting and connecting the wire to VCC.
    You should see the blue LED on the board flash once.
  • Click the Upload button. The blue LED on the WiFi module will flash as the program is being uploaded onto the module.

    If you get an orange "java.io.IOException" or "espcomm_sync failed" error, then reset the module and upload it again.


    If you have set your ssid and password correctly, you should see on the monitor the IP address that is assigned to this webserver, and the message "WiFi connected to network" The IP address shown on the right is 192.168.1.134

    If nothing is being printed on the monitor, then close and re-open the monitor. Disconnect D0 from GND. And then reset the ESP8266 module.
const char* ssid = "your ssid";
const char* password = "your password";


ESP8266 server monitor output
  • Open up a web browser and type in the IP address shown in the serial monitor.
  • You will see this webpage with the words "Hello World"

  • To restart the server program, disconnect D0 from GND, and then reset the ESP8266 by disconnecting and connecting the wire to VCC.




We are now ready to create some fun projects. Click on the Experiments button below to continue.
Browsing to the server