0% found this document useful (0 votes)
10 views

04 Connecting NodeMCU to Firebase

The document provides an overview of connecting NodeMCU ESP8266 to Firebase for IoT applications, specifically detailing a smart umbrella that informs users about weather conditions. It covers the setup of Wi-Fi modes, Firebase project creation, database structure, and code examples for sending and reading data from Firebase. Additionally, it includes references for further learning about the ESP8266 and Firebase integration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

04 Connecting NodeMCU to Firebase

The document provides an overview of connecting NodeMCU ESP8266 to Firebase for IoT applications, specifically detailing a smart umbrella that informs users about weather conditions. It covers the setup of Wi-Fi modes, Firebase project creation, database structure, and code examples for sending and reading data from Firebase. Additionally, it includes references for further learning about the ESP8266 and Firebase integration.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Internet of Things

Connecting NodeMCU to Firebase


IoT Team, BFCAI
NodeMCU ESP8266

▪ NodeMCU is a low-cost open-source IoT platform based on the ESP8266


Wi-Fi system on a chip.
▪ NodeMCU Version 3 runs on the ESP-12E (ESP8266MOD) module.
IoT Application: Smart Umbrella

▪ An umbrella that provides information about the likelihood of rain so


that users can make a simple decision about whether to take the
umbrella with them as they leave their home.
IoT Application: Smart Umbrella

▪ The umbrella has a handle that would illuminate when snow or rain
was in the forecast.
IoT Application: Smart Umbrella

▪ Using existing Wi-Fi technology to pull information about the


weather from the Internet.
NodeMCU ESP8266: WiFi Modes

▪ The NodeMCU WiFi can run in three modes: WiFi Station, Access Point,
or both at the same time.
▪ To set the NodeMCU WiFi mode, you can use the WiFi.mode() function.
WiFi Mode Description
Station Mode (STA)
WiFi.mode(WIFI_STA);
NodeMCU connects to other networks
Access Point Mode (AP)
WiFi.mode(WIFI_AP); NodeMCU creates its own network, and
other WiFi stations can connect to it
Access Point + Station Mode (AP_STA)
WiFi.mode(WIFI_AP_STA); NodeMCU WiFi will act as both Access
Point and WiFi Station at the same time
NodeMCU ESP8266: Access Point Mode
NodeMCU ESP8266: WiFi Station Mode
NodeMCU ESP8266: WiFi Station Mode – Code
#include <ESP8266WiFi.h> // Include ESP8266WiFi library for WiFi features

const char* WIFI_SSID = "iotlab"; // Define the WiFi network SSID


const char* WIFI_PASS = "hostiotlab"; // Define the WiFi network password

void setup() {
Serial.begin(115200); // Start serial communication at 115200 baudrate
WiFi.begin(WIFI_SSID, WIFI_PASS); // Begin WiFi connection using SSID and password

while(WiFi.status() != WL_CONNECTED){ // Check if the WiFi status is not connected


delay(1000); // Wait until the WiFi connection is established
Serial.println("Connecting to WiFi..."); // Print message indicating an attempt to connect to WiFi
}

Serial.println("Connected to WiFi."); // Print message when WiFi connection is successful


Serial.print("IP Address: "); // Print the label for the IP address
Serial.println(WiFi.localIP()); // Print the assigned IP address
}

void loop() {

}
NodeMCU ESP8266: WiFi Station Mode – Output
Firebase

▪ Firebase is Google’s mobile and web application development platform


that includes many services to manage data from IOS, Android, or web
applications.
▪ This includes things like analytics, authentication, databases,
configuration, file storage, push messaging, and the list goes on.
▪ The services are hosted in the cloud and scale with little to no effort on the
part of the developer.
▪ You can use the ESP8266 to connect and interact with your Firebase
project, and you can create applications to control the ESP8266 via
Firebase from anywhere in the world.
Firebase

▪ The ESP8266 can interact with the database from anywhere in the world.
▪ You can have two ESP8266 boards in different networks, with one board
storing data and the other board reading the most recent data, for example.
Firebase

▪ You can have a web or mobile app using Firebase that will use ESP8266
to display sensor readings or control outputs from anywhere in the world.
Firebase: Creating a New Project

▪ Go to https://firebase.google.com
Firebase: Creating a New Project

▪ Click on Go to Console.
Firebase: Creating a New Project

▪ Click on Create a Project.


Firebase: Creating a New Project

▪ Enter the name of your project, accept terms, and click Continue.
Firebase: Creating a New Project

▪ Click Continue.
Firebase: Creating a New Project

▪ Choose the location, accept terms, and click Create Project.


Firebase: Creating a New Project

▪ Wait until the project is created, and click Continue.


Firebase: Creating a New Project

▪ You’ll be redirected to your project console page.


Firebase: Creating a Realtime Database

▪ Expand Build tap, and go to Realtime Database.


Firebase: Creating a Realtime Database

▪ Click on Create Database.


Firebase: Creating a Realtime Database

▪ Select your database location, and click Next.


Firebase: Creating a Realtime Database

▪ For testing purposes, select Start in test mode, and click Enable.
Firebase: Creating a Realtime Database

▪ Your database is now created. You need to copy the database URL.
Firebase: Getting Database Secret

▪ Goto Project settings.


Firebase: Getting Database Secret

▪ Click on Service accounts.


Firebase: Getting Database Secret

▪ Click on Database secrets.


Firebase: Getting Database Secret

▪ Click on Show.
Firebase: Getting Database Secret

▪ Copy the Secret.


Firebase: Structure Your Database

▪ All Firebase Realtime Database data is stored as JSON objects.


▪ You can think of the database as a cloud-hosted JSON tree.
▪ Unlike a SQL database, there are no tables or records.
▪ When you add data to the JSON tree, it becomes a node in the existing
JSON structure with an associated key.
{
"smartHome": {
"kitchen": {
"light": "off",
"temperature": 20
},
"bedroom": {
"light": "on",
"temperature": 18
}
}
}
Firebase: Structure Your Database
{
"smartHome": {
"devices": {
"light": {
"status": "on",
"brightness": 75
},
"thermostat": {
"temperature": 22,
"mode": "auto"
},
"door": {
"status": "closed"
}
},
"security": {
"alarm": {
"status": "armed"
},
"camera": {
"status": "active"
}
}
}
}
Firebase: Adding Nodes to Your Database

▪ Add a new node.


Firebase: Adding Nodes to Your Database

▪ Enter node key, value, choose its data type, and click Add.
Firebase: Adding Nodes to Your Database

▪ Add a second node.


Firebase: Adding Nodes to Your Database

▪ Now, we have two nodes.


FirebaseESP8266 Library

▪ The FirebaseESP8266 library provides Firebase Realtime database and


Firebase Cloud Messaging functions and supports ESP8266 MCU.
FirebaseESP8266 Library: Installation

▪ Download the ZIP file from he following link


https://www.arduinolibraries.info/libraries/firebase-esp8266-client
FirebaseESP8266 Library: Installation

▪ Select Sketch → Include Library → Add .ZIP Library...


FirebaseESP8266 Library: Installation

▪ Choose Firebase_ESP8266_Client-4.3.19.zip that previously downloaded.


Sending Data to Firebase: NodeMCU ESP8266 Pinout
PIN GPIO Why Not Safe?
HIGH at boot
D0 GPIO16
Used to wake up from deep sleep
D1 GPIO5 -
D2 GPIO4 -
Connected to FLASH button
D3 GPIO0
Boot fails if pulled LOW
HIGH at boot
D4 GPIO2
Boot fails if pulled LOW
D5 GPIO14 -
D6 GPIO12 -
D7 GPIO13 -
Required for boot
D8 GPIO15
Boot fails if pulled HIGH
Sending Data to Firebase: DHT11 – Circuit
Sending Data to Firebase: DHT11 – Steps

1. Connect breadboard power (+) and ground (-) rails to NodeMCU


VIN and ground (GND), respectively.
Sending Data to Firebase: DHT11 – Steps

2. Plug the DHT11 sensor into the breadboard.


Sending Data to Firebase: DHT11 – Steps

3. The sensor GND pin connects to the ground on NodeMCU.


Sending Data to Firebase: DHT11 – Steps

4. The sensor Power pin connects to the VCC on NodeMCU.


Sending Data to Firebase: DHT11 – Steps

5. Wire up the sensor Data pin to the analog pin D5 on NodeMCU.


Sending Data to Firebase: Code
#include <ESP8266WiFi.h> // Include ESP8266WiFi library for WiFi features
#include <FirebaseESP8266.h> // Include FirebaseESP8266 library for Firebase integration
#include "DHT.h" // Include DHT sensor library
#define DHT_PIN D5 // Define the digital pin connected to the DHT sensor
DHT dht(DHT_PIN, DHT11); // Initialize DHT sensor object with pin and sensor type

const char* WIFI_SSID = "iotlab"; // Define the WiFi network SSID


const char* WIFI_PASS = "hostiotlab"; // Define the WiFi network password

// Firebase Realtime Database URL and secret


const char* FIREBASE_HOST = "smart-home-6f8b2-default-rtdb.firebaseio.com";
const char* FIREBASE_AUTH = "pwfOlfS3G02LEVpJQEs8KqLp6sdh18ePRfzdJaba";

FirebaseData fbdo; // Define Firebase Data object


Sending Data to Firebase: Code
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baudrate
WiFi.begin(WIFI_SSID, WIFI_PASS); // Begin WiFi connection using SSID and password

while(WiFi.status() != WL_CONNECTED){ // Check if the WiFi status is not connected


delay(1000); // Wait 1 second between WiFi connection checks
Serial.println("Connecting to WiFi..."); // Print message indicating an attempt to connect to WiFi
}

Serial.println("Connected to WiFi."); // Print message when WiFi connection is successful


Serial.print("IP Address: "); // Print the label for the IP address
Serial.println(WiFi.localIP()); // Print the assigned IP address

dht.begin(); // Start DHT sensor


Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // Initialize Firebase connection
Firebase.reconnectWiFi(true); // Automatic reconnection to WiFi if connection is lost
}
Sending Data to Firebase: Code
void loop() {
delay(2000); // Wait 2 seconds between measurements
float temp = dht.readTemperature(); // Read temperature in Celsius

if(Firebase.setFloat(fbdo, "/temp", temp)){ // Set temperature value in the Firebase under the "/temp" path
Serial.print("Temperature: "); // Print the label "Temperature: "
Serial.print(temp); // Print the temperature value
Serial.println("°C "); // Print the unit "°C"
}
else // If Firebase operation fails,
Serial.println(fbdo.errorReason()); // Print the error reason
}
Sending Data to Firebase: Output
Reading Data from Firebase: Controlling an LED from Anywhere
Reading Data from Firebase: Code
#include <ESP8266WiFi.h> // Include ESP8266WiFi library for WiFi features

#include <FirebaseESP8266.h> // Include FirebaseESP8266 library for Firebase integration

#define LED_PIN D6 // Define the LED pin

const char* WIFI_SSID = "iotlab"; // Define the WiFi network SSID

const char* WIFI_PASS = "hostiotlab"; // Define the WiFi network password

// Firebase Realtime Database URL and secret

const char* FIREBASE_HOST = "smart-home-6f8b2-default-rtdb.firebaseio.com";

const char* FIREBASE_AUTH = "pwfOlfS3G02LEVpJQEs8KqLp6sdh18ePRfzdJaba";

FirebaseData fbdo; // Define Firebase Data object

String led_status; // A variable to store LED status


Reading Data from Firebase: Code
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baudrate
WiFi.begin(WIFI_SSID, WIFI_PASS); // Begin WiFi connection using SSID and password
pinMode(LED_PIN, OUTPUT); // Initialize the pin D6 as an output

while(WiFi.status() != WL_CONNECTED){ // Check if the WiFi status is not connected


delay(1000); // Wait 1 second between WiFi connection checks
Serial.println("Connecting to WiFi..."); // Print message indicating an attempt to connect to WiFi
}

Serial.println("Connected to WiFi."); // Print message when WiFi connection is successful


Serial.print("IP Address: "); // Print the label for the IP address
Serial.println(WiFi.localIP()); // Print the assigned IP address

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // Initialize Firebase connection


Firebase.reconnectWiFi(true); // Automatic reconnection to WiFi if connection is lost
}
Reading Data from Firebase: Code
void loop() {
if(Firebase.getString(fbdo, "/led")){ // Try to get the LED status from the Firebase
led_status = fbdo.to<String>(); // Get the LED status from the Firebase

if(led_status == "on"){ // If LED status is "on",


digitalWrite(LED_PIN, HIGH); // Turn on LED
Serial.println("LED is on."); // Print LED status
}
else if(led_status == "off"){ // If LED status is "off",
digitalWrite(LED_PIN, LOW); // Turn off LED
Serial.println("LED is off."); // Print LED status
}
}
else // If Firebase operation fails,
Serial.println(fbdo.errorReason()); // Print the error reason

delay(1000); // Check Firebase for LED status every 1 second


}
Reading Data from Firebase: Output
Firebase ESP Client Library

▪ The Firebase ESP8266 library provides Firebase Realtime database and


Firebase Cloud Messaging functions and supports only ESP8266 MCU.
Firebase ESP8266 - GitHub
Firebase ESP32 - GitHub
▪ If you use other Arduino devices or want to use more Firebase services
included Firestore database, Firebase Storage, Google Cloud Storage and
Cloud Functions for Firebase, use Firebase ESP Client library instead.
Firebase ESP Client - GitHub
References and Tutorials

▪ ESP32 WiFi Tutorial & Library Examples (Arduino IDE)


▪ ESP8266 NodeMCU Access Point (AP) for Web Server
▪ ESP8266 NodeMCU: Getting Started with Firebase
▪ Firebase Realtime Database Arduino Library for ESP8266
▪ Firebase ESP8266 Client
▪ Firebase - Structure Your Database
▪ Simple Example to Store and Read Data from the Firebase

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy