先记下来代码,后续再说
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
WiFiUDP Udp;
unsigned int UDPPort = 8888; //wifi端口
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //接收udp数据长度
uint16_t lx, ly, rx, ry; //接收APP的信号量
//l298n 驱动器连接
uint8_t cmd = 0; // 指令 命令类型
//wifi udp 数据传输初始化
void wifiUdpInit() {
Udp.begin(UDPPort);
}
//udp数据循环获取数据
void wifiUdpGetMsg() {
int packetSize = Udp.parsePacket();
if (packetSize) {
//等待接受UDP数据,一旦收到就进入处理
int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
packetBuffer[n] = 0;
Serial.print("收到数据:");
Serial.println(packetBuffer);
String line = packetBuffer;
//摇杆数据
if (line.indexOf("lx") != -1) {
String tline = line.substring(line.indexOf("lx"));
lx = tline.substring(2, tline.indexOf(",")).toInt();
tline = line.substring(line.indexOf("ly"));
ly = tline.substring(2, tline.indexOf(",")).toInt();
tline = line.substring(line.indexOf("rx"));
rx = tline.substring(2, tline.indexOf(",")).toInt();
tline = line.substring(line.indexOf("ry"));
ry = tline.substring(2, tline.indexOf(",")).toInt();
Serial.println(lx);
Serial.println(ly);
Serial.println(rx);
Serial.println(ry);
if (rx == 50 and ry == 50) {
if (lx > 25 and lx < 75 and ly < 25) {
//前进
cmd = 1;
} else if (lx > 25 and lx < 75 and ly > 75) {
//后退
cmd = 2;
} else if (ly > 25 and ly < 75 and lx < 25) {
//右
cmd = 4;
} else if (ly > 25 and ly < 75 and lx > 75) {
//左
cmd = 3;
} else {
//停止
cmd = 0;
}
}else{
cmd = 0;
}
}
//按钮数据
if (line.indexOf("fire") != -1) {
cmd = 15;
}
if (line.indexOf("on") != -1) {
cmd = 16;
}
if (line.indexOf("off") != -1) {
cmd = 17;
}
}
}
void wifiInit() {
WiFi.mode(WIFI_AP); //设置Wi-Fi为AP模式
IPAddress softLocal(192, 168, 1, 1); //设置内网WIFI IP地址
IPAddress softGateway(192, 168, 1, 1); //设置网关
IPAddress softSubnet(255, 255, 255, 0);
WiFi.softAPConfig(softLocal, softGateway, softSubnet);
String apName = ("ESP8266-cat-car" ); // 2 设置WIFI名称
const char *softAPName = apName.c_str();
WiFi.softAP(softAPName, "87654321"); // 3创建wifi 名称 +密码 adminadmin
IPAddress myIP = WiFi.softAPIP(); // 4输出创建的WIFI IP地址
Serial.println("");
Serial.print("AP直链模式 网关IP: ");
Serial.println(myIP);
Serial.print("AP SSID: "); // 5输出WIFI 名称
Serial.println(apName);
Serial.print("AP 密码: "); // 5输出WIFI 名称
Serial.println("12345678"); // 5输出WIFI 名称
}
//根据指令执行程序
void handCmd() {
Serial.println(cmd);
switch (cmd) {
case 0:
// 停止
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
break;
case 1:
//前进
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
break;
case 2:
//后退
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
break;
case 3:
//左转
digitalWrite(D1, HIGH);
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
digitalWrite(D4, HIGH);
break;
case 4:
//右转
digitalWrite(D1, LOW);
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
digitalWrite(D4, LOW);
break;
}
}
void setup() {
Serial.begin(115200);
wifiInit();//wifi 初始化
Udp.begin(UDPPort);//udp初始化
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
digitalWrite(D3, LOW);
digitalWrite(D4, LOW);
digitalWrite(D1, LOW);
digitalWrite(D2, LOW);
}
void loop() {
wifiUdpGetMsg();//循环获取数据
handCmd();
}