Android things, Firebase and a little more
This article is continue to describe my journey in the IoT world, in the previous article I described how to prepare Raspberry pi to run Android things, this article will be about what you can actually do with that.
My goal was simple, create a button that when I press it will turn on the led and update realtime Firebase table that light is on. For starting the project I used Google example as my base, ordered hardware kit from the internet (it is much cheaper from one that google proposing on there website) and we are ready to go.
Before you start lets understand a little bit about how to do it, we will need to use breadboard for connecting everything, it is the best for prototyping circuits. You can read more in this Article.
First step is to connect all the peripherals to your board. Its like puzzle, just follow the image and connect everything on the board to look the same. The example is for Android things board but for my example I had used Raspberry pi this is his I/O.
Next step will be to create a project in Android studio, You will need to create new empty Android project and start to write the code, I will describe here the basic things to start doing it, you can find the full project in GitHub.
First step is to define play services dependency in the root build.gradle file, we will need it to supporting Firebase integration.
...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.0.0'
}
...
After that continue to the module build.gradle file and define dependencies for Android things and Firebase.
...
dependencies {
provided 'com.google.android.things:androidthings:0.2-devpreview'
compile 'com.google.firebase:firebase-database:10.0.1'
} apply plugin: 'com.google.gms.google-services'
Final thing is to create activity and define connection to the peripheral manager service that will use like a service to access the gpio of the board.
PeripheralManagerService service = new PeripheralManagerService();
...//Accessing to the button
buttonGpio = service.openGpio(BoardDefaults.getGPIOForButton());
...//Accessing to the LED
ledGpio = service.openGpio(BoardDefaults.getGPIOForLed());
For the button we will need to define the callback method to get events of button click.
private GpioCallback mCallback = new GpioCallback() {
@Override
public boolean onGpioEdge(Gpio gpio) {
...
//Seting the value to turn on and off the LED
ledGpio.setValue(!gpio.getValue());
//Storing the value to the Database
Database.setNewLedValue(!gpio.getValue());
...
return true;
}
};
Database class implementing the connection to Firebase and storing the value. We almost ready, now we will need to config Firebase, you can follow this documentation.
Last thing before you can build the project and put it on your board is to connect the board to your computer with usb and run this command in the terminal (Change the IP with IP of your device, you can read my previous article to get it).
./adb connect 192.168.2.11
Now you are ready, build you project and target to your device, you can see in the video how it works, all the source code available in the GitHub project.
After understanding how we can communicate with Gpio’s and to have remote updates through the Firebase we can continue to more complex projects, for example doing smart sensor like Nest, just keep your mind open and you can create amazing things with only basic Android knowledge.