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.
Additionally, a DHT object is initialized to interface with the DHT11 sensor:
#include<DHT.h>#defineDHTPIN2 // Pin where the DHT11 is connected#defineDHTTYPE DHT11 // DHT 11 sensor typeDHTdht(DHTPIN,DHTTYPE);
2. Setup
The setup() function initializes the WiFi connection, data transmission, OTA updates, and the DHT11 sensor.
voidsetup() {dht.begin(); // Initialize DHT11 sensorboard.initWiFi(ssid, pass); // Begin WiFi connectionboard.beginSend(SendApiKey, BoardApiKey); // Initialize data transmission to cloudboard.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 Celsiusfloat 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.
vector <double> sensorValues[] = {temperature, humidity}; // sensor data arrayconstchar* sensorInfo[] = {"Temperature","Humidity"}; // sensor information arrayboard.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>#defineFIRMWARE_VERSION"0.1"#defineDHTPIN2 // Pin where the DHT11 is connected#defineDHTTYPE DHT11 // DHT 11 sensor typeDHTdht(DHTPIN,DHTTYPE); // Create DHT objectConsentiumThingsDaltonboard(FIRMWARE_VERSION); // Create ConsentiumThings object with firmware versionconstchar*ssid ="YourWiFiSSID"; // Add WiFi SSIDconstchar*pass ="YourWiFiPassword"; // Add WiFi passwordconstexprint interval =5000; // Wait for 5 seconds between loopsconstchar*SendApiKey ="YourSendApiKey"; // Send API keyconstchar*ReceiveApiKey ="YourReceiveApiKey"; // Receive API keyconstchar*BoardApiKey ="YourBoardApiKey"; // Board API keyconstint updateInterval =100; // Check for update after 100 cyclesint loopCounter =0; // Counter to keep track of loop cyclesvoidsetup() {Serial.begin(9600); // Initialize serial communication for debuggingdht.begin(); // Initialize DHT11 sensorboard.initWiFi(ssid, pass); // Begin WiFi connectionboard.beginSend(SendApiKey, BoardApiKey); // Initialize data transmission to cloudboard.beginOTA(ReceiveApiKey, BoardApiKey); // Enable OTA updates}voidloop() { // Reading temperature and humidity from the DHT11 sensorfloat temperature =dht.readTemperature(); // Read temperature in Celsiusfloat humidity =dht.readHumidity(); // Read humidity in percentage // Check if any readings failed and exit the loop if soif (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 vector <double> sensorValues[] = {temperature, humidity}; // Sensor data arrayconstchar* sensorInfo[] = {"Temperature","Humidity"}; // Sensor information array // Send sensor data to the Consentium IoT cloudboard.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.