Firebase libraryFirebase examples
Simple LED Control
Basic LED control example using DecentIoT Firebase Library
Simple LED Control
This example demonstrates the most basic usage of the DecentIoT Firebase Library - controlling an LED from your web dashboard with real-time synchronization.
Hardware Required
- ESP8266 or ESP32 development board
- LED (any color)
- 220Ω resistor
- Breadboard and jumper wires
Wiring
ESP8266/ESP32 LED Circuit
GPIO 2 → LED (+) → 220Ω Resistor → GND
Code
#include <DecentIoT.h>
#include <WiFi.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"
// WiFi credentials
#define WIFI_SSID "your-wifi-ssid"
#define WIFI_PASS "your-wifi-password"
// Pin definitions
const int ledPin = 2;
// LED state handler
DECENTIOT_RECEIVE(P0) {
digitalWrite(ledPin, value);
Serial.printf("[P0] LED state = %s\n", value ? "ON" : "OFF");
}
void setup() {
Serial.begin(115200);
Serial.println("\n--- Initializing DecentIoT Device ---");
// Initialize LED pin
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Ensure LED is off initially
// 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 (Firebase connection)
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 Widget: Toggle switch named
led1
- Map Widget to virtual pin
P0
- Save and test the toggle
How It Works
- Virtual Pin P0 receives commands from the dashboard
- DECENTIOT_RECEIVE(P0) macro handles incoming commands
- LED state changes based on the received value
- Real-time sync ensures dashboard reflects current state
- Automatic processing handles all communication seamlessly
Firebase vs MQTT Differences
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 |
Testing
- Upload the code to your ESP8266/ESP32
- Open Serial Monitor (115200 baud)
- Wait for "WiFi Connected!" and "DecentIoT Initialized!" messages
- Go to your DecentIoT dashboard
- Toggle the LED switch
- Watch the LED turn on/off and see the serial output
Troubleshooting
LED not responding:
- Check wiring connections
- Verify GPIO pin number (D4 = GPIO 2)
- Check Serial Monitor for connection status
Connection issues:
- Verify WiFi credentials
- Check Firebase project settings
- Ensure authentication is enabled
- Check database rules
Authentication errors:
- Verify email/password credentials
- Check Firebase Authentication settings
- Ensure user account exists
Next Steps
- Sensor Monitoring Example - Add sensor readings
- Advanced Control Example - Multiple devices
- MQTT Examples - Alternative to Firebase