Real-Time Data-Base With Firebase and Python

Firebase is a popular platform for building web and mobile applications. It provides a suite of tools and services that make it easy to develop high-quality apps quickly. Firebase is also highly scalable and flexible, making it an excellent choice for start-ups and enterprises alike.

In this blog post, we’ll show you how to get started with Firebase and Python. We’ll walk you through the steps you need to take to configure your Firebase project, authenticate your Python script with Firebase, and use Firebase services such as the Realtime Database, Cloud Firestore, and Cloud Storage in your Python script.

Step 1: Creating a Firebase Project

To get started with Firebase, you first need to create a Firebase project. To do this, go to the Firebase console and sign in with your Google account. Once you’ve signed in, click the “Create Project” button and follow the instructions to set up your project.

firebase

 

 

 

 

 

A. Follow the below instruction to create a new project on Firebase.

 

 

 

 

 

-Disable the option “Enable Google Analytics” for this project as it is not required and then click on Cre

ate project button.

-It will take a few seconds to set up your project. Then, click Continue when it’s ready

-You’ll be redirected to your Project console page, as shown below.

B. Set Authentication Methods: – On the left sidebar, click on Authentication and then on Get Started.

There are several authentication methods like email and password, Google Account, Facebook account, and others. For testing purposes, we can select the Anonymous user (require authentication without requiring users to sign in first by creating temporary anonymous accounts). Enable that option and click on Save button.

C. Creating a Real-time Database: – The next step is to create a Real-time Database for your project. To set up a database, click on Real-time Database shown at the left sidebar and then click on Create Database.

Select your database location. It should be the closest to your current location.

 

Set up security rules for your database. For testing purposes, select Start in test mode. In later tutorials, you’ll learn how to secure your database using database rules.

 

 

 

 

 

Your database is now created. You need to copy and save the database URL—as highlighted in the image shown below — as you will need it later for your ESP8266 code.

 

 

 

 

 

 

Step 2: Installing the Firebase SDK for Python

To use Firebase with Python, you need to install the Firebase SDK for Python. You can do this using pip, which is the package manager for Python. Open a command prompt or terminal window and run the following command:

Copy to Clipboard

This will install the Firebase SDK for Python on your system.

Step 3: Importing Firebase Modules

Once you’ve installed the Firebase SDK for Python, you can import the necessary Firebase modules into your Python script. Here’s an example of how to import the Firebase Real-time Database module:

Copy to Clipboard

You can similarly import other Firebase modules, depending on which Firebase services you want to use.

Step 4: Authenticating Your Python Script with Firebase

To use Firebase services in your Python script, you need to authenticate it with Firebase. To do this, you need to generate a service account key and set up environment variables.

Here are the steps to follow:

  • Go to the Firebase console and select your project.
  • Click the “Settings” icon and then click “Service Accounts”.
  • Click the “Generate New Private Key” button to generate a service account key.
  • Download the service account key and save it to a safe location on your computer.
  • Set up the GOOGLE_APPLICATION_CREDENTIALS environment variable by running the following command in your terminal or command prompt:
Copy to Clipboard

Replace “key.json” with your key file to the service account key file you just downloaded.

Step 5: Using Firebase Services in Your Python Script

Now that you’ve authenticated your Python script with Firebase, you can start using Firebase services in your script.

Here’s an example of how to write data to the Firebase Realtime Database using Python:

Copy to Clipboard

Step 6: Get a reference to the location in the database where you want to set the data:

Copy to Clipboard

Step 7: Set random values using a random library using the set () method on the reference object with an infinite while loop.

Copy to Clipboard

You can use a similar code(s) to access other Firebase services such as Cloud Firestore and Cloud Storage.

Step 8: Program the ESP8266 to Interface with Firebase

WIFI CPU ESP8266

Now that the Firebase Real-time Database is created, you’ll learn how to interface python with the database. To program the ESP8266, you can use Arduino IDE, VS Code with the Platform extension, or other suitable software.If you’re using Arduino IDE, follow the next steps to install the library.

Go to Sketch > Include Library > Manage Libraries

Search for Firebase ESP Client and install the Firebase Arduino Client Library for ESP8266 and ESP32 by Mobitz.

Now, you’re all set to start programming the ESP8266 board to interact with the database.

Copy to Clipboard

Upload the code to your ESP8266 board. Don’t forget to insert your network credentials, database URL path, and the project API key.

After uploading the code, open the Serial Monitor at a baud rate of 115200 and press the ESP8266 on-board reset button so it starts running the code.

Congratulations! You’ve successfully stored data in Firebase’s real-time database using the ESP8266. In the next blog, you can learn about how to create an Automatic Door-Locking System using Face recognition via Firebase link

Conclusion:-

In the above blog, we have learned how to create a real-time database using Firebase Realtime Database and Python, and how to use an ESP8266 board to send and receive data from the database. Firebase Realtime Database is a powerful tool for real-time data synchronization and can be easily integrated into your Python projects. ESP8266 is a low-cost microchip with Wi-Fi capabilities that can be programmed using the Arduino IDE. By combining Firebase Realtime Database and ESP8266, we can create powerful IoT applications that can interact with a cloud database in real time.