ESP8266接入小爱同学系列--5.传感器类型--温湿度

硬件改动

本次程序是基于ESP-01S模块,之前都是NodeMCU模块。程序区别不大,有区别的在于烧录配置。

Arduino 项目配置

开发板:Generic ESO8266 Module
Flash Size:1M(64K SPIFFS)
Flash Mode:DOUT

这几项很重要,不对的话会出问题。此坑已踩
ESP8266接入小爱同学系列--5.传感器类型--温湿度_第1张图片

IO配置

ESP-01S的IO配置如下:
GPIO0 – 0
GPIO2 – 2
本次采用IO2引脚与传感器通讯,因此配置为: #define DHTPIN 2

代码

/* *****************************************************************
 *
 * 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_WIFI
#define BLINKER_MIOT_SENSOR

#include 
#include 
int count = 0;
bool WIFI_Status = true;

char auth[] = "09403eac3fdc";

BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");

#include 

#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;

float humi_read, temp_read;

/* 智能配网模式 */
void smartConfig()
{
  WiFi.mode(WIFI_STA);//设置STA模式
  Serial.println("\r\nWait for Smartconfig...");//打印log信息
  WiFi.beginSmartConfig();//开始SmartConfig,等待手机端发出用户名和密码
  while(1)
  {
    Serial.println(".");
    digitalWrite(LED_BUILTIN,HIGH);//指示灯闪烁
    delay(1000);
    digitalWrite(LED_BUILTIN,LOW);//指示灯闪烁
    delay(1000);
    if(WiFi.smartConfigDone())//配网成功,接收到SSID和密码
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;      
      }
    }  
}
void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);
    heartbeat();
    switch (queryCode)
    {
       case BLINKER_CMD_QUERY_PM25_NUMBER :
            BLINKER_LOG("MIOT Query PM25");
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.print();
            break;
        case BLINKER_CMD_QUERY_HUMI_NUMBER :
            BLINKER_LOG("MIOT Query HUMI");
            BlinkerMIOT.humi((int)humi_read);
            BlinkerMIOT.print();
            
            break;
        case BLINKER_CMD_QUERY_TEMP_NUMBER :
            BLINKER_LOG("MIOT Query TEMP");
            BlinkerMIOT.temp((int)temp_read);
            BlinkerMIOT.print();
            break;

        default :
            BlinkerMIOT.temp((int)temp_read);
            BlinkerMIOT.humi((int)humi_read);
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
    }
}

void dataRead(const String & data)
{
    BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
    
    uint32_t BlinkerTime = millis();
    
    Blinker.print("millis", BlinkerTime);
}

void heartbeat()
{
    HUMI.print(humi_read);
    TEMP.print(temp_read);
}
void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);

    Serial.println("\r\n正在连接WIFI...");
    while(WiFi.status()!=WL_CONNECTED)//判断是否连接WIFI成功
    {
      if(WIFI_Status)
      {
          Serial.print(".");
          digitalWrite(LED_BUILTIN, HIGH);  
          delay(500);                       
          digitalWrite(LED_BUILTIN, LOW);    
          delay(500);                 
          count++;
          if(count>=5)//5s
          {
              WIFI_Status = false;
              Serial.println("WiFi连接失败,请用手机进行配网"); 
          }        
        }
        else
        {
          smartConfig();  //微信智能配网
          }
      }

   Serial.println("连接成功");  
   Serial.print("IP:");
   Serial.println(WiFi.localIP());

   Blinker.begin(auth,WiFi.SSID().c_str(),WiFi.psk().c_str());
    Blinker.attachData(dataRead);

    BlinkerMIOT.attachQuery(miotQuery);
    Blinker.attachHeartbeat(heartbeat);
    dht.begin();
}

void loop()
{
    Blinker.run();
    if (read_time == 0 || (millis() - read_time) >= 1000)
    {
        read_time = millis();

        float h = dht.readHumidity();
        float t = dht.readTemperature();        

        if (isnan(h) || isnan(t)) {
            BLINKER_LOG("Failed to read from DHT sensor!");
            return;
        }

        float hic = dht.computeHeatIndex(t, h, false);

        humi_read = h;
        temp_read = t;

        BLINKER_LOG("Humidity: ", h, " %");
        BLINKER_LOG("Temperature: ", t, " *C");
        BLINKER_LOG("Heat index: ", hic, " *C");
        heartbeat();
    }
}

你可能感兴趣的:(ESP266+小爱同学)