|
楼主 |
发表于 2020-6-1 17:33
|
显示全部楼层
代码部分:
#define BLINKER_WIFI//调用库
#include <DHT.h>
#include <Blinker.h>
#define jidianqi D3 //定义继电器针脚号为D3号引脚(设备连在D8)
#define yanwu A0 //定义烟雾传感器针脚号为A0号引脚(设备连在A0)
#define wenshidu D1 //定义温湿度针脚号为D1号引脚(设备连在D3)
char auth[] = "*****"; //blinker设备连接码
char ssid[] = "****"; //wifi名称
char pswd[] = "wifi密码";
//blinker上控件绑定
BlinkerButton Button1("btn-jidianqi");
BlinkerNumber Number1("num-tem");
BlinkerNumber Number2("num-hum");
BlinkerNumber Number3("num-smk");
BlinkerButton Button2("btn-yujing");
DHT dht(wenshidu, DHT11);//获取温湿度原始数据
bool switch_onoff = 0; //开关标志位
//开灯函数
void ON() {
digitalWrite(jidianqi, HIGH);
Button1.color("#00EE00");
Button1.text("已开启");
Button1.print();
Button2.text("一切正常");
Button2.color("#00EE00");
Button2.print();
}
//关灯函数
void OFF() {
digitalWrite(jidianqi, LOW);
Button1.color("#000000");
Button1.text("已关闭");
Button1.print();
}
//异常预警函数
//int yj = 0; //初始化预警标志
void YJOFF() {
//digitalWrite(jidianqi, LOW);
Button1.color("#000000");
Button1.text("异常,已关闭");
Button1.print();
}
void TEMOFF() {
YJOFF();
Button2.text("环境温度异常!");
Button2.color("#FF0000");
Button2.print();
}
void SMKOFF(){
YJOFF();
Button2.text("环境烟雾异常!");
Button2.color("#FF0000");
Button2.print();
}
// 按下开关按键即会执行开关灯函数
void button1_callback(const String & state)
{
switch_onoff = !switch_onoff;
if (switch_onoff == 1) {
ON();
}
else {
OFF();
}
}
float hum_read, tem_read, smk_read;//定义数据类型
//获取实时数据
void dataRead(const String & data)
{
BLINKER_LOG("Blinker readString: ", data);
Number1.print(tem_read);
Number2.print(hum_read);
Number3.print(smk_read);
}
void heartbeat()//心跳包
{
Number1.print(tem_read);
Number2.print(hum_read);
Number3.print(smk_read);
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);//串口输出
pinMode(jidianqi, OUTPUT);//配置引脚
digitalWrite(jidianqi, LOW);//初始化继电器引脚电平
pinMode(yanwu, INPUT);
// 初始化blinker
Blinker.begin(auth, ssid, pswd);
Blinker.attachData(dataRead);//注册实时数据按钮回调数据
Blinker.attachHeartbeat(heartbeat);//注册回调心跳包函数
Button1.attach(button1_callback);//注册开关按钮回调状态
dht.begin();//启动温湿度算法库
}
void loop()
{
Blinker.run();
float h = dht.readHumidity();
float t = dht.readTemperature();
float s = analogRead(A0);
hum_read = h;
tem_read = t;
smk_read = s;
if (t > 40) //温度预警
{
digitalWrite(jidianqi, LOW);
BLINKER_LOG("temperature is too high , electriical have shut down !");//电器关闭
TEMOFF();
delay(1000);
}
if (s > 200) //烟雾预警
{
digitalWrite(jidianqi, LOW);
BLINKER_LOG("smoke volue is too high ,electriical have shut down !");//电器关闭
SMKOFF();
delay(1000);
}
}
|
|