Tag Archives: mqtt

Using MQTT and Eclipse Paho in Android Things

A couple of days ago, Google announced that they were essentially rebranding Brillo to Android Things (I do love that name, by the way!), and finally opening it for a Developer Preview.

There are a few things I already like very much in Android Things:

  • It is already supported on Intel EdisonNXP Pico and Raspberry Pi 3, and there are ready to use filesystem images that you can just flash to get going with Android Things in just minutes.
  • The Rainbow HAT sensor kit that’s available for Raspberry Pi is very cool, and includes a 4-digit LED display, 7 RGB LEDs, a temperature and barometric sensor, a piezzo buzzer for basic PWM-based audio, and three capacitive touch buttons. Sparkfun has a kit that’s targeting the Edison, while Adafruit’s kit is general purpose and meant for breadboard enthusiasts.
Rainbow HAT for Raspberry Pi (Photo credit: Pimoroni)
  • Anyone who’s tried to manipulate low-level peripherals using Java will be pretty happy to see that Android Things’ Peripheral I/O APIs provide nice wrappers for GPIOsPWMI2CSPI and UART.
  • Implementing IoT sensor drivers taps into the existing sensor framework you may already be familiar with if you’ve already tried to access the gyroscope or light sensor of an Android device in your app, and the same SensorManager API you’re already used to can be used with your new devices (for which a driver may already exist, and if not adding a new one does not seem overly complex)
  • Finding good development tools for building IoT solutions is always a challenge. It’s great to be able to leverage the Android SDK tools and Android Studio for things like device emulation, debugging, etc.

I just received my Rainbow HAT today and thought I would use the opportunity to do a quick tutorial on how to use MQTT with Android Things, using Eclipse Paho. What’s more, I’ll also show you a cool feature in mqtt-spy that will allow us to easily display the live temperature on a chart.

I used the Weather Station example from Android Things as a starting point, as it is already including code to publish data to the cloud using Google Pub/Sub. My fork is available on Github, and as you can see the changes to the original example are very limited!

Check out the video, and let me know if you have any questions!

How to run your web server and MQTT WebSockets broker on the same port

I was just asked how one can deploy a similar setup as the iot.eclipse.org MQTT sandbox, where MQTT over WebSockets is available on port 80, just like the rest of the website.

There are actually two ways of achieving this.

Mosquitto as the main frontend

It’s a little-known fact but together with built-in WebSockets support (added in version 1.4), Mosquitto also can act as basic HTTP server, and directly serve a bunch of static resources for you. The config option you’re looking for is “http_dir“, that will allow you to serve the content of a directory over HTTP.

Granted you are running a version of Mosquitto that has WebSockets support, here how your mosquitto.conf file should look like to enable WebSockets *and* regular HTTP connections:

listener 80
protocol websockets
http_dir /home/johndoe/htdocs

Of course, you will need to make sure that you do not have any other daemons (like Apache, nginx, …) already running and using port 80 🙂

Once Mosquitto is setup this way, you can use any MQTT client that supports WebSockets to connect to ws://yourhost URI.

ws://yourhost/ws, or ws://yourhost:80/foobar would work just fine too – Mosquitto doesn’t care about the path at all!

Apache front-end + mod_websocket_mosquitto

Since it’s likely you actually want a “real” HTTP server to serve your website (for security reasons, for being able to run PHP, etc.), another approach is to use Apache as the main HTTP front-end, as you would normally do, and configure it to tunnel WebSockets connections made on a given URI to your Mosquitto broker.

You can download an Apache module that does exactly that at https://github.com/willem4ever/mod_websocket_mosquitto. The instructions to compile and install it are pretty straightforward and you will end up with something like the following in your Apache configuration:

<IfModule mod_websocket.c>
Loadmodule mod_websocket_mosquitto /usr/lib/apache2/modules/mod_websocket_mosquitto.so
 <Location /mosquitto>
 MosBroker localhost
 MosPort 1883
 SetHandler websocket-handler
 WebSocketHandler /usr/lib/apache2/modules/mod_websocket_mosquitto.so mosquitto_init
 </Location>
</IfModule>

Running Eclipse Mosquitto on Snappy Ubuntu Core

I’ve recently been playing around with Snappy Ubuntu Core, and I thought it would be a nice exercise to try and package Mosquitto as a so-called “Snap”.

This blog post will give you a quick introduction to Snappy, and what it means to have a technology like Mosquitto available as a snap package.

Snappy Ubuntu Core

snappy

Ubuntu Core is a minimal Ubuntu distribution, that doesn’t come with the classical, APT-based, package manager.
Ubuntu Core is targeting cloud & IoT environments, therefore it is using an approach where the applications or services that one installs in a given distro, are self-contained “snaps”, that are completely isolated from the rest of the environment. If a snap depends on a shared library, this library will be bundled with the snap. If a snap needs to be granted rights beyond the scope of their default sandboxed container, it will be specified in the snap metadata. And so on.

Why does it matter for the Internet of Things?

Being able to transactionally update a system definitely makes sense for cloud environments, but it is also key for Internet of Things solutions. Even more so if you take into account how easy it is to fire up a new EC2 machine for a system you just crashed, vs. actually going on the field to troubleshoot an IoT gateway that went off the network after a failed update…

Furthermore, IoT gateways today are operating a very limited set of services, since there’s no clean and safe way to really turn them into extensible platforms, where any partner involved in developing a given solution would be able to run its own bits, in dedicated sandboxes. Application frameworks like Kura certainly enable such things at the application level, but we need solutions like Ubuntu Core for anything closer to the system.

Mosquitto on Snappy Ubuntu Core

If you have ever played with MQTT, there’s a good chance that you have used Eclipse Mosquitto as your broker. It is a very customizable, feature-packed, broker that has recently added support for MQTT over WebSockets. Mosquitto is available in source form, but it is also distributed in binary form and as a ready-to-use package in many Linux distros.

The Mosquitto Snappy package contains a WebSockets-ready version of Mosquitto 1.4.2 that can run on x64 or ARM architectures. Therefore, you are literally 2 minutes away from having an up-and-running Mosquitto broker on Microsoft Azure, Amazon EC2, or Google Compute Engine. Since Ubuntu Core is also available for the Raspberry Pi and the BeagleBone Black, you can install Mosquitto on those platform as well.

If you want to try it yourself on EC2, for example, you can follow this tutorial to bootstrap your Ubuntu Core image. Once you have a shell access to your Core server, simply run sudo snappy install mosquitto.kartben, and Mosquitto will be automatically downloaded from the Ubuntu Software Store, and installed. WebSockets are enabled on port 8080 and as a matter of fact, I have also packaged a few extra resources with the broker to help you get started: if you open http://{myubuntucoreIP}:8080 in your browser, you will be granted with a web page that helps making sure you use the right connection settings, and also with a live visualization of your topics’ activity.

screenshot

MQTT on the TI CC3200 LaunchPad thanks to Paho embedded client

What I really like with MQTT and CoAP is that they both are very simple protocols. When dealing with MQTT, the client itself has almost no state to maintain (at least when you stick to QoS 0 communications) and granted that you have an MQTT packet serializer/unserializer, it’s very simple to stuff such MQTT packets into TCP sockets using the networking APIs that your IoT microcontroller is providing.

I’ve had the opportunity to play with the TI CC3200 LaunchPad platform recently, and thought it would be a good candidate to try out the Paho embedded C Client.

The tutorial below, split into two parts (publishing and subscribing), gives a complete overview on how you can very easily port MQTT to the CC3200 and should probably be useful if you’re targeting another kind of platform, as it walks you through the process of tying in with the networking API of the underlying platform (TI SimpleLink™ in CC3200’s case)

Powering EclipseCon France with IoT magic

In just about a week is EclipseCon France, and here’s one more reason why it’s going to be awesome! This year you will all be actors of a real-world Internet of Things setup, that leverages several Eclipse IoT technologies.

We’ve been wanting to do this for a couple years, and finally the great folks at the Grenoble University gave us a hand to build a system that will allow every EclipseCon France attendee to easily evaluate all the sessions!

How does it work?

Click on the drawing below and read the following chapters to learn more on how this voting mechanism has been implemented.

Voting machine drawing

NFC badges

nfc At the heart of the voting system is the NFC technology. In case you are not familiar, NFC (Near field Communication) allows to transmit information between devices when there are close to each other. The kind of information that can be exchanged depends on what devices are involved in the communication: two phones can e.g. use NFC to exchange contact infos or bookmarks (see Android Beam).

A use case that is very common is the use of RFID tags to “stick” information to an object, and allow this information to be retrieved later by any NFC-reader (smartphone, USB dongle attached to a computer, …). At a very minimum, this information is an ID that is unique to the tag and allows to identify the object you are “reading” ; other information can be stored in the tag but usually you only have just a few hundred bytes of memory on these lil’ things.

In the context of the voting machine, we will be adding an RFID tag to each participant’s badge, allowing to track who’s who when a vote will be performed, according to the unique ID of the badge.

Inside the voting machine, an MQTT publisher

Inside the voting machine is an Intel Galileo board (if you’ve ever heard of the Raspberry Pi, and I bet you did, the Galileo is a very similar beast, running on an Intel chip when the Raspberry Pi runs on an ARM architecture) that runs Linux, iPopo, and a Python MQTT client from Eclipse Paho. Attached to the Galileo are three NFC USB dongles that allow to detect the presence of a tag in front of the -1, 0, and +1 areas of the voting machine. iPopo is an OSGi-like container for Python scripts, and in our case it’s running several bundles for doing the NFC tags detection, handling the MQTT communication, etc. You can actually check out the source code of the Python bundles on Github.

Whenever a tag is in proximity of the voting machine’s -1/0/+1 areas, a very simple MQTT message (the NFC tag ID and the vote are the only informations transmitted) is sent to iot.eclipse.org, allowing any consumer to subscribe to the broker to make sense of this information.

Interaction with eclipsecon.org

One of the strength of MQTT and an actual requirement for making scalable IoT architectures, is the ability to decouple data producers from consumers. And this is exactly what we are leveraging in the context of these voting machines: each voting machine publishes very naive messages to a broker, and it’s very easy to add a new node on the IoT network that will monitor such messages, and react. One node could be responsible for persisting the votes in a database, another could display them graphically, etc. The important point being that we’re not introducing any tight coupling between all these “agents”.

So in order to convert the raw vote “signals” into meaningful votes that can be fed into the eclipsecon.org website, we need to have a new node on the network that will have the knowledge of:

  • the mapping between NFC tags IDs and attendees (since we want to eventually post session reviews on behalf of a conference badge owner),
  • what session is in what room at a given time

Therefore in addition the voting machines, we’ve deployed a Raspberry Pi running a NodeJS application that uses MQTT to retrieve “raw” votes, compute the associated attendee + session for which the vote was made, and turns this information into ah HTTP POST request to the Drupal backend that powers eclipsecon.org!

See you real soon!

EclipseCon France 2014

Thanks again to the LIG team, and to Paul Guermonprez from the Intel Software Academic Program who provided most of the hardware used in the voting machines.

I’m really looking forward to seeing these cool machines in action next week! If you haven’t already, wait no longer and register today before the registration fee goes up!

IoT Toulouse meetup #1 Videos

Arduino pour l’Internet des Objets

Du prototypage rapide avec Arduino au premier prototype industriel

Introduction à MQTT

Turning a toy robot into an IoT device with Eclipse technologies

The Rapiro is a nice open-hardware robot, controlled by an Arduino and a Raspberry Pi.

Earlier this week, I decided that he’d be nice if I could run Eclipse Orion directly on the robot, allowing me to write an app that would bridge the Rapiro to the Internet (there’s a WiFi dongle attached to the RaspberryPi so the robot does have Internet access), using MQTT.

I wrote about my experience in this blog post at Element14, so check it out! If you’re more interested in the final result, you may just want to check out the video below 🙂

Oh, and when I say it’s open-hardware, it really is: the 3D models of all the part of the robot are now on Thingiverse, so you are actually free to 3D-print your own version of the robot!