more flash memory, pingreq debugging, etc.
This commit is contained in:
parent
6a3b6132d9
commit
7c5113e32e
@ -1,6 +1,16 @@
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
|
||||
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const PROGMEM char *cid, const PROGMEM char *user, const PROGMEM char *pass) {
|
||||
servername = server;
|
||||
portnum = port;
|
||||
serverip = 0;
|
||||
clientid = cid;
|
||||
username = user;
|
||||
password = pass;
|
||||
}
|
||||
|
||||
/*
|
||||
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;
|
||||
@ -15,9 +25,8 @@ Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user,
|
||||
|
||||
strncpy(password, pass, PASSWORD_SIZE);
|
||||
password[PASSWORD_SIZE-1] = 0;
|
||||
|
||||
errno = 0;
|
||||
}
|
||||
*/
|
||||
|
||||
uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) {
|
||||
packet[0] = MQTT_CTRL_PINGREQ << 4;
|
||||
@ -33,6 +42,19 @@ static uint8_t *stringprint(uint8_t *p, char *s) {
|
||||
return p+len;
|
||||
}
|
||||
|
||||
static uint8_t *stringprint_P(uint8_t *p, const char *s) {
|
||||
uint16_t len = strlen_P(s);
|
||||
/*
|
||||
for (uint8_t i=0; i<len; i++) {
|
||||
Serial.write(pgm_read_byte(s+i));
|
||||
}
|
||||
*/
|
||||
p[0] = len >> 8; p++;
|
||||
p[0] = len & 0xFF; p++;
|
||||
strncpy_P((char *)p, s, len);
|
||||
return p+len;
|
||||
}
|
||||
|
||||
// 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;
|
||||
@ -49,9 +71,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
p++;
|
||||
|
||||
p[0] = MQTT_CONN_CLEANSESSION;
|
||||
if (username[0] != 0)
|
||||
if (pgm_read_byte(username) != 0)
|
||||
p[0] |= MQTT_CONN_USERNAMEFLAG;
|
||||
if (password[0] != 0)
|
||||
if (pgm_read_byte(password) != 0)
|
||||
p[0] |= MQTT_CONN_PASSWORDFLAG;
|
||||
p++;
|
||||
// TODO: add WILL support?
|
||||
@ -61,13 +83,13 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
|
||||
p++;
|
||||
|
||||
p = stringprint(p, clientid);
|
||||
p = stringprint_P(p, clientid);
|
||||
|
||||
if (username[0] != 0) {
|
||||
p = stringprint(p, username);
|
||||
if (pgm_read_byte(username) != 0) {
|
||||
p = stringprint_P(p, username);
|
||||
}
|
||||
if (password[0] != 0) {
|
||||
p = stringprint(p, password);
|
||||
if (pgm_read_byte(password) != 0) {
|
||||
p = stringprint_P(p, password);
|
||||
}
|
||||
|
||||
len = p - packet;
|
||||
@ -77,7 +99,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
return len;
|
||||
}
|
||||
|
||||
uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, uint8_t qos) {
|
||||
uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic, char *data, uint8_t qos) {
|
||||
uint8_t *p = packet;
|
||||
uint16_t len;
|
||||
|
||||
@ -85,7 +107,7 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, u
|
||||
// fill in packet[1] last
|
||||
p+=2;
|
||||
|
||||
p = stringprint(p, topic);
|
||||
p = stringprint_P(p, topic);
|
||||
|
||||
memcpy(p, data, strlen(data));
|
||||
p+=strlen(data);
|
||||
@ -95,16 +117,14 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, char *topic, char *data, u
|
||||
}
|
||||
|
||||
|
||||
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT &mqttserver, char *feed, uint8_t q) {
|
||||
mqtt = &mqttserver;
|
||||
strncpy(topic, feed, FEEDNAME_SIZE);
|
||||
topic[FEEDNAME_SIZE-1] = 0;
|
||||
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t q) {
|
||||
mqtt = mqttserver;
|
||||
topic = feed;
|
||||
qos = q;
|
||||
|
||||
errno = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
||||
char payload[18];
|
||||
itoa(i, payload, 10);
|
||||
@ -116,3 +136,7 @@ bool Adafruit_MQTT_Publish::publish(uint32_t i) {
|
||||
itoa(i, payload, 10);
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
bool Adafruit_MQTT_Publish::publish(char *payload) {
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
|
@ -19,11 +19,14 @@
|
||||
#define MQTT_QOS_0 0x0
|
||||
|
||||
#define SERVERNAME_SIZE 25
|
||||
|
||||
#define PASSWORD_SIZE 25
|
||||
#define USERNAME_SIZE 41
|
||||
#define FEEDNAME_SIZE 45
|
||||
#define CLIENTID_SIZE 23
|
||||
|
||||
#define FEEDNAME_SIZE 40
|
||||
|
||||
|
||||
#define CONNECT_TIMEOUT_MS 3000
|
||||
#define PUBLISH_TIMEOUT_MS 500
|
||||
#define PING_TIMEOUT_MS 500
|
||||
@ -36,50 +39,51 @@
|
||||
#define MQTT_CONN_CLEANSESSION 0x02
|
||||
#define MQTT_CONN_KEEPALIVE 15 // in seconds
|
||||
|
||||
#define MAXBUFFERSIZE 130
|
||||
#define MAXBUFFERSIZE (60)
|
||||
|
||||
class Adafruit_MQTT {
|
||||
public:
|
||||
Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user, char *pass);
|
||||
Adafruit_MQTT(const char *server, uint16_t port, const PROGMEM char *cid, const PROGMEM char *user, const PROGMEM 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);
|
||||
virtual boolean publish(const char *topic, char *payload, uint8_t qos) {}
|
||||
uint8_t publishPacket(uint8_t *packet, const char *topic, char *payload, uint8_t qos);
|
||||
|
||||
virtual boolean ping(void) {}
|
||||
virtual boolean ping(uint8_t t) {}
|
||||
uint8_t pingPacket(uint8_t *packet);
|
||||
|
||||
protected:
|
||||
int8_t errno;
|
||||
char servername[SERVERNAME_SIZE];
|
||||
const char *servername;
|
||||
uint32_t serverip;
|
||||
int16_t portnum;
|
||||
char clientid[CLIENTID_SIZE];
|
||||
char username[USERNAME_SIZE];
|
||||
char password[PASSWORD_SIZE];
|
||||
const char *clientid;
|
||||
const char *username;
|
||||
const char *password;
|
||||
|
||||
uint8_t buffer[MAXBUFFERSIZE];
|
||||
};
|
||||
|
||||
class Adafruit_MQTT_Publish {
|
||||
public:
|
||||
Adafruit_MQTT_Publish(Adafruit_MQTT& mqttserver, char *feed, uint8_t qos = 0);
|
||||
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
|
||||
|
||||
//bool publish(char *s);
|
||||
bool publish(char *s);
|
||||
//bool publish(double f);
|
||||
bool publish(int32_t i);
|
||||
bool publish(uint32_t i);
|
||||
|
||||
private:
|
||||
Adafruit_MQTT *mqtt;
|
||||
char topic[FEEDNAME_SIZE];
|
||||
const char *topic;
|
||||
uint8_t qos;
|
||||
int8_t errno;
|
||||
};
|
||||
|
||||
class Adafruit_MQTT_Subscibe {
|
||||
class Adafruit_MQTT_Subscribe {
|
||||
public:
|
||||
Adafruit_MQTT_Subscibe(Adafruit_MQTT *mqttserver, char *feedname);
|
||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, char *feedname);
|
||||
|
||||
bool setCallback(void (*callback)(char *));
|
||||
|
||||
};
|
||||
|
@ -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 *cid, char *user, char *pass) : Adafruit_MQTT(server, port, cid, user, pass), cc3000(cc3k)
|
||||
Adafruit_MQTT_CC3000::Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, const char *server, uint16_t port, const char *cid, const char *user, const char *pass) : Adafruit_MQTT(server, port, cid, user, pass), cc3000(cc3k)
|
||||
{
|
||||
// nothin doin
|
||||
}
|
||||
@ -15,12 +15,13 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
|
||||
// look up IP address
|
||||
if (serverip == 0) {
|
||||
// Try looking up the website's IP address using CC3K's built in getHostByName
|
||||
Serial.print(servername); Serial.print(F(" -> "));
|
||||
strcpy_P((char *)buffer, servername);
|
||||
Serial.print((char *)buffer); Serial.print(F(" -> "));
|
||||
uint8_t dnsretries = 5;
|
||||
|
||||
Watchdog.reset();
|
||||
while (ip == 0) {
|
||||
if (! cc3000->getHostByName(servername, &ip)) {
|
||||
if (! cc3000->getHostByName((char *)buffer, &ip)) {
|
||||
Serial.println(F("Couldn't resolve!"));
|
||||
dnsretries--;
|
||||
Watchdog.reset();
|
||||
@ -38,16 +39,16 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
|
||||
Watchdog.reset();
|
||||
|
||||
// connect to server
|
||||
Serial.println("Connecting to TCP");
|
||||
Serial.println(F("Connecting to TCP"));
|
||||
mqttclient = cc3000->connectTCP(serverip, portnum);
|
||||
uint8_t len = connectPacket(buffer);
|
||||
Serial.println("MQTT connection packet:");
|
||||
Serial.println(F("MQTT connection packet:"));
|
||||
for (uint8_t i=0; i<len; i++) {
|
||||
if (isprint(buffer[i]))
|
||||
Serial.write(buffer[i]);
|
||||
else
|
||||
Serial.print(" ");
|
||||
Serial.print(" [0x");
|
||||
Serial.print(F(" [0x"));
|
||||
if (buffer[i] < 0x10)
|
||||
Serial.print("0");
|
||||
Serial.print(buffer[i],HEX);
|
||||
@ -65,12 +66,7 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Serial.println(F("Reply:"));
|
||||
len = readPacket(buffer, 4, CONNECT_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 (len != 4) return -1;
|
||||
|
||||
@ -82,53 +78,65 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint16_t Adafruit_MQTT_CC3000::readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t timeout) {
|
||||
uint16_t Adafruit_MQTT_CC3000::readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout) {
|
||||
/* Read data until either the connection is closed, or the idle timeout is reached. */
|
||||
uint16_t len = 0;
|
||||
int16_t t = timeout;
|
||||
|
||||
unsigned long lastRead = millis();
|
||||
while (mqttclient.connected() && ((millis() - lastRead) < timeout)) {
|
||||
while (mqttclient.connected() && (timeout > 0)) {
|
||||
Serial.print('.');
|
||||
while (mqttclient.available()) {
|
||||
Serial.print('!');
|
||||
char c = mqttclient.read();
|
||||
Watchdog.reset();
|
||||
timeout = t; // reset the timeout
|
||||
buffer[len] = c;
|
||||
//Serial.print((uint8_t)c,HEX);
|
||||
len++;
|
||||
if (len == maxlen) { // we read all we want, bail
|
||||
|
||||
Serial.print(F("Read packet:\t"));
|
||||
for (uint8_t i=0; i<len; i++) {
|
||||
if (isprint(buffer[i]))
|
||||
Serial.write(buffer[i]);
|
||||
else
|
||||
Serial.write(' ');
|
||||
Serial.print(" [0x"); Serial.print(buffer[i], HEX); Serial.print("], ");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
return len;
|
||||
}
|
||||
lastRead = millis();
|
||||
}
|
||||
Watchdog.reset();
|
||||
timeout-=10;
|
||||
delay(10);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
boolean Adafruit_MQTT_CC3000::ping(void) {
|
||||
uint8_t len = pingPacket(buffer);
|
||||
boolean Adafruit_MQTT_CC3000::ping(uint8_t times) {
|
||||
while (times) {
|
||||
uint8_t len = pingPacket(buffer);
|
||||
Serial.print("Sending...\t");
|
||||
for (uint8_t i=0; i<len; 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;
|
||||
}
|
||||
|
||||
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
|
||||
len = readPacket(buffer, 2, PING_TIMEOUT_MS);
|
||||
|
||||
// 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("], ");
|
||||
if (buffer[0] == (MQTT_CTRL_PINGRESP << 4))
|
||||
return true;
|
||||
}
|
||||
Serial.println();
|
||||
if (buffer[0] == (MQTT_CTRL_PINGRESP << 4))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -136,7 +144,7 @@ int32_t Adafruit_MQTT_CC3000::close(void) {
|
||||
return mqttclient.close();
|
||||
}
|
||||
|
||||
boolean Adafruit_MQTT_CC3000::publish(char *topic, char *data, uint8_t qos) {
|
||||
boolean Adafruit_MQTT_CC3000::publish(const char *topic, char *data, uint8_t qos) {
|
||||
uint8_t len = publishPacket(buffer, topic, data, qos);
|
||||
Serial.println("MQTT publish packet:");
|
||||
for (uint8_t i=0; i<len; i++) {
|
||||
|
@ -8,13 +8,13 @@
|
||||
|
||||
class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
|
||||
public:
|
||||
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, char *server, uint16_t port, char *cid, char *user, char *pass);
|
||||
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, const char *server, uint16_t port, const char *cid, const char *user, const char *pass);
|
||||
int8_t connect(void);
|
||||
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, uint16_t timeout);
|
||||
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout);
|
||||
int32_t close(void);
|
||||
|
||||
boolean publish(char *topic, char *payload, uint8_t qos);
|
||||
boolean ping(void);
|
||||
boolean publish(const char *topic, char *payload, uint8_t qos);
|
||||
boolean ping(uint8_t time);
|
||||
|
||||
private:
|
||||
Adafruit_CC3000 *cc3000;
|
||||
|
Loading…
Reference in New Issue
Block a user