/*
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");
}
}
8266-web-rc433-遥控码
最新推荐文章于 2024-09-02 22:47:56 发布