ESP8266+DHT11温湿度模块+RGB+TTP223电容按键
本帖最后由 jiangshh 于 2019-6-2 16:10 编辑先放一下我做的小系统的图片,原件都是插到面包板上的,面包板的底下放了一块1000毫安的锂电池,加入充电升压模块,自己3D的打印了一个外壳。不过我发现这个esp8266的功耗不是一般的高,1000毫安的电池也就能撑一晚上,如果有需要3D打印外壳文件的可以给我留言。
引脚接线:TTP223电容按键接D1; DHT11接D4; RGB_Red接D5; RGB_Green接D6; RGB_Blue接D7。
下图是Blinker APP端组件布局及键名。
本系统可以实现的功能:
1.APP端温湿度的显示;
2.APP端控制板载LED的开关;
3.APP端控制RGB灯的开关和颜色;
4.APP端控制氛围灯的开关,氛围灯由RGB灯颜色值随机变化加循环执行实现的;
5.APP端显示RGB的三色的色值,不显示氛围灯的三色色值;
6.通过按键可以开启和关闭RGB灯,也可以关闭氛围灯。
注:刷新按键用于刷新APP组件端的状态的
下面附上程序源码:
(TTP223电容按键是连接ESP8266的外部中断进行控制的,之前一直没找到部中断程序,今天刚在例程中看到的)
#define BLINKER_BUTTON
#define BLINKER_BUTTON_PIN D1//外部中断引脚
#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG//EspTouch配网
#include <Blinker.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN D4
#define RGB_Red D5
#define RGB_Green D6
#define RGB_Blue D7
char auth[] = "ff0e913b8883";
DHT dht(DHTPIN, DHTTYPE);
float num_hum_read = 0;
float num_tem_read = 0;
uint32_t read_time = 0;
int keyval = 0;//按键开关值暂存,临时保存按键的扫描值
int old_val = 0;//按键开关值备份,保存前一次的扫描值
int Switch = {};//用于储存各个开关的状态,1表示开启,0表示关闭
// 新建组件对象
BlinkerButton Button1("btn-led");
BlinkerButton Button2("btn-f5");
BlinkerButton Button3("btn-rgb");
BlinkerButton Button4("btn-atmo");
BlinkerNumber Temperature("num-tem");
BlinkerNumber Humidity("num-hum");
BlinkerNumber red("num-red");
BlinkerNumber green("num-green");
BlinkerNumber blue("num-blue");
BlinkerRGB RGB("col-rgb");
void colorRGB(int r_val , int g_val , int b_val)//控制RGB的颜色
{
analogWrite(RGB_Red, r_val);
analogWrite(RGB_Green, g_val);
analogWrite(RGB_Blue, b_val);
}
void button1_callback(const String & state)// 按下按键即会执行该函数(选用开关模式),用于控制板载LED
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
digitalWrite(LED_BUILTIN, LOW);
Switch = 1;
Button1.color("#FF0000"); Button1.text("LED"); Button1.print("on");
}
else if(state == BLINKER_CMD_OFF)
{
digitalWrite(LED_BUILTIN, HIGH);
Switch = 0;
Button1.color("#808080"); Button1.text("LED"); Button1.print("off");
}
}
void button2_callback(const String & state)// 按下按键即会执行该函数(选用普通模式),用于刷新app各组件的状态
{
/***********************板载LED**************************/
if(Switch == 1)
{ Button1.color("#FF0000"); Button1.text("LED"); Button1.print("on"); }
else if(Switch == 0)
{ Button1.color("#808080"); Button1.text("LED"); Button1.print("off");}
/***********************RGB开关状态************************/
if(Switch == 1)
{ Button3.color("#FF0000"); Button3.text("RGB"); Button3.print("on"); }
else if(Switch == 0)
{ Button3.color("#808080"); Button3.text("RGB"); Button3.print("off");}
/***********************氛围灯状态**************************/
if(Switch == 1)
{ Button4.color("#FF0000"); Button4.text("氛围灯"); Button4.print("on"); }
else if(Switch == 0)
{ Button4.color("#808080"); Button4.text("氛围灯"); Button4.print("off");}
/********************RGB的数值和调色盘***********************/
red.print(Switch);green.print(Switch);blue.print(Switch);
RGB.print(Switch, Switch, Switch, Switch);
}
void button3_callback(const String & state)// 按下按键即会执行该函数(选用开关模式),用于控制RGB灯的亮灭
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
Switch = 255; Switch = 255; Switch = 255; Switch = 1;
colorRGB( Switch , Switch , Switch );
Button3.color("#FF0000"); Button3.text("RGB");Button3.print("on");
red.print(Switch);green.print(Switch);blue.print(Switch);
RGB.print(Switch, Switch, Switch, Switch);
}
else if (state == BLINKER_CMD_OFF)
{
Switch = 0; Switch = 0; Switch = 0;Switch = 0;
colorRGB( Switch , Switch , Switch );
Button3.color("#808080"); Button3.text("RGB");Button3.print("off");
Button4.color("#808080"); Button4.text("氛围灯"); Button4.print("off");
red.print(Switch);green.print(Switch);blue.print(Switch);
RGB.print(Switch, Switch, Switch, Switch);
}
}
void button4_callback(const String & state)// 按下按键即会执行该函数(选用开关模式),用于控制氛围灯的亮灭
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
Switch = 1; Switch = 1;
Button4.color("#FF0000"); Button4.text("氛围灯"); Button4.print("on");
}
else if(state == BLINKER_CMD_OFF)
{
Switch = 0; Switch = 0; Switch = 0;Switch = 0; Switch = 0;
colorRGB( Switch , Switch , Switch );
Button4.color("#808080"); Button4.text("氛围灯"); Button4.print("off");
}
while( Switch == 1 && Switch == 1 )
{
colorRGB(random(0,255),random(0,255),random(0,255));
Blinker.delay(1000);
}
}
void rgb_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)// 调色板用于控制RGB灯的颜色,不能改变亮度
{
BLINKER_LOG("R value: ", r_value);
BLINKER_LOG("G value: ", g_value);
BLINKER_LOG("B value: ", b_value);
BLINKER_LOG("Rrightness value: ", bright_value);
Switch = (int)r_value; Switch = (int)g_value; Switch = (int)b_value; Switch = 127;
colorRGB( Switch , Switch , Switch );
Switch = 1;
Button3.color("#FF0000"); Button3.text("RGB");Button3.print("on");
RGB.print(Switch, Switch, Switch, Switch);
red.print(Switch); green.print(Switch); blue.print(Switch);
}
void buttonTick()// 外部中断函数,通过按键可以控制RGB灯的状态
{
int keyval = digitalRead(D1);
if(keyval!= old_val)
{
if( old_val == 1) //如果前次值为1,则说明当前是弹起动作
{
if( Switch == 1 )
{
Switch = 0; Switch = 0; Switch = 0;Switch = 0; Switch = 0;
colorRGB( Switch , Switch , Switch );
Button3.color("#808080"); Button3.text("RGB");Button3.print("off");
Button4.color("#808080"); Button4.text("氛围灯"); Button4.print("off");
red.print(Switch);green.print(Switch);blue.print(Switch);
RGB.print(Switch, Switch, Switch, Switch);
}
else if( Switch == 0 )
{
Switch = 255; Switch = 255; Switch = 255;Switch = 1;
colorRGB( Switch , Switch , Switch );
Button3.color("#FF0000"); Button3.text("RGB"); Button3.print("on");
red.print(Switch);green.print(Switch);blue.print(Switch);
RGB.print(Switch, Switch, Switch, Switch);
}
}
old_val=keyval;//更新备份为当前值,以备进行下次比较
}
}
void heartbeat()
{
/***********************温湿度传感器****************************/
if (read_time == 0 || (millis() - read_time) >= 2000)
{
read_time = millis();
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
if (isnan(h) || isnan(t) || isnan(f))
{
BLINKER_LOG(F("Failed to read from DHT sensor!"));
return;
}
num_hum_read = h;
num_tem_read = t;
}
Temperature.print(num_tem_read);
Humidity.print(num_hum_read);
}
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print(BlinkerTime);
Blinker.print("millis", BlinkerTime);
}
void setup()
{
// 初始化串口
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
// 初始化IO
pinMode(RGB_Red, OUTPUT);
pinMode(RGB_Green, OUTPUT);
pinMode(RGB_Blue, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
colorRGB(0,0,0);
// 初始化blinker
Blinker.begin(auth);
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Button1.attach(button1_callback);
Button2.attach(button2_callback);
Button3.attach(button3_callback);
Button4.attach(button4_callback);
RGB.attach(rgb_callback);
#if defined(BLINKER_BUTTON)
attachInterrupt(BLINKER_BUTTON_PIN, buttonTick, CHANGE);
#endif
dht.begin();
}
void loop()
{
Blinker.run();
} 桥边等 发表于 2019-6-11 12:14
中断的例程是哪一个,我也一直找不到
BLINKER_PRO_ESP在这个里面 我想问下这个app是什么APP啊有链接吗 protect、 发表于 2019-6-3 13:32
我想问下这个app是什么APP啊有链接吗
blinker,https://blinker.app/ 感谢分享
使用ESP8266-12F板耗电要比开发板低,
前几天刚看过一个。
如何在ESP8266中使用深度睡眠模式降低功耗
https://www.yiboard.com/thread-1162-1-1.html 前段时间把氛围灯的程序优化了一下,有了更好的呼吸效果。
代码如下:
void button4_callback(const String & state)// 按下按键即会执行该函数(选用开关模式),用于控制氛围灯的亮灭
{
BLINKER_LOG("get button state: ", state);
if (state == BLINKER_CMD_ON)
{
Switch = 1; Switch = 1;
Button4.color("#FF0000"); Button4.text("氛围灯"); Button4.print("on");
}
else if(state == BLINKER_CMD_OFF)
{
Switch = 0; Switch = 0; Switch = 0;Switch = 0; Switch = 0;
colorRGB( Switch , Switch , Switch );
Button4.color("#808080"); Button4.text("氛围灯"); Button4.print("off");
}
while( Switch == 1 && Switch == 1 )
{
Switch = Switch + fadeAmount;
Switch = Switch + fadeAmount;
Switch = Switch + fadeAmount;
colorRGB( Switch , Switch , Switch );
int maxval = max(Switch,max(Switch,Switch));
int minval = min(Switch,min(Switch,Switch));
if(maxval >= 255) fadeAmount = -fadeAmount;
Blinker.delay(30);
if(minval <= 0)
{
colorRGB( 0 , 0 , 0 );
Blinker.delay(100);
Switch = random(50,150);
Switch = random(50,150);
Switch = random(50,150);
fadeAmount = -fadeAmount;
}
}
} 中断的例程是哪一个,我也一直找不到
请问你在IDE里面选开发板,你选哪个了,我为什么加入了中断后,就不在线了! 桥边等 发表于 2019-6-14 22:59
请问你在IDE里面选开发板,你选哪个了,我为什么加入了中断后,就不在线了! ...
NodeMCU 1.0 TTP223电容按键是连接ESP8266的外部中断进行控制