Internet of Things – Wearable Actuators

Today I will show you how a wearable can be controlled from the cloud via a mobile phone. I will create a simple iPhone app that subscribe to an MQTT broker topic and set the color of an LED accordingly.

Just as we can send sensor data from a wearable to the cloud (as shown in my previous video), we can also let a wearable be controlled via the cloud. Similar to how a flower could be used to visualize ticket sales (as shown in my video called “Internet of Things – Business Intelligence Flower”) I will show you how a wearable can be used as a similar indicator.

Let’s create an iPhone app that subscribe to an MQTT broker topic and change the color of a wearable LED. 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/ios), and you need a Lightblue Bean (www.punchthrough.com/bean), which is 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-iOS-OSX-SDK) and the MQTTKit (https://github.com/mobile-web-messaging/MQTTKit) libraries to the project. Then update ViewContoller.h to look like shown here…

…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 here…

…and first we set up the bean manager (on lines 4-5). Then we set up the MQTT client to use the device’s unique identifier as the client identity (on line 6). When the bean manager is on (checked on line 11), we start looking for beans (on line 13), and when a bean is found, we connect to it (on line 19). When connected to the bean, we create a message handler (on line 27) that will respond to a subscription event by converting the message to a color (on lines 29-32), and setting the LED of the Lightblue Bean (on line 33). Then we connect to the MQTT broker (on line 35), and when connected, we subscribe to the topic (on line 39).

When you run the code on an iPhone, you can test it by publishing to the topic by opening the terminal, and enter the command on the top right:

curl -X POST --data-binary "ff0000" http://eclipse.mqttbridge.com/chris%2fpocket%2fled

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 the LED can be a continuously indicator of anything happening on the Internet.

That’s how a wearable can be controlled from the cloud via a mobile phone.