06. Realtime Read and Write with Firebase + ESP32 + Android Studio - IoT Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
for today's lesson you'll learn how to build a custom mobile iot project by integrating Android Studio Firebase real-time database and esp32s nodemcu you'll learn how to write Android applications using kotlin programming language to store and read data to and from a Firebase real-time database and be able to send and read data to and from an esp32 board you'll learn how to write codes that listen to any changes in data in a Firebase database so that your Android app and your esp32 board get updated data instantly with minimal latency at the end of this lesson you should be able to create an app that reads sensor data from your esp32 and control any output devices attached to your esp32 from your Android phone in real time hi my name is Joe Edgar and welcome to lesson 6 of the iot development training course with esp32 please note that this is a continuation of your previous lesson 5 integrating Firebase and esp32 so please make sure to check it first because I'll be using the exact same program and circuit connection for this demonstration to begin let's open our code from lesson 5. recall that we read and store ldr data in the Firebase real-time database and control two LEDs as well one digital and one analog through pulse width modulation now inside this Loop function we perform everything at a specified interval every 5000 milliseconds using the Middle East function well the main problem with this is that our code has to wait for 5 seconds before it reads any possible data changes in our database as you can see if I change this LED digital value to true there is at most five second delay before the change is reflected in our esp32 board same goes for the lead analog value well we can reduce the interval to like one millisecond but that's not very efficient because we will just be consuming resources in bandwidth in exchange for minimal latency because we will be reading data continuously even though there are no changes to our database so to resolve this we will Implement Firebase stream wherein our esp32 reads data from this real-time database only when the data changes in a specific node path I'll add two more Firebase database objects fbdo S1 and fbdos2 for stream 1 and stream 2. each will have its own database path one for the lead analog and the other for lead digital please note that there is another way to do this having one database object that can point to multiple paths however for now I'll show you the simplest the next thing is in the setup function after we configured everything we can call the begin stream function of the Firebase real-time database this accepts two arguments a reference to the database object and the database path now this function call returns a Boolean value depending on whether it is successful or not so let's put a condition here that if it returns false I'll print an error message so that we know what causes it so I'll duplicate this code and do the same for database object stream 2. now inside the loop function we no longer want to read the lead analog and the lead digital values based on the specified interval so I'll cut this code block and move it outside this if block and now we'll be reading data from a real-time database on data change to do this I'll copy this same condition that checks if the Firebase is ready and we have successfully signed up and then enclose this entire code block inside this if statement now we need to call the read stream function and pass in the reference of the stream 1 database object I'll also convert this into an if statement that if it can't read the stream then print an error message then to actually get the data we don't use the get in but instead we'll use the stream available function I'll change this to stream one and the rest of the database objects here however I'll do nothing if the stream is unavailable now I'll copy this code and then do the same for stream 2 database object and that's it let's try to upload our code so now when I change this node value to True notice an instant update on our serial Monitor and the red LED in our circuit same goes once I change this analog value to 255. only the change in that node is retrieved instantly and now to complete this iot project let's create an Android application where we can read and display the sensor value as well as control these LEDs in real time so open your Android studio and create a new project select empty activity template and click next I'll name a project iot Dev lesson 6. by default we only have one text view in the middle of our layout so I have prepared a very simple layout prior to this and to make this demonstration short I'll paste my previously created XML layout code here please note that you don't have to copy the exact layout that I have here feel free to have your own layout if you want to learn more about creating Android applications please check my other video training course Android app development using kotlin the link is provided in the description below now in my layout I have a button LED set value an edit text for signing the LED pwm value I also have another button to read the sensor data and a text view to display the sensor reading in vaults let's see how it looks in the emulator so what we want here is that this button set pwm value once click should get the value we place in this edit X pwm and then store it to the Firebase real-time database LED analog path also the read sensor data button once clicked should read a value from the Firebase real-time database sensor voltage path and display the corresponding voltage to these text view volts so let's begin first let's enable view binding in our project and now let's start a very simple reading of sensor data stored in our database for this I'll call set on click listener for this button lead and then I'll call this function read data which I'm about to create now let's implement the code that reads data from a Firebase real-time database and to do that the simplest way is to click on tools Firebase and the Firebase assistant window will appear locate the real-time database section and click the get started with real-time database kotlin click the first step which is to connect your app to Firebase your browser should launch and redirects you to the Firebase console make sure that you are currently logged in with your account select your target database and after a few seconds your Android Studio project is ready to connect after clicking the connect button we can now use Firebase in our Android project and start using the Firebase SDK going back to the Firebase assistant window click the Step 2 to add the real-time database SDK to your app now it says here that it will add rules to include the Google services Gradle plugin apply the plugin and implementation please note that you must have the updated version of your Android Studio for this to work also for this current version make sure that you are using this version of 20.1.0 in your build.gradle file otherwise some of the methods that I will be using in this demo might not be available in the lower versions and wait for a couple of seconds while your project is being configured and once done you should see these two green checks for Step 1 and step 2. I'll skip step 3 for now and jump right away to reading or writing data to our Firebase real-time database so to read data from a Firebase real-time database first we need to declare a variable database of type database reference I'll press Alt Enter to import the required package for this and then inside our read data function we need to call the get reference method of our Firebase database instance to access a specific location in our database to read and write data in our Firebase database what we want is to access this sensor node and its child voltage so in this get reference I'll pass in the node path sensor and access its child voltage and then call the get method now I'll put this add-on success listener so that I can write code that gets executed when the task of getting the reference is completed so when we do this we can work with the data from our Firebase database through this data snapshot to be sure I'll ask first if the data snapshot exists and then I'll create a local variable voltage of type float and assigns a value from this data snapshot I'll display a toast message saying successful voltage read and finally I'll display this voltage value to our text view volts however if the data snapshot doesn't exist I'll also display a toast message also we can add the on failure listener to display another toast message that it failed to get the database reference at the specified location so I'll double check my code okay I forgot to pass in the root view of this view binding variable so now let's check it in our emulator in here we can see the value from our sensor voltage path now when I click this read button sensor it should access this specific location and read whatever value stored in that location and it works so now let's try writing or store values to a specific location in our database when we click this set pwm value button we store whatever we type in this edit text view to our database at location LED slash analog so I'll set up this button set lead value that once clicked I'll call this set data function first I'll create a variable pwm value of type int and then try to get whatever value specified by the user hoping that it is a valid integer but if not I'll display a toast message containing the specific error message and then exit this function however if it succeeds then I'll get the reference of the lead node path and its child path analog and then set the integer value that we type in the edit text pwm similarly once it succeeded I'll simply display a toast message pwm value set successful but if it failed I'll display another message saying fail to set a new pwm value now let's test it again first let's test if the previous functionality still works and it's good now I'll enter a pwm value of 255. and once I click this button you see an instant change in our Firebase real-time database as well as the blue LED in our esp32 circuit Let's test it again and it works great and now one thing that we need to improve here is the real-time reading of this sensor data we don't want to be clicking this button every time to check if the values stored in our database has changed and for this we'll implement the on data change method in Firebase to listen for the changes to the contents in a given database path and read its content in the oncreate function I'll call this custom function database listener and I'll press Alt Enter to create this function and then I'll get the reference of this entire database which means we're going to listen to changes in any of these node paths please note that if you have a huge three structure of your database then only get the reference of the specific node path that you want to monitor for data changes otherwise there's going to be a performance hit in your system so I'll create a local variable called post listener and assign an object that implements value event listener interface this interface requires to implement two members the on data change and the on canceled methods inside this on data change method I'll create a local variable voltage and get the value from the sensor voltage database path and then I'll display this voltage value to the text view volts if there's an error I'll simply display a toast message fail to read sensor data and finally We Are The Listener using the add value listener method now each time the data changes your listener will be called with an immutable snapshot of the data so let's try our app one last time now you'll notice that the voltage reading is being displayed in real time as the data in the sensor voltage path is changed so is the display in our app I'll check the rest of the functionality and still works perfectly now you might notice that we have not yet implemented a switch or a button in our app that will toggle this LED digital Boolean value and I will leave this test as your challenge activity so to practice your skills in Android development with Firebase and esp32 try to improve this program by adding a switch widget in your app here you will practice how to read and write to a specific database node path for that once your app is launched your program must check the current Boolean value from the LED digital path to toggle the switch accordingly and then you can toggle the switch to update the Boolean value in the LED digital path [Music] again thank you very much for watching and if you've learned something of value here please support this channel by clicking 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: 33,813
Rating: undefined out of 5
Keywords: Android Studio, Firebase, RTDB, Firebase Stream, onDataChange, ValueEventListener, addValueEventListener, ESP32, IoT, IoT ESP32, IoT Firebase, Read, Write, Android Database
Id: LaUzGdtLFiQ
Channel Id: undefined
Length: 18min 33sec (1113 seconds)
Published: Sun Dec 04 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.