Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
ESP8266 Internet of Things Cookbook

You're reading from   ESP8266 Internet of Things Cookbook Over 50 recipes to help you master ESP8266 functionality

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781787288102
Length 268 pages
Edition 1st Edition
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
Marco Schwartz Marco Schwartz
Author Profile Icon Marco Schwartz
Marco Schwartz
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Configuring the ESP8266 FREE CHAPTER 2. Your First ESP8266 Projects 3. More ESP8266 Functions 4. Using MicroPython on the ESP8266 5. Cloud Data Monitoring 6. Interacting with Web Services 7. Machine to Machine Interactions 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 to do it…

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.

You have been reading a chapter from
ESP8266 Internet of Things Cookbook
Published in: Apr 2017
Publisher: Packt
ISBN-13: 9781787288102
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime