小爱同学获取BME280信息+自动配网-Arduino中文社区 - Powered by Discuz! Archiver

YLGZS 发表于 2020-3-18 09:29

小爱同学获取BME280信息+自动配网

本帖最后由 YLGZS 于 2020-3-18 10:51 编辑

代码有错误有意者自行更改,大佬可以把更改后代码分享。:)

















#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR//支持小爱同学
#include <Blinker.h>
char auth[] = "密钥";
//char ssid[] = "WIFI";
//char pswd[] = "密码";
// BME280

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
      case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.temp(100);
            BlinkerMIOT.humi(100);
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
      default :
            BlinkerMIOT.temp(100);
            BlinkerMIOT.humi(100);
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
    }
}

//BME280

BlinkerNumber TEMP("num-htn"); // Temperature
BlinkerNumber HUMI("num-ft"); // Humidity
BlinkerNumber PRES("num-hi"); // Pressure
BlinkerNumber ALTI("num-nn"); // Altitude
int tempValue = 0;
int humiValue = 0;
int presValue = 0;
int altiValue = 0;
// DHT11
BlinkerNumber DHTTEMP("dht-temp"); // Temperature
BlinkerNumber DHTHUMI("dht-humi"); // Humidity
int dhtTempValue = 0;
int dhtHumiValue = 0;
// RUNTIME
uint32 startTime;
uint32 runTime;
// Download Adafruit-BMP085-Library library here:
// https://github.com/adafruit/Adafruit_BME280_Library
// https://github.com/adafruit/Adafruit_Sensor
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

#include <DHT.h>
#define DHTPIN D8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

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

    Blinker.vibrate();

    uint32_t BlinkerTime = millis();

    Blinker.print("millis", BlinkerTime);
}

void dataStorage()
{
    readSensor();
    Blinker.dataStorage("temp", tempValue);
    Blinker.dataStorage("humi", humiValue);
    Blinker.dataStorage("pres", altiValue);
    Blinker.dataStorage("dht-temp", dhtTempValue);
    Blinker.dataStorage("dht-humi", dhtHumiValue);
}

void heartbeat()
{
    readSensor();
    TEMP.print(tempValue);
    HUMI.print(humiValue);
    PRES.print(presValue);
    ALTI.print(altiValue);
    DHTTEMP.print(dhtTempValue);
    DHTHUMI.print(dhtHumiValue);
}

void readSensor()
{
    tempValue = bme.readTemperature();
    humiValue = bme.readHumidity();
    presValue = bme.readPressure();
    altiValue = bme.readAltitude(SEALEVELPRESSURE_HPA);

    dhtTempValue = dht.readTemperature();
    dhtHumiValue = dht.readHumidity();
    if (isnan(dhtTempValue) || isnan(dhtHumiValue))
    {
      dhtTempValue = 0;
      dhtHumiValue = 0;
    }
}

void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
   smartConfig ();//执行一键配网
   Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());

    if (! bme.begin(0x76, &Wire)) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      while (1);
    }
    dht.begin();
    readSensor();
    Blinker.attachData(dataRead);
    Blinker.attachHeartbeat(heartbeat);
    Blinker.attachDataStorage(dataStorage);
   BlinkerMIOT.attachQuery(miotQuery);
}

void loop()
{
    Blinker.run();
}

void smartConfig () //一键配网函数
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();//SmartConfig配网开始
while (!WiFi.smartConfigDone())
{
    delay(500);
    Serial.println(".");
}
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
}

奈何col 发表于 2020-3-18 10:08

blinker有配网支持
https://diandeng.tech/doc/dev-tool

YLGZS 发表于 2020-3-18 10:43

本帖最后由 YLGZS 于 2020-3-18 11:12 编辑

修改版功能正常
本代码支持DHT11(可能需要自行更改)






#define BLINKER_WIFI


#define BLINKER_ESP_SMARTCONFIG


#define BLINKER_MIOT_SENSOR//支持小爱同学


#include <Blinker.h>


char auth[] = "Blinker密钥";


//char ssid[] = "WiFi名称";


//char pswd[] = "密码";





//BME280





BlinkerNumber TEMP("num-htn"); // Temperature


BlinkerNumber HUMI("num-ft"); // Humidity


BlinkerNumber PRES("num-hi"); // Pressure


BlinkerNumber ALTI("num-nn"); // Altitude


int tempValue = 0;


int humiValue = 0;


int presValue = 0;


int altiValue = 0;


// DHT11


BlinkerNumber DHTTEMP("dht-temp"); // Temperature


BlinkerNumber DHTHUMI("dht-humi"); // Humidity


int dhtTempValue = 0;


int dhtHumiValue = 0;








// RUNTIME


uint32 startTime;


uint32 runTime;


// Download Adafruit-BMP085-Library library here:


// https://github.com/adafruit/Adafruit_BME280_Library


// https://github.com/adafruit/Adafruit_Sensor


#include <Wire.h>


#include <Adafruit_Sensor.h>


#include <Adafruit_BME280.h>





#define SEALEVELPRESSURE_HPA (1013.25)





Adafruit_BME280 bme;





#include <DHT.h>


#define DHTPIN D8


#define DHTTYPE DHT11


DHT dht(DHTPIN, DHTTYPE);





void dataRead(const String &data)


{


    BLINKER_LOG("Blinker readString: ", data);





    Blinker.vibrate();





    uint32_t BlinkerTime = millis();





    Blinker.print("millis", BlinkerTime);


}





void dataStorage()


{


    readSensor();


    Blinker.dataStorage("temp", tempValue);


    Blinker.dataStorage("humi", humiValue);


    Blinker.dataStorage("pres", altiValue);


    Blinker.dataStorage("dht-temp", dhtTempValue);


    Blinker.dataStorage("dht-humi", dhtHumiValue);


}





void heartbeat()


{


readSensor();


    TEMP.print(tempValue);


    HUMI.print(humiValue);


    PRES.print(presValue);


    ALTI.print(altiValue);


    DHTTEMP.print(dhtTempValue);


    DHTHUMI.print(dhtHumiValue);


}





void readSensor()


{


    tempValue = bme.readTemperature();


    humiValue = bme.readHumidity();


    presValue = bme.readPressure();


    altiValue = bme.readAltitude(SEALEVELPRESSURE_HPA);





    dhtTempValue = dht.readTemperature();


    dhtHumiValue = dht.readHumidity();


    if (isnan(dhtTempValue) || isnan(dhtHumiValue))


    {


      dhtTempValue = 0;


      dhtHumiValue = 0;


    }


}





void miotQuery(int32_t queryCode)


{


    BLINKER_LOG("MIOT Query codes: ", queryCode);





    switch (queryCode)


    {


      case BLINKER_CMD_QUERY_ALL_NUMBER :


            BLINKER_LOG("MIOT Query All");


            BlinkerMIOT.temp(tempValue);


            BlinkerMIOT.humi(humiValue);


            BlinkerMIOT.pm25(presValue);


            BlinkerMIOT.co2(altiValue);


            BlinkerMIOT.print();


            break;





      default :


            BlinkerMIOT.temp(20);


            BlinkerMIOT.humi(20);


            BlinkerMIOT.pm25(20);


            BlinkerMIOT.co2(20);


            BlinkerMIOT.print();


            break;


    }


}








void setup()


{


    Serial.begin(115200);


    BLINKER_DEBUG.stream(Serial);


    pinMode(LED_BUILTIN, OUTPUT);


    digitalWrite(LED_BUILTIN, LOW);





   Blinker.begin(auth);


               //(! bme.begin(BME280地址, &Wire))


    if (! bme.begin(0x76, &Wire)) {


      Serial.println("Could not find a valid BME280 sensor, check wiring!");


      while (1);


    }


    dht.begin();


    readSensor();


    Blinker.attachData(dataRead);


    Blinker.attachHeartbeat(heartbeat);


    Blinker.attachDataStorage(dataStorage);


   BlinkerMIOT.attachQuery(miotQuery);


}








void loop()


{


    Blinker.run();


}



C:\Users\30210\Desktop\QQ图片20200318103642.jpg


C:\Users\30210\Desktop\QQ图片20200318103637.jpg









YLGZS 发表于 2020-3-18 10:45

本帖最后由 YLGZS 于 2020-3-18 10:56 编辑

YLGZS 发表于 2020-3-18 10:43
修改版功能正常





#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR//支持小爱同学
#include <Blinker.h>
char auth[] = "Blinker密钥";
//char ssid[] = "WiFi名称";
//char pswd[] = "密码";

//BME280

BlinkerNumber TEMP("num-htn"); // Temperature
BlinkerNumber HUMI("num-ft"); // Humidity
BlinkerNumber PRES("num-hi"); // Pressure
BlinkerNumber ALTI("num-nn"); // Altitude
int tempValue = 0;
int humiValue = 0;
int presValue = 0;
int altiValue = 0;
// DHT11
BlinkerNumber DHTTEMP("dht-temp"); // Temperature
BlinkerNumber DHTHUMI("dht-humi"); // Humidity
int dhtTempValue = 0;
int dhtHumiValue = 0;


// RUNTIME
uint32 startTime;
uint32 runTime;
// Download Adafruit-BMP085-Library library here:
// https://github.com/adafruit/Adafruit_BME280_Library
// https://github.com/adafruit/Adafruit_Sensor
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme;

#include <DHT.h>
#define DHTPIN D8
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

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

    Blinker.vibrate();

    uint32_t BlinkerTime = millis();

    Blinker.print("millis", BlinkerTime);
}

void dataStorage()
{
    readSensor();
    Blinker.dataStorage("temp", tempValue);
    Blinker.dataStorage("humi", humiValue);
    Blinker.dataStorage("pres", altiValue);
    Blinker.dataStorage("dht-temp", dhtTempValue);
    Blinker.dataStorage("dht-humi", dhtHumiValue);
}

void heartbeat()
{
readSensor();
    TEMP.print(tempValue);
    HUMI.print(humiValue);
    PRES.print(presValue);
    ALTI.print(altiValue);
    DHTTEMP.print(dhtTempValue);
    DHTHUMI.print(dhtHumiValue);
}

void readSensor()
{
    tempValue = bme.readTemperature();
    humiValue = bme.readHumidity();
    presValue = bme.readPressure();
    altiValue = bme.readAltitude(SEALEVELPRESSURE_HPA);

    dhtTempValue = dht.readTemperature();
    dhtHumiValue = dht.readHumidity();
    if (isnan(dhtTempValue) || isnan(dhtHumiValue))
    {
      dhtTempValue = 0;
      dhtHumiValue = 0;
    }
}

void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
      case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.temp(tempValue);
            BlinkerMIOT.humi(humiValue);
            BlinkerMIOT.pm25(presValue);
            BlinkerMIOT.co2(altiValue);
            BlinkerMIOT.print();
            break;
   
      default :
            BlinkerMIOT.temp(20);
            BlinkerMIOT.humi(20);
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
    }
}


void setup()
{
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
   smartConfig ();//执行一键配网
   Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());

    if (! bme.begin(0x76, &Wire)) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      while (1);
    }
    dht.begin();
    readSensor();
    Blinker.attachData(dataRead);
    Blinker.attachHeartbeat(heartbeat);
    Blinker.attachDataStorage(dataStorage);
   BlinkerMIOT.attachQuery(miotQuery);
}



void loop()
{
    Blinker.run();
}

void smartConfig () //一键配网函数
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();//SmartConfig配网开始
while (!WiFi.smartConfigDone())
{
    delay(500);
    Serial.println(".");
}
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
}

YLGZS 发表于 2020-3-18 11:27

手机界面代码


{¨config¨{¨headerColor¨¨transparent¨¨headerStyle¨¨light¨¨background¨{¨img¨¨assets/img/headerbg.jpg¨¨isFull¨«}}¨dashboard¨|{¨type¨¨num¨¨t0¨¨温度¨¨ico¨¨fal fa-question¨¨clr¨¨#EA0909¨¨min¨É¨max¨¢1c¨uni¨´℃´¨bg¨É¨cols¨Í¨rows¨Ë¨key¨¨num-htn¨´x´É´y´Ì¨speech¨|÷¨lstyle¨Ê}{ßAßBßC¨气压¨ßEßFßG¨#FBA613¨ßIÉßJ¢QH2ßK¨pa¨ßLÉßMÍßNËßO¨num-hi¨´x´Í´y´ÌßQ|÷ßRÊ}{ßAßBßC¨湿度¨ßEßFßG¨#076EEF¨ßIÉßJº0ßK´%´ßLÉßMÍßNËßO¨num-ft¨´x´É´y´ÎßQ|÷ßRÊ}{ßAßBßC¨海拔¨ßEßFßG¨#00A90C¨ßIÉßJ¢252ßK´m´ßLÉßMÍßNËßO¨num-nn¨´x´Í´y´ÎßQ|÷ßRÊ}{ßA¨cha¨ßLɨsty¨¨line¨ßGßH¨sty1¨ße¨clr1¨ßX¨sty2¨ße¨clr2¨ßTßMÑßNÍßOßP´x´É´y´ÐßQ|÷ßCßD¨key1¨ßY¨t1¨ßW¨key2¨ßV¨t2¨ßSßRÊ}÷}
页: [1]
查看完整版本: 小爱同学获取BME280信息+自动配网