Sending DHT11 Sensor Data to Consentium IoT Cloud
Last updated
#define FIRMWARE_VERSION "0.1"
ConsentiumThingsDalton board(FIRMWARE_VERSION);const char *ssid = "YourWiFiSSID"; // WiFi SSID
const char *pass = "YourWiFiPassword"; // WiFi password#include <DHT.h>
#define DHTPIN 2 // Pin where the DHT11 is connected
#define DHTTYPE DHT11 // DHT 11 sensor type
DHT dht(DHTPIN, DHTTYPE);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
}float temperature = dht.readTemperature(); // Read temperature in Celsius
float humidity = dht.readHumidity(); // Read humidity in percentagevector <double> sensorValues[] = {temperature, humidity}; // sensor data array
const char* sensorInfo[] = {"Temperature", "Humidity"}; // sensor information array
board.sendData(sensorValues, sensorInfo, sensorCount, LOW_PRE); // Send sensor dataif (loopCounter >= updateInterval) {
board.checkAndPerformUpdate(); // Check and perform OTA updates
loopCounter = 0; // Reset the loop counter
}delay(interval);if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return; // Exit loop early if reading fails
}#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
vector <double> sensorValues[] = {temperature, humidity}; // Sensor data array
const char* sensorInfo[] = {"Temperature", "Humidity"}; // Sensor information array
// 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
}