esp8266接入小爱童鞋
程序:#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口的灯。
希望大神解惑,或者给出教程链接。
感谢
灯不是开就是关,attachPowerState只能一个https://diandeng.tech/doc/xiaoai
奈何col 发表于 2020-3-8 18:42
灯不是开就是关,attachPowerState只能一个https://diandeng.tech/doc/xiaoai
那如果我想控制两个以上的灯呢? snjay001 发表于 2020-3-8 19:50
那如果我想控制两个以上的灯呢?
换成多孔插座例程 奈何col 发表于 2020-3-8 20:04
换成多孔插座例程
请问有例程吗,我找了全网也没找到#define BLINKER_MIOT_MULTI_OUTLET
这个库的例程。 奈何col 发表于 2020-3-8 20:04
换成多孔插座例程
啊,不好意思,我找到了,我只想着去网上找,没想到arduino ide里就有例程。
页:
[1]