#define BLINKER_WIFI
#define BLINKER_ALIGENIE_LIGHT
#include <Blinker.h>
char auth[] = "415e748528f4"; //换成APP获取到的密匙
char ssid[] = "FAST_2738"; //WiFi账号
char pswd[] = "15370457581"; //WIFI密码
// 新建组件对象
BlinkerButton Button1("btn-abc"); //APP组件名称
BlinkerNumber Number1("num-abc"); //blinker新建组件
bool switch_onoff = 0; //开关标志位
//开灯函数
void ON() {
digitalWrite(5, HIGH);
Button1.color("#B22222");
Button1.print();
}
//关灯函数
void OFF() {
digitalWrite(5, LOW);
Button1.color("#000000");
Button1.print();
}
void heartbeat() //心跳包
{
Number1.print(WiFi.RSSI()); //信号强度
if (switch_onoff == 1) {
ON();
}
else {
OFF();
}
}
// 按下APP按键即会执行该函数
void button1_callback(const String & state)
{
switch_onoff = !switch_onoff;
if (switch_onoff == 1) {
ON();
}
else {
OFF();
}
BLINKER_LOG("get button state: ", state);
}
void aligeniePowerState(const String &state) { //阿里精力开关函数
if (state == BLINKER_CMD_ON) {
switch_onoff = 1;
ON();
BlinkerAliGenie.powerState("on");
BlinkerAliGenie.print();
} else if (state == BLINKER_CMD_OFF) {
switch_onoff = 0;
OFF();
BlinkerAliGenie.powerState("off");
BlinkerAliGenie.print();
}
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化IO
pinMode(5, OUTPUT);
digitalWrite(5, LOW);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Blinker.attachHeartbeat(heartbeat);
BlinkerAliGenie.attachPowerState(aligeniePowerState); //天猫精灵开关
}
void loop() {
Blinker.run();
}
|