8266-web-rc433-遥控码

/*

arduino 开发

1 8266 采用ap模式,使用RCSwitch接受发送遥控码

2  D7发送 D8收

3 接受打遥控码在网页上显示,能显示历史数据,倒序。

4 网页上通过text框输入发送遥控代码,按键发送。

5 默认ipserv 192.168.4.1

6 ap密码 12345678 ssid ESP8266AP

代码ai生成 百度ai一次成功,阿里通意就好几次都报错,直接手机可以读取遥控器,发送433mhz 编码。。

2024-9-1

*/

#include <ESP8266WiFi.h>  

#include <ESP8266WebServer.h>  

#include <RCSwitch.h>  



RCSwitch mySwitch = RCSwitch();  

ESP8266WebServer server(80);  

const char *ssid = "ESP8266AP";  

const char *password = "12345678";  

String history = ""; // 用于保存历史数据的字符串  

const int historyMaxLength = 2048; // 历史数据的最大长度  



void setup() {  

  Serial.begin(115200);  

  WiFi.softAP(ssid, password);  

  IPAddress IP = WiFi.softAPIP();  

  Serial.print("AP IP address: ");  

  Serial.println(IP);  



  mySwitch.enableReceive(D7);  // Receiver on interrupt 0 => that is pin #2 (GPIO2)  

  mySwitch.enableTransmit(D8); // Transmitter is connected to GPIO15 (D8 on NodeMCU or Wemos D1 mini)  



  server.on("/", handleRoot);  

  server.on("/send", handleSend);  

  server.begin();  

}  



void loop() {  

  server.handleClient();  



  if (mySwitch.available()) {  

    int value = mySwitch.getReceivedValue();  

    if (value == 0) {  

      Serial.print("Unknown encoding");  

    } else {  

      Serial.print("Received ");  

      Serial.print(mySwitch.getReceivedValue());  

      Serial.print(" / ");  

      Serial.print(mySwitch.getReceivedBitlength());  

      Serial.print("bit ");  

      Serial.print("Protocol: ");  

      Serial.println(mySwitch.getReceivedProtocol());  



      // 更新历史数据字符串  

      String data = String(mySwitch.getReceivedValue()) + " / " + String(mySwitch.getReceivedBitlength()) + "bit<br>";  

      if (history.length() + data.length() > historyMaxLength) {  

        history = history.substring(data.length()) + data; // 移除最旧的数据以保持长度限制  

      } else {  

        history = data + history; // 将新数据添加到字符串开头,实现倒序显示  

      }  

    }  

    mySwitch.resetAvailable();  

  }  

}  



void handleRoot() {  

  String response = "<html><body><h1>Remote Control</h1><form action='/send' method='POST'><input type='text' name='code'><input type='submit' value='Send'></form><h2>Received Codes:</h2><p>" + history + "</p></body></html>";  

  server.send(200, "text/html", response);  

}  



void handleSend() {  

  if (server.arg("code") != "") {  

    int code = server.arg("code").toInt();  

    mySwitch.send(code, 24); // 发送24位的遥控码  

    server.send(200, "text/plain", "Code sent");  

  } else {  

    server.send(400, "text/plain", "No code provided");  

  }  

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值