0% found this document useful (0 votes)
22 views2 pages

Exam - Ste 8 and 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Exam - Ste 8 and 9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <ESP8266WiFi.

h>
#include <FirebaseESP8266.h>

// WiFi Configuration
#define WIFI_SSID "YOUR_WIFI_SSID"
#define WIFI_PASSWORD "YOUR_WIFI_PASSWORD"

// Firebase Configuration
#define FIREBASE_HOST "YOUR_PROJECT_ID.firebaseio.com"
#define FIREBASE_AUTH "YOUR_DATABASE_SECRET"

// LED Pin
#define LED_PIN D4

FirebaseData fbData;
FirebaseConfig config;
FirebaseAuth auth;

void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);

// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");

// Configure Firebase
config.host = FIREBASE_HOST;
config.signer.tokens.legacy_token = FIREBASE_AUTH;

// Initialize Firebase
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
}

void loop() {
// Get value from Firebase
if (Firebase.getString(fbData, "/ledState")) {
String command = fbData.stringData();
command.toUpperCase(); // Convert to uppercase for comparison

if (command == "ON") {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED turned ON");
}
else if (command == "OFF") {
digitalWrite(LED_PIN, LOW);
Serial.println("LED turned OFF");
}
else {
Serial.println("Invalid command. Use ON or OFF");
}
}
else {
Serial.println("Failed to read from Firebase: " + fbData.errorReason());
}

delay(1000);
}

NOTE:
THIS IS ON HOW TO MAKE A SAMPLE CODE THAT CAN CONTROL A LED WIRELESSLY THROUGHT THE
FIREBASE COMMAND.
APPLY THIS TO YOUR APPLICATION CODE.
CHANGE THE PARAMETERS
READ THE COMMENTS IN THE CODE TO MAKE IT WORKING.
SYNTAX USED TO READ THE VALUE IN THE FIREBASE IS (Firebase.getString)
SYNTAX .GET MEANS TO READ THE VALUE IN THE FOLDER "/ledState"
YOU WILL MAKE THE FOUR LED USING THE FIREBASE COMMAND WORK IN YOUR PROGRAM.

******THE ABOVE CODE IS JUST AN EXAMPLE OF ONE LED CONTROLLED BY THE MIT APP
INVENTOR AND FIREBASE******

You might also like