[md]# 温湿度计
## 特点
支持记录温湿度数据
支持小爱同学获取数据
## 硬件
Wifiduino(esp8266)
DHT温湿度传感器
### 绑定小爱同学
1.打开米家App。通过 **我的>其他平台设备>点击添加>点灯科技>绑定账号** ,绑定blinker账号
2.绑定成功后,支持小爱控制的blinker设备会出现在 **我的>其他平台设备>点灯科技** 设备列表中
3.现在可以使用小爱控制该设备了
```
#define BLINKER_WIFI
#define BLINKER_MIOT_SENSOR
#include <Blinker.h>
#include <DHT.h>
char auth[] = "Your Device Secret Key";
char ssid[] = "Your WiFi network SSID or name";
char pswd[] = "Your WiFi network WPA password or WEP key";
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");
#define DHTPIN D7
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
int humi_read = 0, temp_read = 0;
void heartbeat()
{
HUMI.print(humi_read);
TEMP.print(temp_read);
}
void dataStorage()
{
Blinker.dataStorage("temp", temp_read);
Blinker.dataStorage("humi", humi_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;
default :
BlinkerMIOT.temp(20);
BlinkerMIOT.humi(20);
break;
}
}
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);
Blinker.attachDataStorage(dataStorage);
BlinkerMIOT.attachQuery(miotQuery);
dht.begin();
}
void loop()
{
Blinker.run();
int h = dht.readHumidity();
int t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
BLINKER_LOG("Failed to read from DHT sensor!");
}
else
{
humi_read = h;
temp_read = t;
}
Blinker.delay(2000);
}
```[/md] |