Status | ||||||
---|---|---|---|---|---|---|
|
...
- OpenDevice server up and running (guide)
- Arduino IDE (with required OpenDevice Libraries)
- ESP8266 module (like NodeMCU)
- DHT22 sensor (and Adafruit's DHT22 library)
DHT22 | NodeMCU |
---|
Wiring
The most basic DHT humidity and temperature sensor comes in two variants with different levels of accuracy.
DHT-11 | DHT-22 | |
---|---|---|
Humidity range | 20%-80%RH (±5%RH) | 0%-100%RH (±2%RH) |
Temperature range | 0-50°C (±2°C) | -40-80°C (±0.5°C) |
Measurement time | 1s per sample | 2s per sample |
Setup
To program the NodeMCU, we are going to use Arduino's IDE. It's the easiest way to get up and running with the DHT22.
First, you'll have to set up your machine: Device Setup Instructions. These instructions take you through installing the IDE, ESP8266 and the proper libraries.
In addition to this, you'll have to install DHT sensor library to read the sensor, we are going to use Adafruit's DHT22 library.
Code / Firmware
The OpenDevice for Arduino software greatly simplifies application development, managing devices and communicating with the server in a completely transparent way, so you can really focus on your project.
...
Code Block |
---|
#include <ESP8266WiFi.h> // Enable ESP8266 / WiFi #include <PubSubClient.h> // enable MQTT #include <ArduinoOTA.h> // Remote Updates XXXXXXXXXXXX DHT // Sensor Libraries #include <Adafruit_Sensor.h> #include <DHT.h> #include <DHT_U.h> #include <OpenDevice.h> // Must be included after |
Next we need to configure the device name (you choose) and the api key (see this guide).
app code:
Code Block |
---|
#define ODEV_API_KEY "-----"
#define ODEV_MODULE_NAME "ODevESP8266-1" |
app code:
Code Block |
---|
const char* ssid = "--"; const char* password = "--" DHTPIN D2 // Pin which is connected to the DHT sensor. // Available types: DHT11, DHT22, DHT21 DHT_Unified dht(DHTPIN, DHT11); DHT_Unified::Temperature sTemp = dht.temperature(); DHT_Unified::Humidity sHumidity = dht.humidity(); void setup() { ODevSerial.enableDebugbegin(115200); ODev.name(ODEV_MODULE_NAME"ODevHT-01"); ODev.apiKey(ODEV_API_KEY"----APIKEY-----"); ODev.server("192.168.3.106")----SERVER IP-----"); dht.begin(); // Initialize device. ODev.addSensor("HT01_Temperature", new AdafruitSensor(sTemp)) ->setInterval(1 * (1000)); // 1sec Change Server ! ODev.addDeviceaddSensor("LEDHT01_Humidity", 2, Device::DIGITAL);new AdafruitSensor(sHumidity)) -- XXXXXXXXXXXX DHT>setInterval(1 * (1000)); // 1sec WiFi.mode(WIFI_AP_STA); WiFi.begin(ssid, password"---- WIFI----", ---- PASS----"); ODev.begin(); } void loop() { ODev.loop(); } |
HTML Comment |
---|
<b>teste XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</b> |
Next, upload code to the board and open up your serial monitor, you should see the data:IMAGEM DA SERIALLLLLLLLLLLLLLLLLLLLLLLLLL
Code Block |
---|
20:08:06.529 -> DB:WiFi :: Conneted
20:08:07.523 -> DB:SoftAP :: OK
20:08:07.523 -> DB:TCPServer :: OK
20:08:07.523 -> Mode: STA+AP
20:08:07.523 -> Auto connect: 1
20:08:07.523 -> SSID (10): xxxxxxxxxxx
20:08:07.523 -> Passphrase (10): xxxxxxxxxx
20:08:07.523 -> DB:IP STA:
20:08:07.523 -> 192.168.2.113
20:08:07.523 -> DB:IP softAP:
20:08:07.523 -> 192.168.4.1
20:08:07.523 -> DB:MQTT :: BEGIN
20:08:27.640 -> DB:MQTT :: connecting...
20:08:27.673 -> DB:MQTT :: [connected] |
Sending data to the cloud
...