DecentIoT Logo
DecentIoT Docs
Mqtt libraryMqtt examples

Sensor Monitoring

DHT sensor monitoring example with scheduled readings

Sensor Monitoring

This example demonstrates how to read sensor data and send it to your DecentIoT dashboard using scheduled tasks. We'll use a DHT22 temperature and humidity sensor.

Hardware Required

  • ESP8266 or ESP32 development board
  • DHT22 temperature and humidity sensor
  • 10kΩ pull-up resistor
  • Breadboard and jumper wires

Wiring

ESP8266/ESP32    DHT22
GPIO 4 (D2)  →   Data Pin
3.3V         →   VCC
GND          →   GND
GPIO 4 (D2)  →   10kΩ Resistor → 3.3V (pull-up)

Code

/*
  DecentIoT MQTT Library - Simple DHT Sensor Example
  
  Hardware:
  - DHT22 sensor on pin D4
  - LED on pin D6
  
  Virtual Pins:
  - P0: LED Control (receive)
  - P1: Temperature (send)
  - P2: Humidity (send)
*/

#include <DecentIoT.h>
#include <ESP8266WiFi.h>
#include <DHT.h>

// Configuration
#define MQTT_BROKER "your-mqtt-cluster-url"
#define MQTT_PORT 8883
#define MQTT_USERNAME "broker-access-username"
#define MQTT_PASSWORD "broker-access-pass"
#define PROJECT_ID "your-project-id"
#define USER_ID "your-user-id"
#define DEVICE_ID "your-device-id"
#define WIFI_SSID "your-wifi-name"
#define WIFI_PASS "wifi-password"

// Hardware pins
#define LED_PIN D6
#define DHT_PIN D4

// Create DHT sensor
DHT dht(DHT_PIN, DHT22);

// LED control from dashboard
DECENTIOT_RECEIVE(P0) {
    digitalWrite(LED_PIN, value);
    Serial.printf("[P0]LED: %s\n", value ? "ON" : "OFF");
}

// Send temperature every 10 seconds
DECENTIOT_SEND(P1, 10000) {
    float temp = dht.readTemperature();
    DecentIoT.write(P1, temp);
    Serial.printf("[P1] Temperature: %.1f°C\n", temp);
}

// Send humidity every 10 seconds
DECENTIOT_SEND(P2, 10000) {
    float humidity = dht.readHumidity();
    DecentIoT.write(P2, humidity);
    Serial.printf("[P2] Humidity: %.1f%%\n", humidity);
}

void setup() {
    Serial.begin(115200);
    
    // Initialize pins
    pinMode(LED_PIN, OUTPUT);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_PIN, LOW);
    
    // Initialize DHT sensor
    dht.begin();
    
    // Connect to WiFi
    WiFi.begin(WIFI_SSID, WIFI_PASS);
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("[WiFi] connected!");
    
    // Initialize DecentIoT
    DecentIoT.begin(MQTT_BROKER, MQTT_PORT, MQTT_USERNAME, MQTT_PASSWORD, PROJECT_ID, USER_ID, DEVICE_ID);
}

void loop() {
    DecentIoT.run();
    delay(10);
}

Dashboard Setup

  1. Create Project in DecentIoT web dashboard
  2. Add MQTT broker credentials
  3. Create device and datastreams
  4. Add Widgets:
    • P0: Button/Switch for LED control
    • P1: Gauge for temperature
    • P2: Gauge for humidity
  5. Save and monitor the data

How It Works

  1. Scheduled Tasks: DECENTIOT_SEND macros create recurring tasks
  2. Sensor Reading: DHT22 sensor provides temperature and humidity
  3. Virtual Pins: P0 for LED control, P1 for temperature, P2 for humidity
  4. Automatic Updates: Data sent every 10 seconds
  5. Automatic Processing: Library handles all communication seamlessly

Advanced Features

Error Handling

// Enhanced sensor reading with error handling
DECENTIOT_SEND(P1, 10000) {
  float temperature = dht.readTemperature();
  if (!isnan(temperature)) {
    DecentIoT.write(P1, temperature);
    Serial.printf("[P1] Temperature: %.1f°C\n", temperature);
  } else {
    // Send error status
    DecentIoT.write(P1, -999);
    Serial.println("[P1] Sensor error - check connections");
  }
}

Multiple Sensors

// Add more sensors to different virtual pins
DECENTIOT_SEND(P3, 5000) {
  int lightLevel = analogRead(A0);
  DecentIoT.write(P3, lightLevel);
  Serial.printf("[P3] Light Level: %d\n", lightLevel);
}

DECENTIOT_SEND(P4, 15000) {
  float pressure = readPressure(); // Your pressure sensor function
  DecentIoT.write(P4, pressure);
  Serial.printf("[P4] Pressure: %.2f hPa\n", pressure);
}

Custom Intervals

// Different reading intervals for different sensors
DECENTIOT_SEND(P1, 5000) {   // Temperature every 5 seconds
  float temp = dht.readTemperature();
  DecentIoT.write(P1, temp);
}

DECENTIOT_SEND(P2, 30000) {  // Humidity every 30 seconds
  float humidity = dht.readHumidity();
  DecentIoT.write(P2, humidity);
}

Troubleshooting

Sensor not reading:

  • Check wiring connections
  • Verify DHT22 pin assignment
  • Check pull-up resistor (10kΩ)
  • Wait 2 seconds after power-on for sensor initialization

Invalid readings:

  • Check power supply (3.3V)
  • Verify sensor is DHT22 (not DHT11)
  • Check for loose connections

No data on dashboard:

  • Verify virtual pin mapping (P1, P2)
  • Check MQTT connection status
  • Ensure dashboard widgets are configured correctly

Required Libraries

Install these libraries via Arduino Library Manager:

  1. DHT sensor library by Adafruit
  2. Adafruit Unified Sensor (dependency)

Next Steps