ESP8266——The mobile phone controls the LED lights on the Arduino (LAN AT command mode)

Principle of the experiment

The mobile phone uses the mobile phone software to control the on and off of the Arduino LED that has been connected to the 8266.

Experimental steps

Step 1 : Connect ESP8266 via USB-TTL to set AT command and save the transparent transmission mode. The wiring is as follows:

ESP8266 USB-TTL
VCC 3.3V
GND GND
IN 3.3V
RX TX
TX RX

Insert picture description here
Send AT command through serial debugging assistant:

//设置WiFi应用模式为Station
AT+CWMODE_DEF=3
//连接到WiFi路由器,请将SSID替换为路由器名称,Password替换为路由器WiFi密码
AT+CWJAP_DEF="SSID","Password"
//连接单连模式
AT+CIPMUX=0
//设置为透传模式
AT+CIPMODE=1
//进入透传模式,并保存(进入后模块就一直为透传模式,需要退出则取消发送新行,发送+++)
,IP地址为远端设备地址,例:我用手机控制,那么我的手机在路由器WIFI上的IP地址为192.168.0.104
AT+SAVETRANSLINK=1,"你的手机的IP",8080,"TCP"

After the setting is successful, the module will automatically pass through when it is powered on. If you want to exit the pass through mode. To return to AT mode, you need to send a command: + +
feedback: CLOSED. (If you do not exit the transparent transmission, you will never be able to enter the AT mode. For instructions, please refer to the AT command set).

Step 2 : Upload the Arduino to the LED control program. (Be careful not to choose the wrong board model and COM slogan)

int led_pin = 13;
//定义一个10字节的整型数据变量cmd作为命令,这里可以修改为不同的数字。此处设置为10是为了有更好的兼容性。
char cmd[10];
//判断收到的cmd是否有内容
bool valid_cmd = false;

void setup()
{
    
    
//定义连接led的引脚为输出信号
pinMode(led_pin, OUTPUT);
Serial.begin(115200);
}
void loop()
{
    
    
/*以下部分是串口信息处理过程*/
//定义一个整数型变量i
int i;
//如果串口收到有数据
if (Serial.available() > 0)
{
    
    
//变量i最大为10
for (i = 0; i < 10; i++)
{
    
    
//清空缓存,存入cmd变量,并以\0作为结束符
cmd[i] = '\0';
}
//此时i只能取前9位,第10位是结束符\0
for (i = 0; i < 9; i++)
{
    
    
//再次判断串口如果收到有数据,防止数据丢失
if (Serial.available() > 0)
{
    
    
//给变量cmd赋值,取串口收到的前9位字符
cmd[i] = Serial.read();
delay(1);
}
else
{
    
    
//如果串口数据超过9位,后面的字符直接忽略,跳到下一步
break;
}
}
/*以上串口信息处理结束*/

//得到最终变量cmd的有效值
valid_cmd = true;
}

//判断变量cmd的值,开始处理
if (valid_cmd)
{
    
    
//如果变量cmd的前2位的值是on
if (0 == strncmp(cmd, "on", 2))
{
    
    
//则连接led的引脚电压被置高5V,
digitalWrite(led_pin, HIGH);
//串口打印返回值ON,表示ON的操作执行成功
Serial.println("ON");
}
else if (0 == strncmp(cmd, "off", 3)) //否则如果变量cmd的前3位的值是off
{
    
    
//则连接继电器的引脚电压被置低0V,灯的电路被断开,灯灭
digitalWrite(led_pin, LOW);
//串口打印返回值F,表示OFF的操作执行成功
Serial.println("OFF");
}
else //如果以上两个条件都不成立,前2位不是ON,或者前3位不是OFF,即不正确的命令
{
    
    
//仅串口打印返回值X,表示指令错误。
Serial.println("X");
}
//到此,变量cmd的指令被处理完毕
valid_cmd = false;
}
//延迟10毫秒,返回loop主程序继续读取新的串口指令
delay(10);
}

Step 3 : Connect ESP8266 and LED lights to Arduino.

ESP8266 Arduino
Vcc 3.3V
GND GND
IN 3.3V
RX TX
TX RX

Step 4 : Open the mobile phone APP, add a TCP SERVER, the port number should be consistent
1. Open the network debugging assistant, click TCP SERVER-configuration
Insert picture description here
2. When the server is turned on, the ESP8266 is automatically connected.
Insert picture description here
3. Send "on" in the sending box to light up the 13-pin LED on the Arduino.
4. Send "off" in the sending box to turn off the 13-pin LED on the Arduino.

Reminders:
1. If ESP8266 does not appear on the server (mobile phone), please check whether the phone and ESP8266 are connected to the same router. Please do not use secondary routers (ie routing-routing), otherwise it may block the network and cause failure to connect.
2. In this small experiment: you must remember to save the transparent transmission mode. If you don't save it, you won't be able to automatically enter the transparent transmission mode after power-on again. It is recommended that you first learn about the use of ESP8266 before trying this experiment.
3. The IP address is the IP address of the mobile phone on the WIFI of the router. Don't fill in the wrong one.
4. The network debugging assistant on the mobile phone, the lowercase "on" and "off" are sent in the sending box, and the feedback is uppercase "ON" and "OFF"; the feedback "X" means that the first two digits in the sending box are not on, The first three digits are not off, so feedback "X" to indicate it, depending on the definition in the program.

Guess you like

Origin blog.csdn.net/weixin_44925547/article/details/106974684