新建开关程序
This commit is contained in:
commit
ac45a7b11e
172
IR_WIFI.ino
Normal file
172
IR_WIFI.ino
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <IRremoteESP8266.h>
|
||||||
|
#include <IRsend.h>
|
||||||
|
#include <IRrecv.h>
|
||||||
|
#include <IRutils.h>
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
|
#include <DNSServer.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
#include <WiFiManager.h>
|
||||||
|
#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
|
||||||
|
}
|
||||||
|
}
|
||||||
18
platformio.ini
Normal file
18
platformio.ini
Normal file
@ -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
|
||||||
1
readme.txt
Normal file
1
readme.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
上电 ,连接WIFI“IR_WIFI”,发送端和接收端都要先配网。两边连上同一个WIFI,一边接收到红外遥控器信号会通过UDP发送,另一边接收到UDP数据会通过红外线发送。
|
||||||
Loading…
Reference in New Issue
Block a user