arduino esp8266-AP-控制led

网上很少这个ap控制led的灯,求人还不如求自己,自己开搞
我用了esp8266-01s,只有一个引脚可以使用,那就用吧。esp8266创建一个热点,端口为8888,监听udp数据。app代码为e4a
app发送的数据为LED=1(关)LED=0(开)

#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();
	
            }
	
    }

apk我也有编译好的,连接链接:https://pan.baidu.com/s/1O7DqSksn9NWP7yzRF7rynQ
提取码:hoxv
只有安卓的,具体具体自己修改哈,引脚多多;qq交流群467948028

  • 3
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值