Thursday, April 27, 2017

My Android Things

AndroidThings

Android Things is the new name that Google has adopted for its OS for Internet of Things. Before, the code name was “Brillo”.
Google has made available Android Things to developers, as a developer preview, at the end of 2016.
At the beginning of April 2017 (06/04), Google has released the DP3, adding for example BLE support, and it has been generally made available.

See the announcement here:


What is interesting is that it can be installed on a set of prototyping boards, such as Raspberry PI 3 (RPI3) and Intel Edison and therefore you can easily test it (obviously if you have one of these boards available).

My AndroidThings first setup

I have decided to give it a try on an Intel Edison Board with Arduino Breakout Board. I have also available one old BPM180 sensor (from Bosch, a cheap temperature and pressure sensor).
You can find the instructions to download the Edison image and to flash the image here:


The suggested IDE for development is Android Studio. I have downloaded and installed the version 2.3.1 on my MacBook and have resorted to all my energies to remember my Android’s knowledge, developed along my Coursera’s MOOCs.

Anyway, if you download and study the available examples, it is not difficult. Well, there are some important things to be aware of:
  • It is a Developer Preview; If you have a close look at the LogCat, you will see scrolling many warnings and errors; Normally, don’t pay too much attention, but be careful about what can be of interest (well, rather difficult if you see a lot of warnings scrolling, don’t you agree? But it is a DP);
  • You won’t find available drivers for every kind of sensors; actually, there are only a few drivers already developed. To be able to use my old BMP180 sensor I have spent some time searching and investigating, and at the end I have adapted some code taken from here: https://github.com/euler2dot7/android_things_bmp180. In Android Things' examples, you will find a driver for the updated BMP280 sensor.
  • You can use Android Things without a monitor. Actually, Intel Edison hasn’t it. It is ok, and you can also write code managing a UI and run it without the UI.
  • You can use a driver statically and in addition register it with the SensorManager. This way it is treated as all the sensors that you normally have on a phone.
  • The entry point to write a program (to experiment) is always an Activity. Therefore you’ll write the code for initialization in onCreate() method and so on.
  • If you connect your board to the PC using a micro-USB cable, as described in the Google’s instructions, you can develop, deploy and test the program directly inside the IDE; Cool.

I’m interested in the setup of a Sensors’ boards communicating with the rest of my HomeAutomation through MQTT protocol. I have found this interesting resource on the Web on the subject:


So, I have setup quickly a prototype using:
  1. The modified version of the sample WeatherStation, adapted (see link above) to send readings over MQTT (https://github.com/kartben/weatherstation)
  2. MQTT communication is handled using Eclipse Paho libraries; There is an AndroidClient;
  3.  I have used a modified version of the BMP180 driver, taken from  https://github.com/euler2dot7/android_things_bmp180;
  4. The board and the sensor are connected as in the picture: the sensor is I2C based (the Yun is there as part of a previous hacking, sorry).

Well, it has not been an easy exercise. First of all I have discovered some problems with MQTT in Android.
In Android, usually, any long operation must be executed on a thread different from the foreground thread. Normally you would use an Handler or an Async Task.
The MQTT Android Client is a wrapper around a Service. But I had the problem that it tried to publish a message before a connection was established. I added a quick fix testing if the connection was up, before publishing a message (seems a bug in the code, but found no mention on the Net).
The code of the Weather Station was much more complicated than I needed. I removed all references to LedStrip, Display and so on. Then I changed from BMP280 to BMP180.
One annoying problem was that I was not able to change the name of Java packages easily. Probably a limitation in Android Studio, or in my knowledge (higher chance).
Last problem: what was the unit used for Pressure? Well, after some time I discovered it is Pascal (1 Pascal = 1/100 mbars); Now it makes a better sense.
It has worked, and I’m able to read temperature and pressure and send it to my MQTT broker.
I’ll soon upload the code on GitHub.


Conclusions:
  • Android Things is interesting: it is coming from Google and we will see more and more from them in IoT space;
  • With Android Things you can follow the Android Development Model, use Android Studio and develop for Embedded Devices;
  • Google has added an easy access to TensorFlow; Therefore, it should be not complicated to add Machine Learning capabilities (There is one sample that shows Image Recognition using RPI3 and a Camera)
  • There is a growing set of Device Drivers; Intel is porting MRAA and UPM libraries to Android Things (on Edison and Joule);
  • You can develop in Java (useful, if you have confidence with it);
  • The complete set of code samples

No comments:

Post a Comment