Connecting a DHT11 sensor to the cloud with an ESP8266-based board

Alex Stakhanov
Sep 6, 2019 · 5 min read
DHT11, NodeMCU (ESP8266) board


In the previous article, I connected my ESP8266-based NodeMCU board to a Cloud4RPi service. Now, it’s time for a real project!

Hardware requirements

Software and services

Goal: Measure temperature and humidity

I already had a DHT11 sensor, so I decided to use it for temperature and humidity measurements. Let’s choose an Arduino library to read sensor data.

Step 1: Create and configure project

I already described how to create a PlatformIO project and install libraries in the first part. My project is called “MyNodeMCU”. The structure is shown below:

PIO project structure
PIO project structure
PlatformIO Project Structure
default_envs = nodemcuv2

platform = espressif8266
framework = arduino
board = nodemcuv2

Step 2: Install libraries

Libraries installation is quite simple. You can do it from the IDE’s graphical interface, or by adding required library names to the lib_deps section of the file:

; ...lib_deps =
Adafruit Unified Sensor
DHT sensor library
build_flags =
PlatformIO Library Dependencies
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Cloud4RPi.h>
#include "DHT.h"

Step 3: Connect a DHT11 sensor

Adafruit provides a DHTtester.ino example of a sensor connection.

#define DHTPIN 2      // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
// ...struct DHT_Result {
float h;
float t;
DHT_Result dhtResult;
void readSensors() {
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
dhtResult.h = h;
dhtResult.t = t;

Step 4: Sending data to the cloud

Once we have that data, the next step is to send it to the Cloud4RPi service.

#if defined(CLOUD4RPI_TOKEN)
Cloud4RPi c4r("!!!_NO_DEVICE_TOKEN_!!!");
c4r.setVariable("DHT11_Temp", dhtResult.t);
c4r.setVariable("DHT11_Hum", dhtResult.h);

Step 5: Diagnostics

Cloud4RPi supports diagnostic data along with variable values. I used uptime, Wi-Fi signal strength, and IP address as diagnostic data:

c4r.declareDiagVariable("RSSI"); // WiFi signal strength
c4r.setDiagVariable("RSSI", (String)WiFi.RSSI() + " dBm");
c4r.setDiagVariable("IP_Address", WiFi.localIP().toString());
c4r.setDiagVariable("Uptime", uptimeHumanReadable(currentMillis));
String uptimeHumanReadable(unsigned long milliseconds) {
static char uptimeStr[32];
unsigned long secs = milliseconds / 1000;
unsigned long mins = secs / 60;
unsigned int hours = mins / 60;
unsigned int days = hours / 24;
secs -= mins * 60;
mins -= hours * 60;
hours -= days * 24;
sprintf(uptimeStr,"%d days %2.2d:%2.2d:%2.2d", (byte)days, (byte)hours, (byte)mins, (byte)secs);
return String(uptimeStr);

Step 6: Start and debug the project

After compiling the created code and flashing it into NodeMCU, the device connects to a cloud service and starts sending data.

Cloud4RPi Device Page

Step 7: Dashboard configuration

At this step, the data connection to the cloud is operational. Now, let’s configure the visual representation of the data.

Cloud4RPi Control Panel


The full project’s code is available in the gist.

Bonus pics:

VSCode + PlatformIO Serial
NodeMCU and Cloud4RPi Control Panel


Cloud control panel for your IoT projects