Lighting experiment based on ESP8266+ network debugging assistant

ESP8266 serial wifi module

insert image description here

Introduction

ESP8266 is a low-cost, high-performance Wi-Fi module with a built-in TCP/IP protocol stack. It can be used as a separate wireless network controller, or communicate with other microcontrollers through serial ports. It has the following characteristics:
1. Using CH340 chip, it is a cost-effective development board;
2. The design makes the USB interface more firm;
3. The documentation is complete, and anyone can obtain it for free, and it is only for circulation;
4. Each piece is released The pallets are strictly tested (it takes 2-5 minutes to test a board), and the quality is strictly controlled.

Support wireless 802.11 b/g/n standard;
support STA/AP/STA+AP three working modes;
built-in TCP/IP protocol stack, support multiple TCPClient connections (5MAX);
transmission rate: 110-460800bps: support UART/GPIO Data communication interface; support remote firmware upgrade (OTA); working temperature: -40°C~+125°C; drive form: dual-channel high-power H-bridge drive

In this experiment, the ESP8266 mobile phone lighting is realized, that is, the ESP8266 is controlled by the mobile phone APP to realize the lighting of the LED and the extinguishing of the LED.

Experiment preparation

ESP8266 module 1
LED 1
Microusb data cable (for board download program and power supply) 1
dupont line Slightly
Network debugging assistant APP 1

hardware wiring

First determine which pin of the esp8266 the LED is connected to, and check the pin definition of the esp8266:
insert image description here
this experiment uses the D2 pin of the esp8266 module to light the LED. Through the pin definition, you can know that it is GPIO4. So define the led pin in arduino It is 4, where the positive pole of the led is fixedly connected to vin to give a high level, and the negative pole of the led changes the high and low level through the D2 pin to control the on and off of the led. The hardware connection diagram is as follows:

insert image description here
insert image description here

program download

#include <ESP8266WiFi.h>

#define led 4 //led接在esp8266的GPIO4上
const char *ssid     = "YXDZ_8266";    //要连接的wifi名称
const char *password = "12345678";    //连接的wifi密码
const char *host = "172.21.150.2";    //要连接的手机端IP,手机和8266要在同一网络下

WiFiClient client;           //创建WiFiClient对象client,用于处理TCP连接
const int tcpPort = 8080;  //创建服务器时指定的端口号


void setup()
{
    
    
    Serial.begin(9600);               //初始化串口通信,波特率为9600
    pinMode(led,OUTPUT);             //将LED引脚设置为输出模式
    delay(10);
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);          //通过串口显示连接的wifi名称

    WiFi.begin(ssid, password);   //建立WiFi连接

     //等待连接建立
    while (WiFi.status() != WL_CONNECTED)
    {
    
    
        delay(500);
    }

 //连接成功信息及打印8266ip地址
    Serial.println("");
    Serial.println("Connection established!");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void loop()
{
    
    
    while (!client.connected())             //若未连接到主机,则客户端进行连接
    {
    
    
        if (!client.connect(host, tcpPort))//如果客户端未连接,使用client.connect()函数尝试连接主机并在500毫秒后再次尝试
        {
    
    
            Serial.println("connection....");
            delay(500);
        }
    }
    while (client.available())   //如果客户端连接成功,使用while (client.available())循环处理从主机接收到的数据
    {
    
    
        char val = client.read();   //将接收到的字符存储在变量val中
        if(val=='1'){
    
                  //如果val等于'1',则通过将LED引脚设置为LOW来关闭LED

           digitalWrite(led, LOW);
        }
        if(val=='0')             //如果val等于'0',则通过将LED引脚设置为HIGH来打开LED
        {
    
    
            digitalWrite(led, HIGH);
        }
    }
}

The above code implements the following functions:

WiFi connection: connect to the specified WiFi network through the WiFi.begin(ssid, password) statement. In the setup() function, the code waits until it successfully connects to the WiFi network. After the connection is successful, obtain and print the IP address of ESP8266 through WiFi.localIP().

Communication with the mobile terminal: use client.connect(host, tcpPort) to establish a TCP connection with the mobile terminal. In the loop() function, the code checks whether it is connected to the mobile phone. If not connected, wait for some time via delay, and retry the connection. Once successfully connected, the code continues to execute.

Receive and process data: Check whether data is available through client.available(). If data is available, read the data via client.read(). In this example, the code will determine whether the received data is 1 or 0 and turn on or off the LED connected to the ESP8266 according to the received value.

To sum up, the above code realizes the connection to the specified WiFi network through ESP8266, and establishes a TCP connection with the mobile phone for data interaction. By receiving instructions from the mobile phone, control the on and off of the LED. This code can be used as a simple remote control example, which can be used to control the switch state of the remote device.

After the program is completed, we upload the program to the esp8266 module:

insert image description here
After the program is downloaded, the esp8266 will connect to wifi, and you can view the related information of wifi connection through the serial port:
insert image description here

The mobile phone is connected to the same wifi as the esp8266 is connected to,

insert image description here

Open the network debugging assistant on the mobile phone, and create a new server:
insert image description here
click the newly created server to enter, it will automatically monitor, and it will be displayed as connected, indicating that the mobile phone and esp8266 have been successfully connected, and now you can communicate with the esp8266 through the mobile phone.
insert image description here

Select the paper airplane at the bottom to send it quickly, so that you can control the LED on and off more conveniently.
insert image description here

insert image description here
Experimental effect:
insert image description here

Precautions

When using the above code to implement functions, please note the following points:

  1. Replace the values ​​of the ssid and password variables with the credentials of the WiFi network you want to connect to. Make sure the credentials are correct so that the ESP8266 can successfully connect to the WiFi network.

  2. Replace the value of the host variable with the IP address of the host you want to connect to. Make sure the IP address is correct so that the ESP8266 can successfully establish a TCP connection.

  3. Ensures that the connecting host is listening for TCP connections on the specified port. In the code, the port number is 8080, if your host uses a different port, modify the value of the tcpPort constant accordingly.

  4. According to your hardware connection, make sure to connect the LED correctly to the designated pin of the ESP8266 module. In the code, the LED is connected to pin 4, if you connect the LED to other pins, modify the value of the led constant accordingly.

  5. If the data sent by your host is in a different format or protocol, please modify the code for processing the received data in the loop() function according to your needs. The current code simply controls the state of the LED based on the character received.

  6. If there is no corresponding network debugging assistant on the mobile phone, you can also connect through the network debugging assistant on the PC side. Here is a convenient and easy-to-use software NetAssist, which supports sending shortcut commands, and the experimental effect is the same as that on the mobile phone.

Before using the code, make sure you have installed the Arduino development environment for the ESP8266 development board and configured the development environment properly. This includes choosing the correct board and port, and installing the ESP8266 library.

Summarize

This article mainly introduces the function of ESP8266 to connect to WiFi and communicate with mobile phone. Use the ESP8266WiFi library to connect to the WiFi network, and use the WiFiClient library to communicate with the mobile phone. The code defines an LED connected to GPIO4 of ESP8266, and then initializes the serial port, LED pin and WiFi connection in the setup() function. In the loop() function, first check whether it is connected to the server, and start receiving data from the mobile phone after the connection is successful. If the received data is 1, turn on the LED, if it is 0, turn off the LED.

Through this article, we have learned how to use the ESP8266 module to connect to the WiFi network and control the LED by establishing a TCP connection with the host computer. This is a simple example of the potential of the ESP8266 in IoT applications. You can further expand this example to achieve more complex functions according to your needs and ideas. I hope this article is helpful to you. If there are any mistakes above, please leave a message to correct me! thanks for reading!

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/131967561