From ac45a7b11ee99ad1d81eefca41e3bfb9edd16d25 Mon Sep 17 00:00:00 2001 From: aa Date: Mon, 29 Dec 2025 18:25:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=BC=80=E5=85=B3=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IR_WIFI.ino | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ platformio.ini | 18 ++++++ readme.txt | 1 + 3 files changed, 191 insertions(+) create mode 100644 IR_WIFI.ino create mode 100644 platformio.ini create mode 100644 readme.txt diff --git a/IR_WIFI.ino b/IR_WIFI.ino new file mode 100644 index 0000000..7d90872 --- /dev/null +++ b/IR_WIFI.ino @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#define URL "http://www.baidu.com" + +String ssid=""; +String password=""; + +const uint16_t kIrLed = 4; // ESP8266 GPIO pin to use. Recommended: 4 (D2). +const uint16_t kRecvPin = 14; + +IRsend irsend(kIrLed); +IRrecv irrecv(kRecvPin); + +decode_results results; + +WiFiUDP udp; +unsigned int localUdpPort = 9; //本地端口号 +IPAddress remote_IP(255, 255, 255, 255);// 自定义远程监听 IP 地址 + + + +void UDP_IR(); +void IR_UDP(); +void httpClientRequest(); + +const uint16_t tx = 1; +const uint16_t rx = 3; + +void setup() { + WiFiManager wifiManager; + +//短接 TX RX即可清除WIFI账号密码,然后需要重新配置 + pinMode(tx, OUTPUT); + digitalWrite(tx, LOW); + pinMode(rx, INPUT_PULLUP); + if (digitalRead(rx) == LOW) + { + delay(100); + if (digitalRead(rx) == LOW) + wifiManager.resetSettings(); + } + + Serial.begin(115200); + + // 建立WiFiManager对象 + // 自动连接WiFi。以下语句的参数是连接ESP8266时的WiFi名称 + wifiManager.autoConnect("IR_WIFI"); + + Serial.println("\nWiFi connect succcess!"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); //打印本设备IP + Serial.print("gateway address: "); + Serial.println(WiFi.gatewayIP()); //打印网关IP + httpClientRequest(); + + udp.begin(9); // 指定本地端口号 + Serial.println("UDP started"); + + irsend.begin(); // 启动IR发送 + irrecv.enableIRIn(); // 启动IR接收 + +} + + +void loop() { + UDP_IR(); + IR_UDP(); +} + +// 发送HTTP请求并且将服务器响应通过串口输出 +void httpClientRequest(){ + + WiFiClient client; //新添加 + + //重点1 创建 HTTPClient 对象 + HTTPClient httpClient; + + //重点2 通过begin函数配置请求地址。此处也可以不使用端口号和PATH而单纯的 + httpClient.begin(client,URL); + Serial.print("URL: "); Serial.println(URL); + + //重点3 通过GET函数启动连接并发送HTTP请求 + int httpCode = httpClient.GET(); + Serial.print("Send GET request to URL: "); + Serial.println(URL); + + //重点4. 如果服务器响应HTTP_CODE_OK(200)则从服务器获取响应体信息并通过串口输出 + //如果服务器不响应HTTP_CODE_OK(200)则将服务器响应状态码通过串口输出 + if (httpCode == HTTP_CODE_OK) { + // 使用getString函数获取服务器响应体内容 + String responsePayload = httpClient.getString(); + Serial.println("Server Response Payload: "); + Serial.println(responsePayload); + } else { + Serial.println("Server Respose Code:"); + Serial.println(httpCode); + } + + //重点5. 关闭ESP8266与服务器连接 + httpClient.end(); +} + +void IR_UDP(){ + if (irrecv.decode(&results)) { + // print() & println() can't handle printing long longs. (uint64_t) + serialPrintUint64(results.value, HEX); + uint8_t *p=(uint8_t *)&results.value; + udp.beginPacket(remote_IP, localUdpPort); //准备发送数据 + udp.write("IR"); + udp.write(*p); + udp.write(*(p+1)); + udp.write(*(p+2)); + udp.write(*(p+3)); + udp.write(*(p+4)); + udp.write(*(p+5)); + udp.write(*(p+6)); + udp.write(*(p+7)); + udp.endPacket(); + + irrecv.resume(); // Receive the next value + } +} + + +void UDP_IR() { + // 检查是否有数据包到达 + int packetSize = udp.parsePacket(); + if (packetSize) { + // 保证数据包长度不超过缓冲区 + if(packetSize > 255) { + packetSize = 255; + } + + // 读取数据并打印 + uint8_t packetBuffer[11]={0}; + int len = udp.read(packetBuffer, packetSize); +// if (len > 0) { +// packetBuffer[len] = '\0'; +// Serial.println(packetBuffer); +// } + uint64_t nec=0; + uint8_t *p1=(uint8_t *)&packetBuffer[0]; + uint8_t *p2=(uint8_t *)&nec; + if(*p1==0x49 && *(p1+1)==0x52) + { + Serial.println("NEC"); + if(*(p1+5) ==0x00) //按键码 + { + *p2 =*(p1+2); + *(p2+1)=*(p1+3); + *(p2+2)=*(p1+4); + *(p2+3)=*(p1+5); + + } + else if(*(p1+5)==0xff) + nec=0xFFFFFFFFUL; + } + irrecv.pause(); // Pause collection of received IR data. + //0x00FF12EDUL : 用户码(一般为0x00)+用户反码(一般为0xFF)+数据码+数据反码 ,注意大小端模式,会造成数据不对应,实际内容是一样的。 + irsend.sendNEC(nec); + irrecv.resume(); // Receive the next value + } +} diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..6bda1bb --- /dev/null +++ b/platformio.ini @@ -0,0 +1,18 @@ +[platformio] +src_dir = . + +[env] +lib_extra_dirs = ../../ +lib_ldf_mode = deep+ +lib_ignore = examples +framework = arduino +monitor_speed = 115200 +build_flags = ; -D_IR_LOCALE_=en-AU + +[env:nodemcuv2] +platform = espressif8266 +board = nodemcuv2 + +[env:esp32dev] +platform = espressif32 +board = esp32dev diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..2e6acb0 --- /dev/null +++ b/readme.txt @@ -0,0 +1 @@ +上电 ,连接WIFI“IR_WIFI”,发送端和接收端都要先配网。两边连上同一个WIFI,一边接收到红外遥控器信号会通过UDP发送,另一边接收到UDP数据会通过红外线发送。 \ No newline at end of file