(上图为红外线接受模块,下图为红外线发射模块)
红外线接受模块和红外线发射模块均与led外设相同,有三根引脚,其硬件连接方式也大致相同。红外线接受模块只用于空调红外线的解码,而在实品的运用中,仅有一个发射模块即可。
在此工程中仅需要接受模块,代码如下:
#include
#include
#include
#include
const uint16_t kRecvPin = 2; //数据引脚选择(2 = IO2)
IRrecv irrecv(kRecvPin);
decode_results results;
void setup()
{
Serial.begin(115200); //串口通讯波特率
irrecv.enableIRIn(); // 初始化红外接收模块(开始接收)
while (!Serial) //等待初始化完成
delay(50);
Serial.println();
Serial.print("IRrecvDemo is now running and waiting for IR message on Pin "); //打印红外接收模块正在运行
Serial.println(kRecvPin); //打印红外接收端口
}
void loop()
{
if (irrecv.decode(&results))
{ //判断是否接收到红外信号
serialPrintUint64(results.value, HEX); //打印红外信号数据(串口监视器查看)
Serial.println("");
irrecv.resume(); //接收下一个值
}
delay(100);
}
(这个代码仅需要调节引脚的选择,如果报错的话可能是缺少指定的库,这时可以点开工具,然后找到管理库,在里面输入缺少的库,然后安装即可)
烧录完成后,打开工具 -> 串口监视器,将波特率调至115200,对着红外接收模块按动电视遥控按键,将会出现如图所示十六进制数据,这便是空调遥控器各个按键所对应的数据,记录下来即可。
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include
#include //红外头文件
#include //点灯科技头文件
char auth[] = "";//这里面填写设备的密钥key
char ssid[] = "";//这里填写你的wifi名字
char pswd[] = "";//这里填写你的WiFi密码
//定义红外发射的管脚
const uint16_t kIrLed = 14; // 设置kIrLed为GPIO14,D5脚
IRsend irsend(kIrLed); // 将kIrLed设置发送信息
//空调开:25、制冷、大风
uint16_t power_on[199] = {5998, 7318, 478, 1712, 540, 1622, 474, 1712, 542, 1622, 474, 1712, 542, 1620, 474, 1714, 542, 1622, 474, 596, 568, 532, 456, 618, 568, 532, 476, 598, 570, 532, 478, 596, 570, 532, 478, 1712, 542, 1622, 474, 1712, 542, 1622, 474, 1712, 542, 1622, 474, 1714, 542, 1622, 474, 598, 568, 532, 456, 618, 568, 532, 478, 598, 568, 532, 478, 596, 570, 532, 478, 1712, 544, 1622, 474, 1714, 542, 1622, 474, 1712, 544, 1622, 474, 1712, 544, 1622, 474, 598, 570, 532, 456, 618, 568, 532, 476, 600, 566, 532, 478, 602, 566, 530, 478, 1710, 546, 532, 456, 1732, 522, 556, 454, 1732, 546, 532, 526, 1660, 546, 1622, 526, 576, 514, 1650, 476, 626, 514, 1646, 478, 626, 548, 1612, 510, 594, 554, 522, 506, 596, 552, 1610, 506, 1660, 576, 524, 504, 1662, 572, 528, 502, 1664, 566, 1622, 502, 1664, 562, 536, 504, 574, 536, 1652, 504, 576, 534, 1654, 504, 596, 508, 570, 502, 598, 506, 1660, 502, 598, 506, 1660, 502, 598, 504, 1664, 498, 600, 502, 600, 476, 1692, 474, 626, 472, 1718, 448, 630, 470, 1720, 448, 630, 470, 1720, 448, 1720, 470, 7384, 446}; // GOODWEATHER D5292A000000
//空调开:25、制冷、自动风
uint16_t power_on_auto[199] = {6036, 7330, 476, 1716, 514, 1650, 474, 1716, 516, 1650, 474, 1716, 514, 1652, 520, 1670, 514, 1652, 524, 550, 542, 560, 456, 650, 514, 558, 476, 628, 514, 560, 478, 628, 514, 560, 476, 1718, 514, 1652, 474, 1716, 516, 1650, 476, 1716, 514, 1652, 474, 1716, 514, 1652, 474, 600, 540, 562, 456, 648, 514, 560, 476, 628, 512, 562, 478, 598, 540, 562, 456, 624, 538, 1654, 452, 622, 540, 1654, 452, 1738, 512, 1654, 452, 1738, 512, 1652, 454, 1738, 514, 562, 476, 1718, 514, 560, 456, 650, 514, 560, 476, 630, 512, 562, 476, 1718, 514, 560, 478, 1716, 514, 560, 478, 1718, 512, 1654, 454, 1738, 514, 1652, 454, 650, 510, 1654, 474, 630, 512, 1652, 504, 600, 514, 560, 508, 598, 514, 560, 506, 600, 512, 1652, 504, 1688, 514, 560, 508, 1686, 516, 560, 506, 1686, 516, 1652, 504, 1686, 518, 558, 506, 596, 542, 1624, 506, 596, 546, 1622, 528, 574, 552, 524, 558, 544, 580, 1586, 556, 548, 578, 1586, 562, 540, 578, 1586, 560, 546, 576, 500, 526, 1640, 602, 502, 552, 1614, 602, 500, 524, 1640, 602, 502, 498, 1668, 574, 1616, 502, 7332, 456}; // GOODWEATHER D5290A050000
//空调开:26、制冷、大风
uint16_t power_on_26[199] = {6062, 7302, 476, 1718, 538, 1628, 474, 1716, 540, 1626, 454, 1740, 514, 1650, 472, 1718, 516, 1650, 452, 620, 542, 560, 528, 546, 544, 560, 456, 620, 544, 560, 458, 618, 544, 558, 478, 1716, 516, 1650, 452, 1740, 514, 1650, 454, 1738, 516, 1650, 474, 1718, 514, 1652, 454, 618, 544, 558, 458, 620, 542, 560, 456, 620, 544, 560, 456, 620, 544, 560, 528, 1668, 516, 558, 476, 1718, 540, 1626, 476, 1716, 516, 1650, 454, 1738, 516, 1650, 474, 600, 544, 1650, 476, 598, 544, 560, 476, 602, 542, 560, 476, 604, 540, 560, 456, 1740, 514, 560, 476, 1718, 538, 536, 456, 1740, 538, 536, 456, 1738, 516, 1650, 476, 628, 514, 1652, 454, 648, 514, 1652, 454, 648, 514, 1652, 476, 626, 516, 562, 506, 1684, 546, 532, 532, 1660, 552, 524, 556, 1634, 582, 496, 560, 1634, 580, 1584, 562, 542, 580, 1584, 560, 544, 578, 1586, 530, 574, 576, 1588, 530, 576, 548, 526, 504, 602, 546, 1618, 504, 576, 568, 1620, 502, 578, 564, 1624, 504, 576, 562, 536, 502, 1666, 538, 562, 504, 1666, 536, 564, 504, 1666, 534, 566, 502, 1690, 510, 1658, 504, 7332, 502}; // GOODWEATHER D52A2A020000
//空调关:
uint16_t power_off[199] = {6022, 7290, 476, 1714, 510, 1650, 472, 1714, 512, 1650, 518, 1668, 510, 1652, 472, 1714, 512, 1650, 474, 626, 510, 560, 454, 648, 512, 562, 474, 598, 542, 562, 476, 598, 540, 560, 476, 1714, 512, 1650, 472, 1714, 512, 1648, 474, 1712, 512, 1650, 474, 1712, 512, 1650, 474, 598, 538, 560, 508, 568, 538, 560, 476, 596, 542, 560, 476, 628, 510, 562, 476, 1714, 512, 1650, 474, 1714, 510, 1650, 474, 1714, 512, 1650, 474, 1714, 512, 1650, 474, 626, 510, 562, 506, 596, 512, 560, 476, 628, 512, 560, 500, 602, 512, 562, 476, 1714, 514, 1648, 502, 1686, 512, 560, 502, 1688, 514, 560, 508, 1682, 514, 1648, 506, 594, 514, 560, 504, 598, 514, 1650, 502, 596, 516, 1648, 502, 598, 516, 556, 508, 596, 514, 1650, 504, 1682, 518, 558, 506, 1682, 520, 556, 504, 1684, 522, 1644, 504, 1682, 550, 528, 504, 596, 550, 1614, 530, 570, 578, 1586, 558, 544, 578, 498, 560, 542, 576, 1586, 562, 542, 576, 1586, 560, 544, 576, 1586, 558, 546, 576, 502, 552, 1612, 600, 502, 520, 1644, 598, 532, 470, 1664, 572, 530, 500, 1664, 570, 1620, 500, 7328, 456}; // GOODWEATHER D52928000000
// 新建Blinker组件对象
BlinkerButton Button1("btn-on");
BlinkerButton Button2("btn-on-auto");
BlinkerButton Button3("btn-26");
BlinkerButton Button4("btn-off");
void setup()
{
//初始化串口
Serial.begin(115200);
//注册回调函数/
Button1.attach(Button1_callback);
Button2.attach(Button2_callback);
Button3.attach(Button3_callback);
Button4.attach(Button4_callback);
//配网步骤:WIFI接入
Blinker.begin(auth,ssid,pswd);
//红外初始化
irsend.begin();
}
void loop()
{
Blinker.run();
}
//回调函数1
void Button1_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
//检测到普通按钮按下(btn-on)
if (state == BLINKER_CMD_BUTTON_TAP)
{
//发送红外指令
irsend.sendRaw(power_on, 199, 38); // 以38Khz发送红外数据
Button1.print("宿舍空调开:25,制冷,大风");
}
}
//回调函数2
void Button2_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
//检测到btn-on-auto按钮按下
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendRaw(power_on_auto, 199, 38); // 以38Khz发送红外数据
Button2.print("宿舍空调开:25,制冷,自动风");
}
}
//回调函数3
void Button3_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
//检测到按钮btn-26按下
if (state == BLINKER_CMD_BUTTON_TAP)
{
//发送红外指令
irsend.sendRaw(power_on_26, 199, 38); // 以38Khz发送红外数据
Button1.print("宿舍空调开:26,制冷,大风");
}
}
//回调函数4
void Button4_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
//检测到btn-off按钮按下
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendRaw(power_off, 199, 38); // 以38Khz发送红外数据
Button2.print("宿舍空调关");
}
}
(基于用手机控制led的基础,我们可以发现这些代码看似很多,但是其实十分简单,每一个回调函数其实就是对应着我们手机里的一个按键,而类如标红的那一串看似最麻烦的数组,其实就是我们解码所得到的数据。换句话说,我们只需要修改wifi密码、秘钥和按钮所对应的空调红外代码即可)
(ps:由于硬件本身的问题,调试红外线发射器的时候需要离空调近一点才能对空调进行控制,建议两两一组,一个发射一个接受,验证是否运行正常)