adds client id to connect

This commit is contained in:
Todd Treece 2015-05-31 21:07:17 -04:00
parent 1bcef195f1
commit e7b8ee0797
2 changed files with 14 additions and 4 deletions

View File

@ -1,7 +1,7 @@
#include "Adafruit_MQTT.h"
Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *user, char *key) {
Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *user, char *key, char *cid) {
strncpy(servername, server, SERVERNAME_SIZE);
servername[SERVERNAME_SIZE-1] = 0;
portnum = port;
@ -10,6 +10,8 @@ Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *user, char *key)
username[USERNAME_SIZE-1] = 0;
strncpy(userkey, key, KEY_SIZE);
userkey[KEY_SIZE-1] = 0;
strncpy(clientid, cid, CLIENTID_SIZE);
userkey[CLIENTID_SIZE-1] = 0;
errno = 0;
}
@ -45,15 +47,21 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
p++;
uint16_t len = strlen(clientid);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, clientid, len);
p+=len;
if (username[0] != 0) {
uint16_t len = strlen(username);
len = strlen(username);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, username, len);
p+=len;
}
if (userkey[0] != 0) {
uint16_t len = strlen(userkey);
len = strlen(userkey);
p[0] = len >> 8; p++;
p[0] = len & 0xFF; p++;
memcpy(p, userkey, len);

View File

@ -16,6 +16,7 @@
#define USERNAME_SIZE 25
#define KEY_SIZE 41
#define FEEDNAME_SIZE 25
#define CLIENTID_SIZE 23
#define CONNECT_TIMEOUT_MS 3000
@ -38,6 +39,7 @@ class Adafruit_MQTT {
char servername[SERVERNAME_SIZE];
uint32_t serverip;
int16_t portnum;
char clientid[CLIENTID_SIZE];
char username[USERNAME_SIZE];
char userkey[KEY_SIZE];