As a dramatic closing exercise to the Enchanted Objects parade, I chose to try and build a C MQTT client for the SAMA5D4.
It turned out to be not dramatic at all. It was rather easy. Here's my log of the activities.
My ToolSet
I'm cross-building the library and executable on a Windows machine.
I used Eclipse for DS-5 - Community Edition as my IDE, and used the built-in ARM GCC 4.x toolchain.
My telnet client is PuTTY, and WinSCP does the file transfer.
I used the GIT command line to retrieve the Paho client code.
The Paho MQTT client utility was my testbed.
I've made a how-to on using these tools as part of my blog series.
Building the MQTT Shared Library
The source code for the eclipse Paho libraries is available from github.
You can find the instructions on eclipse's MQTT C Client for Posix and Windows.
Because I'm using DS-5 for the build (and also because the makefile didn't work on my Windows PC), I've only followed the steps to download the code.
C:\Users\Jan\Documents\elektronica\atmel\mqtt_lib\paho_from_source>git clone http://git.eclipse.org/gitroot/paho/org.eclipse.paho.mq tt.c.git Cloning into 'org.eclipse.paho.mqtt.c'... remote: Counting objects: 1719, done. remote: Compressing objects: 100% (5/5), done. remote: Total 1719 (delta 0), reused 0 (delta 0) Receiving objects: 100% (1719/1719), 787.24 KiB | 31.00 KiB/s, done. Resolving deltas: 100% (1177/1177), done. Checking connectivity... done.
Then I created a new Shared Library project in DS-5. I've used the makefile source as guideline to check what files to compile and what command line options to pass to the toolchain.
I carefully studied the makefile to understand what files to include/exclude from build, and what settings to port from make to DS-5.
I'm building the async lib version without SSL, so I took over the settings from that lib's make commands.
I excluded the files listed in this variable from compilation, just like in the make script.
SOURCE_FILES_A = $(filter-out $(srcdir)/MQTTClient.c $(srcdir)/MQTTVersion.c $(srcdir)/SSLSocket.c, $(SOURCE_FILES))
I also checked where this particular makefile variable was used:
MQTTASYNC_INIT = MQTTAsync_init
and incorporated it in the linker flags:
Then I built the project. That gave me a compiled .so library.
Build the MQTT Subscriber App
I used the async subscription example that's available in the same git extract. MQTTAsync_subscribe.c
I created a new project in DS-5, and added the source to that project.
I took care that the async shared library was available to the linker:
I modified the source to connect to eclipse's mqtt sandbox, and to listen to my turntable's topic:
#define ADDRESS "tcp://iot.eclipse.org" #define CLIENTID "1cfee8dd0afc_SAMA5D4" #define TOPIC "PerpetuumEbnerSpectrum"
Build, and done!
Load and Run
I deliberately didn't do all the steps to properly register the shared library.
I handled the part of setting the lib search path on the command line when invoking my executable.
There's a nice article on how to do a full-fledged install of a .so here: Shared Libraries.
I moved both the .so and the executable to root's home on the SAMA5D4 with WinSCP,
made both the program objects executable:
$ chmod +x Enchanted_Player_MQTT_Client
note to self: does an .so really need the exex flag set?
answer to self: no; explanation updated
and invoked the whole chebang using:
$ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./Enchanted_Player_MQTT_Client
Nothing more to say. Success. It just worked. My audio spectrum, sampled by the Yún inside my turntable, arrives nicely on the SAMA5D4.
Top Comments