#define BLINKER_ESP_SMARTCONFIG//最好放在第一行,增加配网成功率
#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET //小爱的插座模式。可以更换其他模式
#include <Blinker.h>
char auth[] = "************"; //写入自己的ID
#define LED_BUILTIN 0 //我用的8266-01s的0脚为控制脚,Low为打开,反之
bool oState = false; //默认值,不做解释
BlinkerButton Button1("btn-abc"); //添加的按键名,可以随意添加多个
void miotPowerState(const String & state) //小米的控制指令,复制进即可
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED_BUILTIN, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN,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 button1_callback(const String & state) //按键1按下之后的动作,在这里是取反操作
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Blinker.vibrate();
}
void setup() //参数设置
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial); //调试串口可以不用
pinMode(LED_BUILTIN, OUTPUT); //输出模式
digitalWrite(LED_BUILTIN, HIGH); //默认为高电平,继电器为开路状态
Blinker.begin(auth); //必须有这句,配置网络必备
Blinker.attachData(dataRead); //电灯运行必备函数
Button1.attach(button1_callback);//按键的函数
BlinkerMIOT.attachPowerState(miotPowerState);//小爱的运行函数
BlinkerMIOT.attachQuery(miotQuery);//小爱的运行函数
}
void loop()
{
Blinker.run();
}
/*
我已经测试成功了,有问题的回帖告诉我,老鸟请飞过,菜鸟共同进步
*/
|