Breaking

Sunday, December 17, 2023

SEND DHT11 Sensor Data To Firebase Firestore With ESP8266/ESP32 NodeMCU

In this article, we will see how we can send data to Cloud Firestore. Cloud Firestore is a NoSQL document database that lets you easily store, sync, and query data for your mobile and web apps, today we will integrate Firestore with esp8266 NodeMCU. 

Send DHT Data To Cloud FireStore

Circuit Connection👇


[v = vcc, d = data, g = gnd]

According to This diagram, the DHT11 sensor's data pin is connected to the D4 pin of the esp8266 NodeMCU board

VCC pin connected to 3v3 pin of NodeMCU board.

And GND pin of the DHT11 sensor connected to esp8266 NodeMCU.

Project Overview

To accomplish the task of sending DHT11 data to Firestore. First, we have to set up the Firebase project for Cloud Firestore. then we will program the esp8266 NodeMCU board to read data of DHT11 send and send those data to 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 to send DHT11 data to 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👇
Copy the following code and upload it to Esp8266 NodeMCU, this code will read data from the DHT11 sensor and update the data to Cloud Firestore.

Make sure to replace your wifi SSID Password, the API key, Project ID, and user Email and password.

After uploading the code, open the serial monitor and set the baud rate to 115200, you can see the result like this.
Then open your database, and you will see values are getting updated with intervals of 10 sec.




Congrats you have successfully updated the DHT11 sensor data to Cloud Firestore with Esp8266/Esp32

Conclusion
In this article, we learned how we can update or write Temperature and Humidity data to Cloud Firestore with ESP8266 & ESP32. The code we have seen will work both on esp32 and esp8266. Check out "How to read Valus from Cloud Firestore".
 



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

No comments:

Post a Comment