本帖最后由 asker 于 2020-2-24 19:52 编辑
WIFI模块 ESP 01
物联网继电器模块 GPIO0 低电平控制
代码改自官方”MIOT_OUTLET“例程
如下:
[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "**************";
char ssid[] = "***************";
char pswd[] = "*************";
#define LED 0 //0对应GPIO0 2对应GPIO2
bool oState = false;
BlinkerButton Button1("btn-abc");
//按下按键即会执行该函数
void button1_callback(const String & state) {
digitalWrite(LED, !digitalRead(LED));
BLINKER_LOG("get button state: ", state);
Button1.print(state);
}
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
} else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
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 setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
}
void loop()
{
Blinker.run();
}[/mw_shl_code]
(安装编程环境折腾了半天时间,编译老是出问题,最后把''blinker-library-master''放到程序安装目录下的''libraries''里后竟然好了。。。不知道有没有人和我一样- -)
烧写完程序,把小爱同学设置好,并在点灯app里添加一个开关型按钮,就可以同时用小爱同学和点灯app控制继电器动作。
然后我就发现了一个问题,比如继电器控制一盏灯,用小爱控制继电器闭合后,灯打开了,但是app里的开关组件显示灯还是在关闭状态。
请教大佬们,怎么才能让小爱同学和App里的开关组件状态同步,也就是说用小爱打开或关闭这盏灯,app里的开关组件也能同步显示为开启或关闭状态?
|