源码如下:
#define BLINKER_PRINT Serial
#define BLINKER_MQTT
#include <Blinker.h>
//DHT11
#include <modules/dht/DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define humanse 12
//界面
//#define BUTTON_1 "ButtonKey"
#define TOGGLE_1 "ToggleKey"
#define TOGGLE_2 "Toggleled"
char ssid[] = "newifi";
char pswd[] = "87257258";
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(humanse, INPUT);
Blinker.begin(auth, ssid, pswd);
dht.begin();
//组件初始化, 建议在使用前初始化 Button 、Slider 、 Toggle 及 RGB
Blinker.wInit(TOGGLE_1, W_TOGGLE);
Blinker.wInit(TOGGLE_2, W_TOGGLE);
if (digitalRead(LED_BUILTIN)) {
Blinker.print(TOGGLE_2, "on");
Blinker.delay(1000);
} else {
Blinker.print(TOGGLE_2, "off");
Blinker.delay(1000);
}
}
void loop()
{
Blinker.run();
if (Blinker.available()) {//检测是否收到数据
//BLINKER_LOG2("Blinker.readString(): ", Blinker.readString());
//Blinker.vibrate();//发送手机振动指令, 震动时间, 单位ms 毫秒, 数值范围0-1000, 默认为500
Blinker.delay(1000);
uint32_t BlinkerTime = millis();
Blinker.delay(2000);
int h = dht.readHumidity();
int t = dht.readTemperature();
Blinker.delay(2000);
if (isnan(h) || isnan(t)) {
Blinker.print("temp", "error");
Blinker.delay(1000);
Blinker.print("humi", "error");
Blinker.delay(1000);
} else {
Blinker.print("temp", t, "℃");
Blinker.delay(1000);
Blinker.print("humi", h, "%");
Blinker.delay(1000);
}
Blinker.print("light", 1024 - analogRead(A0));
Blinker.delay(1000);
Blinker.print("millis", BlinkerTime / 60000);
Blinker.delay(1000);
if (digitalRead(LED_BUILTIN)) {
Blinker.print(TOGGLE_2, "on");
Blinker.delay(1000);
} else {
Blinker.print(TOGGLE_2, "off");
Blinker.delay(1000);
}
}
if (Blinker.toggle(TOGGLE_2)) {
digitalWrite(LED_BUILTIN, HIGH);
}
else {
digitalWrite(LED_BUILTIN, LOW);
}
if (digitalRead(humanse) && Blinker.toggle(TOGGLE_1)) {//人体红外感应
Blinker.print("notice", "刚刚有人进入了");
//BLINKER_LOG1("刚刚有人进入了");
Blinker.vibrate(1000);
Blinker.delay(1000);
}
}
在手机上读取DH11温湿度经常出错!!
|