You've already forked Adafruit_MQTT_Library
mirror of
https://github.com/adafruit/Adafruit_MQTT_Library.git
synced 2025-07-30 13:23:06 +03:00
more flash memory, pingreq debugging, etc.
This commit is contained in:
@ -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);
|
||||
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
// 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++) {
|
||||
|
Reference in New Issue
Block a user