Sending DHT11 Sensor Data to Consentium IoT Cloud

This documentation explains how to read data from a DHT11 sensor using an digital pin and send the data to the Consentium IoT cloud using the ConsentiumThings library. The code also facilitates Over-The-Air (OTA) firmware updates for easy firmware management.

Dependencies

The code leverages the ConsentiumThings.h library for communication with the Consentium IoT platform and the DHT.h library for reading data from the DHT11 sensor.

Features

  • Connect to a WiFi network to send data to the cloud.

  • Read temperature and humidity from the DHT11 sensor.

  • Send sensor data to the Consentium IoT platform.

  • Enable Over-The-Air (OTA) firmware updates.

  • Automatically check for updates after 100 loop cycles.

Constants

  • Firmware Version: The firmware version of the board is set to 0.1.

  • Interval: The main loop runs every 5 seconds (5000 ms).

  • Update Interval: The firmware checks for updates after 100 cycles of the main loop.

Code Overview

1. Initialization

The program begins by creating a ConsentiumThingsDalton object to manage communication with the Consentium IoT platform. The initialization requires the firmware version, WiFi credentials, and API keys.

#define FIRMWARE_VERSION "0.1"
ConsentiumThingsDalton board(FIRMWARE_VERSION);

The following variables are set for WiFi credentials and API keys (hidden for security):

const char *ssid = "YourWiFiSSID";  // WiFi SSID
const char *pass = "YourWiFiPassword";  // WiFi password

Additionally, a DHT object is initialized to interface with the DHT11 sensor:

#include <DHT.h>
#define DHTPIN 2    // Pin where the DHT11 is connected
#define DHTTYPE DHT11   // DHT 11 sensor type
DHT dht(DHTPIN, DHTTYPE);

2. Setup

The setup() function initializes the WiFi connection, data transmission, OTA updates, and the DHT11 sensor.

void setup() {
  dht.begin();  // Initialize DHT11 sensor
  board.initWiFi(ssid, pass);  // Begin WiFi connection
  board.beginSend(SendApiKey, BoardApiKey);  // Initialize data transmission to cloud
  board.beginOTA(ReceiveApiKey, BoardApiKey);  // Enable OTA updates
}

3. Main Loop

The loop() function reads the temperature and humidity from the DHT11 sensor, and then sends this data to the Consentium IoT platform.

a. Reading Data from DHT11

The temperature and humidity values are read using the dht.readTemperature() and dht.readHumidity() functions.

float temperature = dht.readTemperature();  // Read temperature in Celsius
float humidity = dht.readHumidity();  // Read humidity in percentage

b. Sending Sensor Data

The temperature and humidity data are sent to the Consentium IoT cloud using the sendData function. The code is designed to handle multiple sensor values.

double sensorValues[] = {temperature, humidity};  // sensor data array
const char* sensorInfo[] = {"Temperature", "Humidity"};  // sensor information array
int sensorCount = sizeof(sensorValues)/sizeof(sensorValues[0]);  // number of sensors

board.sendData(sensorValues, sensorInfo, sensorCount, LOW_PRE);  // Send sensor data

c. OTA Updates

The firmware checks for available updates every 100 cycles. If an update is available, it will automatically perform the update.

if (loopCounter >= updateInterval) {
  board.checkAndPerformUpdate();  // Check and perform OTA updates
  loopCounter = 0;  // Reset the loop counter
}

4. Loop Control

The loop repeats every 5000 milliseconds (5 seconds), allowing for periodic data sending and firmware update checks.

delay(interval);

Error Handling

If the sensor fails to read valid data, you can add error checking to ensure the system handles faulty sensor readings gracefully.

if (isnan(temperature) || isnan(humidity)) {
  Serial.println("Failed to read from DHT sensor!");
  return;  // Exit loop early if reading fails
}

6. Final code

#include <ConsentiumThings.h>
#include <DHT.h>

#define FIRMWARE_VERSION "0.1"
#define DHTPIN 2    // Pin where the DHT11 is connected
#define DHTTYPE DHT11   // DHT 11 sensor type

DHT dht(DHTPIN, DHTTYPE);  // Create DHT object

ConsentiumThingsDalton board(FIRMWARE_VERSION); // Create ConsentiumThings object with firmware version

const char *ssid = "YourWiFiSSID"; // Add WiFi SSID
const char *pass = "YourWiFiPassword"; // Add WiFi password
constexpr int interval = 5000; // Wait for 5 seconds between loops
const char *SendApiKey = "YourSendApiKey"; // Send API key
const char *ReceiveApiKey = "YourReceiveApiKey"; // Receive API key
const char *BoardApiKey = "YourBoardApiKey"; // Board API key
const int updateInterval = 100; // Check for update after 100 cycles

int loopCounter = 0; // Counter to keep track of loop cycles

void setup() {
  Serial.begin(9600);  // Initialize serial communication for debugging
  dht.begin();  // Initialize DHT11 sensor
  board.initWiFi(ssid, pass); // Begin WiFi connection
  board.beginSend(SendApiKey, BoardApiKey); // Initialize data transmission to cloud
  board.beginOTA(ReceiveApiKey, BoardApiKey); // Enable OTA updates
}

void loop() {
  // Reading temperature and humidity from the DHT11 sensor
  float temperature = dht.readTemperature();  // Read temperature in Celsius
  float humidity = dht.readHumidity();  // Read humidity in percentage

  // Check if any readings failed and exit the loop if so
  if (isnan(temperature) || isnan(humidity)) {
    Serial.println("Failed to read from DHT sensor!");
    return;  // Exit the loop early if reading fails
  }

  // Prepare sensor values and info arrays
  double sensorValues[] = {temperature, humidity};  // Sensor data array
  const char* sensorInfo[] = {"Temperature", "Humidity"};  // Sensor information array

  int sensorCount = sizeof(sensorValues)/sizeof(sensorValues[0]); // Number of sensors

  // Send sensor data to the Consentium IoT cloud
  board.sendData(sensorValues, sensorInfo, sensorCount, LOW_PRE); // Send data with precision

  // Increment loop counter for OTA check
  loopCounter++;
  if (loopCounter >= updateInterval) {
    board.checkAndPerformUpdate(); // Check and perform OTA updates
    loopCounter = 0;  // Reset the counter after checking for an update
  }

  delay(interval); // Wait for the next cycle
}

Summary

This code reads temperature and humidity data from a DHT11 sensor and sends it to the Consentium IoT cloud for monitoring. The system also checks for OTA updates, ensuring the firmware remains up-to-date. The design is flexible, allowing for multiple sensors to be added easily.

Last updated