05 Firebase Realtime Database + ESP32 | How to save sensor data & read data to control devices?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for today's lesson you'll learn how to get started quickly with the Google Firebase and have it connected with your esp32 I'll show you the step-by-step process of how to create a project to store and read values from the Firebase real-time database with your esp32 of course we'll be using the Arduino IDE in writing codes for your esp32 to demonstrate these operations in your real-time database at the end of this lesson you should be able to conduct your own experiment to save input data of various types like integer float and Boolean coming from an analog sensor such as a photoresistor or from Digital Data such as a switch as well as be able to read this stored data from your real-time database to control devices that are attached to your esp32 hi my name is Joe edgo and welcome to lesson 5 of the iot development training course with esp32 for this experiment you'll need the following materials two LEDs and two current limiting resistors a light dependent resistor or ldr a momentary normally open push button switch two 10 kilo ohm resistors and your esp32 development board a breadboard and a couple of connecting wires to start our circuit connection ensure that the ground and the 3.3 volt supply of your esp32 development board are connected to your breadboard's negative and positive bus strips then we'll use the gpio12 and gpio14 to connect the two current limiting resistors and the LEDs and then the gpio36 will be used to receive an analog input from our photoresistor through a voltage divider circuit also known as a potential divider please note that there are two configurations for this circuit depending upon the position of the ldr within the potential divider network with our configuration here the voltage output represented by this orange wire increases as the light receives by this ldr increases and finally the tactile switch with one of its pins connected to the 3.3 volts and the other pin is pulled down by this 10 kilo Ohm resistor here we'll use gpio13 to accept this digital input signal so let's plug this into our computer and start working with Google Firebase so what is Firebase Firebase is Google's mobile application development platform that helps you build improve and grow your app it has many services used to manage data from any Android iOS and web application so Firebase is a complete tool set that includes things like analytics authentication databases configuration file storage Push Messaging and so on most importantly the services are hosted in the cloud and scale with little to no effort on the part of the developer so in this demonstration we'll create a Firebase project with a real-time database and we'll use the esp32 to store and read data from that database through Firebase our esp32 can interact with the database from anywhere in the world as long as it is connected to the internet to start open your browser and then go to firebase.google.com to create a new project you must sign in using your Google account and then click the get start button and then click add project to create a new project here you can give any name to your project I'll name mine lesson 5 esp32 rtdb and then click continue next is Google Analytics although this is free and unlimited we will not be using this feature for our current project so I'll disable this option and then click create project this could take some time to set up your project and once your project is ready click continue and you'll be redirected to your project console page next you need to set up the authentication method this takes care of logging in and identifying the users of an app in this case the user is esp32 knowing a user's identity allows an app to securely save the user's data in the cloud and provide the same personalized experience across all the users devices on the left sidebar click build authentication this section allows your project to authenticate and manage users from a variety of providers without server-side code click get started in the sign in method tab you'll see several sign in providers to choose from you can sign in using email password phone Google account Facebook account GitHub and others for testing purposes I'll select the anonymous sign in by enabling Anonymous guest account in our application it enforces user-specific security and Firebase rules without requiring user credentials so click save next is to create a real-time database so click build real-time database and then click on create database and then select the location where your real-time database data will be stored ideally it should be the closest to your location I'm from the Philippines and I'll choose Singapore and click next and then set up the security rules for your database by default the start in lock mode is selected and it says here your data is private by default client read write access will only be granted as specified by your security rules however for testing purposes and for quick setup I'll select start in test mode and with this our data is open by default and we must update our security rules within 30 days to enable long-term client read write access in some of my later videos I'll show you how to secure your database using database rules so click enable and your real-time database is now created now copy and save this database URL in a notepad because we will need it later in our esp32 code another thing that we need to get is this projects API key so to get your project API key click this gear icon on the left sidebar and select project settings in the general tab you'll see the important information about your project and the one that we need here is this web API key again I'll copy and save this to my notepad so just a reminder secure this information only to yourself I am only showing you this publicly for demo purposes only and I'll be deleting this project after I made this video so now we are ready to connect this real-time database with our esp32 to begin open your Arduino IDE then go to tools manage libraries here we need to install the Firebase ESP client choose this Firebase Arduino client library for esp8266 and esp32 by mobizt select the latest version and click install and once done click close first we need to include the required libraries we'll use the wi-fi.h to connect the esp32 to the internet and the Firebase ESP client.h to interface the esp32 with Firebase we also need to include the following Firebase add-ons libraries the token helper for token generation and the rtdb helper for real-time database payload Printing and other helper functions then we need to Define our Network credentials my Wi-Fi SSID is jogo Galaxy with password of one two three four five six seven eight of course use your own Wi-Fi network credentials now we need to Define here our project's API key this is the one that we've copied earlier also the database URL in case you've forgotten it you can get it here then from our circuit board we'll Define the led1 pin at gpio12 and the lead to pin at gpio 14. and then the ldr pin is at gpio 36 so let's create some variables first we need to create the three Firebase objects that we need the Firebase data this will handle the data when there's a change on a specific database node path the Firebase oauth it is needed for authentication and the Firebase config which is required for configuration then this send data privilege variable is needed for our milis function later so that we can read and write to Firebase database at a specified interval in a non-blocking way then the flag variable sign up okay that we're going to use later as well when we have a successful sign up and then an integer ldr data to store the 12-bit ADC value from 0 to 40.95 and finally a float voltage to get the equivalent voltage of the ldr data in the setup function we initialize the serial communication and then connect to the internet using the SSID and password we defined earlier and then print the message connecting to Wi-Fi now I'll check a condition here that if the Wi-Fi is not yet connected just print a single dot every 300 milliseconds and once connected get out of the loop and print the assigned local IP to our esp32 next we need to specify the API key in our Firebase configuration object as well as the database URL then we call the Firebase sign up method here we pass in the Firebase config and the Firebase oath objects the last two arguments are empty strings this denotes an anonymous user sign up note that in the anonymous user signup every time the esp32 connects it creates a new Anonymous user then if the sign in is successful we set the sign up okay variable to true if it fails then we print the signup error message we also need to add the following configuration object assign the Callback function to the long running token generation task please check the add-ons tokenhelper.h for more info and then we initialize and begin the Firebase library with the same configuration and authentication settings we defined earlier inside the loop function what we really want is to send data to the database periodically to do this we check if the Firebase is ready and if signup is successful and we do this periodically every five seconds or at the start of this Loop note that we need to reassign a new release value to this send data print mail list variable every time it exceeds the five second interval to restart the 5000 millisecond counter now to store the sensor data in our real-time database first I'll read the analog data from my ldr PIN note that this is of type integer I also want to get the analog voltage reading and store it to the voltage variable and this is of type float so I'll divide it by 1000 to convert the millivolts to volts now this is how we store the actual data in our real-time database as mentioned in the library documentation to store at the specified node in the Firebase real-time database use the following functions set set in set float set double set string set Json set array set love and set file these functions return a Boolean value indicating the success of the operation which will be true if all the following conditions are met the server returns HTTP status code 200 the data types match between request and response in our program we will send an integer data so we need to use the set in function as follows the first argument is the Firebase data object the second argument is the database node path here you can choose any database path you want if the path does not exist it will be created automatically and the last argument is the value we want to pass to the database path in this case we are passing the value save in the ldr variable so if the set int is successful we'll simply print the ldr data the data path where it was stored and the data type used to store it if it failed then we'll just print the error Json note that we can store multiple data of different types to different node paths so we'll store this voltage of type float I'll duplicate this code and simply change the database node path to sensor voltage also instead of ldr it should be voltage now for float data type we'll use the set float function and that's it so let's compile and upload our code and there's a missing double code here and the Callback must be a small letter b so I'll open my serial monitor to check the status and it looks good our esp32 is consistently sending two values of type in and type load to our real-time database so let's check this in the actual Firebase real-time database console and here you go our database has one sensor node and this node has two nodes ldr data and voltage you'll notice that the database blinks when a new value is saved in this case it could happen every 5 Seconds you might want to adjust yours accordingly so if we read a value that doesn't change after the specified time then you will not see the node blink here I'll focus a LED light to my ldr and notice that the ldr data read and its equivalent voltage read increased significantly reaching up to 2 volts then removing it drops the sensor reading back to about 0.5 volts now blocking the sensor with my finger significantly drops the ldr data read and ultimately reaches zero which means total darkness and that's it congratulations you've successfully stored sensor data of different types into the Firebase real-time database using the esp32 next we'll read values from this real-time database and make use of that data in our esp32 to control the two LEDs in the Firebase real-time database data is stored as Json objects we can think of this database as a cloud hosted json3 there are no tables and Records which means it is a nosql database when we add data to the Json tree it becomes a node in the existing Json structure with an Associated key so I'll put here LED as my key and under it I'll add another node I'll give it a key analog now data stored can be represented as certain native types in the database that corresponds to available Json types to help us write more maintainable code I'll put a value of 0 and Firebase automatically detects this as numeric type under the Same Led node I'll add another node with the key digital I'll put an initial value of false and it is automatically detected as Boolean you can always click the icon beside the value to select the appropriate type you want to use so what we want to do here is that we control one of the LEDs connected to our esp32 through pulse width modulation depending on the integer value we placed here say 8-bit resolution means 0 to 255. and the other LED we will control it digitally through a Boolean value of either true or false now we go back to the Arduino IDE and write codes to read this node's data in real time I'll remove one of the print lines here so it is more organized first I'll Define the pwm channel 0. and then Define a constant frequency of 5000 and the constant resolution of 8-bit and then I'll declare an integer variable pwm value and initialize it to zero we'll use this to store the data that we will be reading from the database LED analog node path next is a Boolean lead status we'll use this to store the value that we will be reading from the database LED digital node path and then in the setup function I'll initialize the lead to pin as output however for the lead one pin I'll use the lead C setup and then attach it to my pwm channel inside the loop function if you want that the data reading happens every Loop so that the two LEDs will change their status instantaneously as the database values change you can put your codes outside this Millis if black the code is fairly similar to the previous code that we use to store data to the database but instead of set int I'll use get int and the node path is Led analog also we need to include additional conditions here to check its data type if it is of type end I'll get the int data of this Firebase database object and then store it in the pwm value variable and then I'll print a successful read message and specify the data path I'll concatenate the pwm value in each data type now we control the lead one using the LED zerite function with this pwm value however if data read is not successful we print failed and display the error reason now I'll duplicate this code and change this get int to get bull I'll also change the node path from lead analog to lead digital and then check the object's data type if it is Boolean if so use the lead status variable and assign the bull data of the red Firebase database object I'll change this print pwm value to LED status and instead of let's see right I'll simply use the digital write function and Ctrl led2 with this led status value and that's it let's upload the code and test and as you can see here in the serial monitor the data readings for the lead analog and the lead digital nodes is happening very fast a couple of successful reads in just under a second much better if we place this code inside a timer or Millis so that it only reads at a specified interval so let's re-upload the code and this is much better we can clearly see the two data saved and the two data read to and from the Firebase real-time database now going back to our Firebase console Let's test both the saving of the sensor data and the reading of the LED node data if I focus this slide to my ldr still the ldr data gets updated increasing its value now if I manually change this digital false value to true the led2 turns on if I change this back to false then lead to turns off in the lead analog node if I change this to 32 LED 1 turns on but the light intensity is a bit dim you can see this clearly if I turn on the led2 using the digital write function and let's try increasing the pwm duty cycle value to 96. and the LED is a bit brighter now but you can still recognize the brightness difference between the two LEDs so changing this to 255 gives a full 3.3 volts and Lead one is now similar to lead to brightness so to practice your skills in Firebase real-time database and esp32 try to implement a similar circuit with a slightly different database node paths and values you'll be adding another node called switch under the sensor node its value must correspond to the physical tactiles which place in your breadboard if the switch is pressed the Boolean value of true is saved however if the switch is not pressed the Boolean value of false is saved the ldr data and the voltage nodes remain the same once the ldr receives light the ldr data and voltage values is stored increase similarly the digital nodes true or false value directly controls the lead tools on and off Behavior however in the lead analog node path instead of using an integer value as pwm duty cycle use a float data type and then you can use values from 0 up to 3.3 volts to adjust the brightness of led1 foreign [Music] thank you very much for watching and I hope that you've learned something of value here to support this Channel please click the like And subscribe button and don't forget to hit that Bell icon to get notifications for every new video I uploaded for this course see you again in the next lesson
Info
Channel: Education is Life
Views: 68,675
Rating: undefined out of 5
Keywords: Firebase, Google Firebase, realtime database, real-time database, RTDB, ESP32, Firebase ESP client, FirebaseAuth, FirebaseData, FirebaseConfig, API Key, millis, LDR, photoresistor, JSON, node, PWM
Id: aO92B-K4TnQ
Channel Id: undefined
Length: 26min 10sec (1570 seconds)
Published: Tue Oct 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.