Today I will talk about how you can make wearable sensors publish their data to the cloud via a mobile phone. I will create a simple iPhone app that collect information from a wearable sensor using Bluetooth and then publish that information to an MQTT broker.
The idea of wearables is not new, and depending on definition, one could argue that one of the first was the 500 year old Abacus ring (http://gizmodo.com/this-wearable-abac…). The thing that has made them more interesting recently is their connectivity, which makes them part of the Internet of Things. Making them connect themselves would require a connection to a cellular network, and even if possible, it wouldn’t be a very practical solution. Instead, using Bluetooth (Low Energy), they can connect to a mobile phone, that in turn would connect to the cloud.
Let’s create an iPhone app that read the temperature sensor of a wearable device and publish it to an MQTT broker. To do that you need an iPhone 4S or later as well as register as a paying iOS developer (you can enroll at https://developer.apple.com/programs/…), and you need a very nice device called the Lightblue Bean (http://www.punchthrough.com/bean). It’s an Arduino-compatible microcontroller with built-in Bluetooth (Low Energy) that has a coin cell battery to be constantly on and connected.
Now it’s time to open Xcode and create a new project as a single view iOS application, and start by adding the framework CoreBluetooth. You also need to add the LightBlue Bean SDK (https://github.com/PunchThrough/Bean-…) and the MQTTKit (https://github.com/mobile-web-messagi…) libraries to the project. Then update ViewController.h to look like shown on http://cforsberg.com/?p=213, and first we import the headers for the libraries (on lines 1-3). Then we add delegates for the bean (on line 5), and define properties for the MQTT client, bean manager, and bean (on lines 7-9). Then we need to update the implementation part of ViewController.m to look like shown on http://cforsberg.com/?p=213, and first we set up the MQTT client to use the device’s unique identifier as the client identity (on line 4). Then we connect to the MQTT broker (on line 5, and note that we are using the free Eclipse sandbox, http://iot.eclipse.org/sandbox.html), and when connected, we set up the bean manager (on line 9-10). When the bean manager is on (checked on line 17), we start looking for beans (on line 19), and when a bean is found, we connect to it (on line 25). When connected to the bean, we set up a timer (on line 32) that read the temperature sensor of the bean (on line 37) every five seconds. Each time the temperature is read, a message is published to the MQTT broker (on lines 42-43, and you can see the published value if you follow the link on the top right, http://eclipse.mqttbridge.com/chris%2…).
The cool thing is that if you set the iPhone app project to allow background Bluetooth LE operation (select the project on the left and the Capabilities tab, find Background Modes, turn it on, and check Uses Bluetooth LE accessories), the app will continue to run when the app is in the background, and even if the phone is locked. So anyone anywhere can continuously monitor my temperature by subscribing to the MQTT topic, and that could be true for any kind of sensor connected to the Lightblue Bean (e.g. heart rate sensor).
That’s how you can make wearable sensors publish their data to the cloud via a mobile phone.