Moved to actions, no doxygen
This commit is contained in:
parent
3693eb8800
commit
148cfb3591
26
.github/workflows/githubci.yml
vendored
Normal file
26
.github/workflows/githubci.yml
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
name: Arduino Library CI
|
||||
|
||||
on: [pull_request, push, repository_dispatch]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
repository: adafruit/ci-arduino
|
||||
path: ci
|
||||
|
||||
- name: pre-install
|
||||
run: bash ci/actions_install.sh
|
||||
|
||||
- name: test platforms
|
||||
run: python3 ci/build_platform.py esp8266 zero
|
||||
|
||||
- name: clang
|
||||
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
|
14
.travis.yml
14
.travis.yml
@ -1,14 +0,0 @@
|
||||
language: c
|
||||
sudo: false
|
||||
before_install:
|
||||
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
|
||||
install:
|
||||
- arduino --install-library "Adafruit SleepyDog Library","Adafruit FONA Library"
|
||||
script:
|
||||
- build_platform esp8266
|
||||
- arduino --install-library "WiFi101"
|
||||
- build_platform zero
|
||||
notifications:
|
||||
email:
|
||||
on_success: change
|
||||
on_failure: change
|
@ -9,8 +9,8 @@
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
@ -21,8 +21,10 @@
|
||||
// SOFTWARE.
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_SAMD)
|
||||
static char *dtostrf (double val, signed char width, unsigned char prec, char *sout) {
|
||||
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || \
|
||||
defined(ARDUINO_ARCH_SAMD)
|
||||
static char *dtostrf(double val, signed char width, unsigned char prec,
|
||||
char *sout) {
|
||||
char fmt[20];
|
||||
sprintf(fmt, "%%%d.%df", width, prec);
|
||||
sprintf(sout, fmt, val);
|
||||
@ -31,23 +33,22 @@ static char *dtostrf (double val, signed char width, unsigned char prec, char *s
|
||||
#endif
|
||||
|
||||
#if defined(ESP8266)
|
||||
int strncasecmp(const char * str1, const char * str2, int len) {
|
||||
int d = 0;
|
||||
while(len--) {
|
||||
int c1 = tolower(*str1++);
|
||||
int c2 = tolower(*str2++);
|
||||
if(((d = c1 - c2) != 0) || (c2 == '\0')) {
|
||||
return d;
|
||||
}
|
||||
int strncasecmp(const char *str1, const char *str2, int len) {
|
||||
int d = 0;
|
||||
while (len--) {
|
||||
int c1 = tolower(*str1++);
|
||||
int c2 = tolower(*str2++);
|
||||
if (((d = c1 - c2) != 0) || (c2 == '\0')) {
|
||||
return d;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void printBuffer(uint8_t *buffer, uint16_t len) {
|
||||
DEBUG_PRINTER.print('\t');
|
||||
for (uint16_t i=0; i<len; i++) {
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
if (isprint(buffer[i]))
|
||||
DEBUG_PRINTER.write(buffer[i]);
|
||||
else
|
||||
@ -55,7 +56,7 @@ void printBuffer(uint8_t *buffer, uint16_t len) {
|
||||
DEBUG_PRINTER.print(F(" [0x"));
|
||||
if (buffer[i] < 0x10)
|
||||
DEBUG_PRINTER.print("0");
|
||||
DEBUG_PRINTER.print(buffer[i],HEX);
|
||||
DEBUG_PRINTER.print(buffer[i], HEX);
|
||||
DEBUG_PRINTER.print("], ");
|
||||
if (i % 8 == 7) {
|
||||
DEBUG_PRINTER.print("\n\t");
|
||||
@ -74,7 +75,7 @@ static uint8_t *stringprint(uint8_t *p, char *s) {
|
||||
}
|
||||
*/
|
||||
|
||||
static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen=0) {
|
||||
static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen = 0) {
|
||||
// If maxlen is specified (has a non-zero value) then use it as the maximum
|
||||
// length of the source string to write to the buffer. Otherwise write
|
||||
// the entire source string.
|
||||
@ -87,20 +88,18 @@ static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen=0) {
|
||||
Serial.write(pgm_read_byte(s+i));
|
||||
}
|
||||
*/
|
||||
p[0] = len >> 8; p++;
|
||||
p[0] = len & 0xFF; p++;
|
||||
p[0] = len >> 8;
|
||||
p++;
|
||||
p[0] = len & 0xFF;
|
||||
p++;
|
||||
strncpy((char *)p, s, len);
|
||||
return p+len;
|
||||
return p + len;
|
||||
}
|
||||
|
||||
|
||||
// Adafruit_MQTT Definition ////////////////////////////////////////////////////
|
||||
|
||||
Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||
uint16_t port,
|
||||
const char *cid,
|
||||
const char *user,
|
||||
const char *pass) {
|
||||
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
||||
const char *user, const char *pass) {
|
||||
servername = server;
|
||||
portnum = port;
|
||||
clientid = cid;
|
||||
@ -108,7 +107,7 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||
password = pass;
|
||||
|
||||
// reset subscriptions
|
||||
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
subscriptions[i] = 0;
|
||||
}
|
||||
|
||||
@ -118,14 +117,10 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||
will_retain = 0;
|
||||
|
||||
packet_id_counter = 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||
uint16_t port,
|
||||
const char *user,
|
||||
const char *pass) {
|
||||
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
|
||||
const char *user, const char *pass) {
|
||||
servername = server;
|
||||
portnum = port;
|
||||
clientid = "";
|
||||
@ -133,7 +128,7 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||
password = pass;
|
||||
|
||||
// reset subscriptions
|
||||
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
subscriptions[i] = 0;
|
||||
}
|
||||
|
||||
@ -143,7 +138,6 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||
will_retain = 0;
|
||||
|
||||
packet_id_counter = 0;
|
||||
|
||||
}
|
||||
|
||||
int8_t Adafruit_MQTT::connect() {
|
||||
@ -166,66 +160,70 @@ int8_t Adafruit_MQTT::connect() {
|
||||
return buffer[3];
|
||||
|
||||
// Setup subscriptions once connected.
|
||||
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
// Ignore subscriptions that aren't defined.
|
||||
if (subscriptions[i] == 0) continue;
|
||||
if (subscriptions[i] == 0)
|
||||
continue;
|
||||
|
||||
boolean success = false;
|
||||
for (uint8_t retry=0; (retry<3) && !success; retry++) { // retry until we get a suback
|
||||
for (uint8_t retry = 0; (retry < 3) && !success;
|
||||
retry++) { // retry until we get a suback
|
||||
// Construct and send subscription packet.
|
||||
uint8_t len = subscribePacket(buffer, subscriptions[i]->topic, subscriptions[i]->qos);
|
||||
uint8_t len = subscribePacket(buffer, subscriptions[i]->topic,
|
||||
subscriptions[i]->qos);
|
||||
if (!sendPacket(buffer, len))
|
||||
return -1;
|
||||
return -1;
|
||||
|
||||
if(MQTT_PROTOCOL_LEVEL < 3) // older versions didn't suback
|
||||
break;
|
||||
if (MQTT_PROTOCOL_LEVEL < 3) // older versions didn't suback
|
||||
break;
|
||||
|
||||
// Check for SUBACK if using MQTT 3.1.1 or higher
|
||||
// TODO: The Server is permitted to start sending PUBLISH packets matching the
|
||||
// Subscription before the Server sends the SUBACK Packet. (will really need to use callbacks - ada)
|
||||
// TODO: The Server is permitted to start sending PUBLISH packets matching
|
||||
// the Subscription before the Server sends the SUBACK Packet. (will
|
||||
// really need to use callbacks - ada)
|
||||
|
||||
//Serial.println("\t**looking for suback");
|
||||
// Serial.println("\t**looking for suback");
|
||||
if (processPacketsUntil(buffer, MQTT_CTRL_SUBACK, SUBACK_TIMEOUT_MS)) {
|
||||
success = true;
|
||||
break;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (! success) return -2; // failed to sub for some reason
|
||||
if (!success)
|
||||
return -2; // failed to sub for some reason
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int8_t Adafruit_MQTT::connect(const char *user, const char *pass)
|
||||
{
|
||||
int8_t Adafruit_MQTT::connect(const char *user, const char *pass) {
|
||||
username = user;
|
||||
password = pass;
|
||||
return connect();
|
||||
}
|
||||
|
||||
uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) {
|
||||
uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer,
|
||||
uint8_t waitforpackettype,
|
||||
uint16_t timeout) {
|
||||
uint16_t len;
|
||||
|
||||
while(true) {
|
||||
while (true) {
|
||||
len = readFullPacket(buffer, MAXBUFFERSIZE, timeout);
|
||||
|
||||
if(len == 0){
|
||||
if (len == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
if ((buffer[0] >> 4) == waitforpackettype)
|
||||
{
|
||||
if ((buffer[0] >> 4) == waitforpackettype) {
|
||||
return len;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ERROR_PRINTLN(F("Dropped a packet"));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16_t timeout) {
|
||||
uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
|
||||
uint16_t timeout) {
|
||||
// will read a packet and Do The Right Thing with length
|
||||
uint8_t *pbuff = buffer;
|
||||
|
||||
@ -233,9 +231,11 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
|
||||
|
||||
// read the packet type:
|
||||
rlen = readPacket(pbuff, 1, timeout);
|
||||
if (rlen != 1) return 0;
|
||||
if (rlen != 1)
|
||||
return 0;
|
||||
|
||||
DEBUG_PRINT(F("Packet Type:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
|
||||
DEBUG_PRINT(F("Packet Type:\t"));
|
||||
DEBUG_PRINTBUFFER(pbuff, rlen);
|
||||
pbuff++;
|
||||
|
||||
uint32_t value = 0;
|
||||
@ -244,64 +244,78 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
|
||||
|
||||
do {
|
||||
rlen = readPacket(pbuff, 1, timeout);
|
||||
if (rlen != 1) return 0;
|
||||
if (rlen != 1)
|
||||
return 0;
|
||||
encodedByte = pbuff[0]; // save the last read val
|
||||
pbuff++; // get ready for reading the next byte
|
||||
pbuff++; // get ready for reading the next byte
|
||||
uint32_t intermediate = encodedByte & 0x7F;
|
||||
intermediate *= multiplier;
|
||||
value += intermediate;
|
||||
multiplier *= 128;
|
||||
if (multiplier > (128UL*128UL*128UL)) {
|
||||
if (multiplier > (128UL * 128UL * 128UL)) {
|
||||
DEBUG_PRINT(F("Malformed packet len\n"));
|
||||
return 0;
|
||||
}
|
||||
} while (encodedByte & 0x80);
|
||||
|
||||
DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value);
|
||||
|
||||
if (value > (maxsize - (pbuff-buffer) - 1)) {
|
||||
DEBUG_PRINTLN(F("Packet too big for buffer"));
|
||||
rlen = readPacket(pbuff, (maxsize - (pbuff-buffer) - 1), timeout);
|
||||
DEBUG_PRINT(F("Packet Length:\t"));
|
||||
DEBUG_PRINTLN(value);
|
||||
|
||||
if (value > (maxsize - (pbuff - buffer) - 1)) {
|
||||
DEBUG_PRINTLN(F("Packet too big for buffer"));
|
||||
rlen = readPacket(pbuff, (maxsize - (pbuff - buffer) - 1), timeout);
|
||||
} else {
|
||||
rlen = readPacket(pbuff, value, timeout);
|
||||
}
|
||||
//DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
|
||||
// DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
|
||||
|
||||
return ((pbuff - buffer)+rlen);
|
||||
return ((pbuff - buffer) + rlen);
|
||||
}
|
||||
|
||||
const __FlashStringHelper* Adafruit_MQTT::connectErrorString(int8_t code) {
|
||||
switch (code) {
|
||||
case 1: return F("The Server does not support the level of the MQTT protocol requested");
|
||||
case 2: return F("The Client identifier is correct UTF-8 but not allowed by the Server");
|
||||
case 3: return F("The MQTT service is unavailable");
|
||||
case 4: return F("The data in the user name or password is malformed");
|
||||
case 5: return F("Not authorized to connect");
|
||||
case 6: return F("Exceeded reconnect rate limit. Please try again later.");
|
||||
case 7: return F("You have been banned from connecting. Please contact the MQTT server administrator for more details.");
|
||||
case -1: return F("Connection failed");
|
||||
case -2: return F("Failed to subscribe");
|
||||
default: return F("Unknown error");
|
||||
}
|
||||
const __FlashStringHelper *Adafruit_MQTT::connectErrorString(int8_t code) {
|
||||
switch (code) {
|
||||
case 1:
|
||||
return F(
|
||||
"The Server does not support the level of the MQTT protocol requested");
|
||||
case 2:
|
||||
return F(
|
||||
"The Client identifier is correct UTF-8 but not allowed by the Server");
|
||||
case 3:
|
||||
return F("The MQTT service is unavailable");
|
||||
case 4:
|
||||
return F("The data in the user name or password is malformed");
|
||||
case 5:
|
||||
return F("Not authorized to connect");
|
||||
case 6:
|
||||
return F("Exceeded reconnect rate limit. Please try again later.");
|
||||
case 7:
|
||||
return F("You have been banned from connecting. Please contact the MQTT "
|
||||
"server administrator for more details.");
|
||||
case -1:
|
||||
return F("Connection failed");
|
||||
case -2:
|
||||
return F("Failed to subscribe");
|
||||
default:
|
||||
return F("Unknown error");
|
||||
}
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::disconnect() {
|
||||
|
||||
// Construct and send disconnect packet.
|
||||
uint8_t len = disconnectPacket(buffer);
|
||||
if (! sendPacket(buffer, len))
|
||||
if (!sendPacket(buffer, len))
|
||||
DEBUG_PRINTLN(F("Unable to send disconnect packet"));
|
||||
|
||||
return disconnectServer();
|
||||
|
||||
}
|
||||
|
||||
|
||||
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
|
||||
return publish(topic, (uint8_t*)(data), strlen(data), qos);
|
||||
return publish(topic, (uint8_t *)(data), strlen(data), qos);
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) {
|
||||
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
|
||||
uint8_t qos) {
|
||||
// Construct and send publish packet.
|
||||
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
|
||||
if (!sendPacket(buffer, len))
|
||||
@ -320,7 +334,8 @@ bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uin
|
||||
packnum <<= 8;
|
||||
packnum |= buffer[3];
|
||||
|
||||
// we increment the packet_id_counter right after publishing so inc here too to match
|
||||
// we increment the packet_id_counter right after publishing so inc here too
|
||||
// to match
|
||||
packnum++;
|
||||
if (packnum != packet_id_counter)
|
||||
return false;
|
||||
@ -329,7 +344,8 @@ bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uin
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos, uint8_t retain) {
|
||||
bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos,
|
||||
uint8_t retain) {
|
||||
|
||||
if (connected()) {
|
||||
DEBUG_PRINT(F("Will defined after connect"));
|
||||
@ -342,22 +358,22 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos, ui
|
||||
will_retain = retain;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
|
||||
uint8_t i;
|
||||
// see if we are already subscribed
|
||||
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
if (subscriptions[i] == sub) {
|
||||
DEBUG_PRINTLN(F("Already subscribed"));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (i==MAXSUBSCRIPTIONS) { // add to subscriptionlist
|
||||
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
if (i == MAXSUBSCRIPTIONS) { // add to subscriptionlist
|
||||
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
if (subscriptions[i] == 0) {
|
||||
DEBUG_PRINT(F("Added sub ")); DEBUG_PRINTLN(i);
|
||||
DEBUG_PRINT(F("Added sub "));
|
||||
DEBUG_PRINTLN(i);
|
||||
subscriptions[i] = sub;
|
||||
return true;
|
||||
}
|
||||
@ -372,22 +388,23 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
||||
uint8_t i;
|
||||
|
||||
// see if we are already subscribed
|
||||
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
|
||||
if (subscriptions[i] == sub) {
|
||||
|
||||
DEBUG_PRINTLN(F("Found matching subscription and attempting to unsubscribe."));
|
||||
DEBUG_PRINTLN(
|
||||
F("Found matching subscription and attempting to unsubscribe."));
|
||||
|
||||
// Construct and send unsubscribe packet.
|
||||
uint8_t len = unsubscribePacket(buffer, subscriptions[i]->topic);
|
||||
|
||||
// sending unsubscribe failed
|
||||
if (! sendPacket(buffer, len))
|
||||
if (!sendPacket(buffer, len))
|
||||
return false;
|
||||
|
||||
// if QoS for this subscription is 1 or 2, we need
|
||||
// to wait for the unsuback to confirm unsubscription
|
||||
if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
|
||||
if (subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
|
||||
|
||||
// wait for UNSUBACK
|
||||
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
|
||||
@ -395,19 +412,17 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
|
||||
if ((len != 5) || (buffer[0] != (MQTT_CTRL_UNSUBACK << 4))) {
|
||||
return false; // failure to unsubscribe
|
||||
return false; // failure to unsubscribe
|
||||
}
|
||||
}
|
||||
|
||||
subscriptions[i] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// subscription not found, so we are unsubscribed
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void Adafruit_MQTT::processPackets(int16_t timeout) {
|
||||
@ -417,30 +432,32 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
|
||||
while (elapsed < (uint32_t)timeout) {
|
||||
Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed);
|
||||
if (sub) {
|
||||
//Serial.println("**** sub packet received");
|
||||
// Serial.println("**** sub packet received");
|
||||
if (sub->callback_uint32t != NULL) {
|
||||
// huh lets do the callback in integer mode
|
||||
uint32_t data = 0;
|
||||
data = atoi((char *)sub->lastread);
|
||||
//Serial.print("*** calling int callback with : "); Serial.println(data);
|
||||
sub->callback_uint32t(data);
|
||||
}
|
||||
else if (sub->callback_double != NULL) {
|
||||
// huh lets do the callback in doublefloat mode
|
||||
double data = 0;
|
||||
data = atof((char *)sub->lastread);
|
||||
//Serial.print("*** calling double callback with : "); Serial.println(data);
|
||||
sub->callback_double(data);
|
||||
}
|
||||
else if (sub->callback_buffer != NULL) {
|
||||
// huh lets do the callback in buffer mode
|
||||
//Serial.print("*** calling buffer callback with : "); Serial.println((char *)sub->lastread);
|
||||
sub->callback_buffer((char *)sub->lastread, sub->datalen);
|
||||
}
|
||||
else if (sub->callback_io != NULL) {
|
||||
// huh lets do the callback in integer mode
|
||||
uint32_t data = 0;
|
||||
data = atoi((char *)sub->lastread);
|
||||
// Serial.print("*** calling int callback with : ");
|
||||
// Serial.println(data);
|
||||
sub->callback_uint32t(data);
|
||||
} else if (sub->callback_double != NULL) {
|
||||
// huh lets do the callback in doublefloat mode
|
||||
double data = 0;
|
||||
data = atof((char *)sub->lastread);
|
||||
// Serial.print("*** calling double callback with : ");
|
||||
// Serial.println(data);
|
||||
sub->callback_double(data);
|
||||
} else if (sub->callback_buffer != NULL) {
|
||||
// huh lets do the callback in buffer mode
|
||||
// Serial.print("*** calling buffer callback with : ");
|
||||
// Serial.println((char *)sub->lastread);
|
||||
sub->callback_buffer((char *)sub->lastread, sub->datalen);
|
||||
} else if (sub->callback_io != NULL) {
|
||||
// huh lets do the callback in io mode
|
||||
//Serial.print("*** calling io instance callback with : "); Serial.println((char *)sub->lastread);
|
||||
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen);
|
||||
// Serial.print("*** calling io instance callback with : ");
|
||||
// Serial.println((char *)sub->lastread);
|
||||
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread,
|
||||
sub->datalen);
|
||||
}
|
||||
}
|
||||
|
||||
@ -457,21 +474,26 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
||||
uint16_t i, topiclen, datalen;
|
||||
|
||||
// Check if data is available to read.
|
||||
uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
|
||||
uint16_t len =
|
||||
readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
|
||||
if (!len)
|
||||
return NULL; // No data available, just quit.
|
||||
DEBUG_PRINT("Packet len: "); DEBUG_PRINTLN(len);
|
||||
return NULL; // No data available, just quit.
|
||||
DEBUG_PRINT("Packet len: ");
|
||||
DEBUG_PRINTLN(len);
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
|
||||
if (len<3) return NULL;
|
||||
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) return NULL;
|
||||
|
||||
if (len < 3)
|
||||
return NULL;
|
||||
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4)
|
||||
return NULL;
|
||||
|
||||
// Parse out length of packet.
|
||||
topiclen = buffer[3];
|
||||
DEBUG_PRINT(F("Looking for subscription len ")); DEBUG_PRINTLN(topiclen);
|
||||
DEBUG_PRINT(F("Looking for subscription len "));
|
||||
DEBUG_PRINTLN(topiclen);
|
||||
|
||||
// Find subscription associated with this packet.
|
||||
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
if (subscriptions[i]) {
|
||||
// Skip this subscription if its name length isn't the same as the
|
||||
// received topic name.
|
||||
@ -479,22 +501,25 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
||||
continue;
|
||||
// Stop if the subscription topic matches the received topic. Be careful
|
||||
// to make comparison case insensitive.
|
||||
if (strncasecmp((char*)buffer+4, subscriptions[i]->topic, topiclen) == 0) {
|
||||
DEBUG_PRINT(F("Found sub #")); DEBUG_PRINTLN(i);
|
||||
if (strncasecmp((char *)buffer + 4, subscriptions[i]->topic, topiclen) ==
|
||||
0) {
|
||||
DEBUG_PRINT(F("Found sub #"));
|
||||
DEBUG_PRINTLN(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i==MAXSUBSCRIPTIONS) return NULL; // matching sub not found ???
|
||||
if (i == MAXSUBSCRIPTIONS)
|
||||
return NULL; // matching sub not found ???
|
||||
|
||||
uint8_t packet_id_len = 0;
|
||||
uint16_t packetid = 0;
|
||||
// Check if it is QoS 1, TODO: we dont support QoS 2
|
||||
if ((buffer[0] & 0x6) == 0x2) {
|
||||
packet_id_len = 2;
|
||||
packetid = buffer[topiclen+4];
|
||||
packetid = buffer[topiclen + 4];
|
||||
packetid <<= 8;
|
||||
packetid |= buffer[topiclen+5];
|
||||
packetid |= buffer[topiclen + 5];
|
||||
}
|
||||
|
||||
// zero out the old data
|
||||
@ -502,17 +527,20 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
||||
|
||||
datalen = len - topiclen - packet_id_len - 4;
|
||||
if (datalen > SUBSCRIPTIONDATALEN) {
|
||||
datalen = SUBSCRIPTIONDATALEN-1; // cut it off
|
||||
datalen = SUBSCRIPTIONDATALEN - 1; // cut it off
|
||||
}
|
||||
// extract out just the data, into the subscription object itself
|
||||
memmove(subscriptions[i]->lastread, buffer+4+topiclen+packet_id_len, datalen);
|
||||
memmove(subscriptions[i]->lastread, buffer + 4 + topiclen + packet_id_len,
|
||||
datalen);
|
||||
subscriptions[i]->datalen = datalen;
|
||||
DEBUG_PRINT(F("Data len: ")); DEBUG_PRINTLN(datalen);
|
||||
DEBUG_PRINT(F("Data: ")); DEBUG_PRINTLN((char *)subscriptions[i]->lastread);
|
||||
DEBUG_PRINT(F("Data len: "));
|
||||
DEBUG_PRINTLN(datalen);
|
||||
DEBUG_PRINT(F("Data: "));
|
||||
DEBUG_PRINTLN((char *)subscriptions[i]->lastread);
|
||||
|
||||
if ((MQTT_PROTOCOL_LEVEL > 3) &&(buffer[0] & 0x6) == 0x2) {
|
||||
if ((MQTT_PROTOCOL_LEVEL > 3) && (buffer[0] & 0x6) == 0x2) {
|
||||
uint8_t ackpacket[4];
|
||||
|
||||
|
||||
// Construct and send puback packet.
|
||||
uint8_t len = pubackPacket(ackpacket, packetid);
|
||||
if (!sendPacket(ackpacket, len))
|
||||
@ -526,11 +554,12 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
||||
void Adafruit_MQTT::flushIncoming(uint16_t timeout) {
|
||||
// flush input!
|
||||
DEBUG_PRINTLN(F("Flushing input buffer"));
|
||||
while (readPacket(buffer, MAXBUFFERSIZE, timeout));
|
||||
while (readPacket(buffer, MAXBUFFERSIZE, timeout))
|
||||
;
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::ping(uint8_t num) {
|
||||
//flushIncoming(100);
|
||||
// flushIncoming(100);
|
||||
|
||||
while (num--) {
|
||||
// Construct and send ping packet.
|
||||
@ -560,15 +589,15 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
|
||||
// fixed header, connection messsage no flags
|
||||
p[0] = (MQTT_CTRL_CONNECT << 4) | 0x0;
|
||||
p+=2;
|
||||
p += 2;
|
||||
// fill in packet[1] last
|
||||
|
||||
#if MQTT_PROTOCOL_LEVEL == 3
|
||||
p = stringprint(p, "MQIsdp");
|
||||
p = stringprint(p, "MQIsdp");
|
||||
#elif MQTT_PROTOCOL_LEVEL == 4
|
||||
p = stringprint(p, "MQTT");
|
||||
p = stringprint(p, "MQTT");
|
||||
#else
|
||||
#error "MQTT level not supported"
|
||||
#error "MQTT level not supported"
|
||||
#endif
|
||||
|
||||
p[0] = MQTT_PROTOCOL_LEVEL;
|
||||
@ -582,14 +611,13 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
|
||||
p[0] |= MQTT_CONN_WILLFLAG;
|
||||
|
||||
if(will_qos == 1)
|
||||
if (will_qos == 1)
|
||||
p[0] |= MQTT_CONN_WILLQOS_1;
|
||||
else if(will_qos == 2)
|
||||
else if (will_qos == 2)
|
||||
p[0] |= MQTT_CONN_WILLQOS_2;
|
||||
|
||||
if(will_retain == 1)
|
||||
if (will_retain == 1)
|
||||
p[0] |= MQTT_CONN_WILLRETAIN;
|
||||
|
||||
}
|
||||
|
||||
if (pgm_read_byte(username) != 0)
|
||||
@ -603,8 +631,8 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
|
||||
p++;
|
||||
|
||||
if(MQTT_PROTOCOL_LEVEL == 3) {
|
||||
p = stringprint(p, clientid, 23); // Limit client ID to first 23 characters.
|
||||
if (MQTT_PROTOCOL_LEVEL == 3) {
|
||||
p = stringprint(p, clientid, 23); // Limit client ID to first 23 characters.
|
||||
} else {
|
||||
if (pgm_read_byte(clientid) != 0) {
|
||||
p = stringprint(p, clientid);
|
||||
@ -631,23 +659,24 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
||||
|
||||
len = p - packet;
|
||||
|
||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
||||
packet[1] = len - 2; // don't include the 2 bytes of fixed header data
|
||||
DEBUG_PRINTLN(F("MQTT connect packet:"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
// as per http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
|
||||
// as per
|
||||
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
|
||||
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
||||
uint8_t *data, uint16_t bLen, uint8_t qos) {
|
||||
uint8_t *data, uint16_t bLen,
|
||||
uint8_t qos) {
|
||||
uint8_t *p = packet;
|
||||
uint16_t len=0;
|
||||
uint16_t len = 0;
|
||||
|
||||
// calc length of non-header data
|
||||
len += 2; // two bytes to set the topic size
|
||||
len += 2; // two bytes to set the topic size
|
||||
len += strlen(topic); // topic length
|
||||
if(qos > 0) {
|
||||
if (qos > 0) {
|
||||
len += 2; // qos packet id
|
||||
}
|
||||
len += bLen; // payload length
|
||||
@ -661,28 +690,28 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
||||
uint8_t encodedByte = len % 128;
|
||||
len /= 128;
|
||||
// if there are more data to encode, set the top bit of this byte
|
||||
if ( len > 0 ) {
|
||||
if (len > 0) {
|
||||
encodedByte |= 0x80;
|
||||
}
|
||||
p[0] = encodedByte;
|
||||
p++;
|
||||
} while ( len > 0 );
|
||||
} while (len > 0);
|
||||
|
||||
// topic comes before packet identifier
|
||||
p = stringprint(p, topic);
|
||||
|
||||
// add packet identifier. used for checking PUBACK in QOS > 0
|
||||
if(qos > 0) {
|
||||
if (qos > 0) {
|
||||
p[0] = (packet_id_counter >> 8) & 0xFF;
|
||||
p[1] = packet_id_counter & 0xFF;
|
||||
p+=2;
|
||||
p += 2;
|
||||
|
||||
// increment the packet id
|
||||
packet_id_counter++;
|
||||
}
|
||||
|
||||
memmove(p, data, bLen);
|
||||
p+= bLen;
|
||||
p += bLen;
|
||||
len = p - packet;
|
||||
DEBUG_PRINTLN(F("MQTT publish packet:"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
@ -696,12 +725,12 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
||||
|
||||
p[0] = MQTT_CTRL_SUBSCRIBE << 4 | MQTT_QOS_1 << 1;
|
||||
// fill in packet[1] last
|
||||
p+=2;
|
||||
p += 2;
|
||||
|
||||
// packet identifier. used for checking SUBACK
|
||||
p[0] = (packet_id_counter >> 8) & 0xFF;
|
||||
p[1] = packet_id_counter & 0xFF;
|
||||
p+=2;
|
||||
p += 2;
|
||||
|
||||
// increment the packet id
|
||||
packet_id_counter++;
|
||||
@ -712,14 +741,12 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
||||
p++;
|
||||
|
||||
len = p - packet;
|
||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
||||
packet[1] = len - 2; // don't include the 2 bytes of fixed header data
|
||||
DEBUG_PRINTLN(F("MQTT subscription packet:"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
||||
|
||||
uint8_t *p = packet;
|
||||
@ -727,12 +754,12 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
||||
|
||||
p[0] = MQTT_CTRL_UNSUBSCRIBE << 4 | 0x1;
|
||||
// fill in packet[1] last
|
||||
p+=2;
|
||||
p += 2;
|
||||
|
||||
// packet identifier. used for checking UNSUBACK
|
||||
p[0] = (packet_id_counter >> 8) & 0xFF;
|
||||
p[1] = packet_id_counter & 0xFF;
|
||||
p+=2;
|
||||
p += 2;
|
||||
|
||||
// increment the packet id
|
||||
packet_id_counter++;
|
||||
@ -740,11 +767,10 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
||||
p = stringprint(p, topic);
|
||||
|
||||
len = p - packet;
|
||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
||||
packet[1] = len - 2; // don't include the 2 bytes of fixed header data
|
||||
DEBUG_PRINTLN(F("MQTT unsubscription packet:"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
return len;
|
||||
|
||||
}
|
||||
|
||||
uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) {
|
||||
@ -794,7 +820,8 @@ bool Adafruit_MQTT_Publish::publish(uint32_t i) {
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
|
||||
char payload[41]; // Need to technically hold float max, 39 digits and minus sign.
|
||||
char payload[41]; // Need to technically hold float max, 39 digits and minus
|
||||
// sign.
|
||||
dtostrf(f, 0, precision, payload);
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
@ -803,13 +830,12 @@ bool Adafruit_MQTT_Publish::publish(const char *payload) {
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
//publish buffer of arbitrary length
|
||||
// publish buffer of arbitrary length
|
||||
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
|
||||
|
||||
return mqtt->publish(topic, payload, bLen, qos);
|
||||
}
|
||||
|
||||
|
||||
// Adafruit_MQTT_Subscribe Definition //////////////////////////////////////////
|
||||
|
||||
Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
||||
@ -837,9 +863,10 @@ void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackBufferType cb) {
|
||||
callback_buffer = cb;
|
||||
}
|
||||
|
||||
void Adafruit_MQTT_Subscribe::setCallback(AdafruitIO_MQTT *io, SubscribeCallbackIOType cb) {
|
||||
void Adafruit_MQTT_Subscribe::setCallback(AdafruitIO_MQTT *io,
|
||||
SubscribeCallbackIOType cb) {
|
||||
callback_io = cb;
|
||||
io_mqtt= io;
|
||||
io_mqtt = io;
|
||||
}
|
||||
|
||||
void Adafruit_MQTT_Subscribe::removeCallback(void) {
|
||||
|
183
Adafruit_MQTT.h
183
Adafruit_MQTT.h
@ -9,8 +9,8 @@
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
@ -44,50 +44,62 @@
|
||||
|
||||
// Define actual debug output functions when necessary.
|
||||
#ifdef MQTT_DEBUG
|
||||
#define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||
#define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
|
||||
#define DEBUG_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
|
||||
#define DEBUG_PRINT(...) \
|
||||
{ DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||
#define DEBUG_PRINTLN(...) \
|
||||
{ DEBUG_PRINTER.println(__VA_ARGS__); }
|
||||
#define DEBUG_PRINTBUFFER(buffer, len) \
|
||||
{ printBuffer(buffer, len); }
|
||||
#else
|
||||
#define DEBUG_PRINT(...) {}
|
||||
#define DEBUG_PRINTLN(...) {}
|
||||
#define DEBUG_PRINTBUFFER(buffer, len) {}
|
||||
#define DEBUG_PRINT(...) \
|
||||
{}
|
||||
#define DEBUG_PRINTLN(...) \
|
||||
{}
|
||||
#define DEBUG_PRINTBUFFER(buffer, len) \
|
||||
{}
|
||||
#endif
|
||||
|
||||
#ifdef MQTT_ERROR
|
||||
#define ERROR_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||
#define ERROR_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
|
||||
#define ERROR_PRINTBUFFER(buffer, len) { printBuffer(buffer, len); }
|
||||
#define ERROR_PRINT(...) \
|
||||
{ DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||
#define ERROR_PRINTLN(...) \
|
||||
{ DEBUG_PRINTER.println(__VA_ARGS__); }
|
||||
#define ERROR_PRINTBUFFER(buffer, len) \
|
||||
{ printBuffer(buffer, len); }
|
||||
#else
|
||||
#define ERROR_PRINT(...) {}
|
||||
#define ERROR_PRINTLN(...) {}
|
||||
#define ERROR_PRINTBUFFER(buffer, len) {}
|
||||
#define ERROR_PRINT(...) \
|
||||
{}
|
||||
#define ERROR_PRINTLN(...) \
|
||||
{}
|
||||
#define ERROR_PRINTBUFFER(buffer, len) \
|
||||
{}
|
||||
#endif
|
||||
|
||||
// Use 3 (MQTT 3.0) or 4 (MQTT 3.1.1)
|
||||
#define MQTT_PROTOCOL_LEVEL 4
|
||||
|
||||
#define MQTT_CTRL_CONNECT 0x1
|
||||
#define MQTT_CTRL_CONNECTACK 0x2
|
||||
#define MQTT_CTRL_PUBLISH 0x3
|
||||
#define MQTT_CTRL_PUBACK 0x4
|
||||
#define MQTT_CTRL_PUBREC 0x5
|
||||
#define MQTT_CTRL_PUBREL 0x6
|
||||
#define MQTT_CTRL_PUBCOMP 0x7
|
||||
#define MQTT_CTRL_SUBSCRIBE 0x8
|
||||
#define MQTT_CTRL_SUBACK 0x9
|
||||
#define MQTT_CTRL_CONNECT 0x1
|
||||
#define MQTT_CTRL_CONNECTACK 0x2
|
||||
#define MQTT_CTRL_PUBLISH 0x3
|
||||
#define MQTT_CTRL_PUBACK 0x4
|
||||
#define MQTT_CTRL_PUBREC 0x5
|
||||
#define MQTT_CTRL_PUBREL 0x6
|
||||
#define MQTT_CTRL_PUBCOMP 0x7
|
||||
#define MQTT_CTRL_SUBSCRIBE 0x8
|
||||
#define MQTT_CTRL_SUBACK 0x9
|
||||
#define MQTT_CTRL_UNSUBSCRIBE 0xA
|
||||
#define MQTT_CTRL_UNSUBACK 0xB
|
||||
#define MQTT_CTRL_PINGREQ 0xC
|
||||
#define MQTT_CTRL_PINGRESP 0xD
|
||||
#define MQTT_CTRL_DISCONNECT 0xE
|
||||
#define MQTT_CTRL_UNSUBACK 0xB
|
||||
#define MQTT_CTRL_PINGREQ 0xC
|
||||
#define MQTT_CTRL_PINGRESP 0xD
|
||||
#define MQTT_CTRL_DISCONNECT 0xE
|
||||
|
||||
#define MQTT_QOS_1 0x1
|
||||
#define MQTT_QOS_0 0x0
|
||||
|
||||
#define CONNECT_TIMEOUT_MS 6000
|
||||
#define PUBLISH_TIMEOUT_MS 500
|
||||
#define PING_TIMEOUT_MS 500
|
||||
#define SUBACK_TIMEOUT_MS 500
|
||||
#define PING_TIMEOUT_MS 500
|
||||
#define SUBACK_TIMEOUT_MS 500
|
||||
|
||||
// Adjust as necessary, in seconds. Default to 5 minutes.
|
||||
#define MQTT_CONN_KEEPALIVE 300
|
||||
@ -97,50 +109,46 @@
|
||||
// 23 char client ID.
|
||||
#define MAXBUFFERSIZE (150)
|
||||
|
||||
#define MQTT_CONN_USERNAMEFLAG 0x80
|
||||
#define MQTT_CONN_PASSWORDFLAG 0x40
|
||||
#define MQTT_CONN_WILLRETAIN 0x20
|
||||
#define MQTT_CONN_WILLQOS_1 0x08
|
||||
#define MQTT_CONN_WILLQOS_2 0x18
|
||||
#define MQTT_CONN_WILLFLAG 0x04
|
||||
#define MQTT_CONN_CLEANSESSION 0x02
|
||||
#define MQTT_CONN_USERNAMEFLAG 0x80
|
||||
#define MQTT_CONN_PASSWORDFLAG 0x40
|
||||
#define MQTT_CONN_WILLRETAIN 0x20
|
||||
#define MQTT_CONN_WILLQOS_1 0x08
|
||||
#define MQTT_CONN_WILLQOS_2 0x18
|
||||
#define MQTT_CONN_WILLFLAG 0x04
|
||||
#define MQTT_CONN_CLEANSESSION 0x02
|
||||
|
||||
// how much data we save in a subscription object
|
||||
// and how many subscriptions we want to be able to track.
|
||||
#if defined (__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__)
|
||||
#define MAXSUBSCRIPTIONS 5
|
||||
#define SUBSCRIPTIONDATALEN 20
|
||||
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__)
|
||||
#define MAXSUBSCRIPTIONS 5
|
||||
#define SUBSCRIPTIONDATALEN 20
|
||||
#else
|
||||
#define MAXSUBSCRIPTIONS 15
|
||||
#define SUBSCRIPTIONDATALEN 100
|
||||
#define MAXSUBSCRIPTIONS 15
|
||||
#define SUBSCRIPTIONDATALEN 100
|
||||
#endif
|
||||
|
||||
class AdafruitIO_MQTT; // forward decl
|
||||
class AdafruitIO_MQTT; // forward decl
|
||||
|
||||
//Function pointer that returns an int
|
||||
// Function pointer that returns an int
|
||||
typedef void (*SubscribeCallbackUInt32Type)(uint32_t);
|
||||
// returns a double
|
||||
typedef void (*SubscribeCallbackDoubleType)(double);
|
||||
// returns a chunk of raw data
|
||||
typedef void (*SubscribeCallbackBufferType)(char *str, uint16_t len);
|
||||
// returns an io data wrapper instance
|
||||
typedef void (AdafruitIO_MQTT::*SubscribeCallbackIOType)(char *str, uint16_t len);
|
||||
typedef void (AdafruitIO_MQTT::*SubscribeCallbackIOType)(char *str,
|
||||
uint16_t len);
|
||||
|
||||
extern void printBuffer(uint8_t *buffer, uint16_t len);
|
||||
|
||||
class Adafruit_MQTT_Subscribe; // forward decl
|
||||
class Adafruit_MQTT_Subscribe; // forward decl
|
||||
|
||||
class Adafruit_MQTT {
|
||||
public:
|
||||
Adafruit_MQTT(const char *server,
|
||||
uint16_t port,
|
||||
const char *cid,
|
||||
const char *user,
|
||||
const char *pass);
|
||||
public:
|
||||
Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
||||
const char *user, const char *pass);
|
||||
|
||||
Adafruit_MQTT(const char *server,
|
||||
uint16_t port,
|
||||
const char *user = "",
|
||||
Adafruit_MQTT(const char *server, uint16_t port, const char *user = "",
|
||||
const char *pass = "");
|
||||
virtual ~Adafruit_MQTT() {}
|
||||
|
||||
@ -162,23 +170,25 @@ class Adafruit_MQTT {
|
||||
// connect(). This returns a __FlashStringHelper*, which points to a
|
||||
// string stored in flash, but can be directly passed to e.g.
|
||||
// Serial.println without any further processing.
|
||||
const __FlashStringHelper* connectErrorString(int8_t code);
|
||||
const __FlashStringHelper *connectErrorString(int8_t code);
|
||||
|
||||
// Sends MQTT disconnect packet and calls disconnectServer()
|
||||
bool disconnect();
|
||||
|
||||
// Return true if connected to the MQTT server, otherwise false.
|
||||
virtual bool connected() = 0; // Subclasses need to fill this in!
|
||||
virtual bool connected() = 0; // Subclasses need to fill this in!
|
||||
|
||||
// Set MQTT last will topic, payload, QOS, and retain. This needs
|
||||
// to be called before connect() because it is sent as part of the
|
||||
// connect control packet.
|
||||
bool will(const char *topic, const char *payload, uint8_t qos = 0, uint8_t retain = 0);
|
||||
bool will(const char *topic, const char *payload, uint8_t qos = 0,
|
||||
uint8_t retain = 0);
|
||||
|
||||
// Publish a message to a topic using the specified QoS level. Returns true
|
||||
// if the message was published, false otherwise.
|
||||
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
|
||||
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos = 0);
|
||||
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
|
||||
uint8_t qos = 0);
|
||||
|
||||
// Add a subscription to receive messages for a topic. Returns true if the
|
||||
// subscription could be added or was already present, false otherwise.
|
||||
@ -190,37 +200,41 @@ class Adafruit_MQTT {
|
||||
bool unsubscribe(Adafruit_MQTT_Subscribe *sub);
|
||||
|
||||
// Check if any subscriptions have new messages. Will return a reference to
|
||||
// an Adafruit_MQTT_Subscribe object which has a new message. Should be called
|
||||
// in the sketch's loop function to ensure new messages are recevied. Note
|
||||
// that subscribe should be called first for each topic that receives messages!
|
||||
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0);
|
||||
// an Adafruit_MQTT_Subscribe object which has a new message. Should be
|
||||
// called in the sketch's loop function to ensure new messages are recevied.
|
||||
// Note that subscribe should be called first for each topic that receives
|
||||
// messages!
|
||||
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout = 0);
|
||||
|
||||
void processPackets(int16_t timeout);
|
||||
|
||||
// Ping the server to ensure the connection is still alive.
|
||||
bool ping(uint8_t n = 1);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
// Interface that subclasses need to implement:
|
||||
|
||||
// Connect to the server and return true if successful, false otherwise.
|
||||
virtual bool connectServer() = 0;
|
||||
|
||||
// Disconnect from the MQTT server. Returns true if disconnected, false otherwise.
|
||||
virtual bool disconnectServer() = 0; // Subclasses need to fill this in!
|
||||
// Disconnect from the MQTT server. Returns true if disconnected, false
|
||||
// otherwise.
|
||||
virtual bool disconnectServer() = 0; // Subclasses need to fill this in!
|
||||
|
||||
// Send data to the server specified by the buffer and length of data.
|
||||
virtual bool sendPacket(uint8_t *buffer, uint16_t len) = 0;
|
||||
|
||||
// Read MQTT packet from the server. Will read up to maxlen bytes and store
|
||||
// the data in the provided buffer. Waits up to the specified timeout (in
|
||||
// milliseconds) for data to be available.
|
||||
virtual uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) = 0;
|
||||
// milliseconds) for data to be available.
|
||||
virtual uint16_t readPacket(uint8_t *buffer, uint16_t maxlen,
|
||||
int16_t timeout) = 0;
|
||||
|
||||
// Read a full packet, keeping note of the correct length
|
||||
uint16_t readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16_t timeout);
|
||||
// Properly process packets until you get to one you want
|
||||
uint16_t processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout);
|
||||
uint16_t processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype,
|
||||
uint16_t timeout);
|
||||
|
||||
// Shared state that subclasses can use:
|
||||
const char *servername;
|
||||
@ -232,37 +246,40 @@ class Adafruit_MQTT {
|
||||
const char *will_payload;
|
||||
uint8_t will_qos;
|
||||
uint8_t will_retain;
|
||||
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
|
||||
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
|
||||
uint16_t packet_id_counter;
|
||||
|
||||
private:
|
||||
private:
|
||||
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];
|
||||
|
||||
void flushIncoming(uint16_t timeout);
|
||||
void flushIncoming(uint16_t timeout);
|
||||
|
||||
// Functions to generate MQTT packets.
|
||||
uint8_t connectPacket(uint8_t *packet);
|
||||
uint8_t disconnectPacket(uint8_t *packet);
|
||||
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
|
||||
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload,
|
||||
uint16_t bLen, uint8_t qos);
|
||||
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
|
||||
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
|
||||
uint8_t pingPacket(uint8_t *packet);
|
||||
uint8_t pubackPacket(uint8_t *packet, uint16_t packetid);
|
||||
};
|
||||
|
||||
|
||||
class Adafruit_MQTT_Publish {
|
||||
public:
|
||||
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
|
||||
public:
|
||||
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed,
|
||||
uint8_t qos = 0);
|
||||
|
||||
bool publish(const char *s);
|
||||
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
|
||||
// This might be ignored and a higher precision value sent.
|
||||
bool publish(
|
||||
double f,
|
||||
uint8_t precision =
|
||||
2); // Precision controls the minimum number of digits after decimal.
|
||||
// This might be ignored and a higher precision value sent.
|
||||
bool publish(int32_t i);
|
||||
bool publish(uint32_t i);
|
||||
bool publish(uint8_t *b, uint16_t bLen);
|
||||
|
||||
|
||||
private:
|
||||
Adafruit_MQTT *mqtt;
|
||||
const char *topic;
|
||||
@ -270,8 +287,9 @@ private:
|
||||
};
|
||||
|
||||
class Adafruit_MQTT_Subscribe {
|
||||
public:
|
||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
|
||||
public:
|
||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname,
|
||||
uint8_t q = 0);
|
||||
|
||||
void setCallback(SubscribeCallbackUInt32Type callb);
|
||||
void setCallback(SubscribeCallbackDoubleType callb);
|
||||
@ -290,13 +308,12 @@ class Adafruit_MQTT_Subscribe {
|
||||
SubscribeCallbackUInt32Type callback_uint32t;
|
||||
SubscribeCallbackDoubleType callback_double;
|
||||
SubscribeCallbackBufferType callback_buffer;
|
||||
SubscribeCallbackIOType callback_io;
|
||||
SubscribeCallbackIOType callback_io;
|
||||
|
||||
AdafruitIO_MQTT *io_mqtt;
|
||||
|
||||
private:
|
||||
private:
|
||||
Adafruit_MQTT *mqtt;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -9,8 +9,8 @@
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
@ -21,15 +21,16 @@
|
||||
// SOFTWARE.
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
|
||||
bool Adafruit_MQTT_Client::connectServer() {
|
||||
// Grab server name from flash and copy to buffer for name resolution.
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
strcpy((char *)buffer, servername);
|
||||
DEBUG_PRINT(F("Connecting to: ")); DEBUG_PRINTLN((char *)buffer);
|
||||
DEBUG_PRINT(F("Connecting to: "));
|
||||
DEBUG_PRINTLN((char *)buffer);
|
||||
// Connect and check for success (0 result).
|
||||
int r = client->connect((char *)buffer, portnum);
|
||||
DEBUG_PRINT(F("Connect result: ")); DEBUG_PRINTLN(r);
|
||||
DEBUG_PRINT(F("Connect result: "));
|
||||
DEBUG_PRINTLN(r);
|
||||
return r != 0;
|
||||
}
|
||||
|
||||
@ -49,26 +50,26 @@ bool Adafruit_MQTT_Client::connected() {
|
||||
|
||||
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
|
||||
int16_t timeout) {
|
||||
/* Read data until either the connection is closed, or the idle timeout is reached. */
|
||||
/* Read data until either the connection is closed, or the idle timeout is
|
||||
* reached. */
|
||||
uint16_t len = 0;
|
||||
int16_t t = timeout;
|
||||
|
||||
|
||||
while (client->connected() && (timeout >= 0)) {
|
||||
//DEBUG_PRINT('.');
|
||||
// DEBUG_PRINT('.');
|
||||
while (client->available()) {
|
||||
//DEBUG_PRINT('!');
|
||||
// DEBUG_PRINT('!');
|
||||
char c = client->read();
|
||||
timeout = t; // reset the timeout
|
||||
timeout = t; // reset the timeout
|
||||
buffer[len] = c;
|
||||
//DEBUG_PRINTLN((uint8_t)c, HEX);
|
||||
// DEBUG_PRINTLN((uint8_t)c, HEX);
|
||||
len++;
|
||||
|
||||
if (maxlen == 0) { // handle zero-length packets
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (len == maxlen) { // we read all we want, bail
|
||||
if (len == maxlen) { // we read all we want, bail
|
||||
DEBUG_PRINT(F("Read data:\t"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
return len;
|
||||
@ -88,14 +89,15 @@ bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
|
||||
// send 250 bytes at most at a time, can adjust this later based on Client
|
||||
|
||||
uint16_t sendlen = len > 250 ? 250 : len;
|
||||
//Serial.print("Sending: "); Serial.println(sendlen);
|
||||
// Serial.print("Sending: "); Serial.println(sendlen);
|
||||
ret = client->write(buffer, sendlen);
|
||||
DEBUG_PRINT(F("Client sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
||||
DEBUG_PRINT(F("Client sendPacket returned: "));
|
||||
DEBUG_PRINTLN(ret);
|
||||
len -= ret;
|
||||
|
||||
if (ret != sendlen) {
|
||||
DEBUG_PRINTLN("Failed to send packet.");
|
||||
return false;
|
||||
DEBUG_PRINTLN("Failed to send packet.");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
DEBUG_PRINTLN(F("Connection failed!"));
|
||||
|
@ -9,8 +9,8 @@
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
@ -22,30 +22,24 @@
|
||||
#ifndef _ADAFRUIT_MQTT_CLIENT_H_
|
||||
#define _ADAFRUIT_MQTT_CLIENT_H_
|
||||
|
||||
#include "Client.h"
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
#include "Client.h"
|
||||
|
||||
// How long to delay waiting for new data to be available in readPacket.
|
||||
#define MQTT_CLIENT_READINTERVAL_MS 10
|
||||
|
||||
|
||||
// MQTT client implementation for a generic Arduino Client interface. Can work
|
||||
// with almost all Arduino network hardware like ethernet shield, wifi shield,
|
||||
// and even other platforms like ESP8266.
|
||||
class Adafruit_MQTT_Client : public Adafruit_MQTT {
|
||||
public:
|
||||
public:
|
||||
Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port,
|
||||
const char *cid, const char *user, const char *pass):
|
||||
Adafruit_MQTT(server, port, cid, user, pass),
|
||||
client(client)
|
||||
{}
|
||||
const char *cid, const char *user, const char *pass)
|
||||
: Adafruit_MQTT(server, port, cid, user, pass), client(client) {}
|
||||
|
||||
Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port,
|
||||
const char *user="", const char *pass=""):
|
||||
Adafruit_MQTT(server, port, user, pass),
|
||||
client(client)
|
||||
{}
|
||||
const char *user = "", const char *pass = "")
|
||||
: Adafruit_MQTT(server, port, user, pass), client(client) {}
|
||||
|
||||
bool connectServer();
|
||||
bool disconnectServer();
|
||||
@ -53,9 +47,8 @@ class Adafruit_MQTT_Client : public Adafruit_MQTT {
|
||||
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout);
|
||||
bool sendPacket(uint8_t *buffer, uint16_t len);
|
||||
|
||||
private:
|
||||
Client* client;
|
||||
private:
|
||||
Client *client;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -9,8 +9,8 @@
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
@ -22,30 +22,25 @@
|
||||
#ifndef _ADAFRUIT_MQTT_FONA_H_
|
||||
#define _ADAFRUIT_MQTT_FONA_H_
|
||||
|
||||
#include <Adafruit_FONA.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include <Adafruit_FONA.h>
|
||||
|
||||
#define MQTT_FONA_INTERAVAILDELAY 100
|
||||
#define MQTT_FONA_QUERYDELAY 500
|
||||
|
||||
|
||||
// FONA-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-FONA platforms (since Arduino will include all .cpp files
|
||||
// in the compilation of the library).
|
||||
class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
||||
public:
|
||||
public:
|
||||
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
||||
const char *cid, const char *user, const char *pass):
|
||||
Adafruit_MQTT(server, port, cid, user, pass),
|
||||
fona(f)
|
||||
{}
|
||||
const char *cid, const char *user, const char *pass)
|
||||
: Adafruit_MQTT(server, port, cid, user, pass), fona(f) {}
|
||||
|
||||
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
||||
const char *user="", const char *pass=""):
|
||||
Adafruit_MQTT(server, port, user, pass),
|
||||
fona(f)
|
||||
{}
|
||||
const char *user = "", const char *pass = "")
|
||||
: Adafruit_MQTT(server, port, user, pass), fona(f) {}
|
||||
|
||||
bool connectServer() {
|
||||
char server[40];
|
||||
@ -59,9 +54,7 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
||||
return fona->TCPconnect(server, portnum);
|
||||
}
|
||||
|
||||
bool disconnectServer() {
|
||||
return fona->TCPclose();
|
||||
}
|
||||
bool disconnectServer() { return fona->TCPclose(); }
|
||||
|
||||
bool connected() {
|
||||
// Return true if connected, false if not connected.
|
||||
@ -72,48 +65,52 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
||||
uint8_t *buffp = buffer;
|
||||
DEBUG_PRINTLN(F("Reading data.."));
|
||||
|
||||
if (!fona->TCPconnected()) return 0;
|
||||
if (!fona->TCPconnected())
|
||||
return 0;
|
||||
|
||||
|
||||
/* Read data until either the connection is closed, or the idle timeout is reached. */
|
||||
/* Read data until either the connection is closed, or the idle timeout is
|
||||
* reached. */
|
||||
uint16_t len = 0;
|
||||
int16_t t = timeout;
|
||||
uint16_t avail;
|
||||
|
||||
while (fona->TCPconnected() && (timeout >= 0)) {
|
||||
//DEBUG_PRINT('.');
|
||||
// DEBUG_PRINT('.');
|
||||
while (avail = fona->TCPavailable()) {
|
||||
//DEBUG_PRINT('!');
|
||||
// DEBUG_PRINT('!');
|
||||
|
||||
if (len + avail > maxlen) {
|
||||
avail = maxlen - len;
|
||||
if (avail == 0) return len;
|
||||
avail = maxlen - len;
|
||||
if (avail == 0)
|
||||
return len;
|
||||
}
|
||||
|
||||
// try to read the data into the end of the pointer
|
||||
if (! fona->TCPread(buffp, avail)) return len;
|
||||
if (!fona->TCPread(buffp, avail))
|
||||
return len;
|
||||
|
||||
// read it! advance pointer
|
||||
buffp += avail;
|
||||
len += avail;
|
||||
timeout = t; // reset the timeout
|
||||
timeout = t; // reset the timeout
|
||||
|
||||
//DEBUG_PRINTLN((uint8_t)c, HEX);
|
||||
// DEBUG_PRINTLN((uint8_t)c, HEX);
|
||||
|
||||
if (len == maxlen) { // we read all we want, bail
|
||||
if (len == maxlen) { // we read all we want, bail
|
||||
DEBUG_PRINT(F("Read:\t"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
return len;
|
||||
return len;
|
||||
}
|
||||
}
|
||||
#ifdef ADAFRUIT_SLEEPYDOG_H
|
||||
Watchdog.reset();
|
||||
#endif
|
||||
timeout -= MQTT_FONA_INTERAVAILDELAY;
|
||||
timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the FONA for avail()
|
||||
timeout -= MQTT_FONA_QUERYDELAY; // this is how long it takes to query the
|
||||
// FONA for avail()
|
||||
delay(MQTT_FONA_INTERAVAILDELAY);
|
||||
}
|
||||
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
@ -121,7 +118,7 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
||||
DEBUG_PRINTLN(F("Writing packet"));
|
||||
if (fona->TCPconnected()) {
|
||||
boolean ret = fona->TCPsend((char *)buffer, len);
|
||||
//DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
||||
// DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
||||
if (!ret) {
|
||||
DEBUG_PRINTLN("Failed to send packet.");
|
||||
return false;
|
||||
@ -133,10 +130,9 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
uint32_t serverip;
|
||||
Adafruit_FONA *fona;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -1,4 +1,4 @@
|
||||
# Adafruit MQTT Library [![Build Status](https://travis-ci.com/adafruit/Adafruit_MQTT_Library.svg?branch=master)](https://travis-ci.com/adafruit/Adafruit_MQTT_Library)
|
||||
# Adafruit MQTT Library [![Build Status](https://github.com/adafruit/Adafruit_MQTT_Library/workflows/Arduino%20Library%20CI/badge.svg)](https://github.com/adafruit/Adafruit_MQTT_Library/actions)
|
||||
|
||||
Arduino library for MQTT support, including access to Adafruit IO. Works with
|
||||
the Adafruit FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
|
||||
|
@ -1,20 +1,27 @@
|
||||
#include "Adafruit_FONA.h"
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#include "Adafruit_FONA.h"
|
||||
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
#define halt(s) \
|
||||
{ \
|
||||
Serial.println(F(s)); \
|
||||
while (1) \
|
||||
; \
|
||||
}
|
||||
|
||||
extern Adafruit_FONA fona;
|
||||
extern SoftwareSerial fonaSS;
|
||||
|
||||
boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *username, const __FlashStringHelper *password) {
|
||||
boolean FONAconnect(const __FlashStringHelper *apn,
|
||||
const __FlashStringHelper *username,
|
||||
const __FlashStringHelper *password) {
|
||||
Watchdog.reset();
|
||||
|
||||
Serial.println(F("Initializing FONA....(May take 3 seconds)"));
|
||||
|
||||
|
||||
fonaSS.begin(4800); // if you're using software serial
|
||||
|
||||
if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
|
||||
|
||||
if (!fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
|
||||
Serial.println(F("Couldn't find FONA"));
|
||||
return false;
|
||||
}
|
||||
@ -23,25 +30,25 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
|
||||
Watchdog.reset();
|
||||
Serial.println(F("Checking for network..."));
|
||||
while (fona.getNetworkStatus() != 1) {
|
||||
delay(500);
|
||||
delay(500);
|
||||
}
|
||||
|
||||
Watchdog.reset();
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
Watchdog.reset();
|
||||
|
||||
|
||||
fona.setGPRSNetworkSettings(apn, username, password);
|
||||
|
||||
Serial.println(F("Disabling GPRS"));
|
||||
fona.enableGPRS(false);
|
||||
|
||||
|
||||
Watchdog.reset();
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
delay(5000); // wait a few seconds to stabilize connection
|
||||
Watchdog.reset();
|
||||
|
||||
Serial.println(F("Enabling GPRS"));
|
||||
if (!fona.enableGPRS(true)) {
|
||||
Serial.println(F("Failed to turn GPRS on"));
|
||||
Serial.println(F("Failed to turn GPRS on"));
|
||||
return false;
|
||||
}
|
||||
Watchdog.reset();
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=Adafruit MQTT Library
|
||||
version=1.0.3
|
||||
version=1.1.0
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware.
|
||||
@ -7,3 +7,4 @@ paragraph=Simple MQTT library that supports the bare minimum to publish and subs
|
||||
category=Communication
|
||||
url=https://github.com/adafruit/Adafruit_MQTT_Library
|
||||
architectures=*
|
||||
depends=Adafruit SleepyDog Library, Adafruit FONA Library, WiFi101
|
||||
|
Loading…
Reference in New Issue
Block a user