本帖最后由 shao1 于 2020-4-19 11:09 编辑
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#define BLINKER_ALIGENIE_MULTI_OUTLET
#include <Blinker.h>
BlinkerButton Button1("btn-lt1"); //设置blinkerapp内数据键名
BlinkerButton Button2("btn-lt2"); //
BlinkerButton Button3("btn-lt3");
BlinkerButton Button4("btn-lt4");
BlinkerText Text1("text_1");
char i;
char auth[] = "******1f074"; //填上刚刚记下来的密钥
char ssid[] = "88****888"; //填上你家WiFi名字
char pswd[] = "186*****721"; //填上你家WiFi密码
bool oState[4] = { false };
void aligeniePowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if(num == 1){
i=0; //指定每一路开关对应在开发板上的通道接口
}else if(num == 2){
i=2;
}else if(num == 3){
i=4;
}else if(num == 4){
i=5;
}
if (state == BLINKER_CMD_ON) {
digitalWrite(i, LOW);
BlinkerAliGenie.powerState("on",num);
BlinkerAliGenie.print();
oState[num] = true;
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(i, HIGH);
BlinkerAliGenie.powerState("off",num);
BlinkerAliGenie.print();
oState[num] = false;
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 5; o_num++)
{
oState[o_num] = false;
}
}
}
}
void aligenieQuery(int32_t queryCode, uint8_t num)
{
BLINKER_LOG("AliGenie Query outlet: ", num,", codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("AliGenie Query All");
BlinkerAliGenie.powerState(oState[num] ? "on" : "off",num);
BlinkerAliGenie.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("AliGenie Query Power State");
BlinkerAliGenie.powerState(oState[num] ? "on" : "off",num);
BlinkerAliGenie.print();
break;
default :
BlinkerAliGenie.powerState(oState[num] ? "on" : "off",num);
BlinkerAliGenie.print();
break;
}
}
void dataRead(const String & data) // 如果未绑定的组件被触发,则会执行其中内容
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
Blinker.print("millis", BlinkerTime);
}
void button1_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(0, !digitalRead(0));
}
void button2_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(2, !digitalRead(2));
}
void button3_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(4, !digitalRead(4));
}
void button4_callback(const String & state) //点灯app内控制按键触发
{
BLINKER_LOG("get button state: ", state);
digitalWrite(5, !digitalRead(5));
}
void setup()
{
Serial.begin(115200);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
BlinkerAliGenie.attachPowerState(aligeniePowerState);
BlinkerAliGenie.attachQuery(aligenieQuery);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
}
void loop()
{
Blinker.run();
}
4路天猫精灵控制 根据你的代码修改的 拿去 亲测在APP4路都可以控制 天猫精灵 也成功了 |