127 lines
3.6 KiB
C++
127 lines
3.6 KiB
C++
#ifndef _ADAFRUIT_MQTT_CC3000_H_
|
|
#define _ADAFRUIT_MQTT_CC3000_H_
|
|
|
|
#include <Adafruit_SleepyDog.h>
|
|
#include <Adafruit_CC3000.h>
|
|
#include "Adafruit_MQTT.h"
|
|
|
|
// delay in ms between calls of available()
|
|
#define MQTT_CC3000_INTERAVAILDELAY 10
|
|
|
|
|
|
// CC3000-specific version of the Adafruit_MQTT class.
|
|
// Note that this is defined as a header-only class to prevent issues with using
|
|
// the library on non-CC3000 platforms (since Arduino will include all .cpp files
|
|
// in the compilation of the library).
|
|
class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
|
|
public:
|
|
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)
|
|
{}
|
|
|
|
bool connectServer() {
|
|
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
|
|
strcpy_P((char *)buffer, servername);
|
|
Serial.print((char *)buffer); Serial.print(F(" -> "));
|
|
uint8_t dnsretries = 5;
|
|
|
|
Watchdog.reset();
|
|
while (ip == 0) {
|
|
if (! cc3000->getHostByName((char *)buffer, &ip)) {
|
|
Serial.println(F("Couldn't resolve!"));
|
|
dnsretries--;
|
|
Watchdog.reset();
|
|
}
|
|
//Serial.println("OK"); Serial.println(ip, HEX);
|
|
if (!dnsretries) return false;
|
|
delay(500);
|
|
}
|
|
|
|
serverip = ip;
|
|
cc3000->printIPdotsRev(serverip);
|
|
Serial.println();
|
|
}
|
|
|
|
Watchdog.reset();
|
|
|
|
// connect to server
|
|
DEBUG_PRINTLN(F("Connecting to TCP"));
|
|
mqttclient = cc3000->connectTCP(serverip, portnum);
|
|
|
|
return mqttclient.connected();
|
|
}
|
|
|
|
bool disconnect() {
|
|
return (mqttclient.close() == 0);
|
|
}
|
|
|
|
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout,
|
|
bool checkForValidPubPacket = false) {
|
|
/* Read data until either the connection is closed, or the idle timeout is reached. */
|
|
uint16_t len = 0;
|
|
int16_t t = timeout;
|
|
|
|
while (mqttclient.connected() && (timeout >= 0)) {
|
|
//DEBUG_PRINT('.');
|
|
while (mqttclient.available()) {
|
|
//DEBUG_PRINT('!');
|
|
char c = mqttclient.read();
|
|
timeout = t; // reset the timeout
|
|
buffer[len] = c;
|
|
//DEBUG_PRINTLN((uint8_t)c, HEX);
|
|
len++;
|
|
if (len == maxlen) { // we read all we want, bail
|
|
DEBUG_PRINT(F("Read packet:\t"));
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
return len;
|
|
}
|
|
|
|
// special case where we just one one publication packet at a time
|
|
if (checkForValidPubPacket) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
Watchdog.reset();
|
|
timeout -= MQTT_CC3000_INTERAVAILDELAY;
|
|
delay(MQTT_CC3000_INTERAVAILDELAY);
|
|
}
|
|
return len;
|
|
}
|
|
|
|
bool sendPacket(uint8_t *buffer, uint8_t len) {
|
|
if (mqttclient.connected()) {
|
|
uint16_t ret = mqttclient.write(buffer, len);
|
|
DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
|
if (ret != len) {
|
|
DEBUG_PRINTLN("Failed to send complete packet.")
|
|
return false;
|
|
}
|
|
} else {
|
|
DEBUG_PRINTLN(F("Connection failed!"));
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
uint32_t serverip;
|
|
Adafruit_CC3000 *cc3000;
|
|
Adafruit_CC3000_Client mqttclient;
|
|
};
|
|
|
|
|
|
#endif
|