用无线门铃制作远程多功能报警自定义开关-Arduino中文社区 - Powered by Discuz! Archiver

liyuanping 发表于 2020-6-27 10:50

用无线门铃制作远程多功能报警自定义开关

可自定义开关状态,远程温度湿度显示感应报警动作定义,天猫小度小米语音控制 使用nodeMCU esp8266模块






源码
/* *****************************************************************
*
* Download latest Blinker library here:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
*
* Blinker is a cross-hardware, cross-platform solution for the IoT.
* It provides APP, device and server support,
* and uses public cloud services for data transmission and storage.
* It can be used in smart home, data monitoring and other fields
* to help users build Internet of Things projects better and faster.
*
* Make sure installed 2.5.0 or later ESP8266/Arduino package,
* if use ESP8266 with Blinker.
* https://github.com/esp8266/Arduino/releases
*
* Make sure installed 1.0.2 or later ESP32/Arduino package,
* if use ESP32 with Blinker.
* https://github.com/espressif/arduino-esp32/releases
*
* Docs: https://doc.blinker.app/
*       https://github.com/blinker-iot/blinker-doc/wiki
*
* *****************************************************************
*
* Blinker 库下载地址:
* https://github.com/blinker-iot/blinker-library/archive/master.zip
*
* Blinker 是一套跨硬件、跨平台的物联网解决方案,提供APP端、设备端、
* 服务器端支持,使用公有云服务进行数据传输存储。可用于智能家居、
* 数据监测等领域,可以帮助用户更好更快地搭建物联网项目。
*
* 如果使用 ESP8266 接入 Blinker,
* 请确保安装了 2.5.0 或更新的 ESP8266/Arduino 支持包。
* https://github.com/esp8266/Arduino/releases
*
* 如果使用 ESP32 接入 Blinker,
* 请确保安装了 1.0.2 或更新的 ESP32/Arduino 支持包。
* https://github.com/espressif/arduino-esp32/releases
*
* 文档: https://doc.blinker.app/
*       https://github.com/blinker-iot/blinker-doc/wiki
*
* *****************************************************************/
#define BLINKER_PRINT Serial
#define BLINKER_WIFI
//#define BLINKER_APCONFIG
//#define BLINKER_ALIGENIE_SENSOR
//#define BLINKER_ALIGENIE_LIGHT
//#define BLINKER_MIOT_OUTLET   //小爱同学
//#define BLINKER_DUEROS_OUTLET //小度
#define BLINKER_ALIGENIE_MULTI_OUTLET //天猫精灵

#include <Blinker.h>

char auth[] = "你的密匙";
char ssid[] = "你的WIFI名字";
char pswd[] = "你的WIFI密码";

#define BUTTON_1 "btn-abc"
#define BUTTON_2 "btn-k8a"
#define BUTTON_3 "btn-l7d"
//#define BUTTON_4 "btn-123"
#define Slider_1 "ran-pfo"
#define Slider_2 "ran-z2c"
#define TEXTE_1 "tex-g0a"

#define LED_BUILTIN1 16 //D0(GPIO16)USER LAMP OUT用户灯
#define LED_BUILTIN2 2//D4(GPIO2)FLASH LAMP OUT flash灯
#define DHTPIN 14   //D5(GPIO14) 温湿度接的是GPIO14端口
const int AlarmOutPin = 5;   //D1(GPIO5)ALARM OUT报警输出

const int FlameIn = 12;      //D6(GPIO12)FLAME IN火焰输入端口
const int SoundSwitch = 4;   //D2(GPIO4)声音控制开关
const int LightSwitch = 0;   //D3(GPIO0)亮度控制开关
const int SmokeSwitch = 13;   //D7(GPIO13)烟雾开关
const int ManSwitch = 15;   //D8(GPIO15)人体感应开关

int EnAlarm=0;    //允许禁止报警标志
int SetTemp=40;//set temp报警温度设置40
int SetHumi=75; //设置湿度75%H

//int counter = 0;
char AlarmState=0;
BlinkerButton Button1(BUTTON_1);    //打开关1
BlinkerButton Button2(BUTTON_2);    //打开关2
BlinkerButton Button3(BUTTON_3);    //开关报警
BlinkerNumber Number1("num-abc");   //计数次数显示
BlinkerNumber Number2("num-eiq");   //报警显示
BlinkerText Text1(TEXTE_1);

BlinkerNumber HUMI("humi");   //湿度显示
BlinkerNumber TEMP("temp");   //温度显示

// Download Adafruit DHT-sensor-library library here:
// https://github.com/adafruit/DHT-sensor-library
#include <DHT.h>
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22   // DHT 22(AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);

uint32_t read_time = 0;
bool oState = { false };
float humi_read=0, temp_read=0;

#define Tab_1 "tab-s1t"
BlinkerTab Tab1(Tab_1);
bool tab = { false };
bool tab_save = { false };
void tab1_callback(uint8_t tab_set)
{
    BLINKER_LOG("get tab set: ", tab_set);

    switch (tab_set)
    {
      case BLINKER_CMD_TAB_0 :
            tab = true;
            BLINKER_LOG("tab 0 set");
            break;
      case BLINKER_CMD_TAB_1 :
            tab = true;
            BLINKER_LOG("tab 1 set");
            break;
      case BLINKER_CMD_TAB_2 :
            tab = true;
            BLINKER_LOG("tab 2 set");
            break;
      case BLINKER_CMD_TAB_3 :
            tab = true;
            BLINKER_LOG("tab 3 set");
            break;
      case BLINKER_CMD_TAB_4 :
            tab = true;
            BLINKER_LOG("tab 4 set");
            break;
      default:
            break;
    }
}

void tab1_feedback()
{
    for(uint8_t num = 0; num < 5; num++)
    {
      if (tab)
      {
            Tab1.tab(num);
            tab_save = true;   //保存各传感器是否允许功能作用
            tab = false;
      }
      else tab_save = false;
    }
    Tab1.print();
}

BlinkerSlider Slider1(Slider_1);

void slider1_callback(int32_t value)
{
    //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    SetTemp=value;
    BLINKER_LOG("get slider value: ", value);
}

BlinkerSlider Slider2(Slider_2);

void slider2_callback(int32_t value)
{
    //digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    SetHumi=value;
    BLINKER_LOG("get slider2 value: ", value);
}


void button1_callback(const String & state)
{
    //digitalWrite(LED_BUILTIN1, !digitalRead(LED_BUILTIN1));
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_BUTTON_TAP) {
      BLINKER_LOG("Button tap!");

      Button1.icon("icon_1");
      Button1.color("#FFFFFF");
      Button1.text("Your button name or describe");
      Button1.print();
    }
    else if (state == BLINKER_CMD_BUTTON_PRESSED) {
      BLINKER_LOG("Button pressed!");

      Button1.icon("icon_1");
      Button1.color("#FFFFFF");
      Button1.text("Your button name or describe");
      Button1.print();
    }
    else if (state == BLINKER_CMD_BUTTON_RELEASED) {
      BLINKER_LOG("Button released!");

      Button1.icon("icon_1");
      Button1.color("#FFFFFF");
      Button1.text("Your button name or describe");
      // Button1.text("Your button name", "describe");
      Button1.print();
    }
    else if (state == BLINKER_CMD_ON) {
      digitalWrite(LED_BUILTIN1, HIGH);
      BLINKER_LOG("Toggle on!");
      Button1.icon("fas fa-power-off");
      Button1.color("#00FF00");
      Button1.text("关闭1");
      // Button1.text("Your button name", "describe");
      //Button1.print("on");
         Button1.print(state);
      oState=true;
    }
    else if (state == BLINKER_CMD_OFF) {
      digitalWrite(LED_BUILTIN1, LOW);
      BLINKER_LOG("Toggle off!");
      Button1.icon("fas fa-power-off");
      Button1.color("#F0F0F0");
      Button1.text("打开1");
      // Button1.text("Your button name", "describe");
      //Button1.print("off");
      Button1.print(state);
      oState=false;
    }
    else {
      BLINKER_LOG("Get user setting: ", state);

      Button1.icon("icon_1");
      Button1.color("#FFFFFF");
      Button1.text("Your button name or describe");
      // Button1.text("Your button name", "describe");
      Button1.print();
    }
    BlinkerAliGenie.powerState(oState ? "on" : "off");
    BlinkerAliGenie.print();
}

void button2_callback(const String & state)
{
    //digitalWrite(LED_BUILTIN2, !digitalRead(LED_BUILTIN2));
    BLINKER_LOG("get button state: ", state);

    if (state == BLINKER_CMD_BUTTON_TAP) {
      BLINKER_LOG("Button tap!");

      Button2.icon("icon_1");
      Button2.color("#FFFFFF");
      Button2.text("Your button name or describe");
      Button2.print();
    }
    else if (state == BLINKER_CMD_BUTTON_PRESSED) {
      BLINKER_LOG("Button pressed!");

      Button2.icon("icon_1");
      Button2.color("#FFFFFF");
      Button2.text("Your button name or describe");
      Button2.print();
    }
    else if (state == BLINKER_CMD_BUTTON_RELEASED) {
      BLINKER_LOG("Button released!");

      Button2.icon("icon_1");
      Button2.color("#FFFFFF");
      Button2.text("Your button name or describe");
      // Button2.text("Your button name", "describe");
      Button2.print();
    }
    else if (state == BLINKER_CMD_ON) {
      digitalWrite(LED_BUILTIN2, 1);
      BLINKER_LOG("Toggle on!");

      Button2.icon("fas fa-radiation");
      Button2.color("#0000FF");
      Button2.text("关闭2");
      // Button2.text("Your button name", "describe");
      Button2.print("on");
      oState=true;
    }
    else if (state == BLINKER_CMD_OFF) {
      digitalWrite(LED_BUILTIN2, 0);
      BLINKER_LOG("Toggle off!");
      Button2.icon("fas fa-radiation");
      Button2.color("#f0f0f0");
      Button2.text("打开2");
      // Button2.text("Your button name", "describe");
      Button2.print("off");
      oState=false;
    }
    else {
      BLINKER_LOG("Get user setting: ", state);

      Button2.icon("icon_1");
      Button2.color("#FFFFFF");
      Button2.text("Your button name or describe");
      // Button2.text("Your button name", "describe");
      Button2.print();
    }
    BlinkerAliGenie.powerState(oState ? "on" : "off");
    BlinkerAliGenie.print();
}

void button3_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    if (state == BLINKER_CMD_ON) {
      //digitalWrite(AlarmOutPin, 1);
      EnAlarm=1;
      BLINKER_LOG("Toggle on!");

      Button3.icon("fas fa-bell");
      Button3.color("#FF00FF");
      Button3.text("报警器关");
      // Button3.text("Your button name", "describe");
      Button3.print("on");
      oState=true;
    }
    else if (state == BLINKER_CMD_OFF) {
      //digitalWrite(AlarmOutPin, 0);
      EnAlarm=0;
      BLINKER_LOG("Toggle off!");
      Button3.icon("fas fa-bell");
      Button3.color("#f0f0f0");
      Button3.text("报警器开");
      // Button2.text("Your button name", "describe");
      Button3.print("off");
      oState=false;
    }
    else {
      BLINKER_LOG("Get user setting: ", state);

      Button3.icon("icon_1");
      Button3.color("#FFFFFF");
      Button3.text("Your button name or describe");
      // Button3.text("Your button name", "describe");
      Button3.print();
    }   

}


/*
void miotPowerState(const String & state, uint8_t num)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {   //小爱同学控制开命令
      //digitalWrite(0, HIGH);
      digitalWrite(LED_BUILTIN1, HIGH);
      BlinkerMIOT.powerState("on");

      BlinkerMIOT.print();

      oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {   //小爱同学控制关命令
      //digitalWrite(0,LOW);
      digitalWrite(LED_BUILTIN1, LOW);
      BlinkerMIOT.powerState("off");

      BlinkerMIOT.print();

      oState = false;
    }
}

void miotQuery(int32_t queryCode, uint8_t num)      //小爱同学控制
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
      case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
      case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("MIOT Query Power State");
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
      default :
            BlinkerMIOT.powerState(oState ? "on" : "off");
            BlinkerMIOT.print();
            break;
    }
}

void duerPowerState(const String & state, uint8_t num)
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {
      //digitalWrite(0, HIGH);
      digitalWrite(LED_BUILTIN1, HIGH);
      BlinkerDuerOS.powerState("on");
      BlinkerDuerOS.print();

      oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
      //digitalWrite(0, LOW);
      digitalWrite(LED_BUILTIN1, LOW);
      BlinkerDuerOS.powerState("off");
      BlinkerDuerOS.print();

      oState = false;
    }
}

void duerQuery(int32_t queryCode, uint8_t num)
{
    BLINKER_LOG("DuerOS Query codes: ", queryCode);

    switch (queryCode)
    {
      case BLINKER_CMD_QUERY_TIME_NUMBER :
            BLINKER_LOG("DuerOS Query time");
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
            case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("DuerOS Query time");
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
      default :
            BlinkerDuerOS.powerState(oState ? "on" : "off");
            BlinkerDuerOS.print();
            break;
    }
}
*/

void aligeniePowerState(const String & state, uint8_t num)
{
   BLINKER_LOG("need set outlet: ", num, ", power state: ", state);
      char i;
      if(num == 1){
      i=LED_BUILTIN1;          //指定每一路开关对应在开发板上的通道接口
    }else if(num == 2){
      i=LED_BUILTIN2;
    }else if(num == 3){
      i=AlarmOutPin;
    }

    if (state == BLINKER_CMD_ON) {
      digitalWrite(i , HIGH);
      BlinkerAliGenie.powerState("on",num);
      BlinkerAliGenie.print();
      Button1.icon("fas fa-power-off");
      Button1.color("#00FF00");
      Button1.text("关闭1");
      Button1.print("on");

      oState = true;
    }
    else if (state == BLINKER_CMD_OFF) {
      digitalWrite(i , LOW);
      BlinkerAliGenie.powerState("off",num);
      BlinkerAliGenie.print();
      Button1.icon("fas fa-power-off");
      Button1.color("#F0F0F0");
      Button1.text("打开1");
      Button1.print("off");

      oState = false;

         if (num == 0)
      {
            for (uint8_t o_num = 0; o_num < 3; o_num++)
            {
                oState = false;
            }
      }
    }
}

void aligenieQuery(int32_t queryCode, uint8_t num)
{
    BLINKER_LOG("AliGenie Query outlet: ", num,", codes: ", queryCode);
    int humi_read_int=humi_read;   //去掉湿度浮点

    switch (queryCode)
    {
      case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("AliGenie Query All");
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.temp(temp_read);
            BlinkerAliGenie.humi(humi_read_int);
            BlinkerAliGenie.print();
            break;
      case BLINKER_CMD_QUERY_POWERSTATE_NUMBER :
            BLINKER_LOG("AliGenie Query Power State");
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.print();
            break;
      default :
            BlinkerAliGenie.powerState(oState ? "on" : "off");
            BlinkerAliGenie.temp(temp_read);
            BlinkerAliGenie.humi(humi_read_int);
            BlinkerAliGenie.print();
            break;
    }
}

void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);
//    counter++;
//    Number1.print(counter);

    HUMI.print(humi_read);
    TEMP.print(temp_read);

    BLINKER_LOG("Blinker readString: ", data);
    Blinker.vibrate();
    uint32_t BlinkerTime = millis();
    Blinker.print("millis", BlinkerTime);
    Text1.print("开关");//, BlinkerTime);
//    Blinker.wechat("Hello blinker!","state","message");
   // digitalWrite(LED_BUILTIN2, !digitalRead(LED_BUILTIN2));
    Slider1.color("#0000FF");
    Slider1.print(SetTemp);
}

void heartbeat()
{
//***    if (switch_state) BUILTIN_SWITCH.print("on");
//***    else BUILTIN_SWITCH.print("off");
//    Number1.print(counter);
    //反馈湿度数据
    //设置ui组件图标和颜色
    HUMI.icon("far fa-humidity");
    HUMI.color("#fddb10");
    HUMI.print(humi_read);
    //反馈温度数据
    //TEMP.print(temp_read);
    TEMP.icon("fas fa-thermometer-three-quarters");
    TEMP.color("#00FF00");
    TEMP.print(temp_read);
    //if (oState) Button1.print("on");else Button1.print("off");
    //if (oState) Button2.print("on");else Button2.print("off");
    //if (oState) Button3.print("on");else Button3.print("off");
}

void dataStorage()
{
Blinker.dataStorage("temp",temp_read);
Blinker.dataStorage("humi",humi_read);
}

//配网
void smartConfig()
{
   WiFi.mode(WIFI_STA);
   Serial.println("Wait for Smartconfig");
   Blinker.delay(500);   // 等待配网
   WiFi.beginSmartConfig();
   while (1){
   Blinker.delay(200);
   if (WiFi.smartConfigDone()){
   Serial.println("SmartConfig Success");
   WiFi.setAutoConnect(true);// 设置自动连接
   break;
   }
   }
}

void setup()
{
    Serial.begin(115200);
    //Blinker.init();
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
    pinMode(AlarmOutPin, OUTPUT);
    pinMode(FlameIn, INPUT_PULLUP);
    pinMode(SmokeSwitch, INPUT_PULLUP);
    pinMode(ManSwitch, INPUT_PULLUP);
    pinMode(SoundSwitch, INPUT_PULLUP);
    pinMode(LightSwitch, INPUT_PULLUP);

    pinMode(LED_BUILTIN1, OUTPUT);
    digitalWrite(LED_BUILTIN1, LOW);
    pinMode(LED_BUILTIN2, OUTPUT);
    digitalWrite(LED_BUILTIN2, LOW);

    /*smartConfig();
    while (WiFi.status() != WL_CONNECTED)
    {
   Serial.print("..");
   Blinker.delay(300);
    }
    Serial.println("WiFi connected");
    Blinker.begin(auth, WiFi.SSID().c_str(),WiFi.psk().c_str());*/

    //Blinker.begin(auth);
    Blinker.begin(auth, ssid, pswd);
    Blinker.attachData(dataRead);
    Blinker.attachHeartbeat(heartbeat);
    Button1.attach(button1_callback);
    Button1.icon("fas fa-power-off");
    Button1.color("#F0F0F0");
    Button1.text("打开1");
    // Button1.text("Your button name", "describe");
    Button1.print("off");
    Button2.attach(button2_callback);
    Button2.icon("fas fa-radiation");
    Button2.color("#f0f0f0");
    Button2.text("打开2");
    // Button2.text("Your button name", "describe");
    Button2.print("off");
    Button3.attach(button3_callback);
    Button3.icon("fas fa-bell");
    Button3.color("#f0f0f0");
    Button3.text("报警器开");
      // Button2.text("Your button name", "describe");
    Button3.print("off");
    //BUILTIN_SWITCH.attach(switch_callback);
    Blinker.attachDataStorage(dataStorage);
    dht.begin();   
    BlinkerAliGenie.attachPowerState(aligeniePowerState);
    BlinkerAliGenie.attachQuery(aligenieQuery);
//   BlinkerDuerOS.attachPowerState(duerPowerState);
//   BlinkerDuerOS.attachQuery(duerQuery);
//   BlinkerMIOT.attachPowerState(miotPowerState);
//   BlinkerMIOT.attachQuery(miotQuery);
    Tab1.attach(tab1_callback, tab1_feedback);
    Slider1.attach(slider1_callback);
    Slider2.attach(slider2_callback);
    Slider1.color("#0000FF");
    Slider1.print(SetTemp);
    Slider2.color("#0000FF");
    Slider2.print(SetHumi);
    Tab1.print();
}

void loop()
{
    Blinker.run();
    Blinker.autoRun();   
    if (read_time == 0 || (millis() - read_time) >= 2000)
    {
      read_time = millis();
//BLINKER_LOG("允许报警 ",EnAlarm,"火焰 烟雾 人体 声音 光感 ",tab_save,tab_save,tab_save,tab_save,tab_save);
      float h = dht.readHumidity();
      float t = dht.readTemperature();      

      if (isnan(h) || isnan(t)) {
            BLINKER_LOG("Failed to read from DHT sensor!");
       //   return;
      }
         else
      {
         //BLINKER_LOG("Humidity: ", h, " %");
         //BLINKER_LOG("Temperature: ", t, " *C");
         humi_read = h;
         temp_read = t;
      }
      if (t > SetTemp)
      {
          //BLINKER_LOG("设置温度",SetTemp);         
          //Blinker.wechat("Title:机房温度","state:超过设定温度","Message:机房温度过高");
          //char phone[] = "13533587261";
          //Blinker.sms("机房温度过高!",phone);
          //Blinker.push("超过设定温度");
      }
      float hic = dht.computeHeatIndex(t, h, false);

      //humi_read = h;
      //temp_read = t;
      //BLINKER_LOG("tabset",tab);
/*      BLINKER_LOG("Humidity: ", h, " %");
      BLINKER_LOG("Temperature: ", t, " *C");
      BLINKER_LOG("Heat index: ", hic, " *C");*/
      if (EnAlarm)
      {if (((!digitalRead(FlameIn))&&tab_save)||((!digitalRead(SmokeSwitch))&&tab_save)
         ||((!digitalRead(ManSwitch))&&tab_save)||((digitalRead(SoundSwitch))&&tab_save)
         ||((!digitalRead(LightSwitch))&&tab_save))
         {digitalWrite(AlarmOutPin, HIGH);
            Number2.icon("fas fa-bell");
            Number2.color("#FF0000");
            Number2.unit("报警中...");
            Number2.print();}
      else
          {digitalWrite(AlarmOutPin, LOW);
          Number2.icon("fas fa-bell-slash");
          Number2.color("#F0F0F0");
          Number2.unit("无报警");
          Number2.print();}
          }
      else
          {digitalWrite(AlarmOutPin, LOW);
            Number2.icon("fas fa-bell-slash");
            Number2.color("#F0F0F0");
            Number2.unit("无报警");
            Number2.print();}
    }
}

旭日之下 发表于 2020-7-7 17:29

难度有点打啊,兄弟

810986815 发表于 2020-12-14 11:44

非常牛的程序可以把界面配置代码发出来吗?

g2552 发表于 2020-12-14 11:52

这一串还怪全;P;P;P

810986815 发表于 2020-12-14 17:25

刚刚试了一下,程序不错,可以根据自己的需求删减。要是有界面配置代码就更好了。
页: [1]
查看完整版本: 用无线门铃制作远程多功能报警自定义开关