Saturday, April 2, 2016

IoHeating Part 2

Last time we built an internet connected heater with a REST interface. Being able to control anything physical from a web browser is cool. Being able to control it from your phone is much cooler.
Something like this

This post is about doing that. Let us not waste any time. Open up a copy of the GitHub repo. Even better pull it and open it in Android Studio

The core of this application is the communication with the device via their API. What I have chosen here is a compromise for time. We aren't using the normal method of authentication that you would take if you are releasing a product to a consumer market. Simply we are taking our credentials and building them into the application package. This means anyone who has access to that application package can control your device. You've been warned. On the positive side of this trade off the code is really easy to understand and doesn't require a lot of interactions with authentication APIs. 

The system we are using here is Bearer Authentication and part of OAuth2.0 spec. Conceptually it is a simple two part process. Firstly you the user authenticate to a server. You then say to the server. Give me a secret so big that no one can guess it. This secret is understood by the server to mean (since no one can guess it) that any other actor (program in our case) that knows the secret is authorized by you to do things on your behalf. The second step is exactly that you give a program the secret and it talks to the server to do work for you. We are simply breaking this up and doing the first step in our development environment so that we don't need to write a UI for it in our application. Particle has a detailed write up of how OAuth2,.0 works should you be interested. 

If you haven't already you need to setup the command line interface. Then from the CLI you can use the command. 

particle token new

to generate a token for your application.

There is a second slightly hacky method you can use to obtain a token which is pulling one out of the Particle build system. Open it up and navigate to the settings screen. You should see a token in place of the red square below. This has the advantage (and security issue) that it will not expire. You can also generate longer lived tokens using curl and performing REST calls to the Particle servers. 

You can now use the particle dashboard to find your device ID. Or the CLI if you are so inclined.

Substitute these values into the application (removing the square brackets) and you should be able to communicate with your device.

The Android app show a few patterns for working with this API. As it is a simple application with only a few functions we aren't implementing a service or any more complicated Android patterns. We just use the AsyncTask API.

So that is it. Really simple communication with your development device suitable for your simple use cases like controlling your own device with a bespoke app.

As an added bonus. There is some nice color animation depending on the heat. OR whatever you what to retrieve from your device.

No comments:

Post a Comment