Firebase libraryFirebase examples
Advanced Control
Multiple sensors and actuators with Firebase real-time synchronization
Advanced Control
This example demonstrates a complete IoT system with multiple virtual pins, LED control, and sensor simulation using Firebase real-time synchronization. Perfect for learning the virtual pin architecture and testing your setup.
Hardware Required
- ESP8266 or ESP32 development board
- LED (any color)
- RGB LED or PWM-controlled LED
- 220Ω resistor for LED
- Breadboard and jumper wires
Wiring
ESP8266/ESP32 Components
GPIO 12 (D6) → LED (+) → 220Ω → GND
GPIO 13 (D7) → RGB LED (PWM control)
3.3V → LED VCC
GND → LED GND
Code
#include <Arduino.h>
#include <DecentIoT.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-ssid"
#define WIFI_PASS "your-wifi-password"
const int ledPin = D6; // D6 = GPIO12 on ESP8266
const int rgbPin = D7; // D7 = GPIO13 on ESP8266
// LED state handler
DECENTIOT_RECEIVE(P0) {
digitalWrite(ledPin, value);
Serial.printf("[P0] LED state = %s\n", value ? "ON" : "OFF");
}
DECENTIOT_SEND(P1, 60*1000) {
// Generate random temperature value between 0-100
int temperature = random(0, 101); // random(min, max) generates numbers from min to max-1
DecentIoT.write(P1, temperature); // Send temperature to Firebase
Serial.printf("[P1] Temperature = %d\n", temperature );
}
DECENTIOT_SEND(P2, 60*1000) {
// Generate random humidity value between 0-100
int humidity = random(0, 101); // random(min, max) generates numbers from min to max-1
DecentIoT.write(P2, humidity); // Send humidity to Firebase
Serial.printf("[P2] Humidity = %d\n", humidity );
}
DECENTIOT_RECEIVE(P3) {
int sliderValue = value;
int pwmValue = sliderValue * 255 / 100;
analogWrite(rgbPin, pwmValue);
Serial.printf("[P3] RGB Brightness = %d (slider: %d)\n", pwmValue, sliderValue);
}
void setup() {
Serial.begin(115200);
Serial.println("\n--- Initializing DecentIoT Device ---");
// Initialize LED pin
pinMode(ledPin, OUTPUT);
pinMode(rgbPin, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is off initially
digitalWrite(LED_BUILTIN, LOW);
// User handles WiFi connection
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
Serial.println("WiFi connected!");
digitalWrite(LED_BUILTIN, HIGH);
// Now start DecentIoT (cloud connection)
DecentIoT.begin(FIREBASE_URL, FIREBASE_AUTH, PROJECT_ID, USER_ID, DEVICE_ID, AUTH_EMAIL, AUTH_PASS);
}
void loop() {
// User checks WiFi and reconnects if needed
if (WiFi.status() != WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("[WiFi] Disconnected! Reconnecting...");
WiFi.disconnect();
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Optionally: wait for connection, show status, etc.
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(".");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi reconnected! Restarting device...");
delay(1000);
ESP.restart();
}
}
DecentIoT.run();
delay(10);
}
// Virtual Pin Mappings:
// P0 - LED Control (bool)
// P1 - Temperature (int) - Random values for demo
// P2 - Humidity (int) - Random values for demo
// P3 - RGB Brightness Control (int) - PWM slider
Dashboard Setup
- Create Project in DecentIoT web dashboard
- Add Widgets:
- Switch named
led_control
(maps to P0) - Gauge named
temperature
(maps to P1, range 0-100) - Gauge named
humidity
(maps to P2, range 0-100) - Slider named
rgb_brightness
(maps to P3, range 0-100)
- Switch named
- Configure Layout:
- Group controls together
- Group sensors together
- Save and test all functions
How It Works
Virtual Pin Architecture
Pin | Function | Type | Description |
---|---|---|---|
P0 | LED Control | bool | Manual LED on/off control |
P1 | Temperature | int | Simulated temperature (0-100) |
P2 | Humidity | int | Simulated humidity (0-100) |
P3 | RGB Brightness | int | PWM brightness control (0-100) |
System Features
- LED Control: P0 provides simple on/off control
- Sensor Simulation: P1 and P2 send random values every 60 seconds
- PWM Control: P3 provides brightness control with slider
- WiFi Reconnection: Automatic WiFi reconnection with device restart
- Real-time Sync: Firebase automatically syncs all data
- Automatic Processing: Library handles all communication seamlessly
Firebase Real-time Synchronization
// Data is automatically synced to dashboard
DecentIoT.write("P1", temperature);
// Dashboard updates immediately
// Library automatically handles all communication
Advanced Features
Conditional Control
// Only send data when system is enabled
DECENTIOT_SEND(P1, 10000) {
if (systemEnabled) {
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
DecentIoT.write("P1", temperature);
}
}
// Library automatically handles communication
}
Error Handling
// Enhanced error handling
DECENTIOT_SEND(P1, 10000) {
if (systemEnabled) {
float temperature = dht.readTemperature();
if (!isnan(temperature)) {
DecentIoT.write("P1", temperature);
Serial.printf("[P1] Temperature: %.1f°C\n", temperature);
} else {
DecentIoT.write("P1", -999); // Error value
Serial.println("[P1] Sensor error - check connections");
}
}
// Library automatically handles communication
}
Firebase vs MQTT Comparison
Feature | Firebase Library | MQTT Library |
---|---|---|
Real-time Sync | Automatic | Manual |
Connection | Firebase Realtime DB | MQTT Broker |
Authentication | Firebase Auth | MQTT Credentials |
Processing | Automatic | Automatic |
Setup | Firebase project required | MQTT broker required |
Cost | Firebase free tier | Broker hosting costs |
Offline Support | Built-in | Limited |
Troubleshooting
System not responding:
- Check system status switch (P0) in dashboard
- Verify all wiring connections
- Check Serial Monitor for error messages
Sensors not reading:
- Verify sensor connections
- Check power supply (3.3V)
- Ensure pull-up resistors are connected
Controls not working:
- Check system is enabled (P0)
- Verify output pin assignments
- Check relay module connections
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
- MQTT Examples - Alternative to Firebase
- Scheduling Guide - Advanced scheduling features
- API Reference - Complete method documentation