1、APP的下载地址,以及使用方法可以参考此处,笔者为大家找了一个讲的很详细的网页。
2、关于为什么笔者一直在强调“控制多路接口”,因为官方或者其他网站并未给出此类教程,但这也是一项亟待解决的问题。笔者通过实验,验证了下述算法的可行性,值得大家借鉴。
一、先列出官方提供的例程,便于大家对照比较。
#define BLINKER_WIFI
#include
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
BlinkerButton Button1("btn-abc");
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Blinker.vibrate();
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
}
void loop()
{
Blinker.run();
}
二、提前配置好APP内的开关,以及配套的语音功能。
改进后的程序如下:
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
#include
char auth[] = "******";
char ssid[] = "******";
char pswd[] = "******";
#define BUTTON_1 "btn-po9"
#define BUTTON_2 "btn-hkc"
#define BUTTON_3 "btn-abc"
BlinkerButton Button1(BUTTON_1); //定义3个按钮
BlinkerButton Button2(BUTTON_2);
BlinkerButton Button3(BUTTON_3);
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
digitalWrite(LED_BUILTIN, LOW);
Button1.print("on");
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(LED_BUILTIN, HIGH);
Button1.print("off");
}
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
digitalWrite(LED_BUILTIN, LOW);
Button2.print("on");
}
else if (state == BLINKER_CMD_OFF)
{
digitalWrite(LED_BUILTIN, HIGH);
Button2.print("off");
}
}
void button3_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_BUTTON_TAP)
{
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
Button3.print();
}
else
{
digitalWrite(LED_BUILTIN, HIGH);
Button3.print();
}
}
void setup()
{
Serial.begin(115200);
#if defined(BLINKER_PRINT)
BLINKER_DEBUG.stream(BLINKER_PRINT);
#endif
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback); //回调函数
Button2.attach(button2_callback);
Button3.attach(button3_callback);
}
void loop()
{
Blinker.run();
}
以上程序能通过APP控制3路开关接口,理论上能控制无数路开关接口。
由于手头缺乏实验器材,笔者测试的时候都是依靠NodeMCU上自带的LED(位于D2口)。所有例程均能保证运行良好。
详情请关注亿航创客官方抖音账号:EhangGroup