0% found this document useful (0 votes)
9 views15 pages

Lab3 Firebase HandsOn

Uploaded by

sana bargaoui
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)
9 views15 pages

Lab3 Firebase HandsOn

Uploaded by

sana bargaoui
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/ 15

Lab 1: Introduction to Firebase Cloud

Descrip on
This lab introduces students to the basics of Firebase Cloud. Students will set up an ESP32
microcontroller with a single sensor (DHT11) to publish data to Firebase. This foundational
exercise will help students understand data flow in IoT systems and the role of cloud storage in
monitoring data.

Requirements
 Hardware: ESP32 microcontroller, DHT11 sensor (temperature and humidity).
 Software:
o Arduino IDE
o Firebase Cloud account
o Wi-Fi connection

Create a Firebase project and set up a Real me Database


Sign in to Firebase Console with Google Account
Click here

Click here

Create a new project


Write here your project name

Click here
Click here

Select by default account


Click here

Confirm the selection of the default account.

Wait few seconds


Click here

Configure Authentication and Enable Anonymous Users


Click here

Click here
Click here

Click here
Create new real time dataset
Get URL and Key to write in the real time dataset
Create and Configure a Firebase Client on ESP32 card
Use the following sketch to send random temperature and humidity values.

Attention: Don’t forget to change WiFi and Firebase parameters

#include <Arduino.h>
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>

//Provide the token generation process info.


#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"

// Insert your network credentials


#define WIFI_SSID "RedmiAmira"
#define WIFI_PASSWORD ""

// Insert Firebase project API Key


#define API_KEY "AIzaSyB3af38MoEM-ebrS4V0cIjQRjVLnBLCR1A"

// Insert RTDB URLefine the RTDB URL */


#define DATABASE_URL "https://greengard-d9dff-default-rtdb.firebaseio.com/"

//Define Firebase Data object


FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

unsigned long sendDataPrevMillis = 0;


int count = 0;
bool signupOK = false;

void setup(){
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();

/* Assign the api key (required) */


config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;

/* Sign up */
if (Firebase.signUp(&config, &auth,"","")){
Serial.println("ok");
signupOK = true;
}
else{
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}

/* Assign the callback function for the long running token generation task */
config.token_status_callback = tokenStatusCallback; //see
addons/TokenHelper.h

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

void loop(){
if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 ||
sendDataPrevMillis == 0)){
sendDataPrevMillis = millis();
Serial.println("Firebase is ready data not sent yet");
// Write an Int number on the database path test/int
if (Firebase.RTDB.setFloat(&fbdo, "temperature/float", 0.01 +
random(0,100))){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
count++;

// Write an Float number on the database path test/float


if (Firebase.RTDB.setFloat(&fbdo, "humidity/float", 0.01 + random(0,100))){
Serial.println("PASSED");
Serial.println("PATH: " + fbdo.dataPath());
Serial.println("TYPE: " + fbdo.dataType());
}
else {
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.errorReason());
}
}
}

Send ambient temperature and humidity to Firebase


1. Wire a DHT sensor to the esp32
2. Write the appropriate Arduino sketch to read ambient temperature and humidity
3. Send values to the Firebase

You might also like