所需硬件
ESP8266 开发板(NodeMcu) x1
红外信号发射管Led x1
按键开关 x2
面包板 x1
杜邦线 若干
第三方库
IRremoteESP8266
下载地址:https://github.com/crankyoldgit/IRremoteESP8266
线路图
红外发射管 长的是正极,短的是负极。
实物图
按D3按键关闭空调,按D4开启空调
代码
红外信号原数据是从美的空调遥控器接收的
接收方法见:http://blog.raikay.com/post/2021/arduino-nodemcu-esp8266-vs1838b/
/**
作者: Raikay (raikay.com)
时间: 2021/01/17
说明: 红外发射管控制美的空调
NodeMcu 红外发射管
GND - 负极
D2 - 正极
**/
#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
const uint16_t kIrLed = 4; // NodeMcu D2
const uint16_t On1 = 0; // d3
const uint16_t On2 = 2; // d4
IRsend irsend(kIrLed); // Set the GPIO to be used to sending the message.
// 美的空调原数据 关闭空调
uint16_t rawData1[199] = {4500, 4404, 588, 1578, 590, 488, 590, 1578, 590, 1578, 590, 488, 588, 488, 590, 1578, 590, 488, 586, 490, 588, 1580, 588, 490, 588, 488, 588, 1578, 588, 1580, 586, 490, 588, 1578, 588, 490, 588, 1578, 588, 1578, 588, 1578, 588, 1580, 588, 490, 588, 1580, 588, 1580, 586, 1580, 588, 490, 588, 490, 588, 490, 586, 490, 586, 1580, 588, 490, 586, 490, 586, 1580, 588, 1580, 586, 1580, 586, 490, 588, 490, 588, 490, 586, 490, 586, 490, 588, 490, 586, 490, 586, 490, 586, 1580, 586, 1580, 586, 1580, 586, 1580, 586, 1580, 586, 5208, 4496, 4410, 586, 1580, 588, 490, 586, 1580, 588, 1578, 588, 490, 586, 490, 586, 1580, 588, 490, 586, 490, 586, 1580, 586, 490, 588, 490, 586, 1580, 586, 1580, 588, 490, 586, 1580, 586, 490, 588, 1580, 586, 1580, 586, 1580, 586, 1580, 588, 490, 586, 1580, 588, 1580, 586, 1580, 588, 490, 588, 490, 586, 490, 586, 490, 586, 1582, 584, 492, 586, 490, 586, 1582, 584, 1582, 586, 1580, 586, 490, 586, 490, 586, 490, 586, 490, 586, 490, 586, 492, 586, 490, 586, 490, 590, 1576, 586, 1580, 588, 1580, 586, 1580, 586, 1580, 586}; // COOLIX B27BE0
// 美的空调原数据 开启空调 29度 制热
uint16_t rawData2[199] = {4508, 4394, 600, 1568, 600, 478, 600, 1568, 600, 1568, 598, 480, 598, 478, 598, 1568, 600, 478, 598, 478, 598, 1568, 598, 478, 600, 478, 598, 1568, 598, 1568, 598, 478, 600, 1568, 598, 480, 598, 478, 598, 1568, 598, 1568, 598, 1568, 600, 1568, 600, 1566, 600, 1568, 598, 1568, 600, 1568, 600, 478, 600, 476, 600, 478, 598, 478, 600, 478, 600, 478, 600, 1568, 600, 478, 600, 1568, 600, 478, 600, 1568, 600, 1566, 600, 478, 598, 478, 600, 476, 600, 1568, 600, 478, 600, 1566, 600, 478, 600, 478, 600, 1566, 600, 1568, 600, 5194, 4508, 4398, 600, 1566, 604, 474, 600, 1566, 600, 1566, 600, 476, 600, 476, 600, 1566, 600, 476, 600, 476, 600, 1568, 600, 476, 600, 476, 602, 1566, 598, 1568, 600, 476, 600, 1566, 600, 476, 600, 476, 600, 1566, 600, 1566, 600, 1566, 602, 1566, 600, 1568, 600, 1566, 600, 1566, 600, 1566, 600, 476, 600, 476, 600, 476, 602, 476, 600, 478, 600, 476, 600, 1566, 600, 478, 600, 1568, 600, 476, 600, 1566, 600, 1566, 600, 478, 600, 478, 600, 478, 600, 1568, 600, 476, 600, 1568, 600, 476, 600, 476, 602, 1566, 600, 1568, 598}; // COOLIX B23FAC
void setup() {
irsend.begin();
#if ESP8266
Serial.begin(115200, SERIAL_8N1, SERIAL_TX_ONLY);
#else
Serial.begin(115200, SERIAL_8N1);
#endif
pinMode(On1, INPUT_PULLUP);
pinMode(On2, INPUT_PULLUP);
}
void loop() {
int buttonState1 = digitalRead(On1);
int buttonState2 = digitalRead(On2);
if (buttonState1 != HIGH) {
irsend.sendRaw(rawData1, 199, 38);
delay(5);
irsend.sendRaw(rawData1, 199, 38);
delay(5);
irsend.sendRaw(rawData1, 199, 38);
Serial.println("buttonState1:" + buttonState1);
}
if (buttonState2 != HIGH)
{
irsend.sendRaw(rawData2, 199, 38);
delay(5);
irsend.sendRaw(rawData2, 199, 38);
delay(5);
irsend.sendRaw(rawData2, 199, 38);
Serial.println("buttonState2:" + buttonState2);
}
}
这里把信号发了三遍,是因为,发射一遍信号比较弱,必须角度找的非常准确,即使这样有的时候还接收不到,发射三遍时,可以在任意角度随意发射,空调都可以接收到信号,百发百中!~