Kivy OpenGL console controlling MBED via MQTT

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 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 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 – Breadboard layout

figure 2 – Circuit Schema

figure 4 – Circuit Board – 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 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: %s\r\n”, topic);
printf(“Payload: %s\r\n\r\n”, payload);
//Send incoming payloads back to topic “/mbed”.
printf(“Topic: “);
printf(topic, “\r\n”);
printf(“Payload: “);
printf(payload, “\r\n”);

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

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

//while(1) {pc.putc(pc.getc()

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


int main() {
printf(“\r\n############### MQTTClient Session ###########\r\n\r\n”);

EthernetErr ethErr = ethernet.setup();
if (ethErr) {
printf(“Ethernet Error %d\r\n”, 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(“\r\nConnect to server failed ..\r\n”);
return -1;
printf(“\r\nConnect to server sucessed ..\r\n”);

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

int flag = 0;
/*Keep alive for 300s or 5mins*/
while (flag < 300) {


printf(“#### End of the session.. ####\r\n\r\n”);

Compile and deploy this code to the mbed.

The code can be downloaded from 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..

from import App
import mosquitto
client = mosquitto.Mosquitto(“kivy-client”)

def send(text):
print text

class MyApp(App):
def send_text(self, text_input, *args):
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__’:

And the KV language file. See the language and file format at

#:kivy 1.3.0

#:import App

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

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

id: text_input
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” from your command line.

This video will walk you through the steps