Summary
DHT22 module applies specific digital blocks collection technology and temperature and humidity sensing technology to ensure that products with high reliability and excellent long-term stability. Sensor comprises a capacitive humidity sensing element and a high-precision temperature component, and with a high-performance 8-bit microcontroller connected. Therefore, the product has excellent quality, fast response, anti-interference ability, high cost and so on. Measurable temperature range of the module: -40-80 ℃ ± 0.5 ℃, humidity range: 20-90% RH ± 2% RH. The module is widely used in temperature and humidity regulator, weather station, and other relevant temperature and humidity detection control.
Material preparation
- LinkNode D1 x 1
- DHT22 x 1
- DuPont line
- Arduino IDE
Steps
1. Login linksprite.io ,record the “Device ID” and “API key” ( If there is no account, please sign up. )
- 1. My Profile -> API key
- 2. My device->Create DIY Device
Enter Device Name,Device Type , Group Name , the device number is 03, device name and device grouping can be any.
Note: Device Type must be selected 03(Custom device type) .Click the device you created and record the “Device ID”.
2. Edit and run the code
Modify the “apikey” and “deviceID” with content acquired before
Compile the code and upload.
3. Results
- Serial Monitor
2. LinkSprite IO
Code
#include <ESP8266WiFi.h> #include <WString.h> #include <DNSServer.h> #include <ESP8266WebServer.h> #include <WiFiManager.h> #include "DHT.h" #define DHTPIN D0 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) char *tem=""; String apikey = "162d15b0-2c6d-4d44-b288-10f28a527f96"; const char* deviceID="030000000f"; const char* server = "www.linksprite.io"; WiFiClient client; DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); WiFiManager wifiManager; wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); wifiManager.autoConnect("LinkNodeAP"); Serial.print("WiFi Connected ...\n"); Serial.println("WiFi connected"); Serial.println("DHTxx test!"); dht.begin(); } void loop() { delay(2000); float h = dht.readHumidity(); float t = dht.readTemperature(); // Read temperature as Celsius (the default) float f = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true) // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } float hif = dht.computeHeatIndex(f, h); // Compute heat index in Fahrenheit (the default) float hic = dht.computeHeatIndex(t, h, false);// Compute heat index in Celsius (isFahreheit = false) Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); if (client.connect(server,80)) { String postStr ="{"; postStr +="\"action\":\"update\","; postStr +="\"apikey\":\""; postStr += apikey; postStr +="\","; postStr +="\"deviceid\":\""; postStr += deviceID; postStr +="\","; postStr +="\"params\":"; postStr +="{"; postStr +="\"temperature\":\""; itoa(t,tem,10); postStr +=tem; postStr +="\","; postStr +="\"humidity\":\""; itoa(h,tem,10); postStr +=tem; postStr +="\"\r\n"; postStr +="}"; postStr +="}"; client.print("POST /api/http HTTP/1.1\n"); client.print("Host: "); client.print(server); client.print("\nContent-Type: application/json\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); String request = ""; delay(1000); while (client.available()) { char c = client.read(); request +=c; } if (request!= NULL) { int index1 = request.indexOf(":{"); int index2 = request.indexOf("},"); String param = request.substring(index1, index2 + 1); String data="\0" ; Serial.print("The param is "); Serial.println(param); client.stop(); Serial.println("waiting ..."); delay(1000); } } }