我要实现的是esp8266空调遥控器的一个功能
但是加入红色部分代码之后就会连不上blinker
删除之后又正常的 arduino都没有报错
blinker一刷新设备状态(就是getstate)之后
esp8266就会重启
求懂得帮看看
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <ir_Electra.h>
#include <Blinker.h>
#include <Arduino.h>
char auth[] = "******";//这里面填写设备的密钥
char ssid[] = "gao";//这里填写你的wifi名字
char pswd[] = "13082******";//这里填写你的WiFi密码
BlinkerNumber NUM1("num-temp");
BlinkerNumber NUM2("num-fan");
BlinkerButton kElectraAc_setmode("btn-setmode");
BlinkerButton kElectraAc_power("btn-pwr");//电源
BlinkerButton kElectraAc_Light("btn-deng");//空调屏显
BlinkerButton kElectraAc_auto("btn-auto");//
BlinkerButton kElectraAc_cool("btn-cool");//
BlinkerButton kElectraAc_heat("btn-heat");//
BlinkerButton kElectraAc_dry("btn-dry");//
BlinkerButton kElectraAc_tempup("btn-tempup");
BlinkerButton kElectraAc_tempdown("btn-tempdown");
BlinkerButton kElectraAc_timeup("btn-timeup");
BlinkerButton kElectraAc_timedown("btn-timedown");
BlinkerButton kElectraAc_fanup("btn-fanup");
BlinkerButton kElectraAc_fandown("btn-fandown");
BlinkerButton kElectraAc_sx("btn-sx");//上下风
BlinkerButton kElectraAc_numfan("num-fan");
BlinkerButton kElectraAc_sleep1("btn-sleep1");//26℃睡眠()
//BlinkerButton kElectraAc_sleep2("btn-sleep2");//27℃睡眠
//BlinkerButton kElectraAc_sleep3("btn-sleep3");//28℃睡眠
BlinkerButton kElectraAc_yljian("btn-yljian");//音量-
BlinkerButton kElectraAc_yljia("btn-yljia");//音量+
BlinkerButton kElectraAc_up("btn-up");//上
BlinkerButton kElectraAc_down("btn-down");//下
BlinkerButton kElectraAc_left("btn-left");//左
BlinkerButton kElectraAc_right("btn-right");//右
//BlinkerButton kElectraAc_qd("btn-qd");//确定
//ESP8266的D2脚针
const uint16_t kIrLed = 4;
IRElectraAc ac(kIrLed);
IRsend irsend(kIrLed);
//26度睡眠开
uint16_t rawData1[211] = {9046, 4480, 586, 1644, 586, 1644, 588, 538, 564, 538, 564, 538, 564, 540, 562, 1644, 588, 1644, 588, 1642, 586, 1646, 586, 1668, 564, 538, 564, 1644, 586, 538, 564, 538, 564, 1642, 588, 538, 564, 538, 564, 538, 564, 540, 564, 538, 564, 1646, 586, 1648, 586, 1642, 588, 538, 564, 538, 564, 538, 564, 538, 562, 540, 562, 538, 564, 538, 562, 538, 564, 538, 564, 538, 564, 540, 564, 538, 564, 538, 564, 538, 564, 1644, 586, 538, 564, 538, 562, 540, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 562, 538, 562, 540, 562, 538, 564, 1644, 586, 540, 564, 538, 564, 1644, 588, 538, 564, 538, 564, 538, 564, 536, 564, 538, 564, 538, 588, 514, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 582, 520, 568, 534, 564, 538, 564, 540, 572, 528, 564, 538, 566, 538, 564, 538, 562, 540, 564, 538, 564, 1666, 564, 538, 564, 540, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 1664, 566, 1646, 586, 538, 564, 1646, 586, 538, 564, 538, 564, 538, 564, 538, 564, 1644, 588, 538, 562, 540, 564, 1644, 586, 540, 564, 538, 564, 1646, 586, 1646, 612}; // ELECTRA_AC
//26度睡眠关
uint16_t rawData2[211] = {9044, 4480, 588, 1642, 588, 1644, 588, 538, 564, 538, 566, 536, 564, 538, 564, 1644, 588, 1642, 588, 1644, 588, 1642, 588, 1644, 588, 536, 566, 1644, 588, 516, 586, 516, 586, 1644, 588, 536, 564, 538, 564, 538, 566, 538, 564, 538, 564, 1644, 588, 1642, 590, 1642, 588, 538, 566, 536, 564, 538, 566, 538, 564, 538, 564, 538, 564, 538, 564, 538, 566, 536, 566, 516, 586, 538, 564, 538, 564, 538, 564, 514, 588, 1644, 588, 538, 564, 538, 564, 538, 564, 538, 566, 538, 564, 516, 586, 538, 564, 538, 562, 538, 566, 536, 566, 538, 564, 538, 564, 538, 564, 536, 566, 1644, 588, 536, 564, 538, 566, 536, 566, 536, 564, 538, 564, 538, 566, 536, 566, 536, 566, 536, 566, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 566, 538, 564, 536, 566, 536, 564, 538, 564, 540, 562, 538, 566, 536, 566, 536, 566, 1644, 588, 538, 564, 538, 564, 538, 566, 536, 566, 536, 564, 538, 564, 538, 564, 540, 564, 538, 566, 536, 566, 1644, 588, 1644, 588, 538, 564, 1644, 588, 538, 564, 516, 586, 536, 566, 536, 564, 1644, 588, 536, 564, 1644, 588, 536, 564, 538, 564, 538, 564, 1642, 590, 1642, 614}; // ELECTRA_AC
//27度睡眠开
//uint16_t rawData3[211] = {9044, 4480, 588, 1646, 608, 1622, 586, 542, 562, 518, 608, 496, 606, 496, 596, 1632, 586, 1646, 584, 1648, 608, 1624, 584, 1644, 588, 1646, 584, 1646, 608, 518, 586, 516, 584, 1624, 584, 520, 604, 520, 586, 494, 608, 494, 608, 516, 562, 1646, 610, 1620, 586, 1648, 610, 516, 586, 494, 604, 520, 586, 496, 582, 542, 584, 518, 586, 494, 606, 518, 586, 516, 560, 542, 560, 520, 592, 508, 584, 542, 586, 494, 582, 1644, 586, 542, 560, 518, 608, 516, 586, 494, 582, 542, 584, 516, 586, 494, 582, 518, 590, 514, 582, 540, 584, 518, 586, 1620, 586, 542, 560, 520, 582, 1646, 584, 520, 608, 516, 586, 516, 586, 516, 586, 516, 586, 496, 582, 540, 586, 516, 562, 542, 584, 518, 562, 540, 560, 542, 586, 516, 560, 542, 586, 516, 562, 540, 562, 540, 562, 542, 586, 516, 562, 542, 560, 542, 586, 516, 562, 540, 562, 1646, 592, 532, 586, 516, 586, 516, 572, 530, 562, 540, 586, 516, 562, 542, 584, 518, 584, 516, 564, 538, 586, 1622, 610, 1622, 610, 516, 586, 1622, 610, 516, 562, 542, 584, 518, 586, 516, 586, 1620, 612, 516, 584, 518, 584, 518, 586, 1622, 608, 518, 562, 1644, 610, 1622, 612}; // ELECTRA_AC
//27度睡眠关
//uint16_t rawData4[211] = {9062, 4462, 610, 1622, 610, 1622, 610, 494, 584, 538, 564, 538, 562, 518, 610, 1620, 584, 1646, 610, 1620, 610, 1622, 612, 1620, 586, 1646, 594, 1636, 612, 516, 586, 494, 608, 1620, 586, 516, 600, 504, 584, 540, 562, 518, 584, 540, 586, 1620, 598, 1634, 586, 1646, 608, 516, 562, 518, 584, 540, 588, 516, 562, 518, 584, 516, 608, 516, 562, 540, 562, 540, 562, 540, 566, 514, 584, 520, 606, 492, 586, 540, 560, 1648, 610, 494, 582, 518, 588, 514, 610, 516, 562, 540, 582, 518, 562, 540, 562, 520, 608, 516, 586, 516, 562, 518, 582, 542, 586, 514, 588, 514, 588, 1622, 586, 538, 564, 538, 586, 516, 568, 536, 584, 516, 562, 540, 562, 540, 562, 540, 562, 540, 562, 540, 562, 540, 562, 540, 560, 520, 584, 538, 588, 516, 586, 516, 578, 524, 562, 540, 562, 540, 586, 516, 564, 538, 562, 540, 562, 540, 568, 1640, 606, 518, 562, 540, 564, 540, 562, 540, 562, 540, 568, 534, 586, 516, 582, 520, 586, 516, 586, 516, 586, 1622, 586, 1646, 584, 540, 562, 1644, 586, 540, 562, 540, 588, 514, 586, 516, 560, 1646, 584, 540, 588, 1620, 610, 1620, 612, 516, 562, 540, 586, 1620, 612, 1620, 636}; // ELECTRA_AC
//28度睡眠开
//uint16_t rawData5[211] = {9026, 4500, 568, 1662, 568, 1664, 568, 538, 564, 538, 564, 538, 564, 538, 564, 1664, 570, 1662, 568, 1662, 570, 1664, 568, 1664, 568, 538, 562, 540, 564, 1664, 568, 538, 564, 1662, 568, 538, 562, 540, 562, 540, 562, 538, 566, 538, 564, 1662, 570, 1662, 568, 1664, 568, 538, 576, 526, 564, 538, 564, 538, 564, 538, 562, 540, 564, 538, 566, 536, 566, 538, 564, 536, 566, 538, 564, 538, 564, 538, 566, 536, 564, 1662, 568, 538, 564, 538, 562, 538, 564, 538, 564, 540, 562, 538, 564, 538, 564, 538, 564, 538, 564, 538, 566, 534, 564, 1662, 568, 538, 564, 538, 564, 1662, 570, 538, 566, 538, 564, 538, 564, 538, 564, 538, 564, 538, 566, 536, 564, 538, 564, 538, 566, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 540, 562, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 538, 562, 540, 564, 538, 564, 1660, 570, 538, 588, 514, 564, 538, 562, 538, 566, 538, 564, 538, 564, 538, 564, 538, 564, 538, 564, 540, 562, 1660, 570, 1660, 570, 538, 564, 1660, 570, 538, 564, 538, 564, 538, 564, 538, 564, 1660, 570, 538, 564, 538, 566, 1660, 570, 1660, 570, 538, 566, 1660, 572, 1660, 598}; // ELECTRA_AC
//28度睡眠关
//uint16_t rawData6[211] = {9024, 4500, 568, 1662, 570, 1662, 568, 538, 564, 538, 564, 538, 566, 536, 564, 1662, 570, 1662, 570, 1662, 568, 1662, 570, 1662, 570, 536, 566, 536, 566, 1662, 570, 536, 566, 1662, 570, 536, 568, 536, 566, 536, 566, 536, 566, 536, 566, 1662, 570, 1662, 568, 1662, 568, 538, 566, 536, 566, 536, 566, 536, 566, 536, 570, 532, 566, 538, 566, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 538, 566, 1662, 568, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 536, 566, 538, 566, 536, 566, 536, 566, 536, 566, 536, 566, 1662, 570, 538, 566, 536, 566, 536, 566, 536, 566, 538, 566, 536, 566, 536, 566, 538, 564, 536, 566, 538, 566, 536, 566, 536, 568, 536, 564, 538, 566, 536, 566, 536, 566, 534, 566, 536, 566, 536, 566, 538, 564, 536, 566, 536, 566, 536, 566, 1662, 568, 538, 564, 538, 564, 538, 566, 536, 566, 536, 566, 536, 566, 540, 562, 538, 566, 538, 566, 536, 566, 1660, 570, 1662, 570, 536, 564, 1662, 570, 536, 566, 536, 568, 534, 566, 536, 566, 1660, 570, 538, 566, 1660, 570, 538, 566, 1662, 568, 538, 564, 1662, 570, 1662, 594}; // ELECTRA_AC
void printState() {
Serial.println("Electra A/C remote is in the following state:");
Serial.printf(" %s\n", ac.toString().c_str());
unsigned char* ir_code = ac.getRaw();
Serial.print("IR Code: 0x");
for (uint8_t i = 0; i < kElectraAcStateLength; i++)
Serial.printf("%02X", ir_code);
Serial.println();
}
void setup()
{
irsend.begin();
Serial.begin(115200);
ac.begin();
kElectraAc_power.attach(kElectraAc_power_callback);
kElectraAc_Light.attach(kElectraAc_Light_callback);
kElectraAc_setmode.attach(kElectraAc_setmode_callback);
kElectraAc_fanup.attach(kElectraAc_fanup_callback);
kElectraAc_fandown.attach(kElectraAc_fandown_callback);
kElectraAc_tempup.attach(kElectraAc_tempup_callback);
kElectraAc_tempdown.attach(kElectraAc_tempdown_callback);
kElectraAc_sx.attach(kElectraAc_sx_callback);
kElectraAc_yljian.attach(kElectraAc_yljian_callback);
kElectraAc_yljia.attach(kElectraAc_yljia_callback);
kElectraAc_up.attach(kElectraAc_up_callback);
kElectraAc_down.attach(kElectraAc_down_callback);
kElectraAc_left.attach(kElectraAc_left_callback);
kElectraAc_right.attach(kElectraAc_right_callback);
//kElectraAc_qd.attach(kElectraAc_qd_callback);
kElectraAc_sleep1.attach(kElectraAc_sleep1_callback);
//kElectraAc_sleep2.attach(kElectraAc_sleep2_callback);
//kElectraAc_sleep3.attach(kElectraAc_sleep3_callback);
Serial.println("Default state of the remote.");
printState();
Serial.println("Setting desired state for A/C.");
Blinker.attachHeartbeat(heartbeat);
Blinker.begin(auth,ssid,pswd);
}
void loop()
{
Blinker.run();
}
//暂存温度、风速、模式数据
int nowtemp = 26;
int num_mode = 1;
int nowfan = 2;
//空调电源开关
void kElectraAc_power_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
ac.on();
ac.setMode(kElectraAcCool);
ac.setTemp(26);
ac.setFan(2);
ac.setSwingV(false);
ac.setLightToggle(false);
ac.send();
kElectraAc_power.icon("fal fa-power-off");
kElectraAc_power.color("#00A90C");//绿色
kElectraAc_power.text("开");
kElectraAc_power.print("on");
}
else if (state == BLINKER_CMD_OFF)
{
ac.off();
ac.send();
kElectraAc_power.icon("fal fa-power-off");
kElectraAc_power.color("#EA0909");//红色
kElectraAc_power.text("关");
kElectraAc_power.print("off");
}
}
//空调灯光
void kElectraAc_Light_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
ac.setLightToggle(true);
ac.send();
kElectraAc_Light.color("#FBA613");
kElectraAc_Light.text("灯光");
kElectraAc_Light.print("on");
}
else if (state == BLINKER_CMD_OFF)
{
ac.setLightToggle(true);
ac.send();
kElectraAc_Light.color("#595959");
kElectraAc_Light.text("灯光");
kElectraAc_Light.print("off");
}
}
//上下扇风
void kElectraAc_sx_callback(const String &state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
ac.setSwingV(true);
ac.setLightToggle(false);
ac.send();
kElectraAc_sx.color("#00A90C");
kElectraAc_sx.text("上下风");
kElectraAc_sx.print("on");
}
else if (state == BLINKER_CMD_OFF)
{
ac.setSwingV(false);
ac.setLightToggle(false);
ac.send();
kElectraAc_sx.color("#595959");
kElectraAc_sx.text("上下风");
kElectraAc_sx.print("off");
}
}
//心跳包
void heartbeat()
{
switch (num_mode)
{
case 0:
kElectraAc_setmode.icon("far fa-recycle");
kElectraAc_setmode.color("#7FFFD4");
kElectraAc_setmode.text("自动");
break;
case 1:
kElectraAc_setmode.icon("fas fa-snowflake");
kElectraAc_setmode.color("#1E90FF");
kElectraAc_setmode.text("制冷");
break;
case 2:
kElectraAc_setmode.icon("fas fa-tint");
kElectraAc_setmode.color("#7CFC00");
kElectraAc_setmode.text("除湿");
break;
case 3:
kElectraAc_setmode.icon("fab fa-first-order-alt");
kElectraAc_setmode.color("#E0FFFF");
kElectraAc_setmode.text("送风");
break;
case 4:
kElectraAc_setmode.icon("fas fa-sun");
kElectraAc_setmode.color("#FF0000");
kElectraAc_setmode.text("制热");
break;
}
kElectraAc_setmode.print();
NUM1.print(nowtemp);
}
//空调模式按钮
void kElectraAc_setmode_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
num_mode++;
if (num_mode >= 5)
{
num_mode = 0;
}
switch (num_mode)
{
case 0:
ac.setMode(kElectraAcAuto);
kElectraAc_setmode.icon("fal fa-recycle");
kElectraAc_setmode.color("#008000");
kElectraAc_setmode.text("自动");
break;
case 1:
ac.setMode(kElectraAcCool);
kElectraAc_setmode.icon("fal fa-snowflake");
kElectraAc_setmode.color("#1E90FF");
kElectraAc_setmode.text("制冷");
break;
case 2:
ac.setMode(kElectraAcDry);
kElectraAc_setmode.icon("fal fa-tint-slash");
kElectraAc_setmode.color("#87CEFA");
kElectraAc_setmode.text("除湿");
break;
case 3:
ac.setMode(kElectraAcFan);
kElectraAc_setmode.icon("fab fa-first-order-alt");
kElectraAc_setmode.color("#2E8B57");
kElectraAc_setmode.text("送风");
break;
case 4:
ac.setMode(kElectraAcHeat);
kElectraAc_setmode.icon("fas fa-sun");
kElectraAc_setmode.color("#FF0000");
kElectraAc_setmode.text("制热");
break;
}
kElectraAc_setmode.print();
ac.setLightToggle(false);
ac.send();
}
}
//加风速
void kElectraAc_fanup_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
nowfan++;
if (nowfan >4)
{
nowfan = 1;
}
switch (nowfan)
{
case 4:
ac.setFan(5);
NUM2.print(nowfan);
kElectraAc_numfan.text("自动风");
break;
case 1:
ac.setFan(3);
NUM2.print(nowfan);
kElectraAc_numfan.text("风速");
break;
case 2:
ac.setFan(2);
NUM2.print(nowfan);
kElectraAc_numfan.text("风速");
break;
case 3:
ac.setFan(1);
NUM2.print(nowfan);
kElectraAc_numfan.text("风速");
break;
}
ac.setLightToggle(false);
ac.send();
}
}
//减风速
void kElectraAc_fandown_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
nowfan--;
if (nowfan <1)
{
nowfan = 4;
}
switch (nowfan)
{
case 4:
ac.setFan(5);
NUM2.print(nowfan);
kElectraAc_numfan.text("自动风");
break;
case 1:
ac.setFan(3);
NUM2.print(nowfan);
kElectraAc_numfan.text("风速");
break;
case 2:
ac.setFan(2);
NUM2.print(nowfan);
kElectraAc_numfan.text("风速");
break;
case 3:
ac.setFan(1);
NUM2.print(nowfan);
kElectraAc_numfan.text("风速");
break;
}
ac.setLightToggle(false);
ac.send();
}
}
//空调升温
void kElectraAc_tempup_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
if (nowtemp <32)
{
nowtemp++;
NUM1.print(nowtemp);
ac.setTemp(nowtemp);
ac.setLightToggle(false);
ac.send();
}
else if (nowtemp >=32)
{
Blinker.notify("已经是最高温度啦!");
}
}
}
//空调降温
void kElectraAc_tempdown_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
if (nowtemp >16)
{
nowtemp--;
NUM1.print(nowtemp);
ac.setTemp(nowtemp);
ac.setLightToggle(false);
ac.send();
}
else if (nowtemp <=16)
{
Blinker.notify("已经是最低温度啦!");
}
}
}
//睡眠
void kElectraAc_sleep1_callback(const String &state)
{
if (state == BLINKER_CMD_ON)
{
irsend.sendRaw(rawData1, 211, 38);
kElectraAc_sleep1.print("on");
}
else if (state == BLINKER_CMD_OFF)
{
irsend.sendRaw(rawData2, 211, 38);
kElectraAc_sleep1.print("off");
}
}
//遥控器音量减
void kElectraAc_yljian_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendNEC(0x2FD7887);
}
}
//遥控器音量加
void kElectraAc_yljia_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendNEC(0x2FD58A7);
}
}
//遥控器左
void kElectraAc_left_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendNEC(0x2FD629D);
}
}
//遥控器右
void kElectraAc_right_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendNEC(0x2FDE21D);
}
}
//遥控器上
void kElectraAc_up_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendNEC(0x2FD9867);
}
}
//遥控器下
void kElectraAc_down_callback(const String &state)
{
if (state == BLINKER_CMD_BUTTON_TAP)
{
irsend.sendNEC(0x2FDB847);
}
}
|