|
楼主 |
发表于 2021-1-18 17:23
|
显示全部楼层
本帖最后由 francis00315 于 2021-1-18 17:25 编辑
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_PRINT Serial
#include <Blinker.h>
char auth[] = "******";
char ssid[] = "*****";
char pswd[] = "****";
// 新建组件对象
BlinkerButton Button1("btn-abc");
BlinkerButton Button2("btn-state");
BlinkerNumber Number1("num-abc");
bool ostate = false;
int counter = 0;
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("获取灯的状态: ", state);
//digitalWrite(4, !digitalRead(4));
if (state=="on") {
digitalWrite(4, HIGH);
// 反馈开关状态
BLINKER_LOG("正在为您开灯");
BLINKER_LOG("已打开");
Button1.print("on");
} else if(state=="off"){
digitalWrite(4, LOW);
// 反馈开关状态
BLINKER_LOG("正在为您关灯");
BLINKER_LOG("已关闭");
Button1.print("off");
Button1.color("#000000");
}
}
void button2_callback(const String & state) {
BLINKER_LOG("获取灯的状态: ", state);
if (state=="tap")
{
if (digitalRead(4)==HIGH)
{
Button2.print("灯是打开的");
BLINKER_LOG("已打开");
}
else
{
Button2.print("已经关闭了哦");
BLINKER_LOG("已关闭");
}
}
}
//小爱电源类的操作接口
void miotPowerState(const String & state)
{
BLINKER_LOG("小爱正在读取灯的状态: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(4, HIGH);
BlinkerMIOT.powerState("on");
BLINKER_LOG("已打开");
BlinkerMIOT.print();
ostate = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(4, LOW);
BlinkerMIOT.powerState("off");
BLINKER_LOG("已关闭");
BlinkerMIOT.print();
ostate = false;
}
}
void miotQuery(int32_t queryCode)
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("灯的状态");
BlinkerMIOT.powerState(ostate ? "on" : "off");
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(ostate ? "on" : "off");
BlinkerMIOT.print();
BLINKER_LOG("进入default");
break;
}
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup() {
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
// 初始化有LED的IO
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery); //语音指令与控制函数对应(查询指令)
}
void loop() {
Blinker.run();
}
|
|