自制呼吸时钟(基于Blinker ESP-12F WS2812 DHT11)
本帖最后由 poiuy105 于 2020-7-18 18:51 编辑第一次发分享贴,语言表达欠妥之处请见谅:lol
1.先发一个实物展示视频&图片:
https://v.youku.com/v_show/id_XNDc1NjkzMzMyMA==.html
2.使用硬件清单:
ESP12F开发板,
DHT11模块,
WS2812灯条2米60灯珠,
USB电源,
连接线若干。
3.感谢灵感资料:
Blinker示例 Hello_WiFi
Blinker示例 ws2812_WiFi
Blinker示例 DHT_WiFi
Blinker示例 Time_WiFi
Blinker示例 ws2812_WiFihttps://www.arduino.cn/forum.php?mod=viewthread&tid=98403&highlight=%E6%97%B6%E9%92%9F
https://www.arduino.cn/thread-75674-1-1.html
4.代码参考:
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR
#include <Blinker.h>
#include <DHT.h>
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
// Download Adafruit_NeoPixel library here:
// https://github.com/adafruit/Adafruit_NeoPixel
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN4 4
#define DENG 16
#define NUMPIXELS 78//13
#define NUMPIXELS_unit 13
int r_value = 255;
int g_value = 255;
int b_value = 255;
int bright_value = 255;
Adafruit_NeoPixel pixels1 = Adafruit_NeoPixel(NUMPIXELS, PIN4, NEO_GRB + NEO_KHZ800);
BlinkerRGB RGB1("RGB_1");
void rgb1_callback(uint8_t r_gvalue, uint8_t g_gvalue, uint8_t b_gvalue, uint8_t bright_gvalue)
{
r_value = r_gvalue;
g_value = g_gvalue;
b_value = b_gvalue;
bright_value = bright_gvalue;
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
}
boolean kuai0 = {true, true, true, true, true, true, false, true, true, true, true, true, true};
boolean kuai1 = {true, true, true, true, true, false, false, false, false, false, false, false, false};
boolean kuai2 = {true, false, true, true, true, true, true, true, true, true, true, false, true};
boolean kuai3 = {true, true, true, true, true, true, true, true, true, false, true, false, true};
boolean kuai4 = {true, true, true, true, true, false, true, false, false, false, true, true, true};
boolean kuai5 = {true, true, true, false, true, true, true, true, true, false, true, true, true};
boolean kuai6 = {true, true, true, false, true, true, true, true, true, true, true, true, true};
boolean kuai7 = {true, true, true, true, true, true, false, false, false, false, false, false, true};
boolean kuai8 = {true, true, true, true, true, true, true, true, true, true, true, true, true};
boolean kuai9 = {true, true, true, true, true, true, true, true, true, false, true, true, true};
boolean man0 = {true, true, true, true, true, true, false, true, true, true, true, true, true};
boolean man1 = {true, true, true, true, true, false, false, false, false, false, false, false, false};
boolean man2 = {true, true, true, false, true, true, true, true, true, false, true, true, true};
boolean man3 = {true, true, true, true, true, true, true, true, true, false, true, false, true};
boolean man4 = {true, true, true, true, true, false, true, false, true, true, true, false, false};
boolean man5 = {true, false, true, true, true, true, true, true, true, true, true, false, true};
boolean man6 = {true, false, true, true, true, true, true, true, true, true, true, true, true};
boolean man7 = {true, true, true, true, true, false, false, true, true, false, false, false, false};
boolean man8 = {true, true, true, true, true, true, true, true, true, true, true, true, true};
boolean man9 = {true, true, true, true, true, true, true, true, true, true, true, false, true};
// 新建组件对象
BlinkerButton Button1("btn-1");
BlinkerButton Button2("btn-2");
BlinkerNumber Number1("num-1"); //计数
BlinkerNumber Number2("num-beat"); //beat计数
BlinkerNumber HUMI("humi"); //定义湿度数据键名
BlinkerNumber TEMP("temp"); //定义温度数据键名
BlinkerText Tex_Temp("text_t");//定义文本Key
String Num_in_two(int num)
{
if (num < 10) {return "0" + String(num);} else {return String(num);}
}
String Time_in_six(){
String C_hour = Num_in_two(Blinker.hour()) + ":";
String C_minute = Num_in_two(Blinker.minute()) + ":";
String C_second = Num_in_two(Blinker.second());
return C_hour + C_minute +C_second;
}
#define JDQ 5 //定义模块连接管脚
#define DENG2 14 //定义模块连接管脚
// 按下按键即会执行该函数
void button1_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(JDQ, !digitalRead(JDQ));
}
void button2_callback(const String & state)
{
BLINKER_LOG("get button state: ", state);
digitalWrite(DENG2, !digitalRead(DENG2));
}
#define DHTPIN 13 //定义DHT11模块连接管脚io2
#define DHTTYPE DHT11 // 使用DHT 11温度湿度模块
DHT dht(DHTPIN, DHTTYPE); //定义dht
// 定义变量
int counter = 0, cnt_beat = 0;
float humi_read = 0, temp_read = 0;
float data_temp = 0;
uint32_t read_time = 0;
void miotQuery(int32_t queryCode) //小爱同学语音命令反馈
{
BLINKER_LOG("MIOT Query codes: ", queryCode);
int humi_read_int=humi_read; //去掉湿度浮点
BlinkerMIOT.humi(humi_read_int); //小爱接收湿度
BlinkerMIOT.temp(temp_read); //小爱接收温度
BlinkerMIOT.print();
}
void dataRead(const String & data)//被动提取数据
{
BLINKER_LOG("Blinker readString: ", data);
counter++;
if (counter > 10000)
{
counter = 0;
}
Number1.print(counter);
}
void heartbeat()
{
cnt_beat++;
Number2.print(cnt_beat);
}
void slider1_callback(int32_t slider1_value)
{
int add = 0;
BLINKER_LOG("get slider1 value: ", slider1_value);
//pixels1.setBrightness(bright_value);
switch (slider1_value)
{
case 0:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai0 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 1:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai1 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 2:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai2 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 3:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai3 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 4:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai4 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 5:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai5 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 6:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai6 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 7:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai7 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 8:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai8 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 9:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai9 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
}
//pixels1.show();
}
void slider2_callback(int32_t slider2_value)
{
int add = 13;
BLINKER_LOG("get slider2 value: ", slider2_value);
//pixels1.setBrightness(bright_value);
switch (slider2_value)
{
case 0:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man0 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 1:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man1 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 2:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man2 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 3:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man3 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 4:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man4 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 5:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man5 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
}
//pixels1.show();
}
void slider3_callback(int32_t slider3_value)
{
int add = 26;
BLINKER_LOG("get slider3 value: ", slider3_value);
//pixels1.setBrightness(bright_value);
switch (slider3_value)
{
case 0:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai0 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 1:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai1 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 2:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai2 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 3:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai3 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 4:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai4 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 5:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai5 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 6:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai6 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 7:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai7 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 8:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai8 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 9:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai9 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
}
//pixels1.show();
}
void slider4_callback(int32_t slider4_value)
{
int add = 39;
BLINKER_LOG("get slider4 value: ", slider4_value);
//pixels1.setBrightness(bright_value);
switch (slider4_value)
{
case 0:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man0 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 1:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man1 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 2:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man2 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 3:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man3 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 4:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man4 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 5:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man5 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
}
//pixels1.show();
}
void slider5_callback(int32_t slider5_value)
{
int add = 52;
BLINKER_LOG("get slider5 value: ", slider5_value);
//pixels1.setBrightness(bright_value);
switch (slider5_value)
{
case 0:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai0 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 1:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai1 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 2:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai2 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 3:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai3 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 4:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai4 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 5:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai5 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 6:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai6 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 7:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai7 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 8:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai8 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 9:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (kuai9 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
}
//pixels1.show();
}
void slider6_callback(int32_t slider6_value)
{
int add = 65;
BLINKER_LOG("get slider4 value: ", slider6_value);
//pixels1.setBrightness(bright_value);
switch (slider6_value)
{
case 0:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man0 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 1:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man1 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
case 2:
for(int i = 0; i < NUMPIXELS_unit; i++){
if (man2 == true){pixels1.setPixelColor(i + add, r_value, g_value, b_value);}
else {pixels1.setPixelColor(i + add, 0, 0, 0);}
}
break;
}
//pixels1.show();
}
int bianhua_bright = -5;
int bianhua_r = -5;
int bianhua_g = -5;
int bianhua_b = -5;
void set_bright_value(uint8_t time_hour)
{
if (time_hour < 23 && time_hour > 6) {
switch (random(0,4)){
case 0:
bright_value = bright_value + bianhua_bright;//亮度呼吸
if (bright_value == 10 || bright_value == 255) {bianhua_bright = -bianhua_bright;}
break;
case 1:
r_value = r_value + bianhua_r;//红色呼吸
if (r_value == 0 || r_value == 255) {bianhua_r = -bianhua_r;}
break;
case 2:
g_value = g_value + bianhua_g;//绿色呼吸
if (g_value == 0 || g_value == 255) {bianhua_g = -bianhua_g;}
break;
case 3:
b_value = b_value + bianhua_b;//蓝色呼吸
if (b_value == 0 || b_value == 255) {bianhua_b = -bianhua_b;}
break;
}
if (Blinker.second() % 2 == 1) {
digitalWrite(DENG, LOW);
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(DENG, HIGH);
digitalWrite(LED_BUILTIN, LOW);
}
}
else {
bright_value = 10;
digitalWrite(DENG, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
Blinker.attachData(dataRead);
Blinker.setTimezone(8.0);
dht.begin();
BlinkerMIOT.attachQuery(miotQuery);
// 初始化有LED的IO
pinMode(JDQ, OUTPUT);
digitalWrite(JDQ, LOW);
pinMode(DENG2, OUTPUT);
digitalWrite(DENG2, LOW);
// 初始化按键回调函数
Button1.attach(button1_callback);
Button2.attach(button2_callback);
pinMode(DENG, OUTPUT);
digitalWrite(DENG, LOW);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pixels1.begin();
RGB1.attach(rgb1_callback);
}
void loop()
{
Blinker.run();
if (read_time == 0 || (millis() - read_time) >= 30000)
{
read_time = millis();
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;
HUMI.print(humi_read); //给blinkerapp回传湿度数据
TEMP.print(temp_read); //给blinkerapp回传温度数据
//Tex_Temp.print(String(Blinker.hour()) + "-" + String(Blinker.minute()) + "-" + String(Blinker.second()));
Tex_Temp.print(Time_in_six());
Blinker.dataStorage("data_temp", temp_read); //上传到云端
}
}
if (Blinker.second() == -1) {
pixels1.clear();
}
else {
slider5_callback(Blinker.second() % 10);
slider6_callback((Blinker.second() - (Blinker.second() % 10)) / 10);
slider1_callback(Blinker.minute() % 10);
slider2_callback((Blinker.minute() - (Blinker.minute() % 10)) / 10);
slider3_callback(Blinker.hour() % 10);
slider4_callback((Blinker.hour() - (Blinker.hour() % 10)) / 10);
if (Blinker.second() == 0 && Blinker.minute() == 30 && Blinker.hour() == 6) {ESP.restart();}
set_bright_value(Blinker.hour());
pixels1.setBrightness(bright_value);
pixels1.show();
}
}
:D学习一下先
页:
[1]