From 6e06f88265a7132c067fb761cd263b0791bc295d Mon Sep 17 00:00:00 2001 From: alphamobius Date: Tue, 21 Aug 2018 20:05:16 -0400 Subject: [PATCH] Allow connection to brokers with no authentication Added a check condition in case the username and password are equal to "" in the .ino file. For example, connection to test.mosquitto.org requires no authentication. This allows ease of use for simply testing that the arduino and fona are working properly together. Tested to function on an arduino uno, with a fona 808, sending test messages to test.mosquitto.org:1883. Without the check condition, and the username and password set to "", the library still asserts the MQTT_CONN_USERNAMEFLAG and MQTT_CONN_PASSWORDFLAG flags, and extends the overall header by four bytes. Example packet: 0x10 0x10 0x0 0x4 0x4D 0x51 0x54 0x54 0x4 0xFFFFFFC2 0x1 0x2C 0x0 0x0 0x0 0x0 0x0 0x0 Corrected packet: 0x10 0xC 0x0 0x4 0x4D 0x51 0x54 0x54 0x4 0x2 0x1 0x2C 0x0 0x0 --- Adafruit_MQTT.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 6855f84..62c979d 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -552,6 +552,7 @@ bool Adafruit_MQTT::ping(uint8_t num) { // small differences in the protocol): // http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#connect uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { + String nullString = ""; uint8_t *p = packet; uint16_t len; @@ -589,10 +590,12 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { } - if (pgm_read_byte(username) != 0) + if (!(nullString.equals(username)) && pgm_read_byte(username) != 0){ p[0] |= MQTT_CONN_USERNAMEFLAG; - if (pgm_read_byte(password) != 0) + } + if (!(nullString.equals(password)) && pgm_read_byte(password) != 0){ p[0] |= MQTT_CONN_PASSWORDFLAG; + } p++; p[0] = MQTT_CONN_KEEPALIVE >> 8; @@ -619,10 +622,10 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { p = stringprint(p, will_payload); } - if (pgm_read_byte(username) != 0) { + if (!(nullString.equals(username)) && pgm_read_byte(username) != 0) { p = stringprint(p, username); } - if (pgm_read_byte(password) != 0) { + if (!(nullString.equals(password)) && pgm_read_byte(password) != 0) { p = stringprint(p, password); }