Breaking

Thursday, December 21, 2023

Read Data From Cloud Firestore and Control LED's with Esp32/Esp8266

In this article, we will see how we can read data from Cloud Firestore with Esp32 and Esp8266 NodeMCU. We will create a project to read boolean values from Firestore. 

Project Overview

We will read two boolean values from the database and control led's according to the boolean values, To accomplish the task of controlling led's with Firestore. First, we have to set up the Firebase project for Cloud Firestore. then we will program the esp8266 NodeMCU board to communicate with Cloud Firestore.

Let's Setup the Firebase Account

Create a new project👇 

  •  Go to Firebase and sign in with your Gmail account.
  •  Click Get Started then Add Project.
  •  Set a name for your project, for example, "DHT11 Firestore".

 

  • After Continue, you can disable Google Analytics. and click the [Create Project] button


  • Within a few seconds, your project will be created. Click [Continue]
  • Then you will be redirected to the console page of your project.

Now We need to set Authentication for the Project👇 
Firebase needs to know the identity of the user, with an authentication system firebase will only allow authenticated users to write data to the Firestore database and prevent unauthenticated users.
  • On the left sidebar expand/click on [Build] then click [Authentication] 
  • Click [Get started] and select the Email/Password provider.
  • Enable Email/Password and save it
  • Now go to Users 
  • Click [Add User] to add a user with an email and password   

Now create a Firestore database👇
  • From the left sidebar click Firestore database
  • Click [Create Database] then next, and select test mode.
  • After clicking [Enable], you will redirected to the Database


After Finishing the database setup now we need project id and api key.

Get Project id & API key👇
  • From the sidebar go to project settings.



Here you have your Project ID and API key.
Now Let's program esp8266 NodeMCU
 Now that we have our Firestore database created, we will program esp8266/esp32 to communicate with cloud Firestore. To program esp8266 we will use Arduino ide.

To communicate with Firebase we will use the Firebase-ESP-Client library

Install Firebase ESP-Client-Library👇

To communicate with Firebase and Cloud Firestore from esp8266 or esp32 this library has a lot of examples 

Let's Install the library to Arduino ide👇
  • Search "Firebase Arduino" on the Library manager and install Firebase Arduino client library for esp8266 & esp32.

After installing the library we are all set to program Nodemcu.

Upload Code to NodeMCU👇
base Arduino" on Copy the following code and upload to your esp32 or esp8266 board to read data from Firestore. 

Make sure to replace
  •  Your wifi SSID Password.
  •  Project ID & API key (copy from project settings).
  •  User Email & Password (Added in Authentication).

After uploading the code, open the serial monitor and set the baud rate to 115200, you can see the result like this.


Getting results on serial monitor according to document values on the database.


By changing the state value from documents you can control connected leds on respective pins.

Conclusion
In this article, we learned how we can read values from Cloud Firestore and control leds with ESP8266 & ESP32. The code we have seen will work both on esp32 and esp8266. Checkout How to write data on Cloud Firestore.


🙏We hope you have found this article useful, Thank you for reading🙏



No comments:

Post a Comment