One of the joys of Android development is that; whatever your idea, there is likely to be a library available to help you out. Most new projects begin with the addition of a few favourites.  If you are going to be consuming any web APIs then high on your list should be retrofit. In Android Things a few of the familiar Android APIs are not available. This may affect you if you are porting some existing Android code. It may also impact your choice of library and you’ll need to check that it doesn’t use a missing API.

In this example we’ll be continuing development on the LCD driver. We’ll be adding retrofit to ping the bus company for bus arrival times at a couple of local bus stops. It’s about time we got to the ‘Internet’ part of IoT 🙂

Here in Wellington some of our bus stops have live displays showing the times of the next few buses. Whenever I get the looser cruiser I check an app to see when I should leave the house,  which is like four or five clicks/swipes etc on my phone. Ain’t nobody got time for that. I need that information displayed in my house somewhere.

 

Two displays, one local stop each.

There is an unofficial API that can be accessed to get the live timing of busses in the system. In this project I want to have more than one LCD display. This means I’ll need to modify the LCD driver to add the ability to specify data and control masks and pin descriptions. These are hard coded values bit I’ll need to make them variables. Passing them in to the constructor is error prone. Getting the right order of arguments sounds like something I’m going to screw up. We’ll use a builder instead.

Changes to the LCD Driver

If you have the code cloned from the previous example, switch to the things-driver-0.1.0 tag. Have a look at the differences here. What I’ve done is to add a builder, this allows me to specify each of the PCF8574 pin functions at construction time. I no longer have to change hard coded values to support different pin configurations.

The first thing we do is initialise an I2cSerialCharLcd.builder. Pass it the LCD’s width and height. Then we specify the pin mapping, the numbers map to port pin names, i,e. 0 is P0 on the PCF8574. You can use this to setup your PCF8574 to LCD mapping. Specify your module’s address with, strangely enough, the address() builder method. This takes the value of your A0 – A2 pins.

Once you have a reference to the lcd, you can connect to it and write as before. One small change to the print() method means that you now pass it the line number as the first argument and not the DDRAM address.

Setting up Retrofit

Setting up retrofit is already extensively documented. The reference I usually go to is CodePath. I took an example of the MetLink ‘API’ from here and ran the response through jsonschema2pojo to generate the model. It’s too easy. The result is upcoming bus arrivals at a couple of local stops, the first column is the route number, the second column is the destination stop, the third is the number of minutes to the bus arrival.

The  Source

Here’s the source for the main activity.