You've already forked Adafruit_MQTT_Library
mirror of
https://github.com/adafruit/Adafruit_MQTT_Library.git
synced 2025-07-27 15:01:49 +03:00
Add Adafruit_MQTT_Client class that implements MQTT code for generic Arduino client. Add ESP8266 example using new MQTT client class. Fix bugs with using library on ESP8266 (move CC3000 to header-only class).
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
#ifndef _ADAFRUIT_MQTT_CC3000_H_
|
||||
#define _ADAFRUIT_MQTT_CC3000_H_
|
||||
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
@ -8,6 +9,10 @@
|
||||
#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,
|
||||
@ -16,11 +21,100 @@ class Adafruit_MQTT_CC3000 : public Adafruit_MQTT {
|
||||
cc3000(cc3k)
|
||||
{}
|
||||
|
||||
bool connectServer();
|
||||
bool disconnect();
|
||||
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);
|
||||
bool sendPacket(uint8_t *buffer, uint8_t len);
|
||||
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;
|
||||
|
Reference in New Issue
Block a user