官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 美姑县| 安岳县| 小金县| 塔城市| 东丰县| 改则县| 屏东县| 中阳县| 肃南| 朝阳区| 化隆| 忻城县| 郸城县| 嘉善县| 方山县| 冀州市| 三原县| 锦屏县| 南充市| 新郑市| 沙坪坝区| 沅陵县| 阜南县| 文昌市| 保定市| 丹巴县| 宕昌县| 永吉县| 崇州市| 左贡县| 新昌县| 柘城县| 大渡口区| 尼勒克县| 洛宁县| 迁西县| 石台县| 三门县| 茌平县| 从化市| 枝江市|