[mw_shl_code=arduino,true]#define BLINKER_WIFI
#define BLINKER_MIOT_OUTLET
#include <Blinker.h>
char auth[] = "";
#define LED 0 //0对应GPIO0 2对应GPIO2
bool oState = false;
// 新建组件对象
BlinkerButton Button1("btn-abc");
//按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED, LOW);
oState = true;
} else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED, HIGH);
oState = false;
}
Button1.print(state);
BlinkerMIOT.powerState(oState ? "on" : "off");
BlinkerMIOT.print();
}
//用于小爱同学控制
void miotPowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state);
if (state == BLINKER_CMD_ON) {
digitalWrite(LED, LOW);
BlinkerMIOT.powerState("on"); //反馈电源状态
Button1.print("on");
BlinkerMIOT.print(); //将以上属性发送出去
oState = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED, HIGH);
BlinkerMIOT.powerState("off"); //反馈电源状态
Button1.print("off");
BlinkerMIOT.print(); //将以上数据发送出去
oState = false;
}
}
//Miot问询
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 heartbeat()
{
if (oState) {
Button1.print("on");
}else {
Button1.print("off");
}
}
//配网
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("Wait for Smartconfig");
Blinker.delay(500); // 等待配网
WiFi.beginSmartConfig();
while (1){
Blinker.delay(200);
if (WiFi.smartConfigDone()){
Serial.println("SmartConfig Success");
WiFi.setAutoConnect(true); // 设置自动连接
break;
}
}
}
void setup()
{
//初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
smartConfig();
while (WiFi.status() != WL_CONNECTED)
{
Serial.print("..");
Blinker.delay(300);
}
Serial.println("WiFi connected");
Blinker.begin(auth, WiFi.SSID().c_str(),WiFi.psk().c_str());
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
BlinkerMIOT.attachPowerState(miotPowerState);
BlinkerMIOT.attachQuery(miotQuery);
Blinker.attachHeartbeat(heartbeat); //注册心跳函数回调
}
void loop()
{
Blinker.run();
} [/mw_shl_code]
z01228 那位兄弟的也不错 |