8 Mar 2012

A new version of the Arduino MQTT client is available – from the usual place.

This release brings a handful of changes, but there are a couple I wanted to draw attention to; particularly as one will require a minor change to sketches using this library.

When I wrote the first prototype of the client, I made the decision to limit packets to 128 bytes. This was a pragmatic decision to balance memory usage and usefulness; who would possible want to publish more than 128 bytes from an Arduino?

I knew the limit was just a #define in the code, so anyone could change the limit if they wanted. But I didn’t think that all the way through and it turns out there were a couple of issues with this:

  • uint8_t was used in lots of places to store lengths, which put an internal limit of 256 bytes,
  • the packet length was always encoded as a single byte – which is only correct for packets shorter than 128 bytes due to the multi-byte integer encoding MQTT uses.

So, with this release, you can now modify the #define in PubSubClient.h to a larger value and it’ll work.

The second change is a wide ranging tidy up of the types used in the library, both internally and on the API. The external part of this is primarily that all of the functions that were defined to return an int type to indicate success or failure, now return a boolean type. This change should be compatible with existing scripts.

A knock-on effect of both of these changes will require a change to any sketch that provides a message callback. The length argument of the callback function signature has changed from int to unsigned int. When you try to compile a sketch without this change, you’ll get an error along the lines of:

error: invalid conversion from 
  'void (*)(char*, byte*, int)' 
 to
  'void (*)(char*, uint8_t*, unsigned int)'

Hopefully that will help the google-juice for anyone who hits this error and hasn’t read the change log.

As ever, check out the project page for more details and a download link.

  1. pingback from Eclipse Paho started, more clients and servers emerge | MQTT: MQ Telemetry TransportMarch 11, 2012

  2. Gianni Lucia • August 29, 2012

    I wanted to know how to change the library PubSubClient.cpp to use the MQTT protocol with a shiel gsm arduino, powers have any advice on this?
    Thanks in advance for your support

  3. nickAugust 30, 2012

    Hi Gianni,
    which GSM Shield were you looking at? Is this the recently announced Telefonica/Arduino shield? I’ve been asked this question a couple times in the last few days. I don’t have one of those shields and when I went to look at its library, it appeared to be incomplete code with very little detail as to the TCP Client API it provides.

    Without details of the shield library, it is impossible to say what changes are needed.

    My library uses the Ethernet api provided by Arduino. The difficulty of porting my library to any other shield depends entirely on how close its api is to the existing one.

  4. Gianni Lucia • August 30, 2012

    Hi nik, you place a link to the library gsm shield, so maybe you can advise me something on how to use the protocol MQTT with this shield.

    This is the link:

    http://code.google.com/p/gsm-shield-arduino/downloads/list

    Thanks again for the support

  5. nickAugust 30, 2012

    Hi Gianni.

    thank you for the link to the library. I see it is very different from the Ethernet library api, so there is not am easy change to make my library work with this.

    I would start with inetGSM.httpGET as an example of creating a TCP connection and then exchanging data and go from there.

    Sorry I cannot be more help at the moment. I would like to try to get this working with the GSM shield – but without the hardware, my ability to do so is limited.

  6. Gianni Lucia • August 30, 2012

    Hi nick,

    I thank you, however, you may advise me how to solve the problem and maybe recommend a site with API MQTT protocol so as to understand the process? I’d need to know step by step procedure of uplink and updating topic.
    Unfortunately, as you know I am no expert but I would do anything to become one.
    Could you advise how to do?

    Thanks again and I apologize for the hassle

  7. nickAugust 30, 2012

    Hi Gianni,

    the best place to start is http://mqtt.org – from there you will find lots of information about the protocol, including the protocol specification itself and lots of implementations in various languages and links to the MQTT Google Group – a mailing list about the protocol.

    I hope that helps.

  8. Gianni Lucia • October 1, 2012

    Hi Nick,

    I wanted to know as never before I was able to update the datastream cosm and now no.
    code is as follows:
    include
    #include
    #include

    // Update these with values suitable for your network.
    byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0xED, 0xff}; // Mac dispositivo Ethernet Shield
    //byte server []={ 173,203,98, 29};
    char server []=”api.cosm.com”;

    void callback(char* topic, byte* payload, unsigned int length) {

    }

    PubSubClient client(server, 1883, callback);

    void setup()
    {
    Serial.begin(9600);
    Ethernet.begin(mac);

    if (client.connect(“Arduino_Client”)) {
    Serial.println(“Connesso”);
    client.publish(“tupSmbDAf5TROfwAjnZsoayz8v2SAKwrdlBjczliVzBncz0g/v2/feeds/66058/datastreams/1.csv “,”200”);
    //client.subscribe(“100”);
    }
    }

    void loop()
    {
    client.loop();
    }

    as usual, I thank you in advance for your support:

  9. soilboy • October 10, 2012

    I have had the same issue publishing to COSM with this library although I have no problems publishing the messages to broker.mqttdashboard.com . (You can subscribe to subscibe to the data here http://www.mqtt-dashboard.com/subscribe)

    I can publish to COSM mqtt gatewate from other clients though.

    Very strange.

    Any ideas anyone?

  10. nickOctober 10, 2012

    Odd – I’ve not tried publishing to Cosm recently. When I get a chance to plugin an arduino in, I’ll give it a go.

  11. Sanchit • December 10, 2012

    Hello Giani,

    I am trying to use the Wire shark Dissector tool to decode the MQTT protocol.
    But i am not able to insert the MQTT protocol inside the Tool.

    Could you please help me in this tool for the usage of MQTT decode.

  12. nickDecember 10, 2012

    Sanchit, have you looked at this Wireshark MQTT dissector? http://false.ekta.is/2011/06/mqtt-dissector-decoder-for-wireshark/

    I have not used it myself (as I know MQTT well enough to decode from the raw bytes), but it may help you.

  13. Sanchit • December 10, 2012

    Hello Giaani,

    Thanks for the prompt reply,

    But i followed the instruction as per given on this page..
    I install Wire shark dissector and put the generic.dll in plugins folder.
    After that i do not understood two things below:
    Put the following files into a directory.
    example_with_capture.wsgd : protocol name, parent dissector …
    example_with_capture.fdesc : data format description

    and then :
    Change PARENT_SUBFIELD and PARENT_SUBFIELD_VALUES (into *.wsgd) to test your data.
    Add a new file xxx.wsgd (into the SAME directory) to define another protocol.

  14. Sanchit • December 10, 2012

    I understand one thing:
    PARENT_SUBFIELD and PARENT_SUBFIELD_VALUES
    tcp, udp and ip address..

    But still lot of confusion over it.

    Could you please help me out in making this tool ready for MQTT decoding..

  15. nickDecember 10, 2012

    Hi Sanchit, I have not used that wireshark dissector, nor am I involved in its development. I suggest you raise your issues with Karl, its author, on his blog.

  16. Sanchit • December 11, 2012

    Ok thank you.

  17. leave a comment

    You must be logged in to post a comment.