|
楼主 |
发表于 2021-11-23 09:39
|
显示全部楼层
#define BLINKER_WIFI
#define BLINKER_WIFI_MULTI
#define BLINKER_WITHOUT_SSL //非SSL加密通信接入,省堆栈
//#define BLINKER_MIOT_SENSOR
#define BLINKER_MIOT_MULTI_OUTLET //支持多路插座,最多4个插孔
#include <DHT.h>
#include <Blinker.h>
#include <Ticker.h>
#include <ArduinoOTA.h>
#include <EEPROM.h>
char auth[] = "************";
//代码配网用下面这两段
char ssid[] = "*************";
char pswd[] = "*************";
char ssid1[] = "***************";
char pswd1[] = "**************";
Ticker flipper;
BlinkerButton Button("MainSwitch"); //组件对象,要和APP组件中的“数据键名”一致,总开关
BlinkerButton Button1("btn-on"); //设置app按键的键名
BlinkerButton Button2("btn-gy"); //zidong
BlinkerText Text1("text1");
BlinkerSlider Slider3("lowhumi");
BlinkerSlider Slider4("highhumi");
//定义温湿度传感器信号针脚
#define DHTPIN 2
int kg=0;//继电器输出
// int kg2=D3;//本地轻触开关,D3与gnd接时动作
//选择传感器类型
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
float humi_read, temp_read;
//定义blinker软件内数据组件键名分别为:humi、temp
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
float zd=0;////反馈自动按钮状态
//#FFFFFF是白色,#FF0000是黑色,#FF0000是红色,#00FF00是绿色,#0000FF是蓝色
//*******app上按下按键即会执行该函数app里按键有2种模式3钟不同样式,下面写出所有的开关状态。
// 在APP控制,按下MainSwitch按键即会执行该函数
void button_callback(const String & state)
{
BLINKER_LOG("操作了MainSwitch: ", state);//APP中的Monitor控件打印的信息
if (state == BLINKER_CMD_ON)
{
Button.color("#0000FF"); //设置app按键是红色
Button.text("总开关开了");
Button.print("on"); //反馈回按键状态是开
digitalWrite(kg,LOW);
Button1.color("#0000FF"); //设置app按键是红色
Button1.text("加湿器开了");
Button1.print("on"); //反馈回按键状态是开
zd=1;
Button2.color("#0000FF"); //设置app按键是红色
Button2.text("开启自动模式");
Button2.print("on"); //反馈回按键状态是开
} else if(state == BLINKER_CMD_OFF)
{
Button.color("#0000FF"); //设置app按键是红色
Button.text("总开关关了");
Button.print("off"); //反馈回按键状态是开
digitalWrite(kg, HIGH);
Button1.color("#FF0000"); //设置app按键是浅蓝色
Button1.text("加湿器关了");
Button1.print("off"); //反馈回按键状态是关
zd=0;
Button2.color("#FF0000"); //设置app按键是浅蓝色
Button2.text("自动关闭");
Button2.print("off"); //反馈回按键状态是关
}
}
void button1_callback(const String & state) //用state来存储组键的值按键 : "tap"(点按); "pre"(长按); "pup"(释放)开关 : "on"(打开); "off"(关闭)
{
BLINKER_LOG("app操作了!: ", state); //串口打印
if (state == BLINKER_CMD_OFF) { //如果state是on状态
BLINKER_LOG("关!"); //串口打印
digitalWrite(kg, HIGH);
Button1.color("#FF0000"); //设置app按键是浅蓝色
Button1.text("加湿器关了");
Button1.print("off"); //反馈回按键状态是关
}
else if (state == BLINKER_CMD_ON) { //如果state是off状态
BLINKER_LOG("开!"); //串口打印
digitalWrite(kg,LOW);
Button1.color("#0000FF"); //设置app按键是红色
Button1.text("加湿器开了");
Button1.print("on"); //反馈回按键状态是开
}
}
void Button2_callback(const String & state3) //用state来存储组键的值按键 : "tap"(点按); "pre"(长按); "pup"(释放)开关 : "on"(打开); "off"(关闭)
{
BLINKER_LOG("app操作了!: ", state3); //串口打印
if (state3 == BLINKER_CMD_OFF) { //如果state是on状态
BLINKER_LOG("自动关闭"); //串口打印
zd=0;
Button2.color("#FF0000"); //设置app按键是浅蓝色
Button2.text("自动关闭");
Button2.print("off"); //反馈回按键状态是关
}
else if (state3 == BLINKER_CMD_ON) { //如果state是off状态
BLINKER_LOG("开启自动模式"); //串口打印
zd=1;
Button2.color("#0000FF"); //设置app按键是红色
Button2.text("开启自动模式");
Button2.print("on"); //反馈回按键状态是开
}
EEPROM.begin(512);
EEPROM.write(3, zd);
EEPROM.commit();
}
float zuidi=43;
float zuigao=48;
void slider3_callback(int32_t value3)
{
// digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("get slider value: ", value3);
zuidi=value3;
EEPROM.begin(512);
EEPROM.write(4, zuidi);
EEPROM.commit();
}
void slider4_callback(int32_t value4)
{
//digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
BLINKER_LOG("get slider value: ", value4);
zuigao=value4;
EEPROM.begin(512);
EEPROM.write(2, zuigao);
EEPROM.commit();
}
//小爱同学控制插座多个插孔
void ctrl_multi_outlet(uint8_t num, uint8_t io_level)
{
switch (num)
{
case 0: //所有插孔
digitalWrite(kg, io_level);//控制继电器1
if (digitalRead(kg)==LOW)
{
Button1.print("on");
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("加湿器开着");
}
if(digitalRead(kg)==HIGH)
{
Button1.print("off");
Button1.color("#FF0000"); //设置app按键是红色
Button1.text("加湿器关着");
}
if (io_level==LOW)
{
zd=1;
Button.print("on");
Button.color("#0000FF"); //设置app按键是蓝色
Button.text("总开关打开");
Button2.print("on");
Button2.color("#0000FF"); //设置app按键是蓝色
Button2.text("自动模式打开");
}
if (io_level==HIGH)
{
zd=0;
Button.print("off");
Button.color("#FF0000"); //设置app按键是红色
Button.text("总开关关闭");
Button2.print("off");
Button2.color("#FF0000"); //设置app按键是红色
Button2.text("自动模式关闭");
}
break;
case 1: //插座插孔一
digitalWrite(kg,io_level);
if (digitalRead(kg)==LOW)
{
Button1.print("on");
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("加湿器开着");
}
if(digitalRead(kg)==HIGH)
{
Button1.print("off");
Button1.color("#FF0000"); //设置app按键是红色
Button1.text("加湿器关着");
}
break;
case 2: //插座插孔一
if (io_level==LOW)
{
zd=1;
Button2.print("on");
Button2.color("#0000FF"); //设置app按键是蓝色
Button2.text("自动模式打开");
}
if (io_level==HIGH)
{
zd=0;
Button2.print("off");
Button2.color("#FF0000"); //设置app按键是红色
Button2.text("自动模式关闭");
}
break;
default:
break;
}
}
//*******如果小爱有对设备进行操作就执行下面
bool oState[2] = { false };
void miotPowerState(const String & state, uint8_t num)
{
BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
if (state == BLINKER_CMD_ON) {
ctrl_multi_outlet(num, LOW);//打开继电器,num表示是多少路(继电器低电平出发)
BlinkerMIOT.powerState("on", num);
BlinkerMIOT.print();
oState[num] = true;
}
else if (state == BLINKER_CMD_OFF) {
ctrl_multi_outlet(num, HIGH);//关闭继电器,num表示是多少路
BlinkerMIOT.powerState("off", num);
BlinkerMIOT.print();
oState[num] = false;
if (num == 0)
{
for (uint8_t o_num = 0; o_num < 2; o_num++)
{
oState[o_num] = false;
}
}
}
}
void miotQuery(int32_t queryCode, uint8_t num)
{
BLINKER_LOG("MIOT Query outlet: ", num,", codes: ", queryCode);
switch (queryCode)
{
case BLINKER_CMD_QUERY_ALL_NUMBER :
BLINKER_LOG("MIOT Query All");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
BLINKER_LOG("MIOT Query Power State");
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.powerState(oState[num] ? "on" : "off", num);
BlinkerMIOT.print();
break;
}
}
void zdms(){
if(zd==0){
BLINKER_LOG("自动模式关闭");
Button2.text("自动模式关闭");
Button2.print("off");
}
if(zd==1){
BLINKER_LOG("自动模式打开");
Button2.text("自动模式打开");
Button2.print("on");
zidong();
}
}
void zidong(){
if(humi_read<zuidi){
digitalWrite(kg,LOW);
BLINKER_LOG("低于最低湿度,加湿器开!");
BlinkerMIOT.print();
Button1.color("#0000FF"); //设置app按键是红色
Button1.text("加湿器开了");
Button1.print("on"); //反馈回按键状态是开
}
if(humi_read>zuigao){
digitalWrite(kg,HIGH);
BLINKER_LOG("高于最高湿度,加湿器关!");
BlinkerMIOT.print();
Button1.color("#FF0000"); //设置app按键是浅蓝色
Button1.text("加湿器关了");
Button1.print("off");
}
}
//void sdms(){
//
//
// if (digitalRead(kg2)==LOW) {
// Blinker.delay(150);
// if (digitalRead(kg2)==HIGH){
// BLINKER_LOG("本地开关动作!");
// BLINKER_LOG("关!"); //串口打印
// digitalWrite(kg, HIGH);
// Button1.color("#FF0000"); //设置app按键是
// Button1.text("加湿器关了");
// Button1.print("off");
// }
//}
//if (digitalRead(kg2)==HIGH) { //
// Blinker.delay(150); //延时150ms不能太少标准按键成功率
// if (digitalRead(kg2)==LOW){
// BLINKER_LOG("本地开关动作!");
// BLINKER_LOG("开"); //串口打印
// digitalWrite(kg,LOW);
// Button1.color("#0000FF"); //设置app按键是
// Button1.text("加湿器开了");
// Button1.print("on");
// }
// }
//
//}
void rtData()
{
Blinker.sendRtData("temp", temp_read);
Blinker.sendRtData("humi", humi_read);
Blinker.printRtData();
}
void heartbeat()
{
BLINKER_LOG("状态同步!");
if ((digitalRead(kg)==LOW)&&zd==1){
Button.color("#0000FF"); //设置app按键是红色
Button.text("总开关开了");
Button.print("on");
}
if ((digitalRead(kg)==HIGH)&&zd==0){
Button.color("#0000FF"); //设置app按键是红色
Button.text("总开关关了");
Button.print("off");
}
if (digitalRead(kg)==LOW)
{
Button1.print("on");
Button1.color("#0000FF"); //设置app按键是蓝色
Button1.text("加湿器开着");
}
if(digitalRead(kg)==HIGH)
{
Button1.print("off");
Button1.color("#FF0000"); //设置app按键是红色
Button1.text("加湿器关着");
}
if (zd==1)
{
Button2.print("on");
Button2.color("#0000FF"); //设置app按键是蓝色
Button2.text("自动模式打开");
}
if (zd==0)
{
Button2.print("off");
Button2.color("#FF0000"); //设置app按键是红色
Button2.text("自动模式关闭");
}
HUMI.print(humi_read);
TEMP.print(temp_read);
Slider3.print(zuidi);
delay(50);
Slider4.print(zuigao);
}
void dataStorage()
{
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_read);
}
void setup()
{
//调试程序
EEPROM.begin(512);
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(kg, OUTPUT);
digitalWrite(kg, LOW); //默认上电是开 HIGH 时的是接通继电器。
// pinMode(kg2,INPUT_PULLUP); //输入上拉
// pinMode(LED_BUILTIN, OUTPUT);
// digitalWrite(LED_BUILTIN, LOW);
zd = int(EEPROM.read(3));
delay(50);
zuidi = int(EEPROM.read(4));
delay(50);
zuigao = int(EEPROM.read(2));
// Blinker.begin(auth); //手机配网用这段
Blinker.addAP(ssid1, pswd1);
Blinker.begin(auth, ssid, pswd); //代码配网用这段
Blinker.attachHeartbeat(heartbeat);
Blinker.attachDataStorage(dataStorage);
Blinker.attachRTData(rtData);
//注册小爱同学状态回调函数
BlinkerMIOT.attachQuery(miotQuery);
Button1.attach(button1_callback); //app上操作必须的注册回调函数关联按键名“Button1”和判断程序“button1_callback"
BlinkerMIOT.attachPowerState(miotPowerState);
Button2.attach(Button2_callback);
Button.attach(button_callback); //绑定按键回调
Slider3.attach(slider3_callback);
Slider4.attach(slider4_callback);
//初始化温湿度程序
dht.begin();
flipper.attach(5, zdms);
ArduinoOTA.setHostname("jiashiqi2");
//ArduinoOTA.setPassword("12345678");
ArduinoOTA.begin();
}
void loop()
{
//sdms();//本地开关手动模式函数
Blinker.run();
ArduinoOTA.handle();
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;
}
} |
|