more flash memory, pingreq debugging, etc.

This commit is contained in:
ladyada 2015-06-02 15:23:42 -04:00
parent 6a3b6132d9
commit 7c5113e32e
4 changed files with 115 additions and 79 deletions

View File

@ -1,6 +1,16 @@
#include "Adafruit_MQTT.h" #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) { Adafruit_MQTT::Adafruit_MQTT(char *server, uint16_t port, char *cid, char *user, char *pass) {
strncpy(servername, server, SERVERNAME_SIZE); strncpy(servername, server, SERVERNAME_SIZE);
servername[SERVERNAME_SIZE-1] = 0; 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); strncpy(password, pass, PASSWORD_SIZE);
password[PASSWORD_SIZE-1] = 0; password[PASSWORD_SIZE-1] = 0;
errno = 0;
} }
*/
uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) { uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) {
packet[0] = MQTT_CTRL_PINGREQ << 4; packet[0] = MQTT_CTRL_PINGREQ << 4;
@ -33,6 +42,19 @@ static uint8_t *stringprint(uint8_t *p, char *s) {
return p+len; 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 // 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 Adafruit_MQTT::connectPacket(uint8_t *packet) {
uint8_t *p = packet; uint8_t *p = packet;
@ -49,9 +71,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p++; p++;
p[0] = MQTT_CONN_CLEANSESSION; p[0] = MQTT_CONN_CLEANSESSION;
if (username[0] != 0) if (pgm_read_byte(username) != 0)
p[0] |= MQTT_CONN_USERNAMEFLAG; p[0] |= MQTT_CONN_USERNAMEFLAG;
if (password[0] != 0) if (pgm_read_byte(password) != 0)
p[0] |= MQTT_CONN_PASSWORDFLAG; p[0] |= MQTT_CONN_PASSWORDFLAG;
p++; p++;
// TODO: add WILL support? // TODO: add WILL support?
@ -61,13 +83,13 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
p[0] = MQTT_CONN_KEEPALIVE & 0xFF; p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
p++; p++;
p = stringprint(p, clientid); p = stringprint_P(p, clientid);
if (username[0] != 0) { if (pgm_read_byte(username) != 0) {
p = stringprint(p, username); p = stringprint_P(p, username);
} }
if (password[0] != 0) { if (pgm_read_byte(password) != 0) {
p = stringprint(p, password); p = stringprint_P(p, password);
} }
len = p - packet; len = p - packet;
@ -77,7 +99,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
return len; 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; uint8_t *p = packet;
uint16_t len; 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 // fill in packet[1] last
p+=2; p+=2;
p = stringprint(p, topic); p = stringprint_P(p, topic);
memcpy(p, data, strlen(data)); memcpy(p, data, strlen(data));
p+=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) { Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t q) {
mqtt = &mqttserver; mqtt = mqttserver;
strncpy(topic, feed, FEEDNAME_SIZE); topic = feed;
topic[FEEDNAME_SIZE-1] = 0;
qos = q; qos = q;
errno = 0;
} }
bool Adafruit_MQTT_Publish::publish(int32_t i) { bool Adafruit_MQTT_Publish::publish(int32_t i) {
char payload[18]; char payload[18];
itoa(i, payload, 10); itoa(i, payload, 10);
@ -116,3 +136,7 @@ bool Adafruit_MQTT_Publish::publish(uint32_t i) {
itoa(i, payload, 10); itoa(i, payload, 10);
return mqtt->publish(topic, payload, qos); return mqtt->publish(topic, payload, qos);
} }
bool Adafruit_MQTT_Publish::publish(char *payload) {
return mqtt->publish(topic, payload, qos);
}

View File

@ -19,11 +19,14 @@
#define MQTT_QOS_0 0x0 #define MQTT_QOS_0 0x0
#define SERVERNAME_SIZE 25 #define SERVERNAME_SIZE 25
#define PASSWORD_SIZE 25 #define PASSWORD_SIZE 25
#define USERNAME_SIZE 41 #define USERNAME_SIZE 41
#define FEEDNAME_SIZE 45
#define CLIENTID_SIZE 23 #define CLIENTID_SIZE 23
#define FEEDNAME_SIZE 40
#define CONNECT_TIMEOUT_MS 3000 #define CONNECT_TIMEOUT_MS 3000
#define PUBLISH_TIMEOUT_MS 500 #define PUBLISH_TIMEOUT_MS 500
#define PING_TIMEOUT_MS 500 #define PING_TIMEOUT_MS 500
@ -36,50 +39,51 @@
#define MQTT_CONN_CLEANSESSION 0x02 #define MQTT_CONN_CLEANSESSION 0x02
#define MQTT_CONN_KEEPALIVE 15 // in seconds #define MQTT_CONN_KEEPALIVE 15 // in seconds
#define MAXBUFFERSIZE 130 #define MAXBUFFERSIZE (60)
class Adafruit_MQTT { class Adafruit_MQTT {
public: 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); uint8_t connectPacket(uint8_t *packet);
virtual boolean publish(char *topic, char *payload, uint8_t qos) {} virtual boolean publish(const char *topic, char *payload, uint8_t qos) {}
uint8_t publishPacket(uint8_t *packet, 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); uint8_t pingPacket(uint8_t *packet);
protected: protected:
int8_t errno; int8_t errno;
char servername[SERVERNAME_SIZE]; const char *servername;
uint32_t serverip; uint32_t serverip;
int16_t portnum; int16_t portnum;
char clientid[CLIENTID_SIZE]; const char *clientid;
char username[USERNAME_SIZE]; const char *username;
char password[PASSWORD_SIZE]; const char *password;
uint8_t buffer[MAXBUFFERSIZE]; uint8_t buffer[MAXBUFFERSIZE];
}; };
class Adafruit_MQTT_Publish { class Adafruit_MQTT_Publish {
public: 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(double f);
bool publish(int32_t i); bool publish(int32_t i);
bool publish(uint32_t i); bool publish(uint32_t i);
private: private:
Adafruit_MQTT *mqtt; Adafruit_MQTT *mqtt;
char topic[FEEDNAME_SIZE]; const char *topic;
uint8_t qos; uint8_t qos;
int8_t errno;
}; };
class Adafruit_MQTT_Subscibe { class Adafruit_MQTT_Subscribe {
public: public:
Adafruit_MQTT_Subscibe(Adafruit_MQTT *mqttserver, char *feedname); Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, char *feedname);
bool setCallback(void (*callback)(char *)); bool setCallback(void (*callback)(char *));
}; };

View File

@ -2,7 +2,7 @@
#include "Adafruit_MQTT_CC3000.h" #include "Adafruit_MQTT_CC3000.h"
#include <Adafruit_Watchdog.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 // nothin doin
} }
@ -15,12 +15,13 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
// look up IP address // look up IP address
if (serverip == 0) { if (serverip == 0) {
// Try looking up the website's IP address using CC3K's built in getHostByName // 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; uint8_t dnsretries = 5;
Watchdog.reset(); Watchdog.reset();
while (ip == 0) { while (ip == 0) {
if (! cc3000->getHostByName(servername, &ip)) { if (! cc3000->getHostByName((char *)buffer, &ip)) {
Serial.println(F("Couldn't resolve!")); Serial.println(F("Couldn't resolve!"));
dnsretries--; dnsretries--;
Watchdog.reset(); Watchdog.reset();
@ -38,16 +39,16 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
Watchdog.reset(); Watchdog.reset();
// connect to server // connect to server
Serial.println("Connecting to TCP"); Serial.println(F("Connecting to TCP"));
mqttclient = cc3000->connectTCP(serverip, portnum); mqttclient = cc3000->connectTCP(serverip, portnum);
uint8_t len = connectPacket(buffer); uint8_t len = connectPacket(buffer);
Serial.println("MQTT connection packet:"); Serial.println(F("MQTT connection packet:"));
for (uint8_t i=0; i<len; i++) { for (uint8_t i=0; i<len; i++) {
if (isprint(buffer[i])) if (isprint(buffer[i]))
Serial.write(buffer[i]); Serial.write(buffer[i]);
else else
Serial.print(" "); Serial.print(" ");
Serial.print(" [0x"); Serial.print(F(" [0x"));
if (buffer[i] < 0x10) if (buffer[i] < 0x10)
Serial.print("0"); Serial.print("0");
Serial.print(buffer[i],HEX); Serial.print(buffer[i],HEX);
@ -65,12 +66,7 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
return -1; return -1;
} }
Serial.println(F("Reply:"));
len = readPacket(buffer, 4, CONNECT_TIMEOUT_MS); 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; if (len != 4) return -1;
@ -82,37 +78,53 @@ int8_t Adafruit_MQTT_CC3000::connect(void) {
return -1; 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. */ /* Read data until either the connection is closed, or the idle timeout is reached. */
uint16_t len = 0; uint16_t len = 0;
int16_t t = timeout;
unsigned long lastRead = millis(); while (mqttclient.connected() && (timeout > 0)) {
while (mqttclient.connected() && ((millis() - lastRead) < timeout)) { Serial.print('.');
while (mqttclient.available()) { while (mqttclient.available()) {
Serial.print('!');
char c = mqttclient.read(); char c = mqttclient.read();
Watchdog.reset(); timeout = t; // reset the timeout
buffer[len] = c; buffer[len] = c;
//Serial.print((uint8_t)c,HEX);
len++; len++;
if (len == maxlen) { // we read all we want, bail if (len == maxlen) { // we read all we want, bail
return len;
}
lastRead = millis();
}
}
return len;
}
boolean Adafruit_MQTT_CC3000::ping(void) { Serial.print(F("Read packet:\t"));
uint8_t len = pingPacket(buffer);
Serial.print("pinging...");
for (uint8_t i=0; i<len; i++) { for (uint8_t i=0; i<len; i++) {
Serial.write(buffer[i]); Serial.print(" [0x"); Serial.print(buffer[i], HEX); Serial.print("], "); 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;
}
}
Watchdog.reset();
timeout-=10;
delay(10);
}
return len;
}
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(); Serial.println();
if (mqttclient.connected()) { if (mqttclient.connected()) {
uint16_t ret = mqttclient.write(buffer, len); uint16_t ret = mqttclient.write(buffer, len);
Serial.print("returned: "); Serial.println(ret); //Serial.print("returned: "); Serial.println(ret);
if (ret != len) return false; if (ret != len) return false;
} else { } else {
Serial.println(F("Connection failed")); Serial.println(F("Connection failed"));
@ -120,15 +132,11 @@ boolean Adafruit_MQTT_CC3000::ping(void) {
} }
// process ping reply // process ping reply
Serial.println(F("Reply:"));
len = readPacket(buffer, 2, PING_TIMEOUT_MS); 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)) if (buffer[0] == (MQTT_CTRL_PINGRESP << 4))
return true; return true;
}
return false; return false;
} }
@ -136,7 +144,7 @@ int32_t Adafruit_MQTT_CC3000::close(void) {
return mqttclient.close(); 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); uint8_t len = publishPacket(buffer, topic, data, qos);
Serial.println("MQTT publish packet:"); Serial.println("MQTT publish packet:");
for (uint8_t i=0; i<len; i++) { for (uint8_t i=0; i<len; i++) {

View File

@ -8,13 +8,13 @@
class Adafruit_MQTT_CC3000 : public Adafruit_MQTT { class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
public: 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); 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); int32_t close(void);
boolean publish(char *topic, char *payload, uint8_t qos); boolean publish(const char *topic, char *payload, uint8_t qos);
boolean ping(void); boolean ping(uint8_t time);
private: private:
Adafruit_CC3000 *cc3000; Adafruit_CC3000 *cc3000;