2015-06-01 00:38:33 +02:00
|
|
|
#include "Adafruit_MQTT.h"
|
|
|
|
#include "Adafruit_MQTT_CC3000.h"
|
|
|
|
#include <Adafruit_Watchdog.h>
|
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
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)
|
2015-06-01 00:38:33 +02:00
|
|
|
{
|
|
|
|
// nothin doin
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t Adafruit_MQTT_CC3000::connect(void) {
|
|
|
|
uint32_t ip = 0;
|
|
|
|
|
|
|
|
Watchdog.reset();
|
|
|
|
|
|
|
|
// look up IP address
|
|
|
|
if (serverip == 0) {
|
|
|
|
// Try looking up the website's IP address using CC3K's built in getHostByName
|
2015-06-02 21:23:42 +02:00
|
|
|
strcpy_P((char *)buffer, servername);
|
|
|
|
Serial.print((char *)buffer); Serial.print(F(" -> "));
|
2015-06-01 00:38:33 +02:00
|
|
|
uint8_t dnsretries = 5;
|
|
|
|
|
|
|
|
Watchdog.reset();
|
|
|
|
while (ip == 0) {
|
2015-06-02 21:23:42 +02:00
|
|
|
if (! cc3000->getHostByName((char *)buffer, &ip)) {
|
2015-06-01 00:38:33 +02:00
|
|
|
Serial.println(F("Couldn't resolve!"));
|
|
|
|
dnsretries--;
|
|
|
|
Watchdog.reset();
|
|
|
|
}
|
|
|
|
//Serial.println("OK"); Serial.println(ip, HEX);
|
|
|
|
if (!dnsretries) return -1;
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
serverip = ip;
|
|
|
|
cc3000->printIPdotsRev(serverip);
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
Watchdog.reset();
|
|
|
|
|
|
|
|
// connect to server
|
2015-06-02 21:23:42 +02:00
|
|
|
Serial.println(F("Connecting to TCP"));
|
2015-06-01 00:38:33 +02:00
|
|
|
mqttclient = cc3000->connectTCP(serverip, portnum);
|
|
|
|
uint8_t len = connectPacket(buffer);
|
2015-06-02 21:23:42 +02:00
|
|
|
Serial.println(F("MQTT connection packet:"));
|
2015-06-01 00:38:33 +02:00
|
|
|
for (uint8_t i=0; i<len; i++) {
|
|
|
|
if (isprint(buffer[i]))
|
|
|
|
Serial.write(buffer[i]);
|
|
|
|
else
|
|
|
|
Serial.print(" ");
|
2015-06-02 21:23:42 +02:00
|
|
|
Serial.print(F(" [0x"));
|
2015-06-01 00:38:33 +02:00
|
|
|
if (buffer[i] < 0x10)
|
|
|
|
Serial.print("0");
|
|
|
|
Serial.print(buffer[i],HEX);
|
|
|
|
Serial.print("], ");
|
|
|
|
if (i % 8 == 7) Serial.println();
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
if (mqttclient.connected()) {
|
|
|
|
uint16_t ret = mqttclient.write(buffer, len);
|
|
|
|
//Serial.print("returned: "); Serial.println(ret);
|
|
|
|
if (ret != len) return -1;
|
|
|
|
} else {
|
|
|
|
Serial.println(F("Connection failed"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = readPacket(buffer, 4, CONNECT_TIMEOUT_MS);
|
|
|
|
|
|
|
|
if (len != 4) return -1;
|
|
|
|
|
|
|
|
if ((buffer[0] == (MQTT_CTRL_CONNECTACK << 4)) && (buffer[1] == 2)) {
|
|
|
|
// good packet structure
|
|
|
|
return buffer[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
uint16_t Adafruit_MQTT_CC3000::readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout) {
|
2015-06-01 00:38:33 +02:00
|
|
|
/* Read data until either the connection is closed, or the idle timeout is reached. */
|
|
|
|
uint16_t len = 0;
|
2015-06-02 21:23:42 +02:00
|
|
|
int16_t t = timeout;
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
while (mqttclient.connected() && (timeout > 0)) {
|
|
|
|
Serial.print('.');
|
2015-06-01 00:38:33 +02:00
|
|
|
while (mqttclient.available()) {
|
2015-06-02 21:23:42 +02:00
|
|
|
Serial.print('!');
|
2015-06-01 00:38:33 +02:00
|
|
|
char c = mqttclient.read();
|
2015-06-02 21:23:42 +02:00
|
|
|
timeout = t; // reset the timeout
|
2015-06-01 00:38:33 +02:00
|
|
|
buffer[len] = c;
|
2015-06-02 21:23:42 +02:00
|
|
|
//Serial.print((uint8_t)c,HEX);
|
2015-06-01 00:38:33 +02:00
|
|
|
len++;
|
|
|
|
if (len == maxlen) { // we read all we want, bail
|
2015-06-02 21:23:42 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2015-06-01 00:38:33 +02:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:23:42 +02:00
|
|
|
Watchdog.reset();
|
|
|
|
timeout-=10;
|
|
|
|
delay(10);
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2015-06-01 01:02:42 +02:00
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
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);
|
2015-06-02 04:51:59 +02:00
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
if (buffer[0] == (MQTT_CTRL_PINGRESP << 4))
|
|
|
|
return true;
|
2015-06-02 04:51:59 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-01 01:02:42 +02:00
|
|
|
int32_t Adafruit_MQTT_CC3000::close(void) {
|
|
|
|
return mqttclient.close();
|
|
|
|
}
|
2015-06-01 05:19:13 +02:00
|
|
|
|
2015-06-02 21:23:42 +02:00
|
|
|
boolean Adafruit_MQTT_CC3000::publish(const char *topic, char *data, uint8_t qos) {
|
2015-06-01 05:19:13 +02:00
|
|
|
uint8_t len = publishPacket(buffer, topic, data, qos);
|
|
|
|
Serial.println("MQTT publish packet:");
|
|
|
|
for (uint8_t i=0; i<len; i++) {
|
|
|
|
if (isprint(buffer[i]))
|
|
|
|
Serial.write(buffer[i]);
|
|
|
|
else
|
|
|
|
Serial.print(" ");
|
|
|
|
Serial.print(" [0x");
|
|
|
|
if (buffer[i] < 0x10)
|
|
|
|
Serial.print("0");
|
|
|
|
Serial.print(buffer[i],HEX);
|
|
|
|
Serial.print("], ");
|
|
|
|
if (i % 8 == 7) Serial.println();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qos > 0) {
|
|
|
|
Serial.println(F("Reply:"));
|
|
|
|
len = readPacket(buffer, 4, PUBLISH_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();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|