mikaelpatel / Cosa-MQTT

MQTT for Cosa Arduino OOP Framework
3 stars 2 forks source link

Example request: Connect using will and user/password #1

Open sehlstrom opened 9 years ago

sehlstrom commented 9 years ago

In the existing examples, connection is made without any security. Can an example using user/password credentials please be added? Also, LWT configurations at the same time would be neat.

The reason asking is that Im trying to connect using a user/password combination, however without any luck. The credentials I use works using other clients than the client created with the Cosa-MQTT library, e.g. using OpenHAB and MQTT bindings to local Mosquitto MQTT broker. Basically, this is what I do:

// MQTT server configurations
#define MQTT_SERVER "10.0.1.191"

// MQTT client configurations
static const char MQTT_CLIENT[] __PROGMEM = "MQTTClient";
static const char MQTT_WILL_TOPIC[] __PROGMEM = "node/gone-offline";
static const char MQTT_WILL_MSG[] __PROGMEM = "MQTTClient";
static const char MQTT_USER[] __PROGMEM = "MQTT";
static const char MQTT_PASSWD[] __PROGMEM = "mypwd";
class MQTTClient : public MQTT::Client {
  public:
    MQTTClient() : Client() {};
    virtual void on_publish(char* topic, void* buf, size_t count);
};

MQTTClient client;
client.begin(ethernet.socket(Socket::TCP));
client.connect(MQTT_SERVER, MQTT_CLIENT, 600, 0, MQTT_WILL_TOPIC, MQTT_WILL_MSG, MQTT_USER, MQTT_PASSWD);

Fail happens at last command with error code -5.

mikaelpatel commented 9 years ago

@sehlstrom I will give you a hint; check the forth parameter. It should be something else than zero. Zero means no more parameters.

sehlstrom commented 9 years ago

Aha, yes I can see that now after having a look at the MQTT.cpp file. It then becomes obvious that you have to provide a flag that tells we want to use a will, a username and a password when connecting.

From MQTT.hh we learn that the connection flags are defined as follows:

WILL_FLAG = 0x04,       //!< Will flag.         0000 0100
USER_NAME_FLAG = 0x80,  //!< User name flag.    1000 0000
PASSWORD_FLAG = 0x40,   //!< Password flag.     0100 0000

Thus, I imagined we wanted to send the byte 1100 0100 or the equivalent hexadecimal 0xC4. But still not any accepted connection. The same error code -5is returned in Cosa-MQTT.

mikaelpatel commented 9 years ago

@sehlstrom What is the full code line? You need to add the will qos as well. Also the parameter order. The doc needs fixing.

sehlstrom commented 9 years ago

@mikaelpatel It reads:

client.connect(MQTT_SERVER, MQTT_CLIENT, 600, 0xC4, MQTT_WILL_TOPIC, MQTT_WILL_MSG, MQTT_USER, MQTT_PASSWD);

How do you set the QoS of the will? Figure 3.4 - Connect Flag bits [1] explains the flags as I understand it, but I do not no know to use the two flags to set the QoS in a proper way.

[1] http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html

mikaelpatel commented 9 years ago

If you use the WILL_FLAG you must provide three parameters! The topic, message and qos. https://github.com/mikaelpatel/Cosa-MQTT/blob/master/MQTT.cpp#L126

Also instead of writing 0xC4 please use the flag symbols and or them together. https://github.com/mikaelpatel/Cosa-MQTT/blob/master/MQTT.hh#L86

By tomorrow you will have forgotten what 0xC4 means :)

Use the QoS symbols. https://github.com/mikaelpatel/Cosa-MQTT/blob/master/MQTT.hh#L37 E.g. MQTT::FIRE_AND_FORGET

sehlstrom commented 9 years ago

Thanks for updating the documentation with the missing QoS argument!

The following works:

// MQTT client configurations
static const char CLIENT[] __PROGMEM = "MQTTClient";
static const char WILL_TOPIC[] __PROGMEM = "node/gone-offline";
static const char WILL_MSG[] __PROGMEM = "MQTTClient";
static const uint16_t will_qos = 0;
static const char USER[] __PROGMEM = "MQTTClient";
static const char PASSWD[] __PROGMEM = "mypwd";
static const uint8_t flag = MQTT::Client::WILL_FLAG |
                            MQTT::Client::USER_NAME_FLAG |
                            MQTT::Client::PASSWORD_FLAG;
...
// Connect to the server/broker
client.connect(SERVER, CLIENT, 600, flag, WILL_TOPIC, WILL_MSG, will_qos, USER, PASSWD);

Thanks for the help. I will set up an example using will, username and password and then make a pull request. Il´l be back.

sehlstrom commented 9 years ago

@mikaelpatel, see pull request: https://github.com/sehlstrom/Cosa-MQTT/commit/af256c411d2d73f2774c9cfbe34b0441446e6fc6