Google Firebase-Controlling LED Using Android App: Saaqib Shaikh 9502
Google Firebase-Controlling LED Using Android App: Saaqib Shaikh 9502
Theory:
Using the IoT hardware & cloud platform, we can control the IoT devices including LEDs
from any part of the world. This program deals with LED Control using Google Firebase
Console & NodeMCU ESP8266 wifi Module. There are various methods of controlling of the
LED such as using Web Server or Webpage, Blynk Application and using other API based
managing,and modifying data generated from any android/IOS application, web services, IoT
sensors & Hardware. FirebaseArduino is a library to simplify connecting to the Firebase database
from Arduino clients. It is a full abstraction of Firebase’s REST API exposed through C++ calls
in a wiring friendly way. All JSON parsing is handled by the library and you may deal in pure
C/Arduino types.The library cannot work standalone. So you need to add the ArduinoJSON
library as well.
To Control the LED using Google Firebase & Nodemcu ESP8266, you need to setup the
Google Firebase first.
Code:
#include <ESP8266WiFi.h>
#include <Firebase_ESP_Client.h>
/* 4. Define the user Email and password that already registered or added in your project */
#define USER_EMAIL "[email protected]"
#define USER_PASSWORD "iotexp"
FirebaseAuth auth;
FirebaseConfig config;
const int ledPin = 18; // Ensure the pin number is valid for ESP8266
void setup()
{
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
// Comment or pass false value when WiFi reconnection will control by your code or third-party
library e.g. WiFiManager
Firebase.reconnectWiFi(true);
// Since v4.4.x, BearSSL engine was used, the SSL buffer needs to be set.
// Large data transmission may require larger RX buffer; otherwise, connection issues or data
read timeouts can occur.
fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer
size in bytes from 512 - 16384 */);
Firebase.begin(&config, &auth);
Firebase.setDoubleDigits(5);
config.timeout.serverResponse = 10 * 1000;
}
void loop()
{
// Firebase.ready() should be called repeatedly to handle authentication tasks.
if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
int ledState;
if (Firebase.RTDB.getInt(&fbdo, "/led/state", &ledState))
{
digitalWrite(ledPin, ledState);
}
else
{
Serial.println(fbdo.errorReason().c_str());
}
}
}
Arduino Screenshot:
Firebase Screenshot:
Post-Lab Questions:
1. Explain the changes required to connect sensor data using Google firebase.
2. Explain the Firebase functions used to connect Nodemcu to Firebase.