Compare commits
No commits in common. "master" and "progmem_fixes" have entirely different histories.
master
...
progmem_fi
46 changed files with 936 additions and 1174 deletions
26
.github/workflows/githubci.yml
vendored
26
.github/workflows/githubci.yml
vendored
|
|
@ -1,26 +0,0 @@
|
|||
name: Arduino Library CI
|
||||
|
||||
on: [pull_request, push, repository_dispatch]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/setup-python@v4
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v3
|
||||
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 .
|
||||
12
.travis.yml
Normal file
12
.travis.yml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
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,Adafruit CC3000 Library,Adafruit_WINC1500"
|
||||
script:
|
||||
- build_main_platforms
|
||||
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,10 +21,8 @@
|
|||
// SOFTWARE.
|
||||
#include "Adafruit_MQTT.h"
|
||||
|
||||
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || \
|
||||
defined(ARDUINO_SAMD_MKR1010) || 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)
|
||||
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);
|
||||
|
|
@ -46,6 +44,7 @@ int strncasecmp(const char *str1, const char *str2, int len) {
|
|||
}
|
||||
#endif
|
||||
|
||||
|
||||
void printBuffer(uint8_t *buffer, uint16_t len) {
|
||||
DEBUG_PRINTER.print('\t');
|
||||
for (uint16_t i=0; i<len; i++) {
|
||||
|
|
@ -88,34 +87,20 @@ 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;
|
||||
}
|
||||
|
||||
// packetAdditionalLen is a helper function used to figure out
|
||||
// how bigger the payload needs to be in order to account for
|
||||
// its variable length field. As per
|
||||
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Table_2.4_Size
|
||||
// See also readFullPacket
|
||||
static uint16_t packetAdditionalLen(uint32_t currLen) {
|
||||
/* Increase length field based on current length */
|
||||
if (currLen < 128) // 7-bits
|
||||
return 0;
|
||||
if (currLen < 16384) // 14-bits
|
||||
return 1;
|
||||
if (currLen < 2097152) // 21-bits
|
||||
return 2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
|
@ -132,12 +117,15 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
|||
will_qos = 0;
|
||||
will_retain = 0;
|
||||
|
||||
packet_id_counter = 1; // MQTT spec forbids packet id of 0 if QOS=1
|
||||
keepAliveInterval = MQTT_CONN_KEEPALIVE;
|
||||
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 = "";
|
||||
|
|
@ -154,8 +142,8 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
|
|||
will_qos = 0;
|
||||
will_retain = 0;
|
||||
|
||||
packet_id_counter = 1; // MQTT spec forbids packet id of 0 if QOS=1
|
||||
keepAliveInterval = MQTT_CONN_KEEPALIVE;
|
||||
packet_id_counter = 0;
|
||||
|
||||
}
|
||||
|
||||
int8_t Adafruit_MQTT::connect() {
|
||||
|
|
@ -170,27 +158,22 @@ int8_t Adafruit_MQTT::connect() {
|
|||
|
||||
// Read connect response packet and verify it
|
||||
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
|
||||
if (len != 4) {
|
||||
if (len != 4)
|
||||
return -1;
|
||||
}
|
||||
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2)) {
|
||||
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
|
||||
return -1;
|
||||
}
|
||||
if (buffer[3] != 0)
|
||||
return buffer[3];
|
||||
|
||||
// Setup subscriptions once connected.
|
||||
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;
|
||||
|
||||
|
|
@ -198,96 +181,57 @@ int8_t Adafruit_MQTT::connect() {
|
|||
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");
|
||||
if (processPacketsUntil(buffer, MQTT_CTRL_SUBACK, SUBACK_TIMEOUT_MS)) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
//Serial.println("\t**failed, retrying!");
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
void Adafruit_MQTT::processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub) {
|
||||
if (sub->callback_uint32t != NULL) {
|
||||
// execute callback in integer mode
|
||||
uint32_t data = 0;
|
||||
data = atoi((char *)sub->lastread);
|
||||
sub->callback_uint32t(data);
|
||||
} else if (sub->callback_double != NULL) {
|
||||
// execute callback in doublefloat mode
|
||||
double data = 0;
|
||||
data = atof((char *)sub->lastread);
|
||||
sub->callback_double(data);
|
||||
} else if (sub->callback_buffer != NULL) {
|
||||
// execute callback in buffer mode
|
||||
sub->callback_buffer((char *)sub->lastread, sub->datalen);
|
||||
} else if (sub->callback_io != NULL) {
|
||||
// execute callback in io mode
|
||||
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen);
|
||||
} else {
|
||||
DEBUG_PRINTLN(
|
||||
"ERROR: Subscription packet did not have an associated callback");
|
||||
return;
|
||||
}
|
||||
// mark subscription message as "read""
|
||||
sub->new_message = false;
|
||||
}
|
||||
|
||||
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 (len = readFullPacket(buffer, MAXBUFFERSIZE, timeout)) {
|
||||
|
||||
while (true) {
|
||||
len = readFullPacket(buffer, MAXBUFFERSIZE, timeout);
|
||||
//DEBUG_PRINT("Packet read size: "); DEBUG_PRINTLN(len);
|
||||
// TODO: add subscription reading & call back processing here
|
||||
|
||||
if (len == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
uint8_t packetType = (buffer[0] >> 4);
|
||||
if (packetType == waitforpackettype) {
|
||||
if ((buffer[0] >> 4) == waitforpackettype) {
|
||||
//DEBUG_PRINTLN(F("Found right packet"));
|
||||
return len;
|
||||
} else {
|
||||
if (packetType == MQTT_CTRL_PUBLISH) {
|
||||
Adafruit_MQTT_Subscribe *sub = handleSubscriptionPacket(len);
|
||||
if (sub)
|
||||
processSubscriptionPacket(sub);
|
||||
} 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;
|
||||
|
||||
uint16_t rlen;
|
||||
uint8_t rlen;
|
||||
|
||||
// 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;
|
||||
|
|
@ -296,8 +240,7 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
|
|||
|
||||
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
|
||||
uint32_t intermediate = encodedByte & 0x7F;
|
||||
|
|
@ -310,11 +253,9 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
|
|||
}
|
||||
} while (encodedByte & 0x80);
|
||||
|
||||
DEBUG_PRINT(F("Packet Length:\t"));
|
||||
DEBUG_PRINTLN(value);
|
||||
DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value);
|
||||
|
||||
// maxsize is limited to 65536 by 16-bit unsigned
|
||||
if (value > uint32_t(maxsize - (pbuff - buffer) - 1)) {
|
||||
if (value > (maxsize - (pbuff-buffer) - 1)) {
|
||||
DEBUG_PRINTLN(F("Packet too big for buffer"));
|
||||
rlen = readPacket(pbuff, (maxsize - (pbuff-buffer) - 1), timeout);
|
||||
} else {
|
||||
|
|
@ -327,29 +268,16 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
|
|||
|
||||
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");
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -361,38 +289,35 @@ bool Adafruit_MQTT::disconnect() {
|
|||
DEBUG_PRINTLN(F("Unable to send disconnect packet"));
|
||||
|
||||
return disconnectServer();
|
||||
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos,
|
||||
bool retain) {
|
||||
return publish(topic, (uint8_t *)(data), strlen(data), qos, retain);
|
||||
|
||||
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t 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 retain) {
|
||||
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,
|
||||
(uint16_t)sizeof(buffer), retain);
|
||||
|
||||
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
|
||||
if (!sendPacket(buffer, len))
|
||||
return false;
|
||||
|
||||
// If QOS level is high enough verify the response packet.
|
||||
if (qos > 0) {
|
||||
len = processPacketsUntil(buffer, MQTT_CTRL_PUBACK, PUBLISH_TIMEOUT_MS);
|
||||
|
||||
len = readFullPacket(buffer, MAXBUFFERSIZE, PUBLISH_TIMEOUT_MS);
|
||||
DEBUG_PRINT(F("Publish QOS1+ reply:\t"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
if (len != 4)
|
||||
return false;
|
||||
|
||||
if ((buffer[0] >> 4) != MQTT_CTRL_PUBACK)
|
||||
return false;
|
||||
uint16_t packnum = buffer[2];
|
||||
packnum <<= 8;
|
||||
packnum |= buffer[3];
|
||||
|
||||
// we increment the packet_id_counter right after publishing so inc here too
|
||||
// to match
|
||||
packnum = packnum + 1 + (packnum + 1 == 0); // Skip zero
|
||||
// we increment the packet_id_counter right after publishing so inc here too to match
|
||||
packnum++;
|
||||
if (packnum != packet_id_counter)
|
||||
return false;
|
||||
}
|
||||
|
|
@ -400,8 +325,7 @@ bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
|
|||
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"));
|
||||
|
|
@ -414,25 +338,7 @@ bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos,
|
|||
will_retain = retain;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
/*!
|
||||
@brief Sets the connect packet's KeepAlive Interval, in seconds. This
|
||||
function MUST be called prior to connect().
|
||||
@param keepAlive
|
||||
Maximum amount of time without communication between the
|
||||
client and the MQTT broker, in seconds.
|
||||
@returns True if called prior to connect(), False otherwise.
|
||||
*/
|
||||
/***************************************************************************/
|
||||
bool Adafruit_MQTT::setKeepAliveInterval(uint16_t keepAlive) {
|
||||
if (connected()) {
|
||||
DEBUG_PRINT(F("keepAlive defined after connection established."));
|
||||
return false;
|
||||
}
|
||||
keepAliveInterval = keepAlive;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
|
||||
|
|
@ -447,8 +353,7 @@ bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
|
|||
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;
|
||||
}
|
||||
|
|
@ -467,8 +372,7 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
|||
|
||||
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);
|
||||
|
|
@ -494,20 +398,49 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
|||
subscriptions[i] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// subscription not found, so we are unsubscribed
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void Adafruit_MQTT::processPackets(int16_t timeout) {
|
||||
uint16_t len;
|
||||
|
||||
uint32_t elapsed = 0, endtime, starttime = millis();
|
||||
|
||||
while (elapsed < (uint32_t)timeout) {
|
||||
Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed);
|
||||
if (sub)
|
||||
processSubscriptionPacket(sub);
|
||||
if (sub) {
|
||||
//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 io mode
|
||||
//Serial.print("*** calling io instance callback with : "); Serial.println((char *)sub->lastread);
|
||||
((sub->io_feed)->*(sub->callback_io))((char *)sub->lastread, sub->datalen);
|
||||
}
|
||||
}
|
||||
|
||||
// keep track over elapsed time
|
||||
endtime = millis();
|
||||
if (endtime < starttime) {
|
||||
|
|
@ -516,61 +449,20 @@ void Adafruit_MQTT::processPackets(int16_t timeout) {
|
|||
elapsed += (endtime - starttime);
|
||||
}
|
||||
}
|
||||
|
||||
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
||||
|
||||
// Sync or Async subscriber with message
|
||||
Adafruit_MQTT_Subscribe *s = 0;
|
||||
|
||||
// Check if are unread messages
|
||||
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
||||
if (subscriptions[i] && subscriptions[i]->new_message) {
|
||||
s = subscriptions[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// not unread message
|
||||
if (!s) {
|
||||
// Check if data is available to read.
|
||||
uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE,
|
||||
timeout); // return one full packet
|
||||
s = handleSubscriptionPacket(len);
|
||||
}
|
||||
|
||||
// it there is a message, mark it as not pending
|
||||
if (s) {
|
||||
s->new_message = false;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
|
||||
uint16_t i, topiclen, datalen;
|
||||
|
||||
if (!len) {
|
||||
// Check if data is available to read.
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
// Parse out length of packet.
|
||||
// NOTE: This includes data in the variable header and the payload.
|
||||
uint16_t remainingLen = len - 4; // subtract the 4 header bytes
|
||||
uint16_t const topicoffset = packetAdditionalLen(remainingLen);
|
||||
uint16_t const topicstart = topicoffset + 4;
|
||||
|
||||
topiclen = int((buffer[2 + topicoffset]) << 8 | buffer[3 + topicoffset]);
|
||||
DEBUG_PRINT(F("Looking for subscription len "));
|
||||
DEBUG_PRINTLN(topiclen);
|
||||
topiclen = buffer[3];
|
||||
DEBUG_PRINT(F("Looking for subscription len ")); DEBUG_PRINTLN(topiclen);
|
||||
|
||||
// Find subscription associated with this packet.
|
||||
for (i=0; i<MAXSUBSCRIPTIONS; i++) {
|
||||
|
|
@ -581,48 +473,36 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
|
|||
continue;
|
||||
// Stop if the subscription topic matches the received topic. Be careful
|
||||
// to make comparison case insensitive.
|
||||
if (strncasecmp((char *)buffer + topicstart, subscriptions[i]->topic,
|
||||
topiclen) == 0) {
|
||||
DEBUG_PRINT(F("Found sub #"));
|
||||
DEBUG_PRINTLN(i);
|
||||
if (subscriptions[i]->new_message) {
|
||||
DEBUG_PRINTLN(F("Lost previous message"));
|
||||
} else {
|
||||
subscriptions[i]->new_message = true;
|
||||
}
|
||||
|
||||
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;
|
||||
uint16_t packetid;
|
||||
// Check if it is QoS 1, TODO: we dont support QoS 2
|
||||
if ((buffer[0] & 0x6) == 0x2) {
|
||||
packet_id_len = 2;
|
||||
packetid = buffer[topiclen + topicstart];
|
||||
packetid = buffer[topiclen+4];
|
||||
packetid <<= 8;
|
||||
packetid |= buffer[topiclen + topicstart + 1];
|
||||
packetid |= buffer[topiclen+5];
|
||||
}
|
||||
|
||||
// zero out the old data
|
||||
memset(subscriptions[i]->lastread, 0, SUBSCRIPTIONDATALEN);
|
||||
|
||||
datalen = len - topiclen - packet_id_len - topicstart;
|
||||
datalen = len - topiclen - packet_id_len - 4;
|
||||
if (datalen > SUBSCRIPTIONDATALEN) {
|
||||
datalen = SUBSCRIPTIONDATALEN-1; // cut it off
|
||||
}
|
||||
// extract out just the data, into the subscription object itself
|
||||
memmove(subscriptions[i]->lastread,
|
||||
buffer + topicstart + 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) {
|
||||
uint8_t ackpacket[4];
|
||||
|
|
@ -640,8 +520,7 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
|
|||
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) {
|
||||
|
|
@ -704,6 +583,7 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||
|
||||
if(will_retain == 1)
|
||||
p[0] |= MQTT_CONN_WILLRETAIN;
|
||||
|
||||
}
|
||||
|
||||
if (pgm_read_byte(username) != 0)
|
||||
|
|
@ -712,9 +592,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||
p[0] |= MQTT_CONN_PASSWORDFLAG;
|
||||
p++;
|
||||
|
||||
p[0] = keepAliveInterval >> 8;
|
||||
p[0] = MQTT_CONN_KEEPALIVE >> 8;
|
||||
p++;
|
||||
p[0] = keepAliveInterval & 0xFF;
|
||||
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
|
||||
p++;
|
||||
|
||||
if(MQTT_PROTOCOL_LEVEL == 3) {
|
||||
|
|
@ -751,11 +631,10 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||
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,
|
||||
uint16_t maxPacketLen, bool retain) {
|
||||
uint8_t *data, uint16_t bLen, uint8_t qos) {
|
||||
uint8_t *p = packet;
|
||||
uint16_t len=0;
|
||||
|
||||
|
|
@ -765,27 +644,10 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
|||
if(qos > 0) {
|
||||
len += 2; // qos packet id
|
||||
}
|
||||
// Calculate additional bytes for length field (if any)
|
||||
uint16_t additionalLen = packetAdditionalLen(len + bLen);
|
||||
|
||||
// Payload remaining length. When maxPacketLen provided is 0, let's
|
||||
// assume buffer is big enough. Fingers crossed.
|
||||
// 2 + additionalLen: header byte + remaining length field (from 1 to 4 bytes)
|
||||
// len = topic size field + value (string)
|
||||
// bLen = buffer size
|
||||
if (!(maxPacketLen == 0 ||
|
||||
(len + bLen + 2 + additionalLen <= maxPacketLen))) {
|
||||
// If we make it here, we got a pickle: the payload is not going
|
||||
// to fit in the packet buffer. Instead of corrupting memory, let's
|
||||
// do something less damaging by reducing the bLen to what we are
|
||||
// able to accomodate. Alternatively, consider using a bigger
|
||||
// maxPacketLen.
|
||||
bLen = maxPacketLen - (len + 2 + packetAdditionalLen(maxPacketLen));
|
||||
}
|
||||
len += bLen; // remaining len excludes header byte & length field
|
||||
len += bLen; // payload length
|
||||
|
||||
// Now you can start generating the packet!
|
||||
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1 | (retain ? 1 : 0);
|
||||
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
|
||||
p++;
|
||||
|
||||
// fill in packet[1] last
|
||||
|
|
@ -809,8 +671,8 @@ uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
|||
p[1] = packet_id_counter & 0xFF;
|
||||
p+=2;
|
||||
|
||||
// increment the packet id, skipping 0
|
||||
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
|
||||
// increment the packet id
|
||||
packet_id_counter++;
|
||||
}
|
||||
|
||||
memmove(p, data, bLen);
|
||||
|
|
@ -835,8 +697,8 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
|||
p[1] = packet_id_counter & 0xFF;
|
||||
p+=2;
|
||||
|
||||
// increment the packet id, skipping 0
|
||||
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
|
||||
// increment the packet id
|
||||
packet_id_counter++;
|
||||
|
||||
p = stringprint(p, topic);
|
||||
|
||||
|
|
@ -850,6 +712,8 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
|||
return len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
||||
|
||||
uint8_t *p = packet;
|
||||
|
|
@ -864,8 +728,8 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
|||
p[1] = packet_id_counter & 0xFF;
|
||||
p+=2;
|
||||
|
||||
// increment the packet id, skipping 0
|
||||
packet_id_counter = packet_id_counter + 1 + (packet_id_counter + 1 == 0);
|
||||
// increment the packet id
|
||||
packet_id_counter++;
|
||||
|
||||
p = stringprint(p, topic);
|
||||
|
||||
|
|
@ -874,6 +738,7 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
|||
DEBUG_PRINTLN(F("MQTT unsubscription packet:"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
return len;
|
||||
|
||||
}
|
||||
|
||||
uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) {
|
||||
|
|
@ -910,36 +775,35 @@ Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
|
|||
topic = feed;
|
||||
qos = q;
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT_Publish::publish(int32_t i, bool retain) {
|
||||
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
||||
char payload[12];
|
||||
ltoa(i, payload, 10);
|
||||
return mqtt->publish(topic, payload, qos, retain);
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT_Publish::publish(uint32_t i, bool retain) {
|
||||
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
|
||||
char payload[11];
|
||||
ultoa(i, payload, 10);
|
||||
return mqtt->publish(topic, payload, qos, retain);
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision, bool retain) {
|
||||
char payload[41]; // Need to technically hold float max, 39 digits and minus
|
||||
// sign.
|
||||
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
|
||||
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, retain);
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
bool Adafruit_MQTT_Publish::publish(const char *payload, bool retain) {
|
||||
return mqtt->publish(topic, payload, qos, retain);
|
||||
bool Adafruit_MQTT_Publish::publish(const char *payload) {
|
||||
return mqtt->publish(topic, payload, qos);
|
||||
}
|
||||
|
||||
//publish buffer of arbitrary length
|
||||
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen,
|
||||
bool retain) {
|
||||
return mqtt->publish(topic, payload, bLen, qos, retain);
|
||||
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,
|
||||
|
|
@ -952,8 +816,7 @@ Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
|||
callback_buffer = 0;
|
||||
callback_double = 0;
|
||||
callback_io = 0;
|
||||
io_mqtt = 0;
|
||||
new_message = false;
|
||||
io_feed = 0;
|
||||
}
|
||||
|
||||
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackUInt32Type cb) {
|
||||
|
|
@ -968,10 +831,9 @@ 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_Feed *f, SubscribeCallbackIOType cb) {
|
||||
callback_io = cb;
|
||||
io_mqtt = io;
|
||||
io_feed = f;
|
||||
}
|
||||
|
||||
void Adafruit_MQTT_Subscribe::removeCallback(void) {
|
||||
|
|
@ -979,5 +841,5 @@ void Adafruit_MQTT_Subscribe::removeCallback(void) {
|
|||
callback_buffer = 0;
|
||||
callback_double = 0;
|
||||
callback_io = 0;
|
||||
io_mqtt = 0;
|
||||
io_feed = 0;
|
||||
}
|
||||
|
|
|
|||
146
Adafruit_MQTT.h
146
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,
|
||||
|
|
@ -30,7 +30,7 @@
|
|||
#endif
|
||||
|
||||
#define ADAFRUIT_MQTT_VERSION_MAJOR 0
|
||||
#define ADAFRUIT_MQTT_VERSION_MINOR 17
|
||||
#define ADAFRUIT_MQTT_VERSION_MINOR 16
|
||||
#define ADAFRUIT_MQTT_VERSION_PATCH 0
|
||||
|
||||
// Uncomment/comment to turn on/off debug output messages.
|
||||
|
|
@ -44,35 +44,23 @@
|
|||
|
||||
// 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)
|
||||
|
|
@ -107,14 +95,7 @@
|
|||
// Largest full packet we're able to send.
|
||||
// Need to be able to store at least ~90 chars for a connect packet with full
|
||||
// 23 char client ID.
|
||||
// Future TODO: This should be replaced by the ability to dynamically allocate a
|
||||
// buffer as needed.
|
||||
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32) || \
|
||||
defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_SAMD)
|
||||
#define MAXBUFFERSIZE (512)
|
||||
#else
|
||||
#define MAXBUFFERSIZE (150)
|
||||
#endif
|
||||
|
||||
#define MQTT_CONN_USERNAMEFLAG 0x80
|
||||
#define MQTT_CONN_PASSWORDFLAG 0x40
|
||||
|
|
@ -124,17 +105,14 @@
|
|||
#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__)
|
||||
// how many subscriptions we want to be able to track
|
||||
#define MAXSUBSCRIPTIONS 5
|
||||
#define SUBSCRIPTIONDATALEN 20
|
||||
#else
|
||||
#define MAXSUBSCRIPTIONS 15
|
||||
#define SUBSCRIPTIONDATALEN MAXBUFFERSIZE
|
||||
#endif
|
||||
|
||||
class AdafruitIO_MQTT; // forward decl
|
||||
// how much data we save in a subscription object
|
||||
// eg max-subscription-payload-size
|
||||
#define SUBSCRIPTIONDATALEN 20
|
||||
|
||||
class AdafruitIO_Feed; // forward decl
|
||||
|
||||
//Function pointer that returns an int
|
||||
typedef void (*SubscribeCallbackUInt32Type)(uint32_t);
|
||||
|
|
@ -143,8 +121,7 @@ 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_Feed::*SubscribeCallbackIOType)(char *str, uint16_t len);
|
||||
|
||||
extern void printBuffer(uint8_t *buffer, uint16_t len);
|
||||
|
||||
|
|
@ -152,10 +129,15 @@ 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);
|
||||
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() {}
|
||||
|
||||
|
|
@ -188,18 +170,12 @@ public:
|
|||
// 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);
|
||||
|
||||
// Sets the KeepAlive Interval, in seconds.
|
||||
bool setKeepAliveInterval(uint16_t keepAlive);
|
||||
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 retain = false);
|
||||
bool publish(const char *topic, uint8_t *payload, uint16_t bLen,
|
||||
uint8_t qos = 0, bool retain = false);
|
||||
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);
|
||||
|
||||
// Add a subscription to receive messages for a topic. Returns true if the
|
||||
// subscription could be added or was already present, false otherwise.
|
||||
|
|
@ -211,18 +187,11 @@ public:
|
|||
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!
|
||||
// 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);
|
||||
|
||||
// Handle any data coming in for subscriptions
|
||||
Adafruit_MQTT_Subscribe *handleSubscriptionPacket(uint16_t len);
|
||||
|
||||
// Execute a subscription packet's associated callback and mark as "read"
|
||||
void processSubscriptionPacket(Adafruit_MQTT_Subscribe *sub);
|
||||
|
||||
void processPackets(int16_t timeout);
|
||||
|
||||
// Ping the server to ensure the connection is still alive.
|
||||
|
|
@ -234,8 +203,7 @@ protected:
|
|||
// 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.
|
||||
// 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.
|
||||
|
|
@ -244,14 +212,12 @@ protected:
|
|||
// 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;
|
||||
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;
|
||||
|
|
@ -263,7 +229,6 @@ protected:
|
|||
const char *will_payload;
|
||||
uint8_t will_qos;
|
||||
uint8_t will_retain;
|
||||
uint16_t keepAliveInterval; // MQTT KeepAlive time interval, in seconds
|
||||
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
|
||||
uint16_t packet_id_counter;
|
||||
|
||||
|
|
@ -275,30 +240,25 @@ private:
|
|||
// 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 maxPacketLen = 0,
|
||||
bool retain = false);
|
||||
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);
|
||||
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const char *feed, uint8_t qos = 0);
|
||||
|
||||
bool publish(const char *s, bool retain = false);
|
||||
bool publish(
|
||||
double f,
|
||||
uint8_t precision =
|
||||
2, // Precision controls the minimum number of digits after decimal.
|
||||
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 retain = false);
|
||||
bool publish(int32_t i, bool retain = false);
|
||||
bool publish(uint32_t i, bool retain = false);
|
||||
bool publish(uint8_t *b, uint16_t bLen, bool retain = false);
|
||||
bool publish(int32_t i);
|
||||
bool publish(uint32_t i);
|
||||
bool publish(uint8_t *b, uint16_t bLen);
|
||||
|
||||
|
||||
private:
|
||||
Adafruit_MQTT *mqtt;
|
||||
|
|
@ -308,13 +268,12 @@ private:
|
|||
|
||||
class Adafruit_MQTT_Subscribe {
|
||||
public:
|
||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname,
|
||||
uint8_t q = 0);
|
||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const char *feedname, uint8_t q=0);
|
||||
|
||||
void setCallback(SubscribeCallbackUInt32Type callb);
|
||||
void setCallback(SubscribeCallbackDoubleType callb);
|
||||
void setCallback(SubscribeCallbackBufferType callb);
|
||||
void setCallback(AdafruitIO_MQTT *io, SubscribeCallbackIOType callb);
|
||||
void setCallback(AdafruitIO_Feed *io, SubscribeCallbackIOType callb);
|
||||
void removeCallback(void);
|
||||
|
||||
const char *topic;
|
||||
|
|
@ -330,12 +289,11 @@ public:
|
|||
SubscribeCallbackBufferType callback_buffer;
|
||||
SubscribeCallbackIOType callback_io;
|
||||
|
||||
AdafruitIO_MQTT *io_mqtt;
|
||||
|
||||
bool new_message;
|
||||
AdafruitIO_Feed *io_feed;
|
||||
|
||||
private:
|
||||
Adafruit_MQTT *mqtt;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
152
Adafruit_MQTT_CC3000.h
Normal file
152
Adafruit_MQTT_CC3000.h
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Adafruit Industries
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
#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)
|
||||
{}
|
||||
|
||||
Adafruit_MQTT_CC3000(Adafruit_CC3000 *cc3k, const char *server, uint16_t port,
|
||||
const char *user = "", const char *pass = ""):
|
||||
Adafruit_MQTT(server, port, 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 disconnectServer() {
|
||||
if (connected()) {
|
||||
return (mqttclient.close() == 0);
|
||||
}
|
||||
else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
bool connected() {
|
||||
return mqttclient.connected();
|
||||
}
|
||||
|
||||
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
Watchdog.reset();
|
||||
timeout -= MQTT_CC3000_INTERAVAILDELAY;
|
||||
delay(MQTT_CC3000_INTERAVAILDELAY);
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
bool sendPacket(uint8_t *buffer, uint16_t len) {
|
||||
if (mqttclient.connected()) {
|
||||
uint16_t ret = mqttclient.write(buffer, (size_t)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
|
||||
|
|
@ -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,16 +21,15 @@
|
|||
// 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;
|
||||
}
|
||||
|
||||
|
|
@ -50,15 +49,10 @@ 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;
|
||||
|
||||
if (maxlen == 0) { // handle zero-length packets
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (client->connected() && (timeout >= 0)) {
|
||||
//DEBUG_PRINT('.');
|
||||
while (client->available()) {
|
||||
|
|
@ -68,7 +62,6 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
|
|||
buffer[len] = c;
|
||||
//DEBUG_PRINTLN((uint8_t)c, HEX);
|
||||
len++;
|
||||
|
||||
if (len == maxlen) { // we read all we want, bail
|
||||
DEBUG_PRINT(F("Read data:\t"));
|
||||
DEBUG_PRINTBUFFER(buffer, len);
|
||||
|
|
@ -83,18 +76,16 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
|
|||
|
||||
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
|
||||
uint16_t ret = 0;
|
||||
uint16_t offset = 0;
|
||||
|
||||
while (len > 0) {
|
||||
if (client->connected()) {
|
||||
// send 250 bytes at most at a time, can adjust this later based on Client
|
||||
|
||||
uint16_t sendlen = len > 250 ? 250 : len;
|
||||
uint16_t sendlen = min(len, 250);
|
||||
//Serial.print("Sending: "); Serial.println(sendlen);
|
||||
ret = client->write(buffer + offset, sendlen);
|
||||
DEBUG_PRINT(F("Client sendPacket returned: "));
|
||||
DEBUG_PRINTLN(ret);
|
||||
ret = client->write(buffer, sendlen);
|
||||
DEBUG_PRINT(F("Client sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
||||
len -= ret;
|
||||
offset += ret;
|
||||
|
||||
if (ret != sendlen) {
|
||||
DEBUG_PRINTLN("Failed to send packet.");
|
||||
|
|
|
|||
|
|
@ -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,36 +22,40 @@
|
|||
#ifndef _ADAFRUIT_MQTT_CLIENT_H_
|
||||
#define _ADAFRUIT_MQTT_CLIENT_H_
|
||||
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Client.h"
|
||||
#include "Adafruit_MQTT.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:
|
||||
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 connected() override;
|
||||
|
||||
protected:
|
||||
bool connectServer() override;
|
||||
bool disconnectServer() override;
|
||||
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen,
|
||||
int16_t timeout) override;
|
||||
bool sendPacket(uint8_t *buffer, uint16_t len) override;
|
||||
bool connectServer();
|
||||
bool disconnectServer();
|
||||
bool connected();
|
||||
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout);
|
||||
bool sendPacket(uint8_t *buffer, uint16_t len);
|
||||
|
||||
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,12 +22,13 @@
|
|||
#ifndef _ADAFRUIT_MQTT_FONA_H_
|
||||
#define _ADAFRUIT_MQTT_FONA_H_
|
||||
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include <Adafruit_FONA.h>
|
||||
#include "Adafruit_MQTT.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
|
||||
|
|
@ -35,20 +36,18 @@
|
|||
class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
||||
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 connected() {
|
||||
// Return true if connected, false if not connected.
|
||||
return fona->TCPconnected();
|
||||
}
|
||||
|
||||
protected:
|
||||
bool connectServer() override {
|
||||
bool connectServer() {
|
||||
char server[40];
|
||||
strncpy(server, servername, 40);
|
||||
#ifdef ADAFRUIT_SLEEPYDOG_H
|
||||
|
|
@ -60,18 +59,23 @@ protected:
|
|||
return fona->TCPconnect(server, portnum);
|
||||
}
|
||||
|
||||
bool disconnectServer() override { return fona->TCPclose(); }
|
||||
bool disconnectServer() {
|
||||
return fona->TCPclose();
|
||||
}
|
||||
|
||||
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen,
|
||||
int16_t timeout) override {
|
||||
bool connected() {
|
||||
// Return true if connected, false if not connected.
|
||||
return fona->TCPconnected();
|
||||
}
|
||||
|
||||
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
|
||||
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;
|
||||
|
|
@ -83,13 +87,11 @@ protected:
|
|||
|
||||
if (len + avail > maxlen) {
|
||||
avail = maxlen - len;
|
||||
if (avail == 0)
|
||||
return 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;
|
||||
|
|
@ -108,15 +110,14 @@ protected:
|
|||
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;
|
||||
}
|
||||
|
||||
bool sendPacket(uint8_t *buffer, uint16_t len) override {
|
||||
bool sendPacket(uint8_t *buffer, uint16_t len) {
|
||||
DEBUG_PRINTLN(F("Writing packet"));
|
||||
if (fona->TCPconnected()) {
|
||||
boolean ret = fona->TCPsend((char *)buffer, len);
|
||||
|
|
@ -137,4 +138,5 @@ private:
|
|||
Adafruit_FONA *fona;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
|||
11
README.md
11
README.md
|
|
@ -1,7 +1,7 @@
|
|||
# Adafruit MQTT Library [](https://github.com/adafruit/Adafruit_MQTT_Library/actions)
|
||||
# Adafruit MQTT Library [](https://travis-ci.org/adafruit/Adafruit_MQTT_Library)
|
||||
|
||||
Arduino library for MQTT support, including access to Adafruit IO. Works with
|
||||
the Adafruit FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
|
||||
the Adafruit CC3000, FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
|
||||
Arduino's Client interface (like Ethernet shield).
|
||||
|
||||
See included examples for how to use the library to access an MQTT service to
|
||||
|
|
@ -11,7 +11,10 @@ spec but is intended to support enough for QoS 0 and 1 publishing.
|
|||
Depends on the following other libraries depending on the target platform:
|
||||
|
||||
- [Adafruit SleepyDog](https://github.com/adafruit/Adafruit_SleepyDog), watchdog
|
||||
library used by FONA code for reliability.
|
||||
library used by FONA and CC3000 code for reliability.
|
||||
|
||||
- [Adafruit CC3000](https://github.com/adafruit/Adafruit_CC3000_Library), required
|
||||
for the CC3000 hardware.
|
||||
|
||||
- [Adafruit FONA](https://github.com/adafruit/Adafruit_FONA_Library), required for
|
||||
the FONA hardware.
|
||||
|
|
@ -36,7 +39,6 @@ ESP8266 | | | X |
|
|||
Atmega2560 @ 16MHz | | | X |
|
||||
ATSAM3X8E | | | X |
|
||||
ATSAM21D | | | X |
|
||||
ATSAMD51J20 | | | X |
|
||||
ATtiny85 @ 16MHz | | | X |
|
||||
ATtiny85 @ 8MHz | | | X |
|
||||
Intel Curie @ 32MHz | | | X |
|
||||
|
|
@ -50,7 +52,6 @@ STM32F2 | | | X |
|
|||
* ATmega2560 @ 16MHz : Arduino Mega
|
||||
* ATSAM3X8E : Arduino Due
|
||||
* ATSAM21D : Arduino Zero, M0 Pro
|
||||
* ATSAMD51J20: Adafruit PyPortal
|
||||
* ATtiny85 @ 16MHz : Adafruit Trinket 5V
|
||||
* ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -46,15 +46,20 @@ Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO
|
|||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
#define PHOTOCELL_FEED AIO_USERNAME "/feeds/photocell"
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
#define ONOFF_FEED AIO_USERNAME "/feeds/onoff"
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
||||
|
||||
/*************************** Error Reporting *********************************/
|
||||
|
||||
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/errors");
|
||||
Adafruit_MQTT_Subscribe throttle = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/throttle");
|
||||
#define ERROR_FEED AIO_USERNAME "/errors"
|
||||
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, ERROR_FEED);
|
||||
|
||||
#define THROTTLE_FEED AIO_USERNAME "/throttle"
|
||||
Adafruit_MQTT_Subscribe throttle = Adafruit_MQTT_Subscribe(&mqtt, THROTTLE_FEED);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1,183 +0,0 @@
|
|||
/****************************************************************
|
||||
Adafruit MQTT Library, Adafruit IO SSL/TLS Example for AirLift
|
||||
|
||||
Must use the latest version of nina-fw from:
|
||||
https://github.com/adafruit/nina-fw
|
||||
|
||||
Works great with Adafruit AirLift ESP32 Co-Processors!
|
||||
--> https://www.adafruit.com/product/4201
|
||||
--> https://www.adafruit.com/product/4116
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Brent Rubell for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
*******************************************************************/
|
||||
|
||||
#include <WiFiNINA.h>
|
||||
#include <SPI.h>
|
||||
|
||||
// For AirLift Breakout/Wing/Shield: Configure the following to match the ESP32 Pins!
|
||||
#if !defined(SPIWIFI_SS)
|
||||
// Don't change the names of these #define's! they match the variant ones
|
||||
#define SPIWIFI SPI
|
||||
#define SPIWIFI_SS 11 // Chip select pin
|
||||
#define SPIWIFI_ACK 10 // a.k.a BUSY or READY pin
|
||||
#define ESP32_RESETN 9 // Reset pin
|
||||
#define ESP32_GPIO0 -1 // Not connected
|
||||
#define SET_PINS 1 // Pins were set using this IFNDEF
|
||||
#endif
|
||||
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "WLAN_SSID"
|
||||
#define WLAN_PASS "WIFI_PASSWORD"
|
||||
int keyIndex = 0; // your network key Index number (needed only for WEP)
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
// Using port 8883 for MQTTS
|
||||
#define AIO_SERVERPORT 8883
|
||||
// Adafruit IO Account Configuration
|
||||
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
|
||||
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
|
||||
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// WiFiSSLClient for SSL/TLS support
|
||||
WiFiSSLClient client;
|
||||
|
||||
// Setup the MQTT client class by WLAN_PASSing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'test' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup()
|
||||
{
|
||||
//Initialize serial and wait for port to open:
|
||||
Serial.begin(115200);
|
||||
while (!Serial)
|
||||
{
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// if the AirLift's pins were defined above...
|
||||
#ifdef SET_PINS
|
||||
WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);
|
||||
#endif
|
||||
|
||||
// check for the wifi module
|
||||
while (WiFi.status() == WL_NO_MODULE)
|
||||
{
|
||||
Serial.println("Communication with WiFi module failed!");
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
String fv = WiFi.firmwareVersion();
|
||||
if (fv < "1.0.0")
|
||||
{
|
||||
Serial.println("Please upgrade the firmware");
|
||||
}
|
||||
|
||||
// attempt to connect to Wifi network:
|
||||
Serial.print("Attempting to connect to SSID: ");
|
||||
Serial.println(WLAN_SSID);
|
||||
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
|
||||
do
|
||||
{
|
||||
status = WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
delay(100); // wait until connected
|
||||
} while (status != WL_CONNECTED);
|
||||
Serial.println("Connected to wifi");
|
||||
printWiFiStatus();
|
||||
}
|
||||
|
||||
uint32_t x = 0;
|
||||
|
||||
void loop()
|
||||
{
|
||||
// Ensure the connection to the MQTT server is alive (this will make the first
|
||||
// connection and automatically reconnect when disconnected). See the MQTT_connect
|
||||
// function definition further below.
|
||||
MQTT_connect();
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending val "));
|
||||
Serial.print(x);
|
||||
Serial.print(F(" to test feed..."));
|
||||
if (!test.publish(x++))
|
||||
{
|
||||
Serial.println(F("Failed"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// wait a couple seconds to avoid rate limit
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect()
|
||||
{
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t retries = 3;
|
||||
while ((ret = mqtt.connect()) != 0)
|
||||
{ // connect will return 0 for connected
|
||||
Serial.println(mqtt.connectErrorString(ret));
|
||||
Serial.println("Retrying MQTT connection in 5 seconds...");
|
||||
mqtt.disconnect();
|
||||
delay(5000); // wait 5 seconds
|
||||
retries--;
|
||||
if (retries == 0)
|
||||
{
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
||||
|
||||
void printWiFiStatus()
|
||||
{
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial.print("SSID: ");
|
||||
Serial.println(WiFi.SSID());
|
||||
|
||||
// print your board's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(ip);
|
||||
|
||||
// print the received signal strength:
|
||||
long rssi = WiFi.RSSI();
|
||||
Serial.print("signal strength (RSSI):");
|
||||
Serial.print(rssi);
|
||||
Serial.println(" dBm");
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1,165 +0,0 @@
|
|||
/***********************************************************************
|
||||
Adafruit MQTT Library ESP32 Adafruit IO SSL/TLS example
|
||||
|
||||
Use the latest version of the ESP32 Arduino Core:
|
||||
https://github.com/espressif/arduino-esp32
|
||||
|
||||
Works great with Adafruit Huzzah32 Feather and Breakout Board:
|
||||
https://www.adafruit.com/product/3405
|
||||
https://www.adafruit.com/products/4172
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Tony DiCola for Adafruit Industries.
|
||||
Modified by Brent Rubell for Adafruit Industries
|
||||
MIT license, all text above must be included in any redistribution
|
||||
**********************************************************************/
|
||||
#include <WiFi.h>
|
||||
#include "WiFiClientSecure.h"
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "WLAN_SSID"
|
||||
#define WLAN_PASS "WIFI_PASSWORD"
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
|
||||
// Using port 8883 for MQTTS
|
||||
#define AIO_SERVERPORT 8883
|
||||
|
||||
// Adafruit IO Account Configuration
|
||||
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
|
||||
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
|
||||
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// WiFiFlientSecure for SSL/TLS support
|
||||
WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// io.adafruit.com root CA
|
||||
const char* adafruitio_root_ca = \
|
||||
"-----BEGIN CERTIFICATE-----\n"
|
||||
"MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n"
|
||||
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
|
||||
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\n"
|
||||
"MjAeFw0xNzExMDIxMjIzMzdaFw0yNzExMDIxMjIzMzdaMGAxCzAJBgNVBAYTAlVT\n"
|
||||
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
|
||||
"b20xHzAdBgNVBAMTFkdlb1RydXN0IFRMUyBSU0EgQ0EgRzEwggEiMA0GCSqGSIb3\n"
|
||||
"DQEBAQUAA4IBDwAwggEKAoIBAQC+F+jsvikKy/65LWEx/TMkCDIuWegh1Ngwvm4Q\n"
|
||||
"yISgP7oU5d79eoySG3vOhC3w/3jEMuipoH1fBtp7m0tTpsYbAhch4XA7rfuD6whU\n"
|
||||
"gajeErLVxoiWMPkC/DnUvbgi74BJmdBiuGHQSd7LwsuXpTEGG9fYXcbTVN5SATYq\n"
|
||||
"DfbexbYxTMwVJWoVb6lrBEgM3gBBqiiAiy800xu1Nq07JdCIQkBsNpFtZbIZhsDS\n"
|
||||
"fzlGWP4wEmBQ3O67c+ZXkFr2DcrXBEtHam80Gp2SNhou2U5U7UesDL/xgLK6/0d7\n"
|
||||
"6TnEVMSUVJkZ8VeZr+IUIlvoLrtjLbqugb0T3OYXW+CQU0kBAgMBAAGjggFAMIIB\n"
|
||||
"PDAdBgNVHQ4EFgQUlE/UXYvkpOKmgP792PkA76O+AlcwHwYDVR0jBBgwFoAUTiJU\n"
|
||||
"IBiV5uNu5g/6+rkS7QYXjzkwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsG\n"
|
||||
"AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB\n"
|
||||
"BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n"
|
||||
"HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n"
|
||||
"b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
|
||||
"HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
|
||||
"AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
|
||||
"c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
|
||||
"ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
|
||||
"jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
|
||||
"TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
|
||||
"7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
|
||||
"-----END CERTIFICATE-----\n";
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'test' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("Adafruit IO MQTTS (SSL/TLS) Example"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
delay(1000);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
delay(2000);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
// Set Adafruit IO's root CA
|
||||
client.setCACert(adafruitio_root_ca);
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
void loop() {
|
||||
// Ensure the connection to the MQTT server is alive (this will make the first
|
||||
// connection and automatically reconnect when disconnected). See the MQTT_connect
|
||||
// function definition further below.
|
||||
MQTT_connect();
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending val "));
|
||||
Serial.print(x);
|
||||
Serial.print(F(" to test feed..."));
|
||||
if (! test.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// wait a couple seconds to avoid rate limit
|
||||
delay(2000);
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
uint8_t retries = 3;
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
Serial.println(mqtt.connectErrorString(ret));
|
||||
Serial.println("Retrying MQTT connection in 5 seconds...");
|
||||
mqtt.disconnect();
|
||||
delay(5000); // wait 5 seconds
|
||||
retries--;
|
||||
if (retries == 0) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -22,18 +22,15 @@
|
|||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "WLAN_SSID"
|
||||
#define WLAN_PASS "WIFI_PASSWORD"
|
||||
#define WLAN_SSID "...your SSID..."
|
||||
#define WLAN_PASS "...your password..."
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
// Using port 8883 for MQTTS
|
||||
#define AIO_SERVERPORT 8883
|
||||
// Adafruit IO Account Configuration
|
||||
// (to obtain these values, visit https://io.adafruit.com and click on Active Key)
|
||||
#define AIO_USERNAME "YOUR_ADAFRUIT_IO_USERNAME"
|
||||
#define AIO_KEY "YOUR_ADAFRUIT_IO_KEY"
|
||||
#define AIO_SERVERPORT 8883 // 8883 for MQTTS
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
|
|
@ -44,21 +41,22 @@ WiFiClientSecure client;
|
|||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// io.adafruit.com SHA1 fingerprint
|
||||
/* WARNING - This value was last updated on 07/14/25 and may not be up-to-date!
|
||||
* If security is a concern for your project, we strongly recommend users impacted by this moving
|
||||
* to ESP32 which has certificate verification by storing root certs and having a
|
||||
* chain-of-trust rather than doing individual certificate fingerprints.
|
||||
*/
|
||||
static const char *fingerprint PROGMEM = "47 D2 CB 14 DF 38 97 59 C6 65 1A 1F 3E 00 1E 53 CC A5 17 E0";
|
||||
const char* fingerprint = "26 96 1C 2A 51 07 FD 15 80 96 93 AE F7 32 CE B9 0D 01 55 C4";
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'test' for publishing.
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/test");
|
||||
#define TEST_FEED AIO_USERNAME "/feeds/test"
|
||||
Adafruit_MQTT_Publish test = Adafruit_MQTT_Publish(&mqtt, TEST_FEED);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
|
||||
// for some reason (only affects ESP8266, likely an arduino-builder bug).
|
||||
void MQTT_connect();
|
||||
void verifyFingerprint();
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
|
@ -85,7 +83,8 @@ void setup() {
|
|||
Serial.println("IP address: "); Serial.println(WiFi.localIP());
|
||||
|
||||
// check the fingerprint of io.adafruit.com's SSL cert
|
||||
client.setFingerprint(fingerprint);
|
||||
verifyFingerprint();
|
||||
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
|
@ -111,6 +110,28 @@ void loop() {
|
|||
|
||||
}
|
||||
|
||||
|
||||
void verifyFingerprint() {
|
||||
|
||||
const char* host = AIO_SERVER;
|
||||
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(host);
|
||||
|
||||
if (! client.connect(host, AIO_SERVERPORT)) {
|
||||
Serial.println("Connection failed. Halting execution.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
if (client.verify(fingerprint, host)) {
|
||||
Serial.println("Connection secure.");
|
||||
} else {
|
||||
Serial.println("Connection insecure! Halting execution.");
|
||||
while(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -49,8 +49,11 @@ Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO
|
|||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider");
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
#define ONOFF_FEED AIO_USERNAME "/feeds/onoff"
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
||||
#define SLIDER_FEED AIO_USERNAME "/feeds/slider"
|
||||
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, SLIDER_FEED);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
|
|
|
|||
0
examples/mqtt_cc3k/.uno.test.skip
Normal file
0
examples/mqtt_cc3k/.uno.test.skip
Normal file
0
examples/mqtt_cc3k/.zero.test.skip
Normal file
0
examples/mqtt_cc3k/.zero.test.skip
Normal file
131
examples/mqtt_cc3k/cc3000helper.cpp
Normal file
131
examples/mqtt_cc3k/cc3000helper.cpp
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
#include <Adafruit_SleepyDog.h>
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include <ccspi.h>
|
||||
#include <SPI.h>
|
||||
|
||||
//#define STATICIP
|
||||
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
|
||||
uint16_t checkFirmwareVersion(void);
|
||||
bool displayConnectionDetails(void);
|
||||
|
||||
extern Adafruit_CC3000 cc3000;
|
||||
|
||||
boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security) {
|
||||
Watchdog.reset();
|
||||
|
||||
// Check for compatible firmware
|
||||
if (checkFirmwareVersion() < 0x113) halt("Wrong firmware version!");
|
||||
|
||||
// Delete any old connection data on the module
|
||||
Serial.println(F("\nDeleting old connection profiles"));
|
||||
if (!cc3000.deleteProfiles()) halt("Failed!");
|
||||
|
||||
#ifdef STATICIP
|
||||
Serial.println(F("Setting static IP"));
|
||||
uint32_t ipAddress = cc3000.IP2U32(10, 0, 1, 19);
|
||||
uint32_t netMask = cc3000.IP2U32(255, 255, 255, 0);
|
||||
uint32_t defaultGateway = cc3000.IP2U32(10, 0, 1, 1);
|
||||
uint32_t dns = cc3000.IP2U32(8, 8, 4, 4);
|
||||
|
||||
if (!cc3000.setStaticIPAddress(ipAddress, netMask, defaultGateway, dns)) {
|
||||
Serial.println(F("Failed to set static IP!"));
|
||||
while(1);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Attempt to connect to an access point
|
||||
Serial.print(F("\nAttempting to connect to "));
|
||||
Serial.print(wlan_ssid); Serial.print(F("..."));
|
||||
|
||||
Watchdog.disable();
|
||||
// try 3 times
|
||||
if (!cc3000.connectToAP(wlan_ssid, wlan_pass, wlan_security, 3)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Watchdog.enable(8000);
|
||||
Serial.println(F("Connected!"));
|
||||
|
||||
uint8_t retries;
|
||||
#ifndef STATICIP
|
||||
/* Wait for DHCP to complete */
|
||||
Serial.println(F("Requesting DHCP"));
|
||||
retries = 10;
|
||||
while (!cc3000.checkDHCP())
|
||||
{
|
||||
Watchdog.reset();
|
||||
delay(1000);
|
||||
retries--;
|
||||
if (!retries) return false;
|
||||
}
|
||||
#endif
|
||||
/* Display the IP address DNS, Gateway, etc. */
|
||||
retries = 10;
|
||||
while (! displayConnectionDetails()) {
|
||||
Watchdog.reset();
|
||||
delay(1000);
|
||||
retries--;
|
||||
if (!retries) return false;
|
||||
}
|
||||
|
||||
Watchdog.reset();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Tries to read the CC3000's internal firmware patch ID
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint16_t checkFirmwareVersion(void)
|
||||
{
|
||||
uint8_t major, minor;
|
||||
uint16_t version;
|
||||
|
||||
if(!cc3000.getFirmwareVersion(&major, &minor))
|
||||
{
|
||||
Serial.println(F("Unable to retrieve the firmware version!\r\n"));
|
||||
version = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Firmware V. : "));
|
||||
Serial.print(major); Serial.print(F(".")); Serial.println(minor);
|
||||
version = major; version <<= 8; version |= minor;
|
||||
}
|
||||
return version;
|
||||
}
|
||||
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Tries to read the IP address and other connection details
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool displayConnectionDetails(void)
|
||||
{
|
||||
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
|
||||
|
||||
if(!cc3000.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
|
||||
{
|
||||
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("\nIP Addr: ")); cc3000.printIPdotsRev(ipAddress);
|
||||
Serial.print(F("\nNetmask: ")); cc3000.printIPdotsRev(netmask);
|
||||
Serial.print(F("\nGateway: ")); cc3000.printIPdotsRev(gateway);
|
||||
Serial.print(F("\nDHCPsrv: ")); cc3000.printIPdotsRev(dhcpserv);
|
||||
Serial.print(F("\nDNSserv: ")); cc3000.printIPdotsRev(dnsserv);
|
||||
Serial.println();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
149
examples/mqtt_cc3k/mqtt_cc3k.ino
Normal file
149
examples/mqtt_cc3k/mqtt_cc3k.ino
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library CC3000 Example
|
||||
|
||||
Designed specifically to work with the Adafruit WiFi products:
|
||||
----> https://www.adafruit.com/products/1469
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit and open-source hardware by purchasing
|
||||
products from Adafruit!
|
||||
|
||||
Written by Limor Fried/Ladyada for Adafruit Industries.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
#include <Adafruit_CC3000.h>
|
||||
#include <SPI.h>
|
||||
#include "utility/debug.h"
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_CC3000.h"
|
||||
|
||||
/*************************** CC3000 Pins ***********************************/
|
||||
|
||||
#define ADAFRUIT_CC3000_IRQ 3 // MUST be an interrupt pin!
|
||||
#define ADAFRUIT_CC3000_VBAT 5 // VBAT & CS can be any digital pins.
|
||||
#define ADAFRUIT_CC3000_CS 10
|
||||
// Use hardware SPI for the remaining pins
|
||||
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "...your SSID..." // can't be longer than 32 characters!
|
||||
#define WLAN_PASS "...your password..."
|
||||
#define WLAN_SECURITY WLAN_SEC_WPA2 // Can be: WLAN_SEC_UNSEC, WLAN_SEC_WEP,
|
||||
// WLAN_SEC_WPA or WLAN_SEC_WPA2
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define AIO_SERVER "io.adafruit.com"
|
||||
#define AIO_SERVERPORT 1883
|
||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||
#define AIO_KEY "...your AIO key..."
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Setup the main CC3000 class, just like a normal CC3000 sketch.
|
||||
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT);
|
||||
|
||||
// Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details.
|
||||
Adafruit_MQTT_CC3000 mqtt(&cc3000, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
// You don't need to change anything below this line!
|
||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||
|
||||
// CC3000connect is a helper function that sets up the CC3000 and connects to
|
||||
// the WiFi network. See the cc3000helper.cpp tab above for the source!
|
||||
boolean CC3000connect(const char* wlan_ssid, const char* wlan_pass, uint8_t wlan_security);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
#define PHOTOCELL_FEED AIO_USERNAME "/feeds/photocell"
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
#define ONOFF_FEED AIO_USERNAME "/feeds/onoff"
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println(F("Adafruit MQTT demo"));
|
||||
|
||||
Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC);
|
||||
|
||||
// Initialise the CC3000 module
|
||||
Serial.print(F("\nInit the CC3000..."));
|
||||
if (!cc3000.begin())
|
||||
halt("Failed");
|
||||
|
||||
mqtt.subscribe(&onoffbutton);
|
||||
|
||||
while (! CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
|
||||
Serial.println(F("Retrying WiFi"));
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t x=0;
|
||||
|
||||
void loop() {
|
||||
// Make sure to reset watchdog every loop iteration!
|
||||
Watchdog.reset();
|
||||
|
||||
// Ensure the connection to the MQTT server is alive (this will make the first
|
||||
// connection and automatically reconnect when disconnected). See the MQTT_connect
|
||||
// function definition further below.
|
||||
MQTT_connect();
|
||||
|
||||
// this is our 'wait for incoming subscription packets' busy subloop
|
||||
Adafruit_MQTT_Subscribe *subscription;
|
||||
while ((subscription = mqtt.readSubscription(1000))) {
|
||||
if (subscription == &onoffbutton) {
|
||||
Serial.print(F("Got: "));
|
||||
Serial.println((char *)onoffbutton.lastread);
|
||||
}
|
||||
}
|
||||
|
||||
// Now we can publish stuff!
|
||||
Serial.print(F("\nSending photocell val "));
|
||||
Serial.print(x);
|
||||
Serial.print("...");
|
||||
if (! photocell.publish(x++)) {
|
||||
Serial.println(F("Failed"));
|
||||
} else {
|
||||
Serial.println(F("OK!"));
|
||||
}
|
||||
|
||||
// ping the server to keep the mqtt connection alive
|
||||
if(! mqtt.ping()) {
|
||||
Serial.println(F("MQTT Ping failed."));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Connecting to MQTT... ");
|
||||
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
Serial.println(mqtt.connectErrorString(ret));
|
||||
if (ret < 0)
|
||||
CC3000connect(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); // y0w, lets connect to wifi again
|
||||
Serial.println("Retrying MQTT connection in 5 seconds...");
|
||||
mqtt.disconnect();
|
||||
delay(5000); // wait 5 seconds
|
||||
}
|
||||
Serial.println("MQTT Connected!");
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
// or... use WiFiClientSecure for SSL
|
||||
// or... use WiFiFlientSecure for SSL
|
||||
//WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
|
|
@ -45,10 +45,12 @@ Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO
|
|||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
#define PHOTOCELL_FEED AIO_USERNAME "/feeds/photocell"
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
#define ONOFF_FEED AIO_USERNAME "/feeds/onoff"
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1,20 +1,13 @@
|
|||
#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)"));
|
||||
|
|
|
|||
|
|
@ -67,10 +67,12 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
|
|||
|
||||
// Setup a feed called 'photocell' for publishing.
|
||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||
#define PHOTOCELL_FEED AIO_USERNAME "/feeds/photocell"
|
||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
||||
|
||||
// Setup a feed called 'onoff' for subscribing to changes.
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||
#define ONOFF_FEED AIO_USERNAME "/feeds/onoff"
|
||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1,140 +0,0 @@
|
|||
/***************************************************
|
||||
Adafruit MQTT Library Retain Flag Example
|
||||
|
||||
This example demonstrates use of the retain flag when publishing messages.
|
||||
If retain is set, the MQTT broker will store the message. When a new
|
||||
client subscribes to the topic, the retained message will be republished
|
||||
to that client. This is useful for configuration messages and 'last known
|
||||
good' values.
|
||||
|
||||
Written by Ben Willmore.
|
||||
MIT license, all text above must be included in any redistribution
|
||||
****************************************************/
|
||||
|
||||
#include <ESP8266WiFi.h> // use <WiFi.h> for ESP32
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
|
||||
/************************* WiFi Access Point *********************************/
|
||||
|
||||
#define WLAN_SSID "...your SSID..."
|
||||
#define WLAN_PASS "...your password..."
|
||||
|
||||
/************************* Adafruit.io Setup *********************************/
|
||||
|
||||
#define MQTT_SERVER "...your MQTT server..."
|
||||
#define MQTT_SERVERPORT 1883 // use 8883 for SSL
|
||||
#define MQTT_USERNAME "MQTT username"
|
||||
#define MQTT_KEY "MQTT key"
|
||||
#define DEVICE_ID "mqtt-retain-example"
|
||||
|
||||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
// Create a WiFiClient class to connect to the MQTT server.
|
||||
WiFiClient client;
|
||||
// or... use WiFiClientSecure for SSL
|
||||
//WiFiClientSecure client;
|
||||
|
||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);
|
||||
|
||||
/****************************** Feeds ***************************************/
|
||||
|
||||
// Set up for publishing and subscribing to the same feed, for demonstration only
|
||||
Adafruit_MQTT_Publish publish_feed = Adafruit_MQTT_Publish(&mqtt, DEVICE_ID "/temp");
|
||||
Adafruit_MQTT_Subscribe subscribe_feed = Adafruit_MQTT_Subscribe(&mqtt, DEVICE_ID "/temp");
|
||||
|
||||
/*************************** Sketch Code ************************************/
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(10);
|
||||
|
||||
Serial.println(F("MQTT retain flag demo"));
|
||||
|
||||
// Connect to WiFi access point.
|
||||
Serial.println(); Serial.println();
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(WLAN_SSID);
|
||||
|
||||
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Connect to MQTT broker, then publish a retained message and a
|
||||
// non-retained message.
|
||||
Serial.print("\nConnecting to MQTT broker...");
|
||||
MQTT_connect();
|
||||
Serial.println("connected");
|
||||
|
||||
Serial.println("Publishing messages while not subscribed");
|
||||
publish_feed.publish("This message should be retained", true);
|
||||
publish_feed.publish("This message should not be retained");
|
||||
|
||||
Serial.println("Disconnecting from MQTT broker\n");
|
||||
mqtt.disconnect();
|
||||
|
||||
subscribe_feed.setCallback(subscribe_callback);
|
||||
mqtt.subscribe(&subscribe_feed);
|
||||
}
|
||||
|
||||
void subscribe_callback(char *data, uint16_t len) {
|
||||
Serial.print("--> Message received: \"");
|
||||
Serial.print(data);
|
||||
Serial.println("\"\n");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
// Connect to MQTT broker. We should receive the retained message only.
|
||||
Serial.println("Connecting to broker. Expect to receive retained message:");
|
||||
MQTT_connect();
|
||||
|
||||
mqtt.processPackets(1000);
|
||||
|
||||
Serial.println("Publishing non-retained message. Expect to receive it immediately:");
|
||||
publish_feed.publish("This message should be received immediately but not retained");
|
||||
|
||||
mqtt.processPackets(1000);
|
||||
|
||||
Serial.println("Publishing retained message. Expect to receive it immediately and on re-subscribing:");
|
||||
publish_feed.publish("This message should be received immediately AND retained", true);
|
||||
|
||||
mqtt.processPackets(10000);
|
||||
|
||||
Serial.println("Disconnecting from broker\n");
|
||||
mqtt.disconnect();
|
||||
|
||||
delay(15000);
|
||||
}
|
||||
|
||||
// Function to connect and reconnect as necessary to the MQTT server.
|
||||
// Should be called in the loop function and it will take care if connecting.
|
||||
void MQTT_connect() {
|
||||
int8_t ret;
|
||||
|
||||
// Stop if already connected.
|
||||
if (mqtt.connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t retries = 3;
|
||||
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
|
||||
Serial.println(mqtt.connectErrorString(ret));
|
||||
Serial.println("Retrying MQTT connection in 5 seconds...");
|
||||
mqtt.disconnect();
|
||||
delay(5000); // wait 5 seconds
|
||||
retries--;
|
||||
if (retries == 0) {
|
||||
// basically die and wait for WDT to reset me
|
||||
while (1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -11,7 +11,8 @@
|
|||
#include <SPI.h>
|
||||
#include "Adafruit_MQTT.h"
|
||||
#include "Adafruit_MQTT_Client.h"
|
||||
#include <WiFi101.h>
|
||||
#include <Adafruit_WINC1500.h>
|
||||
|
||||
|
||||
/************************* WiFI Setup *****************************/
|
||||
#define WINC_CS 8
|
||||
|
|
@ -19,6 +20,8 @@
|
|||
#define WINC_RST 4
|
||||
#define WINC_EN 2 // or, tie EN to VCC
|
||||
|
||||
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
|
||||
|
||||
char ssid[] = "yournetwork"; // your network SSID (name)
|
||||
char pass[] = "yourpassword"; // your network password (use for WPA, or use as key for WEP)
|
||||
int keyIndex = 0; // your network key Index number (needed only for WEP)
|
||||
|
|
@ -35,7 +38,7 @@ int status = WL_IDLE_STATUS;
|
|||
/************ Global State (you don't need to change this!) ******************/
|
||||
|
||||
//Set up the wifi client
|
||||
WiFiClient client;
|
||||
Adafruit_WINC1500Client client;
|
||||
|
||||
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||
|
||||
|
|
@ -57,7 +60,10 @@ Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAM
|
|||
|
||||
|
||||
void setup() {
|
||||
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
|
||||
#ifdef WINC_EN
|
||||
pinMode(WINC_EN, OUTPUT);
|
||||
digitalWrite(WINC_EN, HIGH);
|
||||
#endif
|
||||
|
||||
while (!Serial);
|
||||
Serial.begin(115200);
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1 +0,0 @@
|
|||
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
Adafruit_MQTT KEYWORD1
|
||||
Adafruit_MQTT_CC3000 KEYWORD1
|
||||
Adafruit_MQTT_FONA KEYWORD1
|
||||
Adafruit_MQTT_Client KEYWORD1
|
||||
Adafruit_MQTT_Publish KEYWORD1
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
name=Adafruit MQTT Library
|
||||
version=2.6.0
|
||||
version=0.16.0
|
||||
author=Adafruit
|
||||
maintainer=Adafruit <info@adafruit.com>
|
||||
sentence=MQTT library that supports the FONA, ESP8266, ESP32, Yun, and generic Arduino Client hardware.
|
||||
sentence=MQTT library that supports the CC3000, FONA, ESP8266, Yun, and generic Arduino Client hardware.
|
||||
paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
|
||||
category=Communication
|
||||
url=https://github.com/adafruit/Adafruit_MQTT_Library
|
||||
architectures=*
|
||||
depends=Adafruit SleepyDog Library, Adafruit FONA Library, WiFi101
|
||||
|
|
|
|||
Loading…
Reference in a new issue