|
所需材料:ESP8266 ch340 6路继电器 实现用点灯科技(blinker)APP远程开关 | #define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "abc"; // 点灯科技秘钥
char ssid[] = "wifi"; // WiFi名称
char pswd[] = "mima"; // WiFi密码
#define bt1 5
#define bt2 4
#define bt3 0
#define bt4 2
#define bt5 14
#define bt6 12
// 新建组件对象
BlinkerButton Button1("btn1-abc");
BlinkerButton Button2("btn2-abc");
BlinkerButton Button3("btn3-abc");
BlinkerButton Button4("btn4-abc");
BlinkerButton Button5("btn5-abc");
BlinkerButton Button6("btn6-abc");
// 按下按键即会执行该函数
void button1_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt1, LOW);
// 反馈开关状态
Button1.print("on");
} else if (state == "off") {
digitalWrite(bt1, HIGH);
// 反馈开关状态
Button1.print("off");
}
}
// 按下按键即会执行该函数
void button2_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt2, LOW);
// 反馈开关状态
Button2.print("on");
} else if (state == "off") {
digitalWrite(bt2, HIGH);
// 反馈开关状态
Button2.print("off");
}
}
// 按下按键即会执行该函数
void button3_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt3, LOW);
// 反馈开关状态
Button3.print("on");
} else if (state == "off") {
digitalWrite(bt3, HIGH);
// 反馈开关状态
Button3.print("off");
}
}
// 按下按键即会执行该函数
void button4_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt4, LOW);
// 反馈开关状态
Button4.print("on");
} else if (state == "off") {
digitalWrite(bt4, HIGH);
// 反馈开关状态
Button4.print("off");
}
}
// 按下按键即会执行该函数
void button5_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt5, LOW);
// 反馈开关状态
Button5.print("on");
} else if (state == "off") {
digitalWrite(bt5, HIGH);
// 反馈开关状态
Button5.print("off");
}
}
// 按下按键即会执行该函数
void button6_callback(const String & state) {
BLINKER_LOG("get button state: ", state);
if (state == "on") {
digitalWrite(bt6, LOW);
// 反馈开关状态
Button6.print("on");
} else if (state == "off") {
digitalWrite(bt6, HIGH);
// 反馈开关状态
Button6.print("off");
}
}
void setup() {
// 初始化串口
Serial.begin(115200);
// 初始化有LED的IO
pinMode(bt1, OUTPUT);
digitalWrite(bt1, HIGH);
pinMode(bt2, OUTPUT);
digitalWrite(bt2, HIGH);
pinMode(bt3, OUTPUT);
digitalWrite(bt3, HIGH);
pinMode(bt4, OUTPUT);
digitalWrite(bt4, HIGH);
pinMode(bt5, OUTPUT);
digitalWrite(bt5, HIGH);
pinMode(bt6, OUTPUT);
digitalWrite(bt6, HIGH);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
Button5.attach(button5_callback);
Button6.attach(button6_callback);
}
void loop() {
Blinker.run();
}
|
|