IMONT SDK and the TI SensorTag: a step-by-step guide

Romanas Sonkinas
May 30, 2017 · 7 min read
Image for post
Image for post

We’re going to explore, step-by-step, how to integrate one of the most popular prototyping devices out there — the Texas Instruments CC2650 SensorTag with the IMONT SDK. We’re going to use the Bluetooth Low Energy (BLE) connectivity on the SensorTag to connect it to a Raspberry Pi 3, which will be running an instance of the SDK, and we’ll show you how to connect that to an Android phone running another instance — and how the two will exchange information directly without any intermediaries.

Pre-Requisites

This article assumes you have a working knowledge of Java and the development ecosystem around it — specifically, how to use Gradle/Maven to manage a project and it’s dependencies. Basic JavaScript is useful to work on the SensorTag device driver.

Step 1: Prepare your Pi

Install Java8 — either OpenJDK or OracleJDK will work:

Now we need to get the latest version of Bluez — Linux’ Bluetooth library. The one bundled with Raspbian tends to be fairly out of date (5.23 at the time of writing) and does not support BLE very well, so we’ll want to get the latest and greatest — which (again, at the time of writing) is 5.45. Bluez can be downloaded from the official site here. Full instructions on setting up BlueZ can be found in this nice article from Adafruit, but in a nutshell:

To simplify things and avoid the need for running the app as root, add the default pi user to the ‘bluetooth’ group, which can be done like this:

Then edit the D-Bus configuration file /etc/dbus-1/system.d/bluetooth.conf and add the following policy to allow the bluetooth group to access BlueZ:

Step 2: Get the SDK

Go to https://sdk.imont.io, register with your favourite identity provider and you’ll be able to access the SDK. The SDK is not an application in itself, but a set of libraries — it’s up to you to build the app that would start it. Don’t worry — we provide an example. Follow the instructions on the SDK site to set up Gradle (or Maven if you prefer).

Step 3: Get the example Command Line app

Available on GitHub: https://github.com/imont/sdk-example-cli-app. The app comes pre-configured for Bluetooth on the Pi and the SensorTag driver pre-loaded.

The basic dependencies that the app declares are on the SDK, the Bluetooth/BlueZ Network layer and logback for logging:

Now just build the project with Gradle:

And copy build/distributions/sdk-example-cli-app-0.1-SNAPSHOT.zip to your pi. After unzipping it, you should be able to run the app by typing.

Provided there were no errors, you are ready to go to the next step.

Step 4: Pairing an Android app with the Raspberry Pi

Available on GitHub: https://github.com/imont/sdk-example-android-app

Now that the CLI app is running on the Pi, we are ready to start our phone app and pair the two SDK instances together. Open the app in Android Studio and install it to your phone (Note: using an emulator which is effectively behind a NAT, will prevent the two instances from seeing each other). After starting the example Android app, you will be greeted with a simple screen that shows two options: either creating a new network, or joining an existing one. Since we are starting from scratch, we’ll want to create a new one:

Image for post
Image for post

We’ll be greeted with a screen showing the instance of the SDK running on the Pi:

Image for post
Image for post

If we click on the discovered instance, the phone and the Pi will pair, and you should now see a new screen showing the two devices in your network (drag down to refresh if the Pi does not appear):

Image for post
Image for post

Step 5: Adding the SensorTag as a bridged device

Since the SensorTag is an embedded device and is thus unable to run an instance of the IMONT SDK, we’ll need to add it as a bridged device — and we want the Raspberry Pi to do the bridging. Fortunately we’ve already set everything up on the Pi, so all we need to do is click on the Pi in the app, and then select “Find Bridged Device”

Image for post
Image for post

The SDK on the Pi will now scan the surrounding Bluetooth spectrum and see what devices we’ve got available — you’ll most likely find quite a few here. Once the SensorTag is discovered, click “Acquire” and the Pi will connect to the device for you and pair (this might take up to 15s, you’ll be notified when it’s done).

Image for post
Image for post

Click “Finish” once that is done, and you will see the Sensor Tag as a bridged device in the main device list:

Image for post
Image for post

If you click on the SensorTag device now, you’ll be taken to a screen showing all of the available sensors and their current state, including things like temperature, atmospheric pressure and light intensity. By default, the driver re-reads all the values every two minutes, but you can configure that to be done at a higher interval in the driver itself.

Image for post
Image for post

Leave the device running for some time and observe how those values change.

Appendix: The actual JavaScript driver

The default driver for the SensorTag is bundled together with the example CLI application that you have used previously. It can be found in src/main/resources/drivers/ti-cc2650-sensortag.js. Let’s take a look at some of the basic features that are used in the driver, and how that can be adopted for other Bluetooth Low Energy devices.

First, any driver in the SDK must declare some metadata, telling the SDK what sort of device it is meant for. The metadata for the SensorTag is fairly simple:

When a device is added to the SDK, the first thing that happens is the onLoad function is called. In our case, we set up callbacks to report the various sensor values every once in a while, depending on how often the value is likely to change — feel free to experiment on the exact timings. More frequent polling = higher battery usage and a larger database in the SDK.

By default, none of the sensors are actually switched on, so we make sure they are whenever we do the polling.

When those readings are taken, they are written to the internal database within the SDK — called Mole — which handles replication of data between the various peer instances in our network (the Android app in this case). In the driver, this is done in the reportIfChanged function, which, as the name implies, only saves new readings if it differs from the previous one:

To do the actual reading from Bluetooth, we rely on the bluetooth network layer, and the various primitives that it provides. To anyone who has worked with Bluetooth Low Energy before, they should be the quite familiar concepts of readAttribute and writeAttribute, which are provided in the framework.ble namespace, i.e.:

The SensorTag also has a buzzer and a couple of LEDs which we can control — that is done in onRequestEvent which is invoked automatically by the SDK whenever one of the peers raises a request event — read more about those in the SDK docs. In the simple implementation given in this driver, we just write a characteristic to the sensor tag whenever we receive such an event:

You can view this in action in the app, by clicking on the ON_OFF>ON_OFF attribute in the SensorTag device view.

Resources

IMONT SDK: https://sdk.imont.io
TI SensorTag: http://www.ti.com/ww/en/wireless_connectivity/sensortag/
Example Raspberry Pi app: https://github.com/imont/sdk-example-cli-app
Example Android app: https://github.com/imont/sdk-example-android-app

IMONT Technologies

Developers of the IMONT SDK — A next-generation tool for…

Thanks to Stephen Barker

Romanas Sonkinas

Written by

Developer, DevOp and IoT geek

IMONT Technologies

Developers of the IMONT SDK — A next-generation tool for device connectivity. https://www.imont.io

Romanas Sonkinas

Written by

Developer, DevOp and IoT geek

IMONT Technologies

Developers of the IMONT SDK — A next-generation tool for device connectivity. https://www.imont.io

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store