|
代码:
#define BLINKER_PRINT Serial //用于打开串口
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR //定义为语音控制传感器设备
#include <Blinker.h>
#include <DHT.h>
char auth[] = "bf007dd4e163"; //caiyq52-改成自己在点灯app中生成的key码
char ssid[] = "lumious"; //caiyq52-改成自己的WIFI名称
char pswd[] = "19971209"; //caiyq52-改成自己的WIFI密码
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
#define DHTPIN 12 //
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
uint32_t read_time = 0;
int32_t humi_read = 0;//湿度 小爱同学只能查整数的数据
float temp_read = 0; //温度
void heartbeat()
{
//反馈湿度数据
HUMI.print(humi_read);
//反馈温度数据
TEMP.print(temp_read);
}
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(temp_read);
BlinkerMIOT.humi(humi_read);
BlinkerMIOT.print();
break;
//查询湿度
case BLINKER_CMD_QUERY_HUMI_NUMBER :
BLINKER_LOG("MIOT Query HUMI");
BlinkerMIOT.humi(humi_read);
BlinkerMIOT.print();
break;
//查询温度
case BLINKER_CMD_QUERY_TEMP_NUMBER :
BLINKER_LOG("MIOT Query TEMP");
BlinkerMIOT.temp(temp_read);
BlinkerMIOT.print();
break;
default :
BlinkerMIOT.temp(20);
BlinkerMIOT.humi(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 setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
BLINKER_DEBUG.debugAll();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
Blinker.attachHeartbeat(heartbeat);
BlinkerMIOT.attachQuery(miotQuery); //小爱同学 数据反馈
Blinker.attachData(dataRead);
dht.begin();
}
void loop()
{
Blinker.run();
if (read_time == 0 || (millis() - read_time) >= 2000)
{
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");
}
Blinker.delay(2000);
}
|
|