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 discusses how to use the ESP8266-01, ESP8266-12, and the Wemos D1 mini modules. The main documentation on using the ESP8266 and WiFi functions are available here. Additional documentation on the basic Arduino WiFi functions is available here.

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:
  • Wemos D1 mini, ESP8266-12, or ESP8266-01 WiFi module.
  • The Wemos D1 mini development board is the easiest to work with. Everything you need to program it is built-in. All you need is a micro USB cable.
  • If you are using the ESP8266-12 or the ESP8266-01 then you will also need a CP2102 USB-to-serial adapter to program it. For the ESP8266-12 you will also need some mini-hook jumper wires for connecting it to the USB-to serial adapter. For the ESP8266-01 you will also need some Dupont male-to-female jumper wires for connecting it to the USB-to-serial adapter.


The Wemos D1 mini development board has a ESP8266-12 and a USB-to serial adapter built-in. So this is much easier to work with than just the barebone ESP8266. With this you can directly plug in a micro USB cable to program the on-board ESP8266-12.
Wemos D1 mini ESP8266-12 ESP8266-01 USB-to-Serial adapter Micro USB cable Mini grabber hook jumper wires Dupont male-female jumper wires
1 To program the ESP8266 WiFi module, you need to add the ESP8266 Board definition to the Arduino IDE program. You only need to do this once so if it 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.
  • Click the Install button.
  • After installation, exit and re-launch the Arduino IDE program.
Adding ESP8266 board
2 If you are using the Wemos D1 mini development board then just plug in the micro USB cable between the D1 mini board and your computer, and continue with step 3 below.

If you are using the CP2102 USB-to-serial adapter to program the ESP8266-12 or ESP8266-01 WiFi module, then click here for instructions on how to make the connections. Come back here and continue to step 3 after you have made the connections.
3 Create a new sketch and type in this program.











Replace "your ssid" and "your password" with the ssid and password of the WiFi network that you want to connect to.



Note: You can only connect to a 2.4 GHz wifi signal. A 5 GHz wifi signal will not work. Also you cannot use a "Guest" account that requires a response.
#include <ESP8266WiFi.h>
#define statusLed 2   // built-in led for the ESP8266-12
//#define statusLed 1 // built-in led for the ESP8266-01

void setup() {
  Serial.begin(115200);
  pinMode(statusLed, OUTPUT);

  // connect to your WiFi
  // change the ssid and password to match your's
  WiFi.begin("your ssid", "your password");

  Serial.print("Connecting to wifi");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(statusLed, !digitalRead(statusLed)); // blink led
    delay(500);
    Serial.print(".");
  }
  digitalWrite(statusLed, LOW);
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // blink led to show activity
  digitalWrite(statusLed, LOW);   // turn on led
  delay(30);
  digitalWrite(statusLed, HIGH);  // turn off led
  delay(970);
}
4 Uploading the program to the ESP8266 WiFi module.
  • Select the ESP8266 board from the Arduino v1.6.7 IDE menu
       Tools | Board | Generic ESP8266 Module
  • Select the correct COM port that your wifi module is connected to.
  • Open up the serial monitor and set the baud rate (bottom right corner) to 115200.
  • Click the Upload button as you normally would. The blue built-in LED on the WiFi module will flash as the program is being uploaded onto the module.
Selecting Generic ESP8266 Module board
5 Running the program.
After the program is uploaded successfully, it will immediately run and you should see something like this.

Note the IP address that is automatically assigned by your router to your ESP8266 wifi module. In this example it is 192.168.1.116.

If you don’t see the last line but just more and more dots ........., then most likely the ssid and/or the password for the WiFi network is incorrect. Correct it and upload the program again.

Remember that you can only connect to a 2.4 GHz wifi signal. A 5 GHz wifi signal will not work. Also you cannot use a "Guest" account that requires a response.
Wifi connection
6 The above code doesn't do anything interesting except that it connects to the internet and gets an IP address from your router.

To make it into a simple webserver, we just have to add a few extra lines of code as shown here. The new lines are flagged with ****.



  • Modify your program with this new one.
  • Replace "your ssid" and "your password" in the code with the ssid and password of the WiFi network that you want to connect to.
  • Upload this new program to your ESP8266 module.
  • Bring up a browser and browse to the IP address shown on the serial monitor. In this example it is 192.168.1.116. You should see on the webpage the words "Hello World from ESP8266!".

If there are some garbage characters at the beginning of the printout then just ignore them.

If the program doesn't automatically run, then press the reset button on the Wemos board.

If it cannot connect to the wifi for about 30 seconds, then make sure that you have entered your ssid and password correctly in the code and upload the program again.
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h> // **** include the webserver library
ESP8266WebServer server(80);  // **** create a webserver object

#define statusLed 2   // built-in led for the ESP8266-12
//#define statusLed 1 // built-in led for the ESP8266-01

// **** this function is called when client browses to the root /
// **** it creates a HTML webpage to send back to the client
void indexHTML() {
  String msg = "<html>Hello world from ESP8266!<html>";
  server.send(200, "text/html", msg);
}

// **** (optional) this function is called when client requests an unknown URI
// **** send back not found HTTP status 404
void handleNotFound(){
  server.send(404, "text/plain", "404: Not found");
}

void setup() {
  Serial.begin(115200);
  pinMode(statusLed, OUTPUT);

  // connect to your WiFi
  // change the ssid and password to match your's
  WiFi.begin("your ssid", "your password");

  Serial.print("Connecting to wifi");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(statusLed, !digitalRead(statusLed)); // blink led
    delay(500);
    Serial.print(".");
  }
  digitalWrite(statusLed, LOW);
  Serial.println();
  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", indexHTML);  // **** call function indexHTML when client browses to root /
  // server.on("/setup", setupHTML); // **** (optional) call function setupHTML when client browses to /setup
  server.onNotFound(handleNotFound); // **** (optional) call function handleNotFound when client requests an unknown URI
  server.begin();             // **** start the webserver
}

void loop() {
  server.handleClient();  // **** listen for a client connection
}
7 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 differences are in the I/O port usage.

The Wemos D1 mini uses the ESP8266-12 so they are the same in terms of programming.

For the ESP8266-12, the built-in LED is on pin 2.

For the ESP8266-01, the built-in LED is on pin 1. The ESP8266-01 also uses pin 1 for the connection to the serial monitor. So if you use the built-in LED, it will interfere with the outputs to the serial monitor. In other words, don't use both the built-in LED and the serial monitor in your program. Just use one or the other.

Here are the GPIO pins that you can use for the three different wifi modules.
  • Pins D0 and D15 are used to configure the boot loader, so it is best not to use them.
  • Pins D1 and D3 are used for the communication between your computer and the ESP8266 module, so it is also best not to use them.
  • You can use pins D16, D14, D12, D13, D5, D4 and D2 for digital I/Os and analog output (PWM).
  • You can use ADC for analog input.
Wemos D1 mini pins













ESP8266-01 pins

ESP8266-12 pins
We are now ready to create some fun wifi projects. Click on the Experiments button below to continue.