|
#define BLINKER_WIFI
#define BLINKER_MIOT_MULTI_OUTLET //设置为小爱多个插座的模式
#include <Blinker.h>
BlinkerButton Button1("btn-km"); //设置blinkerapp内数据键名
BlinkerButton Button2("btn-kckm"); //这里只用了其中三个
BlinkerButton Button3("btn-gckm");
BlinkerButton Button4("btn-ackm");
BlinkerText Text1("text_1");
char i;
char auth[] = "bfbf5f3fb57b";
char ssid[] = "MERCURY_07A8";
char pswd[] = "xg888888";
bool oState[5] = { false }; //设置5路开关控制
void miotPowerState(const String & state, uint8_t num) //小爱同学控制指令
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if(num==1){
i=D1; //指定每一路开关对应在开发板上的通道接口
}else if(num==2){
i=D2;
}else if(num==3){
i=D5;
}else if(num==4){
i=D6;
}
if (state == BLINKER_CMD_ON) {
digitalWrite(i, LOW); //这里控制模式都是点动模式
BlinkerMIOT.powerState("on", num);
BlinkerMIOT.print();
oState[num] = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(i, HIGH);
BlinkerMIOT.powerState("off", num);
BlinkerMIOT.print();
oState[num] = false;
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
}
}
}
}
void miotQuery(int32_t queryCode, uint8_t num)
{
BLINKER_LOG("AliGenie Query outlet: ", num,", codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
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) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D1, LOW);
Blinker.delay(300);
digitalWrite(D1, HIGH);
Blinker.print("开门命令已经处理");
Text1.print("shuju");
}
void button2_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D2, HIGH);
Blinker.delay(300);
Blinker.print("开车库门已经处理");
digitalWrite(D2, LOW);
}
void button3_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D5, HIGH);
Blinker.delay(400);
Blinker.print("关车库门已经处理");
digitalWrite(D5, LOW);
}
void button4_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(D6, HIGH);
Blinker.delay(400);
Blinker.print("关门命令处理");
digitalWrite(D6, LOW);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(D1, OUTPUT);
digitalWrite(D1, LOW);
pinMode(D2, OUTPUT);
digitalWrite(D2, LOW);
pinMode(D5, OUTPUT);
digitalWrite(D5, LOW);
pinMode(D6, OUTPUT);
digitalWrite(D6, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
}
void loop()
{
Blinker.run();
}
我发个完整的,原来是3路控制我加一路,就是怎么改只能实现一个继电器自锁,其他还是点动。望大佬指点 |
|