MuhammadRaheelNaseem / Fetch-Data-From-Firebase

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Firebase Data Display

This project showcases real-time data display from Firebase in an HTML webpage. It comprises two essential files:

  • index.html: This HTML file is responsible for rendering the sensor data on the webpage.
  • app.js: This JavaScript file configures Firebase and updates the sensor data displayed in the HTML.

Getting Started

To run this project successfully, you need to set up a Firebase project and configure it with your Firebase credentials. Follow these detailed steps:

  1. Create a Firebase Project:

How To Configure Firebase with Raspberry Pi:

image

image

image

Then press continue

image

Then press continue

image

Then Click Create Project

image

image

Our project is ready to use Press Continue

You directly redirect to firebase console

image

image

After Click Build Button You Can See Realtime Database Just Click There

image

Now You Directly Redirect Realtime Database

image

Click Create Database

image

After clcik create Database then Click On Next button

image

Make sure you on test mode only

image

Press Enable Button

image

Database is created Successfully

image

One more thing is here go "Project Overview"

image

Click web button

image

Enter Web App Name

image

Like This

image

click on Button

image

Copy this thing into the code

image

  1. Configure Firebase:
    • In your Firebase project, navigate to Project Settings.
    • Under the "General" tab, find your Firebase configuration object.

Firebase Configuration

In the app.js file, replace the placeholder Firebase configuration object with your actual Firebase configuration. This includes your API key, authentication domain, database URL, and other necessary details.

const firebaseConfig = {
    apiKey: "YOUR_API_KEY",
    authDomain: "YOUR_AUTH_DOMAIN",
    databaseURL: "YOUR_DATABASE_URL",
    projectId: "YOUR_PROJECT_ID",
    storageBucket: "YOUR_STORAGE_BUCKET",
    messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
    appId: "YOUR_APP_ID",
    measurementId: "YOUR_MEASUREMENT_ID"
};
  1. Include Firebase SDK:
    • In the index.html file, include the Firebase SDK scripts. This project utilizes Firebase Realtime Database, so ensure to include firebase-app.js and firebase-database.js scripts.
<!-- Firebase SDK -->
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-database.js"></script>

How It Works

Database Structure

This project assumes the Firebase Realtime Database is structured as follows:

tuesday-class-acf4f
│
└── Sensor
    ├── sensor1
    │   ├── data: value
    │   └── timestamp: value
    └── sensor2
        ├── data: value
        └── timestamp: value

JavaScript Logic

  • Initializing Firebase: app.js initializes Firebase with the provided configuration.
  • Database Reference: It obtains references to two nodes in the database: Sensor/sensor1 and Sensor/sensor2.
  • Updating Sensor Data: Two functions, updateSensor1Data() and updateSensor2Data(), are responsible for updating the HTML with real-time sensor data.
  • Listening for Changes: The script listens for changes to the sensor data using Firebase's on() method and updates the HTML accordingly.

Running the Project

To view real-time sensor data, simply open the index.html file in a web browser.

Files Included

  • index.html: HTML file for sensor data display.
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Firebase Data Display</title>
</head>

<body>
    <h1>Sensor Datas</h1>
    <div id="sensor1Data"></div>
    <div id="sensor2Data"></div>

    <!-- Firebase SDK -->
    <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/7.14.6/firebase-database.js"></script>
    <script src="app.js"></script>

</body>

</html>
  • app.js: JavaScript file for Firebase configuration and data update logic.
// Firebase configuration
const firebaseConfig = {
    apiKey: "AIzaSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "tuesday-cxxxxxxxxx.firebaseapp.com",
    databaseURL: "https://tuesday-cxxxxxxxxxxxxxxxxxxxx.firebaseio.com",
    projectId: "tuesday-cXxxxxxxxxxxxxxx",
    storageBucket: "tuesday-cxxxxxxxxxxxx.appspot.com",
    messagingSenderId: "373xxxxxxxxxx",
    appId: "1:3734Xxxxxxxxxxx:web:6a715a4xxxxxxxxxxx",
    measurementId: "G-6RDxxxxxxxxx"
};

// Initialize Firebase
const app = firebase.initializeApp(firebaseConfig);
console.log("Firebase initialized:", app);

// Get a reference to the database
const db = firebase.database();
console.log("Database reference:", db);

// Get a reference to the database nodes
const sensor1Ref = db.ref('Sensor');
const sensor2Ref = db.ref('Sensor');

// Function to update sensor 1 data in HTML
function updateSensor1Data(data) {
    document.getElementById('sensor1Data').innerHTML = "<p><strong>Sensor 1 Data:</strong> " + JSON.stringify(data) + "</p>";
}

// Function to update sensor 2 data in HTML
function updateSensor2Data(data) {
    document.getElementById('sensor2Data').innerHTML = "<p><strong>Sensor 2 Data:</strong> " + JSON.stringify(data) + "</p>";
}

// Listen for changes to sensor 1 data
sensor1Ref.on('value', (snapshot) => {
    const data = snapshot.val();
    console.log("Sensor 1 Data:", data);
    updateSensor1Data(data);
});

// Listen for changes to sensor 2 data
sensor2Ref.on('value', (snapshot) => {
    const data = snapshot.val();
    console.log("Sensor 2 Data:", data);
    updateSensor2Data(data);
});

Output:

image

About

License:GNU General Public License v3.0