MQTT - Qatja ability to connect using username and password ?

edited August 2016 in Library Questions

Many cloud services use MQTT and they require an initial connection using a username and password. I have not found the ability to set these parameters with any of the MQTT type libraries i have found for processing. Is this capability available ?

Answers

  • edited August 2016

    here is an example from the mqtt library

    i added a few client connect strings

    the one for broker.shiftr.io has user and password strings

    the code publishes a message with every key press

    it also times out in 15 seconds

    try uncommenting some of the other client.connect command lines (only one at a time)

    and try some of the other subscriptions

    test.mosquitto.org has a lot of messages in the $SYS subscriptions

    hope this helps

    you may already know all of this, but someone else may not

        // This example sketch connects to shiftr.io
        // and sends a message on every kestroke.
        //
        // After starting the sketch you can find the
        // client here: [https://shiftr.io/try]
        //
        // by Joël Gähwiler
        // [https://github.com/256dpi/procssing-mqtt]
    
        import mqtt.*;
    
        int time;
        int wait = 15000;    // milliseconds (15 seconds)
    
    
        MQTTClient client;
    
        void setup() {
          client = new MQTTClient(this);
    
          client.connect("mqtt://try:try@broker.shiftr.io", "ESP8266Client");  // try:try = user:pwd
    
        //  client.connect("mqtt://iot.eclipse.org");
        //  client.connect("mqtt://test.mosquitto.org", "ESP8266Client");
        //  client.connect("mqtt://test.mosquitto.org");
    
        //  client.subscribe("$SYS/#");    // # - all, + - one
        //  client.subscribe("$SYS/+/+");
        //  client.subscribe("bbc/subtitles/bbc_news24/raw");
    
          client.subscribe("/example");
          client.subscribe("$SYS/broker/log/M/#");
    
        time = millis();
    
        }
    
        void draw() {
    
          if (millis() > (time + wait)) {
            client.unsubscribe("$SYS/broker/log/M/#");
            client.unsubscribe("ssjh/#");
            client.unsubscribe("/example");
            println("done");
            exit();
          } 
        }
    
        void keyPressed() { client.publish("/example", "pressed key - " + char(key)); }
    
        void messageReceived(String topic, byte[] payload) {
          println("new message: " + topic + " - " + new String(payload));
        }
    
  • edited August 2016

    some of the code got messed up by the forum software

    the beginning of the code should read as follows

    // This example sketch connects to shiftr.io

    // and sends a message on every kestroke.

    //

    // After starting the sketch you can find the

    // client here: https://shiftr.io/try.

    //

    // by Joël Gähwiler

    // https://github.com/256dpi/processing-mqtt

  • Thank you Joel

    your example obviously works with your broker but when i try to use this format with the service i want to work with, i get the following:

    MQTT 1.6.1 by Joel Gaehwiler https://github.com/256dpi

    [MQTT] failed to connect: MqttException

    [MQTT] failed to subscribe: Client is not connected

    here is the client connect format i am trying to use, where the user name is the 70B3.... section and the pw is the xPD8..... section (replacing your try:try )

    are these incompatible with this implementation ? or did i not implement it properly ?

    i can use this user/pw with other clients on an Android device or MQTTlens on a pc and it works just fine. I would like to be able to do the same with processing.

    let me know if you have any suggestions.

    client.connect("mqtt://70B3D57ED00009CB:xPD8muP6/3ZG0q8IMudHS4cAKpynuwTeEQeGOxtQxT4=@staging.thethingsnetwork.org","processing55AA"); // try:try = user:pwd

    thanks Ken

  • edited August 2016

    it is the forward slash in the password that throws the exception

    without it you get "[MQTT] failed to connect: Not authorized to connect"

    btw. i am not joel

  • edited August 2016 Answer ✓

    it is because the string is a URL, therefore the slash has to be escaped with %2F (replaced)

    see http://w3schools.com/tags/ref_urlencode.asp

    this should work

      client.connect("mqtt://70B3D57ED00009CB:xPD8muP6%2F3ZG0q8IMudHS4cAKpynuwTeEQeGOxtQxT4%3D@staging.thethingsnetwork.org","processing55AA");
    
  • That solved the issue.

    Now i am able to connect and subscribe and publish properly thru this broker using the test account i used as an example. thanks for taking the time to provide a solution and explanation regarding URL encodeing.

    Ken

Sign In or Register to comment.