|
程序:
#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#include <Blinker.h>
char auth[] = "93350c1a00bc";
char ssid[] = "snjay";
char pswd[] = "74266260";
// 新建组件对象
BlinkerButton Button1("btn-1");
BlinkerButton Button2("btn-2");
BlinkerButton Button3("btn-3");
BlinkerNumber Number1("num-1");
int counter = 0;
// 按下按键即会执行该函数
void Button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
void Button2_callback(const String & state2)
{
BLINKER_LOG("get button state2: ", state2);
digitalWrite(4, !digitalRead(4));
}
void Button3_callback(const String & state3)
{
BLINKER_LOG("get button state3: ", state3);
digitalWrite(5, !digitalRead(5));
}
//小爱同学控制函数
void Button1_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();
}
else if (state == BLINKER_CMD_OFF) {
digitalWrite(LED_BUILTIN, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void Button2_miotPowerState(const String & state2)
{
BLINKER_LOG("need set power state: ", state2);
if (state2 == BLINKER_CMD_ON) {
digitalWrite(4, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state2 == BLINKER_CMD_OFF) {
digitalWrite(4, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
void Button3_miotPowerState(const String & state3)
{
BLINKER_LOG("need set power state: ", state3);
if (state3 == BLINKER_CMD_ON) {
digitalWrite(5, LOW);
BlinkerMIOT.powerState("on");
BlinkerMIOT.print();
}
else if (state3 == BLINKER_CMD_OFF) {
digitalWrite(5, HIGH);
BlinkerMIOT.powerState("off");
BlinkerMIOT.print();
}
}
// 如果未绑定的组件被触发,则会执行其中内容
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
Number1.print(counter);
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化有LED的IO
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);
Button1.attach(Button1_callback);
Button2.attach(Button2_callback);
Button3.attach(Button3_callback);
BlinkerMIOT.attachPowerState(Button1_miotPowerState);
BlinkerMIOT.attachPowerState(Button2_miotPowerState);
BlinkerMIOT.attachPowerState(Button3_miotPowerState);//注册小爱同学回调函数
}
void loop() {
Blinker.run();
}
一共定义了3个输出口,分别是板载LED_BUILTIN ,GPIO4 , GPIO5,用binker添加了3个按键,分别都可以控制,接入小爱之后,只能控制GPIO5口的灯。
希望大神解惑,或者给出教程链接。
感谢
|
|