赞
踩
在之前就了解了点灯科技,这次对比了MIXIO,点灯在物联APP个性化设置上更丰富完善些,但是只支持免费绑定一个设备。查看Blinker.h源码看到最常用的Blinker_DHT、Blinker_RGB、Blinker_button等等。button组件已经能很好理解,但关于switch组件在源码中没理解怎么使用,这里只用button代替。
这次不分那么多单一的组件,直接是将多个组件写到一起了,分别是显示温湿度DHT11、ws2812色盘调色、三Led灯模拟交通灯间隔点亮同时也能手动单独点亮和蜂鸣器警报开关。原理都差不多,将就看。
对MIXIO 感兴趣欢迎查看其他博文:掌控板+Mixly+MixIO 初试物联网1-摇杆篇
可点击链接https://www.diandeng.tech/dev到官网下载新版本的应用
Adafruit_NeoPixel.zip
blinker.zip
DHT.zip
// #define BLINKER_PRINT Serial #define BLINKER_WIFI //放在Blinker.h的前面,否则报错 #define BLINKER_MIOT_LIGHT #include <Blinker.h> #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> #endif #define PIN 15 // DIN PIN (GPIO15, D8) #define NUMPIXELS 12 // Number of you led Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define RGB_1 "col-83q" BlinkerRGB WS2812(RGB_1); //新建RGB灯组件对象 #define BLINKER_MIOT_SENSOR //Xiao Ai defined it as sensor equipment #include <DHT.h> char auth[] = "81404c68e552"; //Lamp APP to obtain the device char ssid[] = "xxx"; //WiFi name char pswd[] = "xxxxxx"; //WiFi password #define BUTTON_G "btn-k8a" //绿灯组件 #define BUTTON_Y "btn-i0k" //黄灯组件 #define BUTTON_R "btn-8jv" //红灯组件 #define BUILTIN_SW "btn-rml" //自动交通灯开关组件 #define Music_SW "btn-7co" //蜂鸣器开关组件 const int ledPin_G = 5; //GPIO5==>D1 const int ledPin_Y = 4; //GPIO4==>D2 const int ledPin_R = 0; //GPIO0==>D3 const int buzzerPin = 13; //GPIO13==>D7 bool ledG_state = false; bool ledY_state = false; bool ledR_state = false; bool TrafficState = false; bool MusicState = false; BlinkerNumber HUMI("num-6fm"); //Define the humidity data key name BlinkerNumber TEMP("num-7tk"); //Define the temperature data key name #define DHTPIN 2 //Define pin GPIO2(D4) for DHT11 module connection #define DHTTYPE DHT11 // Use the DHT 11 temperature and humidity module //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 //#define DHTTYPE DHT21 // DHT 21 (AM2301) BlinkerButton ButtonG(BUTTON_G); // 绿灯按钮实例化 BlinkerButton ButtonY(BUTTON_Y); // 黄灯按钮实例化 BlinkerButton ButtonR(BUTTON_R); // 红灯按钮实例化 BlinkerButton ButtonSW(BUILTIN_SW); // 蜂鸣器开关按钮实例化 BlinkerButton MusicSW(Music_SW); // 蜂鸣器开关按钮实例化 DHT dht(DHTPIN, DHTTYPE); // dht温湿度传感器实例化 float humi_read = 0, temp_read = 0; // 温湿度变量 unsigned long previousMillis; // 红绿灯开关前一刻时间截 int lightNumber = 1; // 第几盏灯 const long interval = 3000; // 红绿灯切换间隔时间3秒 //绿灯开关回调函数 void buttonG_callback(const String & state) { MusicState = false;//要先关掉自动交通灯开关 if(!TrafficState){ ledG_state = !ledG_state; if (ledG_state) { ButtonG.icon("fas fa-lightbulb"); ButtonG.color("#00FF00"); ButtonG.print(); }else{ ButtonG.icon("far fa-lightbulb"); ButtonG.color("#008800"); ButtonG.print(); } } } //黄灯开关回调函数 void buttonY_callback(const String & state) { MusicState = false;//要先关掉自动交通灯开关 if(!TrafficState){ ledY_state = !ledY_state; if (ledY_state) { ButtonY.icon("fas fa-lightbulb"); ButtonY.color("#FFFF00"); ButtonY.print(); }else{ ButtonY.icon("far fa-lightbulb"); ButtonY.color("#888800"); ButtonY.print(); } } } //红灯开关回调函数 void buttonR_callback(const String & state) { MusicState = false;//要先关掉自动交通灯开关 if(!TrafficState){ ledR_state = !ledR_state; if (ledR_state) { ButtonR.icon("fas fa-lightbulb"); ButtonR.color("#FF0000"); ButtonR.print(); }else{ ButtonR.icon("far fa-lightbulb"); ButtonR.color("#660000"); ButtonR.print(); } } } //交通灯开关回调函数 void switch_callback(const String & state) { MusicState = false; TrafficState =!TrafficState; if(TrafficState){ ButtonSW.icon("fas fa-toggle-on"); ButtonSW.color("#fa0000"); ButtonSW.print("on"); lightNumber = 2; previousMillis = millis(); //每次打开或关闭开关都获取一次当前时间截 ledG_state =HIGH;ledY_state =LOW;ledR_state =LOW; //打开自动红绿灯开关时默认绿灯状态亮起 LEDcontrol(); }else{ ledG_state =LOW;ledY_state =LOW;ledR_state =LOW; //关闭自动红绿灯开关时默认绿灯状态亮起 ButtonSW.icon("fas fa-toggle-off"); ButtonSW.color("#880000"); ButtonSW.print("off"); } } //蜂鸣器开关回调函数 void MusicSW_callback(const String & state) { MusicState = !MusicState; if(MusicState){ MusicSW.icon("far fa-stop-circle");// MusicSW.color("#0000FF"); MusicSW.print("on"); }else{ MusicSW.icon("fas fa-play-circle"); MusicSW.color("#000088"); MusicSW.print("off"); } } //APP RGB颜色设置回调函数 void ws2812_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value) { // digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); BLINKER_LOG("R value: ", r_value); BLINKER_LOG("G value: ", g_value); BLINKER_LOG("B value: ", b_value); BLINKER_LOG("Rrightness value: ", bright_value); pixels.setBrightness(bright_value); for(int i = 0; i < NUMPIXELS; i++){ pixels.setPixelColor(i, r_value, g_value, b_value); } // Blinker.delay(200); pixels.show(); } void heartbeat() { HUMI.print(humi_read); //Return humidity data to blinkerapp TEMP.print(temp_read); //Return temperature data to blinkerapp } void miotQuery(int32_t queryCode) //Xiao Ai classmates voice command feedback { int humi_read_int = humi_read; //Remove humidity floating point BlinkerMIOT.humi(humi_read_int); //Little love receives humidity BlinkerMIOT.temp(temp_read); //Little love receives the temperature } void dataRead(const String & data) { BLINKER_LOG("Blinker readString: ", data); Blinker.vibrate(); uint32_t BlinkerTime = millis(); Blinker.print("millis", BlinkerTime); } void setup() { Serial.begin(115200); pinMode(ledPin_G, OUTPUT); pinMode(ledPin_Y, OUTPUT); pinMode(ledPin_R, OUTPUT); pinMode(buzzerPin, OUTPUT); digitalWrite(ledPin_G, LOW); digitalWrite(ledPin_Y, LOW); digitalWrite(ledPin_R, LOW); digitalWrite(buzzerPin, LOW); BLINKER_DEBUG.stream(Serial); // 初始化blinker Blinker.begin(auth, ssid, pswd); pixels.begin(); //WS2812初始化 WS2812.attach(ws2812_callback); //注册调节颜色的回调函数 Blinker.attachHeartbeat(heartbeat); //每隔一分钟刷新一次温湿度到APP dht.begin(); BlinkerMIOT.attachQuery(miotQuery); Blinker.attachData(dataRead); //三个LED的icon图标和文本初始化设置 ButtonG.color("#008800"); ButtonG.text("green light"); ButtonY.color("#888800"); ButtonY.text("yellow light"); ButtonR.color("#880000"); ButtonR.icon("far fa-lightbulb"); ButtonG.attach(buttonG_callback);//手动控制G灯回调函数 ButtonY.attach(buttonY_callback);//手动控制Y灯回调函数 ButtonR.attach(buttonR_callback);//手动控制R灯回调函数 ButtonSW.attach(switch_callback);//自动交通灯开关回调函数 MusicSW.attach(MusicSW_callback);//音乐开关回调函数 } void loop() { Blinker.run(); dhtReadData(); LEDcontrol(); traffic_run(); MusicControl(); } //三个LED控制函数 void LEDcontrol(){ if(ledG_state){ digitalWrite(ledPin_G,HIGH); }else{ digitalWrite(ledPin_G,LOW); } if(ledY_state){ digitalWrite(ledPin_Y,HIGH); }else{ digitalWrite(ledPin_Y,LOW); } if(ledR_state){ digitalWrite(ledPin_R,HIGH); }else{ digitalWrite(ledPin_R,LOW); } } //温湿度读取函数 void dhtReadData(){ 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; } Blinker.delay(2000); } //红绿灯开关函数 void traffic_run(){ if (TrafficState) { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= interval){ //非阻塞延时 previousMillis = currentMillis; switch (lightNumber){ case 1:ledG_state =HIGH;ledY_state =LOW;ledR_state =LOW;break; case 2:ledG_state =LOW;ledY_state =HIGH;ledR_state =LOW;break; case 3:ledG_state =LOW;ledY_state =LOW;ledR_state =HIGH;break; } lightNumber+=1; if(lightNumber>3){ lightNumber = 1; } } LEDcontrol(); }else{ LEDcontrol(); } } //蜂鸣器控制函数 void MusicControl(){ // Serial.println(MusicState); if(MusicState){ digitalWrite(buzzerPin,HIGH); delay(500); digitalWrite(buzzerPin,LOW); delay(500); }else{ digitalWrite(buzzerPin,LOW); } }
模拟交通灯间隔数秒钟循环亮起不同的灯,避免干扰其他控制,加上之前在论坛看了这种非阻塞写法,就用上了。
免费下载附件:界面配置txt
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。