UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 

MicroZed Chronicles: MicroBlaze, Linux, MQTT and IoT Frameworks

Xilinx Employee
Xilinx Employee
0 0 385

This content is republished from the MicroZed Chronicles, with permission from the author and Hackster.io.

 

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 MQTT protocol r the protocol to use its full name. 

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 once another node publishes data, the broker distributes the data to its subscribers. 

 

293_Fig1.png

In the remainder of this blog we are going to create a MQTT example which 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. 

293_Fig2.png

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

293_Fig3.png

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)

293_Fig4.png

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 (IoT) technologies such as If This Then That 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.