|
[pre]#define BLINKER_WIFI //官方wifi协议库
#define BLINKER_APCONFIG
#define BLINKER_ALIGENIE_LIGHT //天猫灯类库
#define BLINKER_PRINT Serial //串口协议库
#include <Blinker.h> //官方库
char auth[] = ""; //设备key号
bool oState = false;
int kg=0;//继电器输出
int kg2=2;//本地轻触开关,io2与gnd接时动作
//*******新建组件对象
BlinkerButton Button1("a1"); //设置app按键的键名
BlinkerButton Button2("a2");
BlinkerButton Button3("a3");
//*******app上按下按键即会执行该函数app里按键有2种模式3钟不同样式,下面写出所有的开关状态。
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)
{
BLINKER_LOG("app操作了!: ", state); //串口打印
if (digitalRead(kg)==LOW) { //如果state是on状态
BLINKER_LOG("亮灯!"); //串口打印
}
else if (digitalRead(kg)==HIGH) { //如果state是off状态
BLINKER_LOG("灭灯!"); //串口打印
}
}
void button2_callback(const String & state)
{
digitalWrite(kg,LOW); //如果state是on状态
BLINKER_LOG("get button state:on", state);
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("亮");
Button1.print("On"); //反馈回按键状态是开
}
void button3_callback(const String & state) //点灯app内控制按键触发
{
digitalWrite(kg,HIGH); //如果state是off状态
BLINKER_LOG("get button state:off", state);
Button1.color("#000000"); //设置app按键是黑色
Button1.text("灭");
Button1.print("off"); //反馈回按键状态是关
}
//*******如果天猫有对设备进行操作就执行下面
void AliGeniePowerState(const String & state)
{
BLINKER_LOG("need set power state: ", state); //串口打印
if (state == BLINKER_CMD_ON){
digitalWrite(kg,LOW);
BlinkerAliGenie.powerState("on");
BLINKER_LOG("亮灯!");
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("亮");
Button1.print("on");
BlinkerAliGenie.print();
oState = true;
}
else if (state == BLINKER_CMD_OFF){
digitalWrite(kg,HIGH);
BlinkerAliGenie.powerState("off");
BLINKER_LOG("灭灯!");
Button1.color("#000000"); //设置app按键是黑色
Button1.text("灭");
Button1.print("off");
BlinkerAliGenie.print();
oState = false;
}
}
void AliGenieQuery(int32_t queryCode) //caiyq52-天猫精灵控制
{
BLINKER_LOG("AliGenie Query codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
default :
BlinkerAliGenie.powerState(oState ? "on" : "off");
BlinkerAliGenie.print();
break;
}
}
//*******app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态30s~60s一次
void heartbeat()
{
BLINKER_LOG("状态同步!");
if (digitalRead(kg)==LOW)
{
Button1.print("on");
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("亮");
}
else
{
Button1.print("off");
Button1.color("#000000"); //设置app按键是黑色
Button1.text("灭");
}
}
///如果本地开关有动作执行下面手动模式
void sdms(){
if (digitalRead(kg)==HIGH&&digitalRead(kg2)==LOW) { //
Blinker.delay(150); //延时150ms不能太少标准按键成功率
if (digitalRead(kg2)==HIGH){
BLINKER_LOG("亮灯!"); //串口打印
digitalWrite(kg,LOW);
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("亮");
Button1.print("on");
}
}
if (digitalRead(kg)==LOW&&digitalRead(kg2)==LOW) {
Blinker.delay(150);
if (digitalRead(kg2)==HIGH){
BLINKER_LOG("灭灯!"); //串口打印
digitalWrite(kg, HIGH);
Button1.color("#000000"); //设置app按键是黑色
Button1.text("灭");
Button1.print("off");
}
}
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
pinMode(kg, OUTPUT);
digitalWrite(kg, LOW); //默认上电是开 LOW 时的是接通继电器。
pinMode(kg2,INPUT_PULLUP); //输入上拉
// 初始化blinker
Blinker.begin(auth);
Blinker.attachData(dataRead);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);//app上操作必须的注册回调函数关联按键名“Button1”和判断程序“button1_callback"
BlinkerAliGenie.attachPowerState(AliGeniePowerState); //天猫语音操作注册函数
Blinker.attachHeartbeat(heartbeat); //app定时向设备发送心跳包, 设备收到心跳包后会返回设备当前状态进行语音操作和app操作同步。
}
void loop() {
sdms();//本地开关手动模式函数
Blinker.run();
}[/pre] |
|