2015-06-05 06:16:08 +02:00
|
|
|
#include <Adafruit_SleepyDog.h>
|
2015-06-01 00:38:33 +02:00
|
|
|
#include "Adafruit_MQTT.h"
|
|
|
|
#include "Adafruit_MQTT_CC3000.h"
|
|
|
|
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
bool Adafruit_MQTT_CC3000::connectServer(void) {
|
2015-06-01 00:38:33 +02:00
|
|
|
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-05 06:16:08 +02:00
|
|
|
Serial.println(F("Couldn't resolve!"));
|
|
|
|
dnsretries--;
|
|
|
|
Watchdog.reset();
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
|
|
|
//Serial.println("OK"); Serial.println(ip, HEX);
|
2015-06-05 06:16:08 +02:00
|
|
|
if (!dnsretries) return false;
|
2015-06-01 00:38:33 +02:00
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
|
|
|
|
serverip = ip;
|
|
|
|
cc3000->printIPdotsRev(serverip);
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
|
|
|
|
Watchdog.reset();
|
|
|
|
|
|
|
|
// connect to server
|
2015-06-05 06:16:08 +02:00
|
|
|
DEBUG_PRINTLN(F("Connecting to TCP"));
|
2015-06-01 00:38:33 +02:00
|
|
|
mqttclient = cc3000->connectTCP(serverip, portnum);
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
return mqttclient.connected();
|
|
|
|
}
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
bool Adafruit_MQTT_CC3000::disconnect(void) {
|
|
|
|
return (mqttclient.close() == 0);
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
uint16_t Adafruit_MQTT_CC3000::readPacket(uint8_t *buffer, uint8_t maxlen,
|
|
|
|
int16_t timeout,
|
|
|
|
bool checkForValidPubPacket) {
|
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-03 00:33:03 +02:00
|
|
|
while (mqttclient.connected() && (timeout >= 0)) {
|
2015-06-05 06:16:08 +02:00
|
|
|
//DEBUG_PRINT('.');
|
2015-06-01 00:38:33 +02:00
|
|
|
while (mqttclient.available()) {
|
2015-06-05 06:16:08 +02:00
|
|
|
//DEBUG_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-05 06:16:08 +02:00
|
|
|
//DEBUG_PRINTLN((uint8_t)c, HEX);
|
2015-06-01 00:38:33 +02:00
|
|
|
len++;
|
|
|
|
if (len == maxlen) { // we read all we want, bail
|
2015-06-05 06:16:08 +02:00
|
|
|
DEBUG_PRINT(F("Read packet:\t"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
2015-06-03 00:33:03 +02:00
|
|
|
}
|
2015-06-02 21:23:42 +02:00
|
|
|
|
2015-06-03 00:33:03 +02:00
|
|
|
// special case where we just one one publication packet at a time
|
|
|
|
if (checkForValidPubPacket) {
|
2015-06-05 06:16:08 +02:00
|
|
|
if ((buffer[0] == (MQTT_CTRL_PUBLISH << 4)) && (buffer[1] == len-2)) {
|
|
|
|
// oooh a valid publish packet!
|
|
|
|
DEBUG_PRINT(F("Read PUBLISH packet:\t"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
|
|
|
}
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-02 21:23:42 +02:00
|
|
|
Watchdog.reset();
|
2015-06-03 00:33:03 +02:00
|
|
|
timeout -= MQTT_CC3000_INTERAVAILDELAY;
|
|
|
|
delay(MQTT_CC3000_INTERAVAILDELAY);
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
2015-06-01 01:02:42 +02:00
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
bool Adafruit_MQTT_CC3000::sendPacket(uint8_t *buffer, uint8_t len) {
|
2015-06-01 05:19:13 +02:00
|
|
|
if (mqttclient.connected()) {
|
|
|
|
uint16_t ret = mqttclient.write(buffer, len);
|
2015-06-05 06:16:08 +02:00
|
|
|
DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
|
|
|
if (ret != len) {
|
|
|
|
DEBUG_PRINTLN("Failed to send complete packet.")
|
|
|
|
return false;
|
2015-06-03 00:33:03 +02:00
|
|
|
}
|
2015-06-05 06:16:08 +02:00
|
|
|
} else {
|
|
|
|
DEBUG_PRINTLN(F("Connection failed!"));
|
2015-06-03 00:33:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-05 06:16:08 +02:00
|
|
|
return true;
|
2015-06-03 00:33:03 +02:00
|
|
|
}
|