Some time ago, IBM released a very cool project to the open source community. MQTT or message queue telemetry transport was originally used for communicating with orbiting satellites. Today, MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport and it is useful in practical applications from push notification in mobile Apps to distributed Sensor networks via message brokers.

In this particular project I am using the IBM Really Small Message Broker that supports MQTT, the download has executables for just about every platform out there.. Get it from here

My goal with this project was to create a quick operations console to interface with embedded hardware for messaging and switching. As I am part of an active project called Kivy.org I figured it would be a perfect tool to provide the user interface for the console. Kivy is a cross platform development framework based on Python and the C language. For further reading I urge you to click over to the http://kivy.org website and download a copy of the framework for your operating system and run some examples. The API is ultra easy and very feature rich including 2D & 3D support with standard widget libraries and Advanced multitouch gesturing support that all runs in the hardware GPU on Microsoft windows operating systems, Apple Mac OSx operating systems including IOS for the iPhone and iPad as well as support for Android and Linux. The example code I am providing in this blog entry can easily be built simultaneously for each of the above operating systems.

figure 1 – MQTT Topology for Messaging

The 1st step however in the project is to wire the circuit for the MBED Arm Cortex-M3 based system on chip. I chose the MBED for its power however you could equally use and Arduino with an MQTT library (1) to support the same function as the MBED. the MBED contains four pin configurable LEDs that I wanted to link to buttons in the Kivy UI Console, this can easily demonstrate the function of unlocking a door, turning on a fan, or switching on light. Additionally I also want to be able to send messages from the console in human readable text and have them show up on an LCD screen connected to the MBED.

I am assuming that you will be connecting to either a MAGJACK RJ45 Female connecter or use anMBED Dev board with one already connected. At the time of writing I could not find one in the Fritzing Part list.

figure 2 – MBED_MQTT_bb.png

figure 2 – MBED_MQTT_pcb.png

figure 4 – MBED_MQTT_pcb.png – Missing MAGJACK connection…

For configuration of the ethernet take a look here

Ok so now onto the MBED code, I have published the program to mbed.org for a quick start.

I have used the mbed MQTT Client Library found here and the TextLCD library.

include “mbed.h”

include “EthernetNetIf.h”

include “MQTTClient.h”

include “TextLCD.h”

EthernetNetIf ethernet;

Serial pc(USBTX, USBRX);

DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4);

TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2B); // rs, e, d4-d7

IpAddr serverIpAddr(192,168,0,2); /Sever ip address/

void callback(char topic, char payload); /Callback function prototype/

MQTTClient mqtt(serverIpAddr, 1883, callback);

void callback(char topic, char payload) { printf(“Topic: %srn”, topic); printf(“Payload: %srnrn”, payload); //Send incoming payloads back to topic “/mbed”. printf(“Topic: “); printf(topic, “rn”); printf(“Payload: “); printf(payload, “rn”);

char string_1 = “LED1″; char string_2 = “LED2″; char string_3 = “LED3″; char string_4 = “LED4″;

if (strcmp(payload, string_1)==0) { printf(payload); printf(“Setting LED1″); myled1 = 1; } else if (strcmp(payload, string_2)==0) { printf(payload); printf(“‘Setting LED2″); myled2 = 1; } else if (strcmp(payload, string_3)==0) { printf(payload); printf(“Setting LED3″); myled3 = 1; } else if (strcmp(payload, string_4)==0) { printf(payload); printf(“Setting LED4″); myled4 = 1; } else { lcd.printf(payload); }

//pc.printf(payload); //while(1) {pc.putc(pc.getc()

mqtt.publish(“/mbed”, payload);

}

int main() { printf(“rn############### MQTTClient Session ###########rnrn”);

EthernetErr ethErr = ethernet.setup(); if (ethErr) { printf(“Ethernet Error %drn”, ethErr); return -1; }

char clientID[] = “mbed”; /Client nanme show for MQTT server/ char pub_topic[] = “/mbed”; /Publish to topic : “/mbed” / char sub_topic[] = “/kivy”; /Subscribe to topic : “/mirror” /

if (!mqtt.connect(clientID)) { printf(“rnConnect to server failed ..rn”); return -1; } printf(“rnConnect to server sucessed ..rn”);

mqtt.publish(pub_topic, “Hello here is mbed…”); mqtt.subscribe(sub_topic);

int flag = 0; /Keep alive for 300s or 5mins/ while (flag < 300) { Net::poll(); wait(1); flag++; mqtt.live(); }

mqtt.disconnect();

printf(“#### End of the session.. ####rnrn”); }

Compile and deploy this code to the mbed.

The Kivy.org code can be downloaded from http://kivy.org/#download as a binary and simply installed, there are a few more steps for IOS and Android App Packaging, links to this documentation can be found in the download page.

The Kivy Console consists of two files. The python language file..

runlayout.py

from kivy.app import App import mosquitto client = mosquitto.Mosquitto(“kivy-client”) client.connect(“192.168.0.2″)

def send(text): print text

class MyApp(App): def send_text(self, text_input, *args): send(text_input.text) uni = text_input.text msg = uni.encode(‘utf-8′) client.publish(“/kivy”, msg, 1) text_input.text = ”

def callback(self, instance): print ‘The button <%s> is being pressed’ % instance.text client.publish(“/kivy”, instance.text, 1) instance.text = ”

if name == ‘main’: MyApp().run()

And the KV language file. See the language and file format at http://kivy.org/docs/guide/kvlang.html

:kivy 1.3.0

:import App kivy.app.App

BoxLayout: orientation: ‘vertical’ BoxLayout: Button: text: ‘LED1′ on_press: App.get_running_app().callback(self) Button: text: ‘LED2′ on_press: App.get_running_app().callback(self) Button: text: ‘LED3′ on_press: App.get_running_app().callback(self) Button: text: ‘LED4′ on_press: App.get_running_app().callback(self)

FloatLayout: Button: pos_hint: {‘center_x’: .5, ‘center_y’: .5} size_hint: None, None size: 200, 100

BoxLayout: TextInput: id: text_input Button: size_hint: None, 1 width: 50 on_press: App.get_running_app().send_text(text_input)

Start the really small message broker with ./broker from your command line.

run “python runlayout.py” from your command line.

This video will walk you through the steps