MQTT with credentials

Hi i am using test.mosquito as broker for publishing my data using an Argon and importing the MQTT library available on the web ide. I need to use username and password to protect my data, therefore i used the tutorials available on the net to install in my PC the mosquito broker and to allow the authentication with username and password. I am able to publish and subscribe through the terminal etc… Now i would like to translate it to my code to flash in the Argon but i am not sure how to do it. I see that i have to write a connect function? Could you explain better? thank you

Can you show what code you already have on your Argon side?
Maybe we can build from there rather than (re)inventing the wheel when you may already have a (somewhat) working car :wink:

BTW, this thread is really old and the last post before yours was almost three years ago.For that it would be better to start a new thread (with reference(s) to others you may already have read through).

(Hence I’ve split of this discussion into it’s own thread)

Hi, thanks. I have in my code the common mqtt structure to publish without credentials( here below). I have followed Mosquitto Username and Password Authentication -Configuration and Testing, to install mosquito and I am able to publish with " mosquito_pub -h locahost -m “message sent” -u my username -P my password" and subscribe similarly. However I didn’t get how to translate this in my code.

Thank you

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

//MQTT client("broker.hivemq.com", 1883, callback);

MQTT client("test.mosquitto.org", 1883, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length+1]; 
    memcpy (p, payload, length);
    p[length]=NULL;
    val= String(p);
    //Serial.print(val);
    value= val.toInt();
}

Your code is not showing if, where and how you are calling client.connect()

A good start for learning how and where to do it would be in the examples provided with the library you're using (there are some variants available).

Assuming you are using this library you'll see a line like this ...

    client.connect("sparkclient");

... in the examples. And given my hint from the other thread ...

... the simple change would be to keep the client-id as-is and merely add the credentials as you chose and the parameter names suggest :wink:
e.g.

  client.connect("yourClientID", "yourUserName", "yourPassPhrase");

so my code should look like this rigth?

String clientId = "????????"; // SHOULD I WRITE MY IP ADDRESS?
String username = "my username";
String password = "my password";
char server[] = "test.mosquitto.org";
void callback(char* topic, byte* payload, unsigned int length);


MQTT client(server, 1883, callback);

void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    val= String(p);
    //Serial.print(val);
    value= val.toInt();
}


void setup() {
    // connect to the server
    client.connect(clientId,username,password);

    if (client.isConnected()) {
        client.publish("outTopic/message","hello world");
        client.subscribe("inTopic/message");
    }
}

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

However I don’t know what is my clientid, is it my IP address, given that the modifications that I have made are as localhost in my PC? What if I would like to make the client id remote and not locally related to the PC in which I install the mosquito broker?

IIRC that ID is there to identify your client for the server to distinguish between clients when sharing credentials and topics for multiple clients.

You can use whatever you want for that.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.