放在后面了还是没用,顺便问一下小爱会返回动作信息吗?
#define BLINKER_PRINT Serial
#define BLINKER_MIOT_OUTLET
#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG
#include <Blinker.h>
char auth[] = " "; //设备key
BlinkerButton Button1("btn-1");
BlinkerButton Button2("btn-2");
BlinkerButton Button3("btn-3");
char data1on[4] = {0xA0, 0x01, 0x01, 0xA2};
char data1off[4] = {0xA0, 0x01, 0x00, 0xA1};
char data2on[4] = {0xA0, 0x02, 0x01, 0xA3};
char data2off[4] = {0xA0, 0x02, 0x00, 0xA2};
unsigned long Timers[3]={0};
char i;
int D1=0;
int D2=2;
bool oState=false;
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(D1, HIGH);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
Blinker.delay(200);
Serial.write(data1on, 4);
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(D1, LOW);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
Blinker.delay(200);
Serial.write(data1off, 4);
oState = false;
}
}
void miotQuery(int32_t queryCode) //小爱同学控制
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
break;
}
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(D2, LOW);
Blinker.delay(50);
digitalWrite(D1, HIGH);
Blinker.delay(50);
Serial.write(data2off, 4);
Blinker.delay(50);
Serial.write(data1on, 4);
Button1.print("on");
Button1.icon("fas fa-lightbulb");
Button1.color("#fddb00");
Button1.text("打开啦");
Button2.print("off");
Button2.icon("fas fa-lightbulb");
Button2.color("#000000");
Button2.text("关灯");
}
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, LOW);
Blinker.delay(50);
digitalWrite(D2, HIGH);
Blinker.delay(50);
Serial.write(data1off, 4);
Blinker.delay(50);
Serial.write(data2on, 4);
Button2.print("on");
Button2.icon("fas fa-lightbulb");
Button2.color("#fddb00");
Button2.text("打开啦");
Button1.print("off");
Button1.icon("fas fa-lightbulb");
Button1.color("#000000");
Button1.text("关灯");
}
void button3_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, LOW);
Blinker.delay(50);
digitalWrite(D2, LOW);
Blinker.delay(50);
Serial.write(data1off, 4);
Blinker.delay(50);
Serial.write(data2off, 4);
Blinker.delay(50);
Button1.print("off");
Button1.icon("fas fa-lightbulb");
Button1.color("#000000");
Button1.text("关灯");
Button2.print("off");
Button2.icon("fas fa-lightbulb");
Button2.color("#000000");
Button2.text("关灯");
Button3.print("off");
Button3.icon("fas fa-lightbulb");
Button3.color("#000000");
Button3.text("关闭");
}
void heartbeat(){
BLINKER_LOG("状态同步!");
if(digitalRead(D1)==HIGH){
Button1.print("on");
Button1.icon("fas fa-lightbulb");
Button1.color("#fddb00");
Button1.text("打开啦");
}
else{
Button1.print("off");
Button1.icon("fas fa-lightbulb");
Button1.color("#000000");
Button1.text("关灯");
}
if(digitalRead(D2)==HIGH){
Button2.print("on");
Button2.icon("fas fa-lightbulb");
Button2.color("#fddb00");
Button2.text("打开啦");
}
else{
Button2.print("off");
Button2.icon("fas fa-lightbulb");
Button2.color("#000000");
Button2.text("关灯");
}
}
void setup() {
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
// 初始化有LED的IO
pinMode(D1, OUTPUT);
digitalWrite(D1, LOW);
pinMode(D2, OUTPUT);
digitalWrite(D2, LOW);
// 初始化blinker
Blinker.begin(auth);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Blinker.attachHeartbeat(heartbeat);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Blinker.attachData(dataRead);
}
void loop() {
Blinker.run();
}
这是我的代码,麻烦大神看看 |