Problems with MQTT and Ubidots

Hello @ScruffR, I’m very sorry to disturb you, the example was a fast wrapper to help to rdavidsson as soon as possible that I made based on the official example of the library for MQTT of hirotakaster which is one of the most popular libraries that I found at Particle’s online IDE (3235 of included times in apps), maybe I should check it better before to post it here, my mistake. Keeping the last in mind, I explain some things about:

  • The callback is a function that is necessary to be declared, “Why do we see a forward declaration of callback() immediately prior the actual implementation?” You are right, it should see only one declaration, what I wanted to show is that the programmer must declare it and to setup the routine to run the logic inside it, the callback must have three parameters: topic that is the topic that you are subscribed, payload that is the answer directly from the broker and lenght that is the size of the payload char array, these parameters are returned directly from the library and that’s why you call the constructor with the callback function. If you don’t need to subscribe to any topic anyway you have to declare it in this way:

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

  • What’s the use of this line that comes up in so many samples I see, but often is rather superfluous String message§; - message is created but never used?”, "Why do we copy payload to p but fail to use p?"
    As I said previously, the programmer is the one who should use the payload in the way that he believes convenient for his app, and yes, as you say, “Please limit the use of String to the absolute minimum as heap fragmentation might impair the stability of embedded systems like this” this should be avoided and the original maker did his example using it, my error to make a fast example was to let as it was but maybe the suggestion should go for him too, anyway, I attach maybe a better callback implementation:
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  Serial.println("payload:");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

So keeping in mind your suggestions please tell me if this would a proper example for you:

    /****************************************
     * Include Libraries
     ****************************************/

    #include "MQTT/MQTT.h"

    /****************************************
     * Define Constants
     ****************************************/
    #ifndef TOKEN
    #define TOKEN "...."  // Add here your Ubidots TOKEN
    #endif

    /****************************************
     * Auxiliar Functions
     ****************************************/

    void callback(char* topic, byte* payload, unsigned int length) {
        Serial.print("Message arrived [");
        Serial.print(topic);
        Serial.print("] ");
        Serial.println("payload obtained from server:");
        for (int i=0;i<length;i++) {
            Serial.print((char)payload[i]); // prints the answer of the broker for debug purpose
        }
        // Some stuff to make with the payload obtained
            // ......
       //
        Serial.println();
    }


    /****************************************
     * Instances
     ****************************************/

    MQTT client = MQTT("things.ubidots.com", 1883, callback, 512);
    String clientName = "";

    /****************************************
     * Main Functions
     ****************************************/

    void setup() {
        Serial.begin(115200);
        clientName = System.deviceID();
        client.connect(clientName, TOKEN, NULL);
        if(client.isConnected()){
            client.subscribe("/v1.6/devices/my-device/my-variable/lv");
        }
    }

    void loop() {
        if(!client.isConnected()){
            Serial.println("attemping to connect");
        }
        while(!client.isConnected()){
            client.connect(clientName, TOKEN, NULL);
            Serial.print(".");
            delay(5000);
        }
        Serial.println("connected");
        client.publish("/v1.6/devices/test-m", "{\"test-var\":1}"); // Publish to device 'test-m' and variable 'test-var'
        client.subscribe("/v1.6/devices/my-device/my-variable/lv"); // Subscribe to 'my-variable' at 'my-device' device
        client.loop();
        delay(1000);
    }

Apologies for the delay and the issues, I have been working hard this week to have as soon as possible this library wrapper ready for all Ubidots users.

2 Likes