|
- //未配网成功,板载灯闪
- #define BLINKER_PRINT Serial
- #define BLINKER_WIFI
- #define led D4 //D4
- #define Relay_5 D5 //D5
- #define Relay_6 D6 //D6
- #define Relay_7 D7 //D7
- #define Relay_8 D8 //D8
- #include <DHT.h>
- #include <Blinker.h>
- #include <ESP8266WiFi.h>
- #define DHTPIN D1 //D0
- #define DHTTYPE DHT11 // DHT 11
- char auth[] = "*******";
- char ssid[] = "*******";
- char pswd[] = "*******";
-
- // 新建组件对象
- BlinkerButton Button5("btn-d5");
- BlinkerButton Button6("btn-d6");
- BlinkerButton Button7("btn-d7");
- BlinkerButton Button8("btn-d8");
- BlinkerButton Button9("btn-re");
- BlinkerButton Button("btn-w");
- BlinkerNumber HUMI("num-shidu");//caiyq52-此组件未点灯app中自己设置的湿度数据键名,必须与自己APP中设定的名称一致
- BlinkerNumber TEMP("num-wendu");//caiyq52-此组件未点灯app中自己设置的温度数据键名,必须与自己APP中设定的名称一致
- DHT dht(DHTPIN, DHTTYPE);
- uint32_t read_time = 0;
- float humi_read=0, temp_read=0;
- //心跳包
- void heartbeat()
- {
- HUMI.print(humi_read);
- TEMP.print(temp_read);
- }
-
- void button5_callback(const String & state) {
-
- BLINKER_LOG("Relay_5 get button state: ", state);
- if (state=="on") {
- digitalWrite(Relay_5, HIGH); // 反馈开关状态
- Button5.print("on");
- } else if(state=="off"){
- digitalWrite(Relay_5, LOW); // 反馈开关状态
- Button5.print("off");
- }
- Blinker.vibrate();
- }
-
- void button6_callback(const String & state) {
-
- BLINKER_LOG("Relay_6 get button state: ", state);
- if (state=="on") {
- digitalWrite(Relay_6, HIGH); // 反馈开关状态
- Button6.print("on");
- } else if(state=="off"){
- digitalWrite(Relay_6, LOW); // 反馈开关状态
- Button6.print("off");
- }
- Blinker.vibrate();
- }
-
- void button7_callback(const String & state) {
-
- BLINKER_LOG("Relay_7 get button state: ", state);
- if (state=="on") {
- digitalWrite(Relay_7, HIGH); // 反馈开关状态
- Button7.print("on");
- } else if(state=="off"){
- digitalWrite(Relay_7, LOW); // 反馈开关状态
- Button7.print("off");
- }
- Blinker.vibrate();
- }
-
- void button8_callback(const String & state) {
-
- BLINKER_LOG("Relay_8 get button state: ", state);
- if (state=="on") {
- digitalWrite(Relay_8, HIGH); // 反馈开关状态
- Button8.print("on");
- } else if(state=="off"){
- digitalWrite(Relay_8, LOW); // 反馈开关状态
- Button8.print("off");
- }
- Blinker.vibrate();
- }
- //刷新显示所有开关状态
- void button9_callback(const String & state) {
-
- BLINKER_LOG("get button state: ", state);
-
- //5678继电器刷新
- if(digitalRead (Relay_5)==HIGH){
- Button5.print("on"); }
- else if(digitalRead (Relay_5)==LOW){
- Button5.print("off");
- }
-
- if(digitalRead (Relay_6)==HIGH){
- Button6.print("on");
- }
- else if(digitalRead (Relay_6)==LOW){
- Button6.print("off");
- }
- if(digitalRead (Relay_7)==HIGH){
- Button7.print("on");
- }
- else if(digitalRead (Relay_7)==LOW){
- Button7.print("off");
- }
- if(digitalRead (Relay_8)==HIGH){
- Button8.print("on");
- }
- else if(digitalRead (Relay_8)==LOW){
- Button8.print("off");
- }
- //温湿度刷新
- HUMI.print(humi_read);
- TEMP.print(temp_read);
- }
- void setup() {
-
- // 初始化串口,并开启调试信息
-
- Serial.begin(115200);
-
- BLINKER_DEBUG.stream(Serial);
- BLINKER_DEBUG.debugAll();
-
- // 初始化有LED的IO
-
- pinMode(led, OUTPUT);
- digitalWrite(led, HIGH);
-
- pinMode(Relay_5, OUTPUT);
- digitalWrite(Relay_5,LOW);
-
- pinMode(Relay_6, OUTPUT);
- digitalWrite(Relay_6, LOW);
- pinMode(Relay_7, OUTPUT);
- digitalWrite(Relay_7, LOW);
-
- pinMode(Relay_8, OUTPUT);
- digitalWrite(Relay_8, LOW);
-
- // 初始化blinker
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachHeartbeat(heartbeat);
- dht.begin();
-
- // Button4.attach(button4_callback);
- Button5.attach(button5_callback);
- Button6.attach(button6_callback);
- Button7.attach(button7_callback);
- Button8.attach(button8_callback);
- Button9.attach(button9_callback);
- //Blinker.attachDataStorage(dataStorage);
- gotoWifi();
- }
-
- void loop() {
- if(WiFi.status()!=WL_CONNECTED){
- WiFi.disconnect();
- Blinker.begin(auth, ssid, pswd);
- }
-
- Blinker.run();
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t))
- {
- BLINKER_LOG("Failed to read from DHT sensor!");
- }
- else
- {
- BLINKER_LOG("Humidity: ", h, " %");
- BLINKER_LOG("Temperature: ", t, " *C");
- humi_read = h;
- temp_read = t;
- if (h>60)
- { Button.print("on");}
- if (h<60)
- {Button.print("off");}
- }
- Blinker.delay(2000);
- }
- void gotoWifi(){
- while(WiFi.status()!=WL_CONNECTED){
- bool is=digitalRead(led);
- digitalWrite(led,!is);
- delay(500);
- }
- digitalWrite(led,0);
- }
-
- 参考的一位大佬程序,连接wifi时,LED闪烁,连接后,常亮
-
复制代码
|
|