04 Connecting NodeMCU to Firebase
04 Connecting NodeMCU to Firebase
▪ The umbrella has a handle that would illuminate when snow or rain
was in the forecast.
IoT Application: Smart Umbrella
▪ 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
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baudrate
WiFi.begin(WIFI_SSID, WIFI_PASS); // Begin WiFi connection using SSID and password
void loop() {
}
NodeMCU ESP8266: WiFi Station Mode – Output
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
▪ Enter the name of your project, accept terms, and click Continue.
Firebase: Creating a New Project
▪ Click Continue.
Firebase: Creating a New Project
▪ 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
▪ Click on Show.
Firebase: Getting Database Secret
▪ Enter node key, value, choose its data type, and click Add.
Firebase: Adding Nodes to Your Database
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