ESP8266 Internet of Things Cookbook

By Marco Schwartz
    What do you get with a Packt Subscription?

  • Instant access to this title and 7,500+ eBooks & Videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Free Chapter
    Configuring the ESP8266
About this book

The ESP8266 Wi-Fi Module is a self contained System on Chip (SOC) with an integrated TCP/IP protocol stack and can give any microcontroller access to your Wi-Fi network. It is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor.

This book contains practical recipes that will help you master all ESP8266 functionalities. You will start by configuring and customizing the chip in line with your requirements. Then you will focus on core topics such as on-board processing, sensors, GPIOs, programming, networking, integration with external components, and so on. We will also teach you how to leverage Arduino using the ESP8266 and you'll learn about its libraries, file system, OTA updates, and so on. The book also provide recipes on web servers, testing, connecting with the cloud, and troubleshooting techniques. Programming aspects include MicroPython and how to leverage it to get started with the ESP8266. Towards the end, we will use these concepts and create an interesting project (IOT).

By the end of the book, readers will be proficient enough to use the ESP8266 board efficiently.

Publication date:
April 2017
Publisher
Packt
Pages
268
ISBN
9781787288102

 

Chapter 1. Configuring the ESP8266

In this chapter, we will cover:

  • Setting up the Arduino development environment for the ESP8266

  • Choosing an ESP8266

  • Required additional components

  • Uploading your first sketch to the ESP8266

  • Connecting the ESP8266 to your local Wi-Fi network

  • Connecting the ESP8266 to a cloud server

  • Troubleshooting basic ESP8266 issues

 

Introduction


This being the first chapter, we will be looking at how to get started with configuring the ESP8266 to connect to the Internet. This chapter will be the stepping stone to the much more fun and exciting projects that are in this book. Therefore, follow the instructions provided carefully.

In this chapter, you will learn how to set up the Arduino IDE and upload sketches to the ESP8266. You will also be guided on how to choose an ESP8266 module for your project and how to use the ESP8266 to connect to Wi-Fi networks and the Internet.

 

Setting up the Arduino development environment for the ESP8266


To start us off, we will look at how to set up an Arduino IDE development environment so that we can use it to program the ESP8266. This will involve installing the Arduino IDE and getting the board definitions for our ESP8266 module.

Getting ready

The first thing you should do is download the Arduino IDE if you do not already have it installed on your computer. You can do that from this link:

https://www.arduino.cc/en/Main/Software.

The web page will appear as shown. It features that latest version of the Arduino IDE. Select your operating system and download the latest version that is available when you access the link (it was 1.6.13 at the time of writing):

When the download is complete, install the Arduino IDE and run it on your computer.

Now that the installation is complete, it is time to get the ESP8266 definitions. Open the preference window in the Arduino IDE from File|Preferences or by pressing CTRL + Comma.

Copy this URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json.

Paste it in the file labeled Additional Board Manager URLs, as shown in the following screenshot. If you are adding other URLs too, use a comma to separate them:

Open the board manager from the Tools|Board menu and install the ESP8266 platform. The board manager will download the board definition files from the link provided in the preferences window and install them. When the installation is complete, the ESP8266 board definitions should appear as shown in the screenshot. Now you can select your ESP8266 board from the Tools|Board menu:

How it works…

The Arduino IDE is an open source development environment used for programming Arduino boards and Arduino-based boards. It is also used to upload sketches to other open source boards, such as the ESP8266. This makes it an important accessory when creating Internet of Things (IoT) projects.

See also

These are the basics for the Arduino framework and they will be applied throughout this book to develop IoT projects.

 

Choosing an ESP8266 board


The ESP8266 module is a self-contained System On Chip (SOC), which features an integrated TCP/IP protocol stack that allows you to add Wi-Fi capability to your projects. The module is usually mounted on circuit boards that break out the pins of the ESP8266 chip, making it easy for you to program the chip and to interface with input and output devices.

ESP8266 boards come in different forms, depending on the company that manufactures them. All the boards use Espressif's ESP8266 chip as the main controller, but they have different additional components and different pin configurations, giving each board unique additional features.

Therefore, before embarking on your IoT project, take some time to compare and contrast the different types of ESP8266 boards that are available. This way, you will be able to select the board that has features best suited for your project.

Available options

The simple ESP8266-01 module is the most basic ESP8266 board available on the market. It has eight pins, which include four General Purpose Input/Output (GPIO) pins, serial communication TX and RX pins, an enable pin and power pins, and VCC and GND. Since it only has four GPIO pins, you can only connect three inputs or outputs to it.

The 8-pin header on the ESP8266-01 module has a 2.0 mm spacing that is not compatible with breadboards. Therefore, you have to look for another way to connect the ESP8266-01 module to your setup when prototyping. You can use female to male jumper wires to do that:

The ESP8266-07 is an improved version of the ESP8266-01 module. It has 16 pins, which consist of nine GPIO pins, serial communication TX and RX pins, a reset pin, an enable pin and power pins, and VCC and GND. One of the GPIO pins can be used as an analog input pin. The board also comes with a UFL. connector that you can use to plug an external antenna in case you need to boost Wi-Fi signal.

Since the ESP8266 has more GPIO pins, you can have more inputs and outputs in your project. Moreover, it supports both SPI and I2C interfaces, which can come in handy if you want to use sensors or actuators that communicate using any of those protocols. Programming the board requires the use of an external FTDI breakout board based on USB to serial converters, such as the FT232RL chip.

The pads/pinholes of the ESP8266-07 have a 2.0 mm spacing, which is not breadboard- friendly. To solve this, you have to acquire a plate holder that breaks out the ESP8266-07 pins to a breadboard-compatible pin configuration, with 2.54 mm spacing between the pins. This will make prototyping easier.

This board has to be powered from a 3.3V, which is the operating voltage for the ESP8266 chip:

The Olimex ESP8266 module is a breadboard-compatible board that features the ESP8266 chip. As with the ESP8266-07 board, it has SPI, I2C, serial UART, and GPIO interface pins. In addition to that, it also comes with a Secure Digital Input/Output (SDIO) interface, which is ideal for communication with an SD card. This adds six extra pins to the configuration, bringing the total to 22 pins.

Since the board does not have an on-board USB to serial converter, you have to program it using an FTDI breakout board or a similar USB to serial board/cable. Moreover, it has to be powered from a 3.3V source, which is the recommended voltage for the ESP8266 chip:

The Sparkfun ESP8266 Thing is a development board for the ESP8266 Wi-Fi SOC. It has 20 pins that are breadboard-friendly, which makes prototyping easy. It features SPI, I2C, serial UART, and GPIO interface pins, enabling it to be interfaced with many input and output devices. There are eight GPIO pins, including the I2C interface pins.

The board has a 3.3V voltage regulator, which allows it to be powered from sources that provide more than 3.3V. It can be powered using a micro USB cable or Li-Po battery. The USB cable also charges the attached Li-Po battery, thanks to the Li-Po battery charging circuit on the board.

Programming has to be done via an external FTDI board:

The Adafruit Huzzah ESP8266 is a fully standalone ESP8266 board. It has a built-in USB to serial interface that eliminates the need for using an external FTDI breakout board to program it. Moreover, it has an integrated battery charging circuit that charges any connected Li-Po battery when the USB cable is connected. There is also a 3.3V voltage regulator on the board that allows the board to be powered with more than 3.3V.

Though there are 28 breadboard friendly pins on the board, only 22 are useable. Ten of those pins are GPIO pins and they can also be used for SPI as well as I2C interfacing. One of the GPIO pins is an analog pin:

What to choose?

All the ESP8266 boards will add Wi-Fi connectivity to your project. However, some of them lack important features and are difficult to work with. So the best option would be to use the module that has the most features and is easy to work with. The Adafruit ESP8266 fits the bill.

The Adafruit ESP8266 is completely standalone and easy to power, program, and configure due to its on-board features. Moreover, it offers many input/output pins that will enable you to add more features to your projects. It is affordable and small enough to fit in projects with limited space.

There's more…

Wi-Fi isn't the only technology that we can use to connect our projects to the Internet. There are other options such as Ethernet and 3G/LTE. There are shields and breakout boards that can be used to add these features to open source projects. You can explore these other options and see which works for you.

See also

Now that we have chosen the board to use in our project, you can proceed to the next step, which is understanding all the components we will use with Adafruit ESP8266 in this book.

 

Required additional components


To demonstrate how the ESP8266 works, we will use some additional components. These components will help us learn how to read sensor inputs and control actuators using GPIO pins. Through this you can post sensor data to the Internet and control actuators from Internet resources, such as websites.

Required components

The components we will use include:

  • Sensors:

    • DHT11

    • Photocell

    • Soil humidity

  • Actuators:

    • Relay

    • Power switch tail kit

    • Water pump

  • Breadboard

  • Jumper wires

  • Micro USB cable

Sensors

Let's discuss the three sensors that we will be using.

DHT11

The DHT11 is a digital temperature and humidity sensor. It uses a thermistor and capacitive humidity sensor to monitor the humidity and temperature of the surrounding air, and produces a digital signal on the data pin. A digital pin on the ESP8266 can be used to read the data from the sensor data pin.

Note

The DHT11 sensor is not very precise, but it is perfect for experimenting, which we'll be doing in this book.

Photocell

A photocell is a light sensor that changes its resistance depending on the amount of incident light it is exposed to. They can be used in a voltage divider setup to detect the amount of light in the surroundings. In a setup where the photocell is used in the VCC side of the voltage divider, the output of the voltage divider goes high when the light is bright and low when the light is dim. The output of the voltage divider is connected to an analog input pin and the voltage readings can be read:

Soil humidity sensor

The soil humidity sensor is used for measuring the amount of moisture in soil and other similar materials. It has two large exposed pads that act as a variable resistor. If there is more moisture in the soil, the resistance between the pads drops, leading to a higher output signal. The output signal is connected to an analog pin from where its value is read.

Note

This sensor is mainly used for demonstration purposes, but it is perfect for the projects we'll do in this book.

Actuators

Let's discuss the actuators.

Relays

A relay is a switch that is operated electrically. It uses electromagnetism to switch large loads using small voltages. It consists of three parts: a coil, spring, and contacts. When the coil is energized by a high signal from a digital pin on the ESP8266, it attracts the contacts, forcing them closed. This completes the circuit and turns on the connected load. When the signal on the digital pin goes low, the coil is no longer energized and the spring pulls the contacts apart. This opens the circuit and turns off the connected load:

Power switch tail kit

A power switch tail kit is a device that is used to control standard wall outlet devices with microcontrollers. It is already packaged to prevent you from having to mess around with high voltage wiring. Using it, you can control appliances in your home using the ESP8266:

Water pump

A water pump is used to increase the pressure of fluids in a pipe. It uses a DC motor to rotate a fan and create a vacuum that sucks up the fluid. The sucked fluid is then forced to move by the fan, creating a vacuum again that sucks up the fluid behind it. This in effect moves the fluid from one place to another:

Breadboard

A breadboard is used to temporarily connect components without soldering. This makes it an ideal prototyping accessory that comes in handy when building circuits:

Jumper wires

Jumper wires are flexible wires that are used to connect different parts of a circuit on a breadboard:

Micro USB cable

A micro USB cable will be used to connect the Adafruit ESP8266 board to the computer:

See also

Having understood the components that you are going to use with Adafruit ESP8266, you can now proceed to the next step and learn how to upload sketches to your ESP8266.

 

Uploading your first sketch to the ESP8266


In this recipe, we are going to look at how to upload sketches to the ESP8266. This will enable you to understand some basics of the Arduino programming language and get you to upload your first sketch to your ESP8266 board.

Getting ready

Ensure that the board definitions of the ESP8266 board are installed in your Arduino IDE as explained earlier, and then connect your ESP8266 board to the computer. Now you can proceed to do the rest of the connections.

In this project, you will require a few extra components. They are:

Start by mounting the LED onto the breadboard. Connect one end of the 220 Ω resistor to the positive leg of the LED (the positive leg of an LED is usually the taller one of the two legs). Connect the other end of the resistor on another rail of the breadboard and connect one end of the jumper wire to that rail and the other end of the jumper wire to pin 5 of the ESP8266 board. Take another jumper wire and connect one if its ends to the negative leg of the LED and connect the other end to the GND pin of the ESP8266.

How to do it…

We will configure the board to output a low signal on pin 5 for a duration of one second and then output a high signal on pin 5 for a duration of two seconds. The process will repeat forever:

// LED pin
int ledPin = 5;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  
  // OFF
  digitalWrite(ledPin, LOW);                                
  delay(1000);             

  // ON
  digitalWrite(ledPin, HIGH); 
  delay(2000);                    
}

Refer the following steps:

  1. Copy the code and paste it in your Arduino IDE.

  2. Ensure your ESP8266 board is connected to your computer, and then proceed to select the board in Tools|Board menu. If the ESP8266 board definitions were properly installed, you should see a list of all ESP8266 boards in the menu:

  3. Choose the board type you are using. In this case it is the Adafruit HUZZAH ESP8266.

  4. Select the serial port where the ESP8266 board is connected from the Tools|Port menu and then proceed to upload the code to your board:

How it works…

This is a simple LED blinking program that we used to demonstrate how to upload code to an Adafruit ESP8266 board. The program blinks an LED connected to pin 5 of the Adafruit ESP8266 board every three seconds. This is done by turning off the LED for one second and turning on the LED for two seconds, continuously.

There's more…

The frequency at which the LED blinks can be increased or reduced by adjusting the delays in the program. For instance, the LED can be made to blink faster through reducing the second delay from two seconds to one second, by changing this statement delay(2000); to delay(1000);.

See also

Having successfully uploaded your first sketch to your ESP8266 board, you can go to the next recipe and learn how to connect your ESP8266 board to a local Wi-Fi network.

 

Connecting the ESP8266 to your local Wi-Fi network


In this recipe, we will show you how to connect your ESP8266 board to your local Wi-Fi network. Before you can send or receive data to and from the Internet, your ESP8266 board has to be connected to a Wi-Fi network that has Internet connectivity. This is done in the setup part of your program since it is supposed to be done once, when the ESP8266 is powered on.

Getting ready

Ensure your ESP8266 board is connected to your computer via the USB cable. No other components will be used in this exercise, since there are no inputs or outputs that are required.

How to do it…

We will include the ESP8266 library in the program and set the Wi-Fi network ssid and password so that the ESP8266 connects to the network when it gets powered on. We will print out a confirmation and the IP address of the ESP8266 when the connection is successful.

This is the sketch for this exercise:

// Libraries
#include <ESP8266WiFi.h>

// WiFi network
const char* ssid     = "your-ssid";
const char* password = "your-password";

void setup() {
  
  // Start serial
  Serial.begin(115200);
  delay(10);

  // Connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  
}

Refer the following steps:

  1. Copy this sketch and paste it in your Arduino IDE.

  2. Change the SSID in the code from your-ssid to the name of your Wi-Fi network, and the password from your-password to the password of your Wi-Fi network.

  3. Make sure you have selected the correct board from the Tools|Board menu, which in this case is Adafruit HUZZAH ESP8266, and the correct serial port from the Tools|Port menu.

  4. Upload the code to your ESP8266 board.

  5. Open the serial monitor (and make sure that the serial speed is set at 115200) so that you can check to see whether the ESP8266 board has connected to the Wi-Fi network.

The results are as shown in the following screenshot. The ESP8266 prints its IP address once the connection has been successfully established:

How it works…

The program sets up the Wi-Fi SSID and password using the WiFi.begin() function of the ESP8266Wifi library, and then executes the WiFi.status() function to try to connect to the Wi-Fi network. The program checks whether the WiFi.status() function returns a true value to indicate that the ESP8266 board has successfully connected to the Wi-Fi network. If the connection was not successful, the sketch tries to connect again using the WiFi.status() function and repeats that until the WiFi.status() function returns a true value to indicate that the ESP8266 has successfully connected to the Wi-Fi network.

When the ESP8266 connects to the Wi-Fi network, the sketch displays a message, WiFi connected, and the IP address of the ESP8266 on the serial monitor.

There's more…

You can enter the wrong password and SSID and see what output you get in the serial monitor.

See also

Now that you have successfully connected your ESP8266 board to your local Wi-Fi network, you can proceed to the next recipe, where we will be connecting the ESP8266 board to the Internet.

 

Connecting the ESP8266 to a cloud server


In this recipe, we will connect the ESP8266 to the Internet and send data to a cloud server. The cloud server we will be sending data to is dweet.io. The data we send to dweet.io will be used later in this book, so ensure that you execute this section successfully.

Getting ready

As in the previous recipe, we won't need any extra components here. All we need to do is ensure that the ESP8266 is connected to the computer.

How to do it…

To accomplish this, follow these steps:

  1. We will connect the ESP8266 to a local Wi-Fi network that has an active Internet connection.

  2. Once the connection is successful, we will send a GET request to the cloud server and then display the reply that the server sends back to the ESP8266 board:

    // Libraries
    #include <ESP8266WiFi.h>
  3. Enter the SSID and password:

    // SSID
    const char* ssid     = "your-ssid";
    const char* password = "your-password";
  4. Store the hostname of the cloud server:

    // Host
    const char* host = "dweet.io";
  5. Configure the SSID and password of the Wi-Fi network and connect the ESP8266 to the Wi-Fi network:

    void setup() {
    
      // Serial
      Serial.begin(115200);
      delay(10);
    
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      
      WiFi.begin(ssid, password);
      
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }
    
      Serial.println("");
      Serial.println("WiFi connected");  
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
    }
  6. Delay for five seconds and then print the name of the host we are connecting to on the serial monitor:

    void loop() {
      
      delay(5000);
    
      Serial.print("connecting to ");
      Serial.println(host);
  7. Connect to the host server:

      // Use WiFiClient class to create TCP connections
      WiFiClient client;
      const int httpPort = 80;
      if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
      }
  8. Formulate the URI for the GET request we will send to the host server:

      // We now create a URI for the request
      String url = "/dweet/for/my-thing-name?value=test";
  9. Send the GET request to the server and check whether the request has been received or if it has timed out:

      // Send request
      Serial.print("Requesting URL: ");
      Serial.println(url);
      
      client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                   "Host: " + host + "\r\n" + 
                   "Connection: close\r\n\r\n");
      unsigned long timeout = millis();
      while (client.available() == 0) {
        if (millis() - timeout > 5000) {
          Serial.println(">>> Client Timeout !");
          client.stop();
          return;
        }
      }
  10. Read incoming data from the host server line by line and display the data on the serial monitor.

  11. Close the connection after all the data has been received from the server:

      // Read all the lines from the answer
      while(client.available()){
        String line = client.readStringUntil('\r');
        Serial.print(line);
      }
    
      // Close connecting
      Serial.println();
      Serial.println("closing connection");
    }
  12. Copy the sketch to your Arduino IDE and change the SSID in the code from your-ssid to the name of your Wi-Fi network and the password from your-password to the password of your Wi-Fi network.

  13. Upload the sketch to your ESP8266 board.

  14. Open the serial monitor so that you can view the incoming data.

The serial monitor should display data, as shown in the following screenshot:

As you can see from the serial monitor, when you send the GET request to dweet.io you receive a reply from the server. The reply is enclosed in curly brackets {}.

How it works…

The program connects to the Wi-Fi network using the provided password and SSID. It then proceeds to connect to the provided cloud/host server using the client.connect() function, and sends the provided URI to the host server using the client.print() function.

Once the data has been successfully sent, the sketch waits for a reply from the server. It does this with the client.available() function, which checks whether there is incoming data from the server. If there is data available, the sketch reads it and displays it on the serial monitor. The process is repeated until the ESP8266 is turned off.

There's more…

Since you have understood how to connect the ESP8266 to a cloud server, see if you can change the sketch so that it connects to the www.google.com host server and searches for the word Arduino. The results from the server should be displayed on the serial monitor.

Tip

Hint: You can check out the following link to see the format for Google GET requests:

https://www.google.com/support/enterprise/static/gsa/docs/admin/72/gsa_doc_set/xml_reference/request_format.html.

See also

You can now continue to the next recipe and look at some of the common issues that you may face when using your ESP8266 and how you can solve them.

 

Troubleshooting basic ESP8266 issues


There are several issues you may face when using ESP8266 boards. Here are some of the common ones and ways to solve them.

The Board is not visible from the Arduino IDE

One of the causes of this problem is a faulty USB cable. You can troubleshoot this by using another USB cable to see whether it works. If it works, then your USB cable is faulty and should be replaced. If the problem still persists, even when you are using another USB cable, the issue may be with the board definitions.

This issue can also occur when the board definitions have not been installed properly, or if they have not been installed at all. So make sure that the board definitions have been installed properly, or try and reinstall them again.

The board cannot be configured from the Arduino IDE

A faulty USB cable can cause this kind of issue. To troubleshoot, use another USB cable and see whether the issue persists. If it persists, keep your USB cable and try another solution. However, if the issue gets solved, it means that your USB cable is indeed faulty and should be replaced.

If the USB cable is not faulty but the issue continues, press the reset button on the board. That should deal with the problem.

The board does not connect to your local Wi-Fi network

If your board does not connect to your local Wi-Fi network, check your Wi-Fi network's security protocol and confirm it is WPA. If it is not, change it to WPA. Moreover, confirm the password you have written in the sketch is the correct one. Also avoid using Wi-Fi passwords with strange characters. It could also be that the serial port is not set to the right speed, therefore you might not see the Wi-Fi connected message.

About the Author
  • Marco Schwartz

    Marco Schwartz is an electrical engineer, entrepreneur, and blogger. He has a master's degree in electrical engineering and computer science from Supélec, France, and a master's degree in micro engineering from the Ecole Polytechnique Fédérale de Lausanne (EPFL), Switzerland.

    He has more than five years' experience working in the domain of electrical engineering. Marco's interests center around electronics, home automation, the Arduino and Raspberry Pi platforms, open source hardware projects, and 3D printing. He has several websites about the Arduino, including the Open Home Automation website, which is dedicated to building home automation systems using open source hardware.

    Marco has written another book on home automation and the Arduino, called Home Automation With Arduino: Automate Your Home Using Open-source Hardware. He has also written a book on how to build Internet of Things projects with the Arduino, called Internet of Things with the Arduino Yun, by Packt Publishing.

    Browse publications by this author
Latest Reviews (6 reviews total)
alles okay, hat prima funktioniert
Book was well laid out and easy to follow. There is one minor pictorial error which for some one used to electroncis will soon spot.
Not received previous purchase
ESP8266 Internet of Things Cookbook
Unlock this book and the full library FREE for 7 days
Start now