Send Sensor data to firebase using ESP32

Modern Gaming Cover YouTube Channel Art
This content provide you Experience

Sensor data to firebase is important for IOT development here you can read sensors reading and control your microcontroller from any where using an application or web-app . You are at right place if you want to learn how to send data to Realtime Database to monitor sensor value and control your microcontroller or an appliances .
You just have to follow these simple steps —

STEP-1 –Setting Up Firebase environment

Search Firebase in google and hit to first link you see this landing page of firebase-
Click on –> Get started

Create a New Project by clicking on Add project

Write Name for your project — I am writing “My first web app” and then click on “Continue”.

Turn OFF this google analytics for this project because we don’t want to analyze visitor’s or track the usage of this database.

It will Take some time to create a project.
After successfully created project Click on continue and you will redirected to a Dashboard page by scrolling down click on authentication .

Then Click on get started –

Click enable and Save

Goto “Users” –> Add user –> Enter email and password and then click on “Add user” .

Enter email and password which you need to enter at the time of longing in to monitoring data.

Click on “Setting” and then “project setting” .

In this Setting You get an API key You need to copy this key and keep it notepad .

Click on arrow

Click on “Build” –> And then “Realtime Database” —> Click on “Create Database”

After creating database Setup database to nearer server

Enable Test mode in Security Rule

You have successfully Setup your Firebase environment –> Just copy this URL and Save it in Notepad

STEP2:- Now Write a code for Esp32

Open Arduino IDE — If you are new at Arduino IDE then setup Arduino IDE for ESP32 By clicking on this link — https://elconics.com/getting-started-withesp32/

Install Firebase library in Arduino ide –> Click on Tools –> manage libraries –>> Search for Firebase esp32

Install this library – Firebase Arduino Clint Library for ESP32 and ESP8266

STEP 3 :- Copy this code and Replace your Realtime Database link

You Need Realtime Database URL and API Key earlier in Notepad. OR you can open clipboard by clicking Windows + V and you get that copied API and LINK.

#include <WiFi.h>
#include <FirebaseESP32.h>


#define FIREBASE_HOST "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
#define FIREBASE_AUTH "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
#define WIFI_SSID "yOUR SSID "
#define WIFI_PASSWORD "@PASSWORD"


//Define FirebaseESP32 data object
FirebaseData firebaseData;
FirebaseJson json;

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();

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);

  //Set database read timeout to 1 minute (max 15 minutes)
  Firebase.setReadTimeout(firebaseData, 1000 * 60);
  //tiny, small, medium, large and unlimited.
  //Size and its write timeout e.g. tiny (1s), small (10s), medium (30s) and large (60s).
  Firebase.setwriteSizeLimit(firebaseData, "tiny");

  /*
  This option allows get and delete functions (PUT and DELETE HTTP requests) works for device connected behind the
  Firewall that allows only GET and POST requests.
  
  Firebase.enableClassicRequest(firebaseData, true);
  */

  //String path = "/data";
  

  Serial.println("------------------------------------");
  Serial.println("Connected...");
  
}

void loop()
{

 int Sdata = random(0,1023) ;
 Serial.println(Sdata); 
delay(100); 
  json.set("/Reading1", Sdata); // Place your sensor data here
  json.set("/Reading2", Sdata-100); 
  json.set("/Reading3", Sdata-59);
  
  Firebase.updateNode(firebaseData,"/Sensor",json);

}

Changes You Have to make in code
Change your SSID and Password here.

#define WIFI_SSID "Your SSID"
#define WIFI_PASSWORD "Your password" 

Enter Realtime database URL and API key

#define FIREBASE_HOST "https://Your project.firebasedatabase.app" 
//Paste you Realtime database url here 

#define FIREBASE_AUTH "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // <---- Paste your API key here

And you Are done Upload this Code to ESP32 and Open Serial monitor you will see that Some Random data is printing

Open Firebase –> Go to Realtime Database

Click on arrow in sensor

Now you Will see that you Data from ESP32 is displayed in a Realtime database.

After sucessfully send Sensor data to firebase, From here you can make applications or and webapp easily STAY TUNED with us You can see our next tutorials on building an webapp and controlling your GPIO Pins .

For more You can visit Our YouTube Channel Elconics

Leave a Comment

Your email address will not be published. Required fields are marked *

Related post