Getting Started Android Things with Raspberry Pi and Firebase

Chatura Dilan Perera
7 min readDec 16, 2016

--

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.

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.

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

adb connect <ip-address>

Once it is successfully connected you will see the following message

connected to <ip-address>:5555

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.

adb shell am startservice \
-n com.google.wifisetup/.WifiSetupService \
-a WifiSetupService.Connect \
-e ssid <Network_SSID> \
-e passphrase <Network_Password>

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.

adb connect <wifi-ip-address>

Once it is successfully connected you will see the following message

connected to <wifi-ip-address>:5555

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.

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

{
"rules": {
".read": "true",
".write": "true"
}
}

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.

Here is the explanation of the code.

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

public static String getGPIOForLED() {
switch (getBoardVariant()) {
case DEVICE_EDISON_ARDUINO:
return "IO13";
case DEVICE_EDISON:
return "GP45";
case DEVICE_RPI3:
return "BCM6";
case DEVICE_NXP:
return "GPIO4_IO20";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}

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

private static String getBoardVariant() {
if (!sBoardVariant.isEmpty()) {
return sBoardVariant;
}
sBoardVariant = Build.DEVICE;
// For the edison check the pin prefix
// to always return Edison Breakout pin name when applicable.
if (sBoardVariant.equals(DEVICE_EDISON)) {
PeripheralManagerService pioService = new PeripheralManagerService();
List<String> gpioList = pioService.getGpioList();
if (gpioList.size() != 0) {
String pin = gpioList.get(0);
if (pin.startsWith("IO")) {
sBoardVariant = DEVICE_EDISON_ARDUINO;
}
}
}
return sBoardVariant;
}

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

public class Config {

private int delay;

public Config() {

}

public int getDelay() {
return delay;
}

public void setDelay(int delay) {
this.delay = delay;
}
}

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

ValueEventListener dataListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Config config = dataSnapshot.getValue(Config.class);
intervalBetweenBlinksMs = config.getDelay();
PeripheralManagerService service = new PeripheralManagerService();
try {
String pinName = BoardDefaults.getGPIOForLED();
mLedGpio = service.openGpio(pinName);
mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
Log.i(TAG, "Start blinking LED GPIO pin");
// Post a Runnable that continuously switch the state of the GPIO, blinking the
// corresponding LED
mHandler.post(mBlinkRunnable);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());

}
};
mDatabase.addValueEventListener(dataListener);

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

private Runnable mBlinkRunnable = new Runnable() {
@Override
public void run() {
// Exit Runnable if the GPIO is already closed
if (mLedGpio == null) {
return;
}
try {
// Toggle the GPIO state
mLedGpio.setValue(!mLedGpio.getValue());
Log.d(TAG, "State set to " + mLedGpio.getValue());

// Reschedule the same runnable in {#intervalBetweenBlinksMs} milliseconds
mHandler.postDelayed(mBlinkRunnable, intervalBetweenBlinksMs);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}
}
};

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.

Original Article : http://www.dilan.me/articles/tech-notes/getting-started-android-things-raspberrypi-firebase/

--

--

Chatura Dilan Perera
Chatura Dilan Perera

Written by Chatura Dilan Perera

Optimistic who loves to explore new technologies

No responses yet