Firebase libraryFirebase examples
Sensor Monitoring
DHT sensor monitoring example with Firebase real-time synchronization
Sensor Monitoring
This example demonstrates how to read sensor data and send it to your DecentIoT dashboard using Firebase real-time synchronization. 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 Firebase 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>
// Firebase credentials
#define FIREBASE_URL "your-firebase-url"
#define FIREBASE_AUTH "your-web-api-key"
#define AUTH_EMAIL "your-auth-email"
#define AUTH_PASS "your-auth-password"
#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(FIREBASE_URL, FIREBASE_AUTH, PROJECT_ID, USER_ID, DEVICE_ID, AUTH_EMAIL, AUTH_PASS);
}
void loop() {
DecentIoT.run();
delay(10);
}
Dashboard Setup
- Create Project in DecentIoT web dashboard
- Add Firebase project credentials
- Create device and datastreams
- Add Widgets:
- P0: Button/Switch for LED control
- P1: Gauge for temperature
- P2: Gauge for humidity
- Save and monitor the data
How It Works
- Scheduled Tasks:
DECENTIOT_SEND
macros create recurring tasks - Sensor Reading: DHT22 sensor provides temperature and humidity
- Virtual Pins: P0 for LED control, P1 for temperature, P2 for humidity
- Real-time Sync: Firebase automatically syncs data to dashboard
- Automatic Processing: Library handles all communication seamlessly
Firebase Real-time Features
Automatic Synchronization
Firebase provides real-time synchronization, so your dashboard automatically updates when new data arrives:
// Data is automatically synced to dashboard
DecentIoT.write("P1", temperature);
// Dashboard updates immediately without additional code
Automatic Processing
The library automatically handles all communication:
DECENTIOT_SEND(P1, 10000) {
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
DecentIoT.write("P1", temperature);
// Library automatically processes all communication
}
}
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 Firebase connection status
- Ensure dashboard widgets are configured correctly
- Check Firebase database rules
Instructions not received:
- Ensure
DecentIoT.run()
is called regularly in your main loop - Check Firebase authentication
- Verify database rules allow read/write access
Required Libraries
Install these libraries via Arduino Library Manager:
- DHT sensor library by Adafruit
- Adafruit Unified Sensor (dependency)
Next Steps
- Advanced Control Example - Multiple sensors and actuators
- MQTT Examples - Alternative to Firebase
- Scheduling Guide - Advanced scheduling features