本文主要对物联网常用场景,读取传感器数据然后反馈到app上查看。
ESP8266 (我使用的是wemos D1,80Mhz,4M flash)
Arduino IDE(提前将开发板安装)
使用示例:
blinker-library/DHT_WiFi.ino at master · blinker-iot/blinker-library · GitHub
- #define BLINKER_WIFI
-
- #include
-
- 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"); //对应blinkerapp上的数据名
- BlinkerNumber TEMP("temp"); //在blinkerapp下有温度数据名为
-
- // Download Adafruit DHT-sensor-library library here:
- // https://github.com/adafruit/DHT-sensor-library
- #include
-
- #define DHTPIN 2
-
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
-
- DHT dht(DHTPIN, DHTTYPE);
-
- uint32_t read_time = 0;
-
- float humi_read, temp_read;
-
- void heartbeat()
- {
- HUMI.print(humi_read); //给blinkerapp湿度数据
- TEMP.print(temp_read); //给blinkerapp温度数据
- }
-
- void setup()
- {
- Serial.begin(115200);
- BLINKER_DEBUG.stream(Serial);
-
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, LOW);
-
- Blinker.begin(auth, ssid, pswd);
- Blinker.attachHeartbeat(heartbeat);//将传感器回调函数加载,回传数据
-
- dht.begin();
- }
-
- void loop()
- {
- Blinker.run();
-
- if (read_time == 0 || (millis() - read_time) >= 2000)
- {
- read_time = millis();
-
- float h = dht.readHumidity();//读取DHT11传感器的湿度 并赋值给h
- float t = dht.readTemperature();
-
- if (isnan(h) || isnan(t)) {
- BLINKER_LOG("Failed to read from DHT sensor!");
- return;
- }
- else
- {
- BLINKER_LOG("Humidity: ", h, " %");
- BLINKER_LOG("Temperature: ", t, " *C");
- humi_read = h;//将读取到的湿度赋值给全局变量humi_read
- temp_read = t;//将读取到的温度赋值给全局变量temp_read
- }
- }
- }
温度数据,湿度设计
复制device的认证key
修改device认证key,wifi路由器ssid和password
- char auth[] = "Your Device Secret Key";
- char ssid[] = "Your WiFi network SSID or name";
- char pswd[] = "Your WiFi network WPA password or WEP key";
因为我用的是dht11,所以需要根据自己的方式修改
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
上传看效果