Firebase Realtime Database

The second approach we are going to explore is Firebase Realtime Database. This is a very common example because it showcases how third-party tools can simplify and speed up your development.

For this part, we will have a shared realtime database used by both the things and the mobile apps. That database will have one row with the current values of the temperature and the LED status.

The things app will write the temperature entry to the database and listen for modifications on the LED one.

The mobile app will do the reverse operation: write the LED value when the switch changes and listen for changes on the temperature value to display them on the UI.

To begin with, we need to create a project on the Firebase console and add a realtime database. We can do that at https://console.firebase.google.com/.

To keep it simple and get things moving, we will start in test mode, but you should take care of authentication if you plan to use this for anything serious. Test mode is just for trying out how it works.

Do not forget to remove test mode and use authentication if you plan to use Firebase on a real-life scenario.

The next step is to add the Android apps (things and mobile) to your Firebase project. Once you do that, you can download a google-services.json file that is generated with the information of the app and the Firebase project. We need to add that file to both of our modules.

With this, the backend part is completed.

Now, to enable Firebase in our project, we need to add Google Services to the classpath. We do that on the project level build.gradle:

buildscript {
  dependencies {
[...] classpath 'com.google.gms:google-services:4.0.1' } }

And then, on each module, we add the dependencies to firebase-core and firebase-database, as well as applying the google-services plugin:

dependencies {
    [...]
    implementation 'com.google.firebase:firebase-core:16.0.0'
implementation 'com.google.firebase:firebase-database:16.0.1'
}
[...] // Add to the bottom of the file apply plugin: 'com.google.gms.google-services'

With the setup completed, let's start with the things module:

lateinit var firebaseReference: DatabaseReference

override fun
onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
FirebaseApp.initializeApp(this)
val database = FirebaseDatabase.getInstance()
firebaseReference = database.getReference()

[...]

configureTemperatureReading()
configureLedSwitch()
}

We initialize the firebase app and then get a reference to the instance of the database. This reference is what we will use to write values and configure listeners.

We configure the temperature reading in the typical fashion of handler/runnable:

private fun configureTemperatureReading() {
handler.post(object : Runnable {
override fun run() {
val temperature = temperatureSensor.readTemperature()
firebaseReference.child("temperature").setValue(temperature)
handler.postDelayed(this, 1000)
}
})
}

Every time we read a new temperature value, we set the value to the child named temperature on the root of the database reference. This is very similar to creating a JSON object in the previous example, except that this time we don't have to pass it anywhere; Firebase will handle it for us in the background.

Reading the LED is a bit more complex:

private fun configureLedSwitch() {
firebaseReference.child("redLED").addValueEventListener (
object: ValueEventListener {
override fun onCancelled(p0: DatabaseError) {
// Nothing to do here
}
override fun onDataChange(snapshot: DataSnapshot) {
val redLedState = snapshot.getValue(Boolean::class.java)
if (redLedState != null) {
redLed.value = redLedState
}
}
})
}

We add a ValueEventListener to the child named redLED. When the data of that value changes, we receive a snapshot of that data. From this snapshot, we can read the value of the Boolean and update the value of the redLed variable.

If we now go to the Database section on the Firebase console, we can check what it looks like. Since we have not written the value for the redLED, that field does not exist, but if we add it manually on the console and change it, the things app will react to it.

Don't forget to get internet connectivity to your dev kit (especially if you are using the iMX7D).

Now, on the mobile side we have a similar logic, although reversed: the redLed switch writes and the temperature has a listener:

lateinit var firebaseReference: DatabaseReference

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

FirebaseApp.initializeApp(this)
val database = FirebaseDatabase.getInstance()
firebaseReference = database.getReference()

redLedSwitch.setOnCheckedChangeListener {
compoundButton: CompoundButton, b: Boolean ->
firebaseReference.child("redLED").setValue(b)
}
}

The initialization of Firebase is also the same as for the things app.

Adding a ValueEventListener and modifying the TextView temperature should be straightforward given everything we have done in this chapter so far, so it is left as an exercise for the reader. However, it is solved in the code examples, in case you want to look it up.

Firebase is extremely simple to set up, and it is not restricted to your local network. You can control your IoT device from anywhere in the world since Firebase is cloud-based. The only drawback of it is that it ties you to a particular service (Firebase) that is outside your control.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.156.140