Cambridge Hackathon


Mbed Cambridge Hackathon

You are viewing an older revision! See the latest version

MQTT Python Broker with Mbed Client

Building an MQTT Python Broker with Mbed Client

This tutorial will guide you through creating an MQTT Python Broker with Mbed Client using the Mbed Connect Cloud board. We first will cover creating the embedded client for the board.

Prerequisites

Required Hardware

Setup

  1. Connect the board to your computer via the micro USB port.
  2. The board mounts as a mass-storage device (like a USB drive). Verify that you can see it (the drive name will be DAPLINK).
  3. Add the u-blox EVK-ODIN-W2 board to your online Mbed Compiler here

Making the Mbed application

Import the example program. DO NOT CHECK the checkbox next to "Update all libraries to the latest revision".

Import programMQTT-Python-Demo

Demo for Mbed Connect Cloud board and an MQTT Python Broker/Client

Printing to the screen

Open main.cpp

Instantiate a C12832 variable to write to the LCD screen on the board. Under 'GLOBAL VARIABLES HERE' add the following code:

C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);

Create a function to print to the LCD screen. Under 'FUNCTION DEFINITIONS HERE' add the following code:

void lcd_print(const char* message) {
    lcd.cls();
    lcd.locate(0, 3);
    lcd.printf(message);
}

Setting up WiFi

We will add some code to initialize the Mbed Connect Cloud board's WiFi module and connect to your local WPA/WPA2 WiFi network.

Open mbed_app.json

We need to tell Mbed the name of our WiFi network's SSID and password.

Under wifi-ssid change the "value" from SSID to your WiFi's name (for example: "\"iotlab\"") and do not remove the quotes.

Under wifi-password change the "value" from PASSWORD to your WiFi's password (for example: "\"abcd1234\"") and do not remove the quotes.

Open main.cpp

The Mbed Connect Cloud board uses the u-blox EVK-ODIN-W2 WiFi module. So to initialize our WiFi connection we need to instantiate a OdinWifiInterface variable. Under 'GLOBAL VARIABLES HERE' add the following code:

OdinWiFiInterface wifi;

Add some code to tell the WiFi module to connect to your wifi network. We will also print to the LCD the status of the WiFi connection. Under 'MAIN CODE HERE' add the following code:

lcd_print("Connecting...");
int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
if (ret != 0) {
    lcd_print("Connection error.");
    return -1;
}
lcd_print("Successfully connected!");

The MBED_CONF_APP_WIFI_SSID and MBED_CONF_APP_WIFI_PASSWORD are variables defined by the mbed_app.json file we edited previously. NSAPI_SECURITY_WPA_WPA2 tells Mbed OS that our WiFi network has WPA or WPA2 security (so it requires an SSID and password).

Creating the MQTT Python Server

This code is based off of the HelloMQTT example here. First, you need to have the "paho-mqtt" Python package installed on your computer in order to run an MQTT broker/client locally on your machine.

Install the "paho-mqtt" Python package by running the following command in your terminal: pip install paho-mqtt

Download broker.py from the example repository's "broker" folder here to your computer.

Open a terminal window on your computer and navigate to the directory you downloaded broker.py into. Run the following command to start the script: python broker.py

You should receive output similar to the following:

$ python broker.py
Your IP address is: 192.168.0.77
Connecting to /Mbed/#
Traceback (most recent call last):
  File "broker.py", line 35, in <module>
    mqttc.connect(host, port=1883, keepalive=60)
  File "/usr/local/lib/python2.7/site-packages/paho/mqtt/client.py", line 767, in connect
    self.connect_async(host, port, keepalive, bind_address)
  File "/usr/local/lib/python2.7/site-packages/paho/mqtt/client.py", line 822, in connect_async
    raise ValueError('Invalid host.')
ValueError: Invalid host.

We receive this error because we did not yet specify our MQTT broker's local host. Copy the IP address printed after "Your IP address is:" in your terminal window. Open broker.py in a text editor on your computer. Paste your IP address in between the quotes after host = on line 10 (under # Settings for connection).

Now run the python broker.py command again in your terminal window. You should now be able to successfully start your MQTT broker/client and your output should be similar to the following:

$ python broker.py
Your IP address is: 192.168.0.77
Connecting to 192.168.0.77/Mbed/#
Connected rc: 0
Subscribed OK

All wikipages