网上很少这个ap控制led的灯,求人还不如求自己,自己开搞
我用了esp8266-01s,只有一个引脚可以使用,那就用吧。esp8266创建一个热点,端口为8888,监听udp数据。app代码为e4a
app发送的数据为LED=1(关)LED=0(开)
[mw_shl_code=arduino,true]#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
unsigned int UDPPort = 8888; // local port to listen on
char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "UP"; // a string to send back
WiFiUDP Udp;
// 复位或上电后运行一次:
void setup() {
//在这里加入初始化相关代码,只运行一次:
Serial.begin(115200);
pinMode(2,OUTPUT);//初始化输出端口
WiFi.softAP("Wi-Fi","12345678");//ssid和pass 自己随便改
Udp.begin(UDPPort);
Serial.println();
Serial.println("Started ap. Local ip: " + WiFi.localIP().toString());
digitalWrite(2, LOW);
}
//一直循环执行:
void loop() {
// 在这里加入主要程序代码,重复执行:
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remoteIp = Udp.remoteIP();
Serial.print(remoteIp);
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println(packetBuffer[4]+1);//测试得到数据为50和49,我也不知道,所以packetBuffer[4]为48和49
if(packetBuffer[4]==48){//当它为LED=0的时候,
digitalWrite(2, HIGH);
}else{
digitalWrite(2, LOW);
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}[/mw_shl_code]
apk我也有编译好的,连接链接:https://pan.baidu.com/s/1O7DqSksn9NWP7yzRF7rynQ
提取码:hoxv
只有安卓的,具体具体自己修改哈,引脚多多;qq交流群467948028
|