程序代码如下:
- #define BLINKER_WIFI
- //#define BLINKER_ESP_SMARTCONFIG
- #define BLINKER_APCONFIG
- #include <Blinker.h>
- //引脚定义
- #define LED 16 //板载LED按钮
- #define RELAY1 5 //继电器1按钮
- #define RELAY2 4 //继电器2按钮
- #define RECONFIG 0 //重置网络实体按钮
- //点灯平台信息
- char auth[] = "马赛克";
- //char ssid[] = "han";
- //char pswd[] = "12345678";
- // 新建组件对象
- BlinkerButton Button_Relay1("btn-relay1"); //继电器1按钮
- BlinkerButton Button_Relay2("btn-relay2"); //继电器2按钮
- BlinkerButton Button_Led("btn-led"); //板载LED按钮
- //重置网络信息按钮事件
- void Button_RstConfig_callback()
- {
- if (digitalRead(RECONFIG) == LOW) {
- delay(3000);
- if (digitalRead(RECONFIG) == LOW) {
- Blinker.reset();
- Serial.print("Reset Config...");
- }
- }
- }
- // 继电器1按钮事件
- void Button_Relay1_callback(const String & state)
- {
- BLINKER_LOG("Relay State: ", state);
- digitalWrite(RELAY1, !digitalRead(RELAY1));
- if (digitalRead(RELAY1) == HIGH) {
- Button_Relay1.icon("fad fa-siren-on");
- Button_Relay1.print("on");
- //digitalWrite(RELAY1, LOW);
- } else if (digitalRead(RELAY1) == LOW) {
- Button_Relay1.icon("fad fa-siren");
- Button_Relay1.print("off");
- //digitalWrite(RELAY1, HIGH);
- }
- }
- // 继电器2按钮事件
- void Button_Relay2_callback(const String & state)
- {
- BLINKER_LOG("Relay State: ", state);
- digitalWrite(RELAY2, !digitalRead(RELAY2));
- if (digitalRead(RELAY2) == HIGH) {
- Button_Relay2.icon("fad fa-siren-on");
- Button_Relay2.print("on");
- //digitalWrite(RELAY2, LOW);
- } else if (digitalRead(RELAY2) == LOW) {
- Button_Relay2.icon("fad fa-siren");
- Button_Relay2.print("off");
- //digitalWrite(RELAY2, HIGH);
- }
- }
- // LED按钮事件
- void Button_Led_callback(const String & state)
- {
- BLINKER_LOG("LED State: ", state);
- digitalWrite(LED, !digitalRead(LED));
- if (digitalRead(LED) == LOW) {
- //digitalWrite(LED, LOW);
- Button_Led.icon("fad fa-lightbulb-on");
- Button_Led.print("on");
- } else if (digitalRead(LED) == HIGH) {
- //digitalWrite(LED, HIGH);
- Button_Led.icon("fad fa-lightbulb");
- Button_Led.print("off");
- }
- }
- // 如果未绑定的组件被触发,则会执行其中内容
- void dataRead(const String & data)
- {
- BLINKER_LOG("Blinker readString: ", data);
- }
- void setup()
- {
- // 初始化串口
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
- // 初始化IO
- pinMode(RELAY1, OUTPUT);
- digitalWrite(RELAY1, LOW);
- pinMode(RELAY2, OUTPUT);
- digitalWrite(RELAY2, LOW);
- pinMode(LED, OUTPUT);
- digitalWrite(LED, HIGH);
- pinMode(RECONFIG, INPUT);
- digitalWrite(RECONFIG, HIGH);
- // 初始化blinker
- //Blinker.begin(auth, ssid, pswd);
- Blinker.begin(auth);
- Blinker.attachData(dataRead);
- Button_Relay1.attach(Button_Relay1_callback);
- Button_Relay2.attach(Button_Relay2_callback);
- Button_Led.attach(Button_Led_callback);
- }
- void loop() {
- Blinker.run();
- Button_RstConfig_callback();
- }
复制代码 |