fixed user/client/password mixup
This commit is contained in:
parent
fa63c335a5
commit
6a3b6132d9
@ -1,26 +1,28 @@
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
|
||||
Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *user, char *key, char *cid) {
|
||||
Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user, char *pass) {
|
||||
strncpy(servername, server, SERVERNAME_SIZE);
|
||||
servername[SERVERNAME_SIZE-1] = 0;
|
||||
portnum = port;
|
||||
serverip = 0;
|
||||
|
||||
strncpy(clientid, cid, CLIENTID_SIZE);
|
||||
clientid[CLIENTID_SIZE-1] = 0;
|
||||
|
||||
strncpy(username, user, USERNAME_SIZE);
|
||||
username[USERNAME_SIZE-1] = 0;
|
||||
|
||||
strncpy(userkey, key, KEY_SIZE);
|
||||
userkey[KEY_SIZE-1] = 0;
|
||||
|
||||
strncpy(clientid, cid, CLIENTID_SIZE);
|
||||
clientid[CLIENTID_SIZE-1] = 0;
|
||||
strncpy(password, pass, PASSWORD_SIZE);
|
||||
password[PASSWORD_SIZE-1] = 0;
|
||||
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
boolean Adafruit_MQTT::ping(void) {
|
||||
|
||||
uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) {
|
||||
packet[0] = MQTT_CTRL_PINGREQ << 4;
|
||||
packet[1] = 0;
|
||||
return 2;
|
||||
}
|
||||
|
||||
static uint8_t *stringprint(uint8_t *p, char *s) {
|
||||
@ -34,6 +36,7 @@ static uint8_t *stringprint(uint8_t *p, char *s) {
|
||||
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718028
|
||||
uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
uint8_t *p = packet;
|
||||
uint16_t len;
|
||||
|
||||
// fixed header, connection messsage no flags
|
||||
p[0] = (MQTT_CTRL_CONNECT << 4) | 0x0;
|
||||
@ -48,7 +51,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
p[0] = MQTT_CONN_CLEANSESSION;
|
||||
if (username[0] != 0)
|
||||
p[0] |= MQTT_CONN_USERNAMEFLAG;
|
||||
if (userkey[0] != 0)
|
||||
if (password[0] != 0)
|
||||
p[0] |= MQTT_CONN_PASSWORDFLAG;
|
||||
p++;
|
||||
// TODO: add WILL support?
|
||||
@ -58,28 +61,20 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
|
||||
p++;
|
||||
|
||||
uint16_t len;
|
||||
if ((clientid[0] != 0) && (strlen(clientid) > 0)) {
|
||||
p = stringprint(p, clientid);
|
||||
}
|
||||
p = stringprint(p, clientid);
|
||||
|
||||
if (username[0] != 0) {
|
||||
p = stringprint(p, username);
|
||||
}
|
||||
if (userkey[0] != 0) {
|
||||
p = stringprint(p, userkey);
|
||||
if (password[0] != 0) {
|
||||
p = stringprint(p, password);
|
||||
}
|
||||
|
||||
uint8_t totallen = p - packet;
|
||||
len = p - packet;
|
||||
|
||||
// add two empty bytes at the end (?)
|
||||
p[0] = 0;
|
||||
p[1] = 0;
|
||||
p+=2;
|
||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
||||
|
||||
packet[1] = totallen;
|
||||
|
||||
return totallen+2;
|
||||
return len;
|
||||
}
|
||||
|
||||
uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, uint8_t qos) {
|
||||
@ -95,7 +90,7 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, u
|
||||
memcpy(p, data, strlen(data));
|
||||
p+=strlen(data);
|
||||
len = p - packet;
|
||||
packet[1] = len-2;
|
||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
||||
return len;
|
||||
}
|
||||
|
||||
|
@ -9,21 +9,24 @@
|
||||
|
||||
#define MQTT_PROTOCOL_LEVEL 3
|
||||
|
||||
#define MQTT_CTRL_CONNECT 0x1
|
||||
#define MQTT_CTRL_CONNECTACK 0x2
|
||||
#define MQTT_CTRL_PUBLISH 0x3
|
||||
#define MQTT_CTRL_CONNECT 0x01
|
||||
#define MQTT_CTRL_CONNECTACK 0x02
|
||||
#define MQTT_CTRL_PUBLISH 0x03
|
||||
#define MQTT_CTRL_PINGREQ 0x0C
|
||||
#define MQTT_CTRL_PINGRESP 0x0D
|
||||
|
||||
#define MQTT_QOS_1 0x1
|
||||
#define MQTT_QOS_0 0x0
|
||||
|
||||
#define SERVERNAME_SIZE 25
|
||||
#define USERNAME_SIZE 25
|
||||
#define KEY_SIZE 41
|
||||
#define PASSWORD_SIZE 25
|
||||
#define USERNAME_SIZE 41
|
||||
#define FEEDNAME_SIZE 45
|
||||
#define CLIENTID_SIZE 23
|
||||
|
||||
#define CONNECT_TIMEOUT_MS 3000
|
||||
#define PUBLISH_TIMEOUT_MS 500
|
||||
#define PING_TIMEOUT_MS 500
|
||||
|
||||
#define MQTT_CONN_USERNAMEFLAG 0x80
|
||||
#define MQTT_CONN_PASSWORDFLAG 0x40
|
||||
@ -37,13 +40,14 @@
|
||||
|
||||
class Adafruit_MQTT {
|
||||
public:
|
||||
Adafruit_MQTT(char *server, uint16_t port, char *user, char *key, char *cid);
|
||||
Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user, char *pass);
|
||||
uint8_t connectPacket(uint8_t *packet);
|
||||
|
||||
virtual boolean publish(char *topic, char *payload, uint8_t qos) {}
|
||||
uint8_t publishPacket(uint8_t *packet, char *topic, char *payload, uint8_t qos);
|
||||
|
||||
boolean ping(void);
|
||||
virtual boolean ping(void) {}
|
||||
uint8_t pingPacket(uint8_t *packet);
|
||||
|
||||
protected:
|
||||
int8_t errno;
|
||||
@ -52,7 +56,7 @@ class Adafruit_MQTT {
|
||||
int16_t portnum;
|
||||
char clientid[CLIENTID_SIZE];
|
||||
char username[USERNAME_SIZE];
|
||||
char userkey[KEY_SIZE];
|
||||
char password[PASSWORD_SIZE];
|
||||
|
||||
uint8_t buffer[MAXBUFFERSIZE];
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
#include "Adafruit_MQTT_CC3000.h"
|
||||
#include <Adafruit_Watchdog.h>
|
||||
|
||||
Adafruit_MQTT_CC3000::Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *user, char *key, char *cid) : Adafruit_MQTT(server, port, user, key, cid), cc3000(cc3k)
|
||||
Adafruit_MQTT_CC3000::Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *cid, char *user, char *pass) : Adafruit_MQTT(server, port, cid, user, pass), cc3000(cc3k)
|
||||
{
|
||||
// nothin doin
|
||||
}
|
||||
@ -102,6 +102,36 @@ uint16_t Adafruit_MQTT_CC3000::readPacket(uint8_t *buffer, uint8_t maxlen, uint1
|
||||
return len;
|
||||
}
|
||||
|
||||
boolean Adafruit_MQTT_CC3000::ping(void) {
|
||||
uint8_t len = pingPacket(buffer);
|
||||
|
||||
Serial.print("pinging...");
|
||||
for (uint8_t i=0; i<len; i++) {
|
||||
Serial.write(buffer[i]); Serial.print(" [0x"); Serial.print(buffer[i], HEX); Serial.print("], ");
|
||||
}
|
||||
Serial.println();
|
||||
if (mqttclient.connected()) {
|
||||
uint16_t ret = mqttclient.write(buffer, len);
|
||||
Serial.print("returned: "); Serial.println(ret);
|
||||
if (ret != len) return false;
|
||||
} else {
|
||||
Serial.println(F("Connection failed"));
|
||||
return false;
|
||||
}
|
||||
|
||||
// process ping reply
|
||||
Serial.println(F("Reply:"));
|
||||
len = readPacket(buffer, 2, PING_TIMEOUT_MS);
|
||||
for (uint8_t i=0; i<len; i++) {
|
||||
Serial.write(buffer[i]); Serial.print(" [0x"); Serial.print(buffer[i], HEX); Serial.print("], ");
|
||||
}
|
||||
Serial.println();
|
||||
if (buffer[0] == (MQTT_CTRL_PINGRESP << 4))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int32_t Adafruit_MQTT_CC3000::close(void) {
|
||||
return mqttclient.close();
|
||||
}
|
||||
|
@ -8,12 +8,13 @@
|
||||
|
||||
class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
|
||||
public:
|
||||
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *user, char *key, char *cid);
|
||||
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *cid, char *user, char *pass);
|
||||
int8_t connect(void);
|
||||
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t timeout);
|
||||
int32_t close(void);
|
||||
|
||||
boolean publish(char *topic, char *payload, uint8_t qos);
|
||||
boolean ping(void);
|
||||
|
||||
private:
|
||||
Adafruit_CC3000 *cc3000;
|
||||
|
Loading…
Reference in New Issue
Block a user