Over the last few weeks, we have looked at how we can get MicroBlaze up and running using PetaLinux and connect it to the internet using Artix and Spartan-7 devices (P1, P2, P3 + P4).

These devices are ideal for running edge nodes which report telemetry and information back to central facility in the cloud. They can also receive information from the cloud and act upon it, driving sensors and actuators for example.

One simple and commonly used method of communication with cloud-based services is the Messaging Queuing Telemetry Transport (MQTT) protocol.

To work with MQTT in our PetaLinux applications, we can use the PAHO MQTT sources provided by Eclipse and available from GitHub here.

If you are not familiar with MQTT, it is very simple protocol — each node connects to a server called a broker. Once connected to the broker, the node can either subscribe to or publish data to the broker.

If we are connected as a subscriber, the broker distributes the data to its subscribers once another node publishes data.

In the remainder of this blog, we are going to create a MQTT example that publishes data to the IBM Watson IoT Platform using the quick start option. Using this option means we do not need to worry about creating or configuring an account.

So, how do we use this in our PetaLinux solution? The first thing we need to do is clone the PAHO MQTT libraries and create a new PetaLinux application.

When we clone the directories, we require the PAHO Embedded C repository; and when offered the choice of OS in different folders, select the Linux folder.

Under the application source, we need to include the PAHO Libraries within the SRC directory.

When it comes to writing the main application, we can easily use the following commands to connect and publish data to the broker:

initialize(Iotfclient *client, char *orgId, char *deviceType, char *deviceId, char *authmethod, char *authtoken);

publishEvent(Iotfclient *client, char *eventType, char *eventFormat, unsigned char* data, enum QoS qos);

To publish data correctly, it needs to be in the JSON (JavaScript Object Notation) format. JSON is a open format which allows us to transmit human readable attribute-value pairs.

A JSON attribute value pair is contained between {} with the attribute declared as text using “” and separated from the value with a :

Inside the JSON object we can have multiple attribute-value pairs separated using a comma.

In our C application running on the MicroBlaze, we can create a JSON string using the sprintf command as shown below:

sprintf(json_str, “{\”d\” : { \”Sine\”: %d}}”,data);

With the JSON string correctly formatted, we can then push this to the broker using the publishEvent call.

When I ran the code which captured a simple sine wave, the image below this was what I could see on the IBM Watson cloud using the quick start function. (Note this application should run on any MicroBlaze processor running PetaLinux.)

If we want to subscribe to a feed, we can use the function:

subscribeCommands(Iotfclient *client);

Now we know how we can work with a MQTT broker, we can also start working with additional Internet of Things technologies such as IFTTT to connect out FPGA to the wider world of applications.

I have uploaded the application to GitHub.

See My FPGA / SoC Projects: Adam Taylor on Hackster.io

Get the Code: ATaylorCEngFIET (Adam Taylor)

Access the MicroZed Chronicles Archives with over 290 articles on the Zynq / Zynq MpSoC updated weekly at MicroZed Chronicles.