Book Image

ESP8266 Internet of Things Cookbook

By : Marco Schwartz
Book Image

ESP8266 Internet of Things Cookbook

By: Marco Schwartz

Overview of 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.
Table of Contents (15 chapters)
ESP8266 Internet of Things Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

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.