element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • 'Choose another store...'
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs An Updated MQTT Client Solution using C++ Templates on the Avnet NB-IoT Sensor Shield
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: james.flynn
  • Date Created: 24 Apr 2018 1:09 AM Date Created
  • Views 772 views
  • Likes 1 like
  • Comments 0 comments
  • embeded
  • mqtt
  • mbed os 5
  • avnet design
  • iot devices
  • iot_design
  • avnet_iot
Related
Recommended

An Updated MQTT Client Solution using C++ Templates on the Avnet NB-IoT Sensor Shield

james.flynn
james.flynn
24 Apr 2018

There are several MQTT solutions available for many different platforms and languages, and when you go looking for a ready-made solution to use, you may end up at https://github.com/mqtt/mqtt.github.io/wiki/libraries which provides several solutions and a lot of history.

 

The solution I wanted would be implemented in C++ and storage would be allocated during object creation as opposed to me separately defining it. Because I was going to use the Mbed OS I decided to use https://os.mbed.com/teams/mqtt/code/MQTT/ as my starting point. Unfortunately, this software is targeted at Mbed OS v2.x which is fairly old given that the last release was v5.8.  So it needs some updating to get it working with Mbed v5.x.

 

In this MQTT solution, the Class methods use a transport Class to communicate with the systems communications interface. In the class from the link, an Ethernet interface (MQTTEthernet.h) is provided but to utilize a different interface, a new transport class would be needed. The good news is that the transport class is very limited in what it implements and as provided, the MQTT solution works; the bad, there are several warnings generated when compiling and some code duplicates functionality already present in the MBed OS.

 

So as I started working with the MQTT library, I had three goals:

  1. Minimize the unnecessary code, specifically, the FP Class
  2. Eliminate the warnings that were being generated when compiled (which incidentally were mostly because of the FP Class)
  3. Remove the duplication of code that was necessary when the MQTT code was used with different Network Interfaces.

 

Not a tremendous amount of work, essentially:

  1. Removing the FP Class callbacks and replace them with the current Mbed OS functions
  2. Creating a new Template class to automatically create the transport class used with the communications interface.

 

In the MQTT Class, the user to must provide pointers to functions for the class to use. In Mbed v2.x, there was no call-back function provided so an FP (Function Pointer) Class was used.  In the downloaded MQTT source, this is an artifact from the earlier Mbed version.  In recent Mbed OS releases, call-back functionality has been incorporated as a Callback class (https://os.mbed.com/docs/v5.6/reference/callback.html).   Even though the FP and Callback classes implement similar functionality, the method names and some of the parameters have changed—so to achieve my goal #1, I removed the FP Class and modified the MQTT template functions with the correct method parameters.   By implementing goal #1, I was also able to achieve goal #2 because the older legacy code was no longer compiling and that was what generated the warnings I was encountering.

 

Goal #3 was achieved by creating a new class template that creates the necessary transport interface at compile time. The class itself simply creates an Interface Class, connects it, and returns a pointer to the class:

 

template 
class MQTTconnect : public MQTTSocket
{
    T eth;
  public:
    MQTTconnect() : MQTTSocket(ð)
    { eth.connect(); }
 
    T& getEth()
    { return eth; }
};

 

When this template is used, it creates an MQTTconnect class using the Interface that the caller specifies.  The class itself simply connects to the interface when created and provides a method to return a pointer to the interface.  To ease the use of this template, a macro was created:

 

#define MQTT_USE(x)  typedef MQTTconnect MQTTct; typedef x MQTTnet;

 

That macro allows a user to define the interface to use with MQTT and creates two types of elements (MQTTct and MQTTnet). This makes using the MQTT software as easy as telling MQTT what interface to use, for example:

 

MQTT_USE(BG96Interface);
MQTTct   net;
MQTTnet& eth = net.getEth();

 

With this MQTT implementation, you can now simply ‘Define’ the Interface to use rather than create a new, separate class.

 

I used this MQTT implementation with the Avnet NB-IoT Sensor Shield (Available from https://www.avnet.com/wps/portal/silica/products/new-products/npi/avnet-nb-iot-shield-sensor) sensor board in combination with a X_NUCLEO_L476RG SBC and X_NUCLEO_IKS01A2 sensors expansion board.

imageimage

 

The code can all be obtained at https://github.com/Avnet/BluemixQS_BG96.  Try it out and see if it makes implementing MQTT easier...

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube