Getting Started Android Things with Raspberry Pi and Firebase

Engineer, Tech Lead and Technopreneur

In this article I am going to show you how to get started with Android Things which is running on RaspberryPi 3 device. Android Things is an Android based embedded operating system platform by Google which is aimed to be used with low-power and memory constrained Internet of Things devices. It is pretty much cool that you can also use Firebase out of the box with Android Things OS.

Here I am going create a small ‘Hello World’ Like application using Android, configure it with Firebase and control the blinking delay of an LED bulb realtime over the air.

To get started you will need following knowledge and equipments.

Knowledge

1. Java Programming.
2. Android Application Development.

Equipments

1.  RaspberryPi 3 Model B
2.  HDMI Cable
3.  Ethernet Cable
4.  Router with Ethernet port and Wifi
5.  Monitor or a TV which support HDMI
4.  LED Bulb
5.  150Ω resistor
6.  Female to Male jumper wires
7.  Breadbord
8.  Power Supply for Raspberry Pi
9. SD Card (8GB or higher)

Let’s get started.

Install Android Things on Raspberry Pi

1. First you have to go to Android Things web site and download the Developer preview.

2. I’m using Raspberry Pi as my device, So I’m going to download Android Thing OS for Raspberry Pi

3. Next step is to Format our SD Card. To format your SD card you can use SD Card Formatter Application. You can download that application for Windows or Mac for free from SD Card Formatter website. If you are on Linux please follow this instruction to format your SD card. Here I’m using Scandisk 16GB Class 10 SD card.

Using SD Card Formatter application you can do a quick format

 

4. After formatting your SD Card you have to install the OS. If you are using Mac OS you can use a handy tool called ApplePi-Backer to Flash to install the OS.  Unzip the developer preview and get the image file. Select your SD card and load the IMG file from your computer to the tool. Then click on ‘Restore Backup’ button. It would take few minutes to install the OS on your SD card. Once you finish it eject the SD card and plug it to your Raspberry Pi 3 device. For Windows and Linux users pleas follow these instructions.

5. Now connect your Raspberry PI to a monitor or a TV using an HDMI cable and power up the device. Please do not forget to connect your Raspberry Pi to your router using the Ethernet cable. Please wait while it is booting up.

Once it boot up, you will see it will automatically connected to your network through the Ethernet cable. You can see an IP address is assigned to your device.

Connect with the Device

6. Connect to the same network from your laptop and type the following command in your terminal to connect to your device with adb. Here the <ip-address> is the device IP address

Once it is successfully connected you will see the following message

7. Next step is to connect with the WiFi. RaspberryPi 3 default come with a Wifi Module, so you do not want to connect any external Wifi Modules. Type this command to connect to Wifi. Once you do so, restart your RaspberryPi device.

8. Once you restated it RaspberryPi will connect to your network through Wifi. You will see another IP address is assigned to your device via Wifi. Now you can disconnect the ethernet cable and connect the device through Wifi.

9. You need to type the same command that we type earlier to connect to adb through the Wifi IP address.

Once it is successfully connected you will see the following message

Setting up the Circuit

10. Now you are ready to install the Android application to your device. Before that you need to setup the LED bulb as follows with your RaspberryPi

As  in the picture you can see the Cathode is connected to the Ground pin of the RaspberryPi and the Anode is conned to the BCM6 pin through the resistor. Please setup your circuit as above image.

Here is the Pinout Diagram of RaspberryPi

Now It’s time to get in to cording.

Connect with Firebase

11. First you need to go to Firebase and create a Firebase Project.  If you do not about Firebase I recommend you to do this Firebase Android Code lab tutorial first to understand about how Firebase works.

12. Please create the Firebase database as follows. Here we have a property called delay.

13. Please go to rules section and change the rules as follows

14. Download the Android-Things-RaspberryPi-Firebase project from the Github. I created this project based on the project.

15. Open the project using Android Studio. Update your Android Studio version, SDK version, build tool version and Gradle version if it required to do so.

16. Get the google-services.json file from the Firebase project and copy it to your app folder.

17. Once it successfully compiled you are about to run your first Android Things project which is configured with Firebase.

18. Click on ‘Run’ button in Android studio and select your device.

19. Now your application will be run on your device and you will see the bulb is blinking.

20. Go to your Firebase console and change the delay

Now you can see the blinking delay of the LED bulb will be changed realtime over the air with Android Things, RaspberryPi and Firebase.

Please see the below video to see it in action.

Explanation of the code.

Here how to get the GPIO pin for RaspberryPi. It is BCM6 for RaspberryPi device. (BoardDefaults.java)

This method will return the name of the board (BoardDefaults.java)

Here is the class that you have to write to get the config (Config.java)

 

This is how you get the config real time from Firebase and get the interval in milliseconds (HomeActivity.java)

This will run a separate thread and change the state of the LED buld

Git Hub Project: https://github.com/chaturadilan/Android-Things-Raspberry-Pi-Firebase

Please feel free to contact me if you have question. I hope you can build many awesome IoT projects with Android Things, RaspberryPi and Firebase.

4,943 total views, 9 views today

 

One thought on “Getting Started Android Things with Raspberry Pi and Firebase

Feel Free to ask questions and share your thoughts