Compare commits
64 Commits
0.13.0
...
master_ups
Author | SHA1 | Date | |
---|---|---|---|
|
a4e1ee0133 | ||
|
c25d31f813 | ||
|
7fffb1e769 | ||
|
f0e97737f7 | ||
|
5b25a96d1b | ||
|
13854f6487 | ||
|
a3f09cfa4c | ||
|
2d384b9697 | ||
|
974f4b8713 | ||
|
ce9bcab0b4 | ||
|
786ca3fdb2 | ||
|
564f34a84b | ||
|
8a8c0b1228 | ||
|
1e3fb01778 | ||
|
f5bb4e4e97 | ||
|
47ebe871e4 | ||
|
08a4d8bdee | ||
|
c5233ead52 | ||
|
3553074d45 | ||
|
c50f42c563 | ||
|
ac37a6e132 | ||
|
ae18e8a6c4 | ||
|
ceeedc2b3c | ||
|
faad8998f6 | ||
|
5e46585e25 | ||
|
e0c88d8d2c | ||
|
b61d7e7024 | ||
|
c56e1b935f | ||
|
bdd08d443e | ||
|
7886b7243c | ||
|
665395482e | ||
|
f8ec7fc08f | ||
|
fe3aebdad8 | ||
|
4b57d0ddcb | ||
|
f009049016 | ||
|
5f3a1e2322 | ||
|
cba09e7499 | ||
|
ae113941a1 | ||
|
49eda71d17 | ||
|
0bfffa1571 | ||
|
ad83c09d20 | ||
|
f793f8d2d0 | ||
|
9b23f6747e | ||
|
5df5ea2f1c | ||
|
ecc300d7a3 | ||
|
dfc670deeb | ||
|
b2a534ede9 | ||
|
2d03ae9c89 | ||
|
029eee956c | ||
|
f7bd3e31a6 | ||
|
63d86e2ca2 | ||
|
82873050b6 | ||
|
4269d87492 | ||
|
9ed79466b2 | ||
|
bb240928e8 | ||
|
ad6ae59725 | ||
|
5d133c3852 | ||
|
b26b4eb32c | ||
|
121aa3865c | ||
|
a7ae377720 | ||
|
737fa4b39d | ||
|
c8253d2128 | ||
|
7d138efc1a | ||
|
c19e155bac |
@ -30,8 +30,24 @@ static char *dtostrf (double val, signed char width, unsigned char prec, char *s
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void printBuffer(uint8_t *buffer, uint8_t len) {
|
#if defined(ESP8266)
|
||||||
for (uint8_t i=0; i<len; i++) {
|
int strncasecmp(const char * str1, const char * str2, int len) {
|
||||||
|
int d = 0;
|
||||||
|
while(len--) {
|
||||||
|
int c1 = tolower(*str1++);
|
||||||
|
int c2 = tolower(*str2++);
|
||||||
|
if(((d = c1 - c2) != 0) || (c2 == '\0')) {
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
void printBuffer(uint8_t *buffer, uint16_t len) {
|
||||||
|
DEBUG_PRINTER.print('\t');
|
||||||
|
for (uint16_t i=0; i<len; i++) {
|
||||||
if (isprint(buffer[i]))
|
if (isprint(buffer[i]))
|
||||||
DEBUG_PRINTER.write(buffer[i]);
|
DEBUG_PRINTER.write(buffer[i]);
|
||||||
else
|
else
|
||||||
@ -41,7 +57,9 @@ void printBuffer(uint8_t *buffer, uint8_t len) {
|
|||||||
DEBUG_PRINTER.print("0");
|
DEBUG_PRINTER.print("0");
|
||||||
DEBUG_PRINTER.print(buffer[i],HEX);
|
DEBUG_PRINTER.print(buffer[i],HEX);
|
||||||
DEBUG_PRINTER.print("], ");
|
DEBUG_PRINTER.print("], ");
|
||||||
if (i % 8 == 7) DEBUG_PRINTER.println();
|
if (i % 8 == 7) {
|
||||||
|
DEBUG_PRINTER.print("\n\t");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DEBUG_PRINTER.println();
|
DEBUG_PRINTER.println();
|
||||||
}
|
}
|
||||||
@ -56,11 +74,11 @@ static uint8_t *stringprint(uint8_t *p, char *s) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static uint8_t *stringprint_P(uint8_t *p, const char *s, uint16_t maxlen=0) {
|
static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen=0) {
|
||||||
// If maxlen is specified (has a non-zero value) then use it as the maximum
|
// If maxlen is specified (has a non-zero value) then use it as the maximum
|
||||||
// length of the source string to write to the buffer. Otherwise write
|
// length of the source string to write to the buffer. Otherwise write
|
||||||
// the entire source string.
|
// the entire source string.
|
||||||
uint16_t len = strlen_P(s);
|
uint16_t len = strlen(s);
|
||||||
if (maxlen > 0 && len > maxlen) {
|
if (maxlen > 0 && len > maxlen) {
|
||||||
len = maxlen;
|
len = maxlen;
|
||||||
}
|
}
|
||||||
@ -71,7 +89,7 @@ static uint8_t *stringprint_P(uint8_t *p, const char *s, uint16_t maxlen=0) {
|
|||||||
*/
|
*/
|
||||||
p[0] = len >> 8; p++;
|
p[0] = len >> 8; p++;
|
||||||
p[0] = len & 0xFF; p++;
|
p[0] = len & 0xFF; p++;
|
||||||
strncpy_P((char *)p, s, len);
|
strncpy((char *)p, s, len);
|
||||||
return p+len;
|
return p+len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,31 +121,6 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Adafruit_MQTT::Adafruit_MQTT(const __FlashStringHelper *server,
|
|
||||||
uint16_t port,
|
|
||||||
const __FlashStringHelper *cid,
|
|
||||||
const __FlashStringHelper *user,
|
|
||||||
const __FlashStringHelper *pass) {
|
|
||||||
|
|
||||||
servername = (const char *)server;
|
|
||||||
portnum = port;
|
|
||||||
clientid = (const char *)cid;
|
|
||||||
username = (const char *)user;
|
|
||||||
password = (const char *)pass;
|
|
||||||
|
|
||||||
// reset subscriptions
|
|
||||||
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
|
|
||||||
subscriptions[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
will_topic = 0;
|
|
||||||
will_payload = 0;
|
|
||||||
will_qos = 0;
|
|
||||||
will_retain = 0;
|
|
||||||
|
|
||||||
packet_id_counter = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
||||||
uint16_t port,
|
uint16_t port,
|
||||||
@ -153,31 +146,6 @@ Adafruit_MQTT::Adafruit_MQTT(const char *server,
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Adafruit_MQTT::Adafruit_MQTT(const __FlashStringHelper *server,
|
|
||||||
uint16_t port,
|
|
||||||
const __FlashStringHelper *user,
|
|
||||||
const __FlashStringHelper *pass) {
|
|
||||||
|
|
||||||
servername = (const char *)server;
|
|
||||||
portnum = port;
|
|
||||||
clientid = "";
|
|
||||||
username = (const char *)user;
|
|
||||||
password = (const char *)pass;
|
|
||||||
|
|
||||||
// reset subscriptions
|
|
||||||
for (uint8_t i=0; i<MAXSUBSCRIPTIONS; i++) {
|
|
||||||
subscriptions[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
will_topic = 0;
|
|
||||||
will_payload = 0;
|
|
||||||
will_qos = 0;
|
|
||||||
will_retain = 0;
|
|
||||||
|
|
||||||
packet_id_counter = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int8_t Adafruit_MQTT::connect() {
|
int8_t Adafruit_MQTT::connect() {
|
||||||
// Connect to the server.
|
// Connect to the server.
|
||||||
if (!connectServer())
|
if (!connectServer())
|
||||||
@ -189,7 +157,7 @@ int8_t Adafruit_MQTT::connect() {
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// Read connect response packet and verify it
|
// Read connect response packet and verify it
|
||||||
len = readFullPacket(buffer, CONNECT_TIMEOUT_MS);
|
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
|
||||||
if (len != 4)
|
if (len != 4)
|
||||||
return -1;
|
return -1;
|
||||||
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
|
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2))
|
||||||
@ -216,11 +184,12 @@ int8_t Adafruit_MQTT::connect() {
|
|||||||
// TODO: The Server is permitted to start sending PUBLISH packets matching the
|
// 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)
|
// Subscription before the Server sends the SUBACK Packet. (will really need to use callbacks - ada)
|
||||||
|
|
||||||
len = processPacketsUntil(buffer, MQTT_CTRL_SUBACK, CONNECT_TIMEOUT_MS);
|
//Serial.println("\t**looking for suback");
|
||||||
if ((len != 5) || (buffer[0] != (MQTT_CTRL_SUBACK << 4))) {
|
if (processPacketsUntil(buffer, MQTT_CTRL_SUBACK, SUBACK_TIMEOUT_MS)) {
|
||||||
continue; // retry!
|
success = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
success = true;
|
//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
|
||||||
}
|
}
|
||||||
@ -228,9 +197,16 @@ int8_t Adafruit_MQTT::connect() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t Adafruit_MQTT::connect(const char *user, const char *pass)
|
||||||
|
{
|
||||||
|
username = user;
|
||||||
|
password = pass;
|
||||||
|
return connect();
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) {
|
uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpackettype, uint16_t timeout) {
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
while (len = readFullPacket(buffer, timeout)) {
|
while (len = readFullPacket(buffer, MAXBUFFERSIZE, timeout)) {
|
||||||
|
|
||||||
//DEBUG_PRINT("Packet read size: "); DEBUG_PRINTLN(len);
|
//DEBUG_PRINT("Packet read size: "); DEBUG_PRINTLN(len);
|
||||||
// TODO: add subscription reading & call back processing here
|
// TODO: add subscription reading & call back processing here
|
||||||
@ -238,12 +214,14 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpack
|
|||||||
if ((buffer[0] >> 4) == waitforpackettype) {
|
if ((buffer[0] >> 4) == waitforpackettype) {
|
||||||
//DEBUG_PRINTLN(F("Found right packet"));
|
//DEBUG_PRINTLN(F("Found right packet"));
|
||||||
return len;
|
return len;
|
||||||
|
} else {
|
||||||
|
ERROR_PRINTLN(F("Dropped a packet"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, 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
|
// will read a packet and Do The Right Thing with length
|
||||||
uint8_t *pbuff = buffer;
|
uint8_t *pbuff = buffer;
|
||||||
|
|
||||||
@ -277,7 +255,12 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t timeout) {
|
|||||||
|
|
||||||
DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value);
|
DEBUG_PRINT(F("Packet Length:\t")); DEBUG_PRINTLN(value);
|
||||||
|
|
||||||
rlen = readPacket(pbuff, value, timeout);
|
if (value > (maxsize - (pbuff-buffer) - 1)) {
|
||||||
|
DEBUG_PRINTLN(F("Packet too big for buffer"));
|
||||||
|
rlen = readPacket(pbuff, (maxsize - (pbuff-buffer) - 1), timeout);
|
||||||
|
} else {
|
||||||
|
rlen = readPacket(pbuff, value, timeout);
|
||||||
|
}
|
||||||
//DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
|
//DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
|
||||||
|
|
||||||
return ((pbuff - buffer)+rlen);
|
return ((pbuff - buffer)+rlen);
|
||||||
@ -314,15 +297,15 @@ bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
|
|||||||
return publish(topic, (uint8_t*)(data), strlen(data), qos);
|
return publish(topic, (uint8_t*)(data), strlen(data), qos);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint8_t bLen, uint8_t qos) {
|
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen, uint8_t qos) {
|
||||||
// Construct and send publish packet.
|
// Construct and send publish packet.
|
||||||
uint8_t len = publishPacket(buffer, topic, data, bLen, qos);
|
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
|
||||||
if (!sendPacket(buffer, len))
|
if (!sendPacket(buffer, len))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// If QOS level is high enough verify the response packet.
|
// If QOS level is high enough verify the response packet.
|
||||||
if (qos > 0) {
|
if (qos > 0) {
|
||||||
len = readFullPacket(buffer, PUBLISH_TIMEOUT_MS);
|
len = readFullPacket(buffer, MAXBUFFERSIZE, PUBLISH_TIMEOUT_MS);
|
||||||
DEBUG_PRINT(F("Publish QOS1+ reply:\t"));
|
DEBUG_PRINT(F("Publish QOS1+ reply:\t"));
|
||||||
DEBUG_PRINTBUFFER(buffer, len);
|
DEBUG_PRINTBUFFER(buffer, len);
|
||||||
if (len != 4)
|
if (len != 4)
|
||||||
@ -403,7 +386,7 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
|||||||
if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
|
if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
|
||||||
|
|
||||||
// wait for UNSUBACK
|
// wait for UNSUBACK
|
||||||
len = readFullPacket(buffer, CONNECT_TIMEOUT_MS);
|
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
|
||||||
DEBUG_PRINT(F("UNSUBACK:\t"));
|
DEBUG_PRINT(F("UNSUBACK:\t"));
|
||||||
DEBUG_PRINTBUFFER(buffer, len);
|
DEBUG_PRINTBUFFER(buffer, len);
|
||||||
|
|
||||||
@ -423,11 +406,55 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
//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_mqtt)->*(sub->callback_io))((char *)sub->lastread, sub->datalen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// keep track over elapsed time
|
||||||
|
endtime = millis();
|
||||||
|
if (endtime < starttime) {
|
||||||
|
starttime = endtime; // looped around!")
|
||||||
|
}
|
||||||
|
elapsed += (endtime - starttime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
||||||
uint8_t i, topiclen, datalen;
|
uint16_t i, topiclen, datalen;
|
||||||
|
|
||||||
// Check if data is available to read.
|
// Check if data is available to read.
|
||||||
uint16_t len = readFullPacket(buffer, timeout); // return one full packet
|
uint16_t len = readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
|
||||||
if (!len)
|
if (!len)
|
||||||
return NULL; // No data available, just quit.
|
return NULL; // No data available, just quit.
|
||||||
DEBUG_PRINT("Packet len: "); DEBUG_PRINTLN(len);
|
DEBUG_PRINT("Packet len: "); DEBUG_PRINTLN(len);
|
||||||
@ -442,11 +469,11 @@ Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
|||||||
if (subscriptions[i]) {
|
if (subscriptions[i]) {
|
||||||
// Skip this subscription if its name length isn't the same as the
|
// Skip this subscription if its name length isn't the same as the
|
||||||
// received topic name.
|
// received topic name.
|
||||||
if (strlen_P(subscriptions[i]->topic) != topiclen)
|
if (strlen(subscriptions[i]->topic) != topiclen)
|
||||||
continue;
|
continue;
|
||||||
// Stop if the subscription topic matches the received topic. Be careful
|
// Stop if the subscription topic matches the received topic. Be careful
|
||||||
// to make comparison case insensitive.
|
// to make comparison case insensitive.
|
||||||
if (strncasecmp_P((char*)buffer+4, subscriptions[i]->topic, topiclen) == 0) {
|
if (strncasecmp((char*)buffer+4, subscriptions[i]->topic, topiclen) == 0) {
|
||||||
DEBUG_PRINT(F("Found sub #")); DEBUG_PRINTLN(i);
|
DEBUG_PRINT(F("Found sub #")); DEBUG_PRINTLN(i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -531,9 +558,9 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||||||
// fill in packet[1] last
|
// fill in packet[1] last
|
||||||
|
|
||||||
#if MQTT_PROTOCOL_LEVEL == 3
|
#if MQTT_PROTOCOL_LEVEL == 3
|
||||||
p = stringprint_P(p, PSTR("MQIsdp"));
|
p = stringprint(p, "MQIsdp");
|
||||||
#elif MQTT_PROTOCOL_LEVEL == 4
|
#elif MQTT_PROTOCOL_LEVEL == 4
|
||||||
p = stringprint_P(p, PSTR("MQTT"));
|
p = stringprint(p, "MQTT");
|
||||||
#else
|
#else
|
||||||
#error "MQTT level not supported"
|
#error "MQTT level not supported"
|
||||||
#endif
|
#endif
|
||||||
@ -571,10 +598,10 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||||||
p++;
|
p++;
|
||||||
|
|
||||||
if(MQTT_PROTOCOL_LEVEL == 3) {
|
if(MQTT_PROTOCOL_LEVEL == 3) {
|
||||||
p = stringprint_P(p, clientid, 23); // Limit client ID to first 23 characters.
|
p = stringprint(p, clientid, 23); // Limit client ID to first 23 characters.
|
||||||
} else {
|
} else {
|
||||||
if (pgm_read_byte(clientid) != 0) {
|
if (pgm_read_byte(clientid) != 0) {
|
||||||
p = stringprint_P(p, clientid);
|
p = stringprint(p, clientid);
|
||||||
} else {
|
} else {
|
||||||
p[0] = 0x0;
|
p[0] = 0x0;
|
||||||
p++;
|
p++;
|
||||||
@ -585,15 +612,15 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (will_topic && pgm_read_byte(will_topic) != 0) {
|
if (will_topic && pgm_read_byte(will_topic) != 0) {
|
||||||
p = stringprint_P(p, will_topic);
|
p = stringprint(p, will_topic);
|
||||||
p = stringprint_P(p, will_payload);
|
p = stringprint(p, will_payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pgm_read_byte(username) != 0) {
|
if (pgm_read_byte(username) != 0) {
|
||||||
p = stringprint_P(p, username);
|
p = stringprint(p, username);
|
||||||
}
|
}
|
||||||
if (pgm_read_byte(password) != 0) {
|
if (pgm_read_byte(password) != 0) {
|
||||||
p = stringprint_P(p, password);
|
p = stringprint(p, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
len = p - packet;
|
len = p - packet;
|
||||||
@ -606,17 +633,37 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
|||||||
|
|
||||||
|
|
||||||
// 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
|
||||||
uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
||||||
uint8_t *data, uint8_t bLen, uint8_t qos) {
|
uint8_t *data, uint16_t bLen, uint8_t qos) {
|
||||||
uint8_t *p = packet;
|
uint8_t *p = packet;
|
||||||
uint16_t len;
|
uint16_t len=0;
|
||||||
|
|
||||||
|
// calc length of non-header data
|
||||||
|
len += 2; // two bytes to set the topic size
|
||||||
|
len += strlen(topic); // topic length
|
||||||
|
if(qos > 0) {
|
||||||
|
len += 2; // qos packet id
|
||||||
|
}
|
||||||
|
len += bLen; // payload length
|
||||||
|
|
||||||
|
// Now you can start generating the packet!
|
||||||
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
|
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
|
||||||
|
p++;
|
||||||
|
|
||||||
// fill in packet[1] last
|
// fill in packet[1] last
|
||||||
p+=2;
|
do {
|
||||||
|
uint8_t encodedByte = len % 128;
|
||||||
|
len /= 128;
|
||||||
|
// if there are more data to encode, set the top bit of this byte
|
||||||
|
if ( len > 0 ) {
|
||||||
|
encodedByte |= 0x80;
|
||||||
|
}
|
||||||
|
p[0] = encodedByte;
|
||||||
|
p++;
|
||||||
|
} while ( len > 0 );
|
||||||
|
|
||||||
// topic comes before packet identifier
|
// topic comes before packet identifier
|
||||||
p = stringprint_P(p, topic);
|
p = stringprint(p, topic);
|
||||||
|
|
||||||
// add packet identifier. used for checking PUBACK in QOS > 0
|
// add packet identifier. used for checking PUBACK in QOS > 0
|
||||||
if(qos > 0) {
|
if(qos > 0) {
|
||||||
@ -631,11 +678,9 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
|||||||
memmove(p, data, bLen);
|
memmove(p, data, bLen);
|
||||||
p+= bLen;
|
p+= bLen;
|
||||||
len = p - packet;
|
len = p - packet;
|
||||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
|
||||||
DEBUG_PRINTLN(F("MQTT publish packet:"));
|
DEBUG_PRINTLN(F("MQTT publish packet:"));
|
||||||
DEBUG_PRINTBUFFER(buffer, len);
|
DEBUG_PRINTBUFFER(buffer, len);
|
||||||
return len;
|
return len;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
||||||
@ -655,7 +700,7 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
|||||||
// increment the packet id
|
// increment the packet id
|
||||||
packet_id_counter++;
|
packet_id_counter++;
|
||||||
|
|
||||||
p = stringprint_P(p, topic);
|
p = stringprint(p, topic);
|
||||||
|
|
||||||
p[0] = qos;
|
p[0] = qos;
|
||||||
p++;
|
p++;
|
||||||
@ -686,7 +731,7 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
|||||||
// increment the packet id
|
// increment the packet id
|
||||||
packet_id_counter++;
|
packet_id_counter++;
|
||||||
|
|
||||||
p = stringprint_P(p, topic);
|
p = stringprint(p, topic);
|
||||||
|
|
||||||
len = p - packet;
|
len = p - packet;
|
||||||
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
packet[1] = len-2; // don't include the 2 bytes of fixed header data
|
||||||
@ -730,29 +775,21 @@ Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
|
|||||||
topic = feed;
|
topic = feed;
|
||||||
qos = q;
|
qos = q;
|
||||||
}
|
}
|
||||||
|
|
||||||
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
|
|
||||||
const __FlashStringHelper *feed, uint8_t q) {
|
|
||||||
mqtt = mqttserver;
|
|
||||||
topic = (const char *)feed;
|
|
||||||
qos = q;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
||||||
char payload[18];
|
char payload[12];
|
||||||
itoa(i, payload, 10);
|
ltoa(i, payload, 10);
|
||||||
return mqtt->publish(topic, payload, qos);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
|
|
||||||
char payload[40]; // Need to technically hold float max, 39 digits and minus sign.
|
|
||||||
dtostrf(f, 0, precision, payload);
|
|
||||||
return mqtt->publish(topic, payload, qos);
|
return mqtt->publish(topic, payload, qos);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
|
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
|
||||||
char payload[18];
|
char payload[11];
|
||||||
itoa(i, payload, 10);
|
ultoa(i, payload, 10);
|
||||||
|
return mqtt->publish(topic, payload, qos);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
return mqtt->publish(topic, payload, qos);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -761,7 +798,7 @@ bool Adafruit_MQTT_Publish::publish(const char *payload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//publish buffer of arbitrary length
|
//publish buffer of arbitrary length
|
||||||
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint8_t bLen) {
|
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
|
||||||
|
|
||||||
return mqtt->publish(topic, payload, bLen, qos);
|
return mqtt->publish(topic, payload, bLen, qos);
|
||||||
}
|
}
|
||||||
@ -775,22 +812,34 @@ Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
|||||||
topic = feed;
|
topic = feed;
|
||||||
qos = q;
|
qos = q;
|
||||||
datalen = 0;
|
datalen = 0;
|
||||||
callback = 0;
|
callback_uint32t = 0;
|
||||||
|
callback_buffer = 0;
|
||||||
|
callback_double = 0;
|
||||||
|
callback_io = 0;
|
||||||
|
io_mqtt = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackUInt32Type cb) {
|
||||||
const __FlashStringHelper *feed, uint8_t q) {
|
callback_uint32t = cb;
|
||||||
mqtt = mqttserver;
|
|
||||||
topic = (const char *)feed;
|
|
||||||
qos = q;
|
|
||||||
datalen = 0;
|
|
||||||
callback = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackType cb) {
|
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackDoubleType cb) {
|
||||||
callback = cb;
|
callback_double = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackBufferType cb) {
|
||||||
|
callback_buffer = cb;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Adafruit_MQTT_Subscribe::setCallback(AdafruitIO_MQTT *io, SubscribeCallbackIOType cb) {
|
||||||
|
callback_io = cb;
|
||||||
|
io_mqtt= io;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Adafruit_MQTT_Subscribe::removeCallback(void) {
|
void Adafruit_MQTT_Subscribe::removeCallback(void) {
|
||||||
callback = 0;
|
callback_uint32t = 0;
|
||||||
|
callback_buffer = 0;
|
||||||
|
callback_double = 0;
|
||||||
|
callback_io = 0;
|
||||||
|
io_mqtt = 0;
|
||||||
}
|
}
|
||||||
|
@ -29,8 +29,14 @@
|
|||||||
#define strncasecmp_P(f1, f2, len) strncasecmp((f1), (f2), (len))
|
#define strncasecmp_P(f1, f2, len) strncasecmp((f1), (f2), (len))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define ADAFRUIT_MQTT_VERSION_MAJOR 0
|
||||||
|
#define ADAFRUIT_MQTT_VERSION_MINOR 17
|
||||||
|
#define ADAFRUIT_MQTT_VERSION_PATCH 0
|
||||||
|
|
||||||
// Uncomment/comment to turn on/off debug output messages.
|
// Uncomment/comment to turn on/off debug output messages.
|
||||||
//#define MQTT_DEBUG
|
//#define MQTT_DEBUG
|
||||||
|
// Uncomment/comment to turn on/off error output messages.
|
||||||
|
#define MQTT_ERROR
|
||||||
|
|
||||||
// Set where debug messages will be printed.
|
// Set where debug messages will be printed.
|
||||||
#define DEBUG_PRINTER Serial
|
#define DEBUG_PRINTER Serial
|
||||||
@ -47,6 +53,16 @@
|
|||||||
#define DEBUG_PRINTBUFFER(buffer, len) {}
|
#define DEBUG_PRINTBUFFER(buffer, len) {}
|
||||||
#endif
|
#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); }
|
||||||
|
#else
|
||||||
|
#define ERROR_PRINT(...) {}
|
||||||
|
#define ERROR_PRINTLN(...) {}
|
||||||
|
#define ERROR_PRINTBUFFER(buffer, len) {}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Use 3 (MQTT 3.0) or 4 (MQTT 3.1.1)
|
// Use 3 (MQTT 3.0) or 4 (MQTT 3.1.1)
|
||||||
#define MQTT_PROTOCOL_LEVEL 4
|
#define MQTT_PROTOCOL_LEVEL 4
|
||||||
|
|
||||||
@ -71,6 +87,7 @@
|
|||||||
#define CONNECT_TIMEOUT_MS 6000
|
#define CONNECT_TIMEOUT_MS 6000
|
||||||
#define PUBLISH_TIMEOUT_MS 500
|
#define PUBLISH_TIMEOUT_MS 500
|
||||||
#define PING_TIMEOUT_MS 500
|
#define PING_TIMEOUT_MS 500
|
||||||
|
#define SUBACK_TIMEOUT_MS 500
|
||||||
|
|
||||||
// Adjust as necessary, in seconds. Default to 5 minutes.
|
// Adjust as necessary, in seconds. Default to 5 minutes.
|
||||||
#define MQTT_CONN_KEEPALIVE 300
|
#define MQTT_CONN_KEEPALIVE 300
|
||||||
@ -78,7 +95,7 @@
|
|||||||
// Largest full packet we're able to send.
|
// Largest full packet we're able to send.
|
||||||
// Need to be able to store at least ~90 chars for a connect packet with full
|
// Need to be able to store at least ~90 chars for a connect packet with full
|
||||||
// 23 char client ID.
|
// 23 char client ID.
|
||||||
#define MAXBUFFERSIZE (125)
|
#define MAXBUFFERSIZE (150)
|
||||||
|
|
||||||
#define MQTT_CONN_USERNAMEFLAG 0x80
|
#define MQTT_CONN_USERNAMEFLAG 0x80
|
||||||
#define MQTT_CONN_PASSWORDFLAG 0x40
|
#define MQTT_CONN_PASSWORDFLAG 0x40
|
||||||
@ -93,13 +110,24 @@
|
|||||||
|
|
||||||
// how much data we save in a subscription object
|
// how much data we save in a subscription object
|
||||||
// eg max-subscription-payload-size
|
// eg max-subscription-payload-size
|
||||||
#define SUBSCRIPTIONDATALEN 20
|
#if defined (__AVR_ATmega32U4__) || defined(__AVR_ATmega328P__)
|
||||||
|
#define SUBSCRIPTIONDATALEN 20
|
||||||
|
#else
|
||||||
|
#define SUBSCRIPTIONDATALEN 100
|
||||||
|
#endif
|
||||||
|
|
||||||
//Function pointer called CallbackType that takes a float
|
class AdafruitIO_MQTT; // forward decl
|
||||||
//and returns an int
|
|
||||||
typedef void (*SubscribeCallbackType)(char *);
|
|
||||||
|
|
||||||
extern void printBuffer(uint8_t *buffer, uint8_t len);
|
//Function pointer that returns an int
|
||||||
|
typedef void (*SubscribeCallbackUInt32Type)(uint32_t);
|
||||||
|
// returns a double
|
||||||
|
typedef void (*SubscribeCallbackDoubleType)(double);
|
||||||
|
// returns a chunk of raw data
|
||||||
|
typedef void (*SubscribeCallbackBufferType)(char *str, uint16_t len);
|
||||||
|
// returns an io data wrapper instance
|
||||||
|
typedef void (AdafruitIO_MQTT::*SubscribeCallbackIOType)(char *str, uint16_t len);
|
||||||
|
|
||||||
|
extern void printBuffer(uint8_t *buffer, uint16_t len);
|
||||||
|
|
||||||
class Adafruit_MQTT_Subscribe; // forward decl
|
class Adafruit_MQTT_Subscribe; // forward decl
|
||||||
|
|
||||||
@ -110,19 +138,11 @@ class Adafruit_MQTT {
|
|||||||
const char *cid,
|
const char *cid,
|
||||||
const char *user,
|
const char *user,
|
||||||
const char *pass);
|
const char *pass);
|
||||||
Adafruit_MQTT(const __FlashStringHelper *server,
|
|
||||||
uint16_t port,
|
|
||||||
const __FlashStringHelper *cid,
|
|
||||||
const __FlashStringHelper *user,
|
|
||||||
const __FlashStringHelper *pass);
|
|
||||||
Adafruit_MQTT(const char *server,
|
Adafruit_MQTT(const char *server,
|
||||||
uint16_t port,
|
uint16_t port,
|
||||||
const char *user,
|
const char *user = "",
|
||||||
const char *pass);
|
const char *pass = "");
|
||||||
Adafruit_MQTT(const __FlashStringHelper *server,
|
|
||||||
uint16_t port,
|
|
||||||
const __FlashStringHelper *user,
|
|
||||||
const __FlashStringHelper *pass);
|
|
||||||
virtual ~Adafruit_MQTT() {}
|
virtual ~Adafruit_MQTT() {}
|
||||||
|
|
||||||
// Connect to the MQTT server. Returns 0 on success, otherwise an error code
|
// Connect to the MQTT server. Returns 0 on success, otherwise an error code
|
||||||
@ -137,6 +157,7 @@ class Adafruit_MQTT {
|
|||||||
// Use connectErrorString() to get a printable string version of the
|
// Use connectErrorString() to get a printable string version of the
|
||||||
// error.
|
// error.
|
||||||
int8_t connect();
|
int8_t connect();
|
||||||
|
int8_t connect(const char *user, const char *pass);
|
||||||
|
|
||||||
// Return a printable string version of the error code returned by
|
// Return a printable string version of the error code returned by
|
||||||
// connect(). This returns a __FlashStringHelper*, which points to a
|
// connect(). This returns a __FlashStringHelper*, which points to a
|
||||||
@ -154,19 +175,11 @@ class Adafruit_MQTT {
|
|||||||
// to be called before connect() because it is sent as part of the
|
// to be called before connect() because it is sent as part of the
|
||||||
// connect control packet.
|
// connect control packet.
|
||||||
bool will(const char *topic, const char *payload, uint8_t qos = 0, uint8_t retain = 0);
|
bool will(const char *topic, const char *payload, uint8_t qos = 0, uint8_t retain = 0);
|
||||||
bool will(const __FlashStringHelper *topic, const char *payload, uint8_t qos = 0, uint8_t retain = 0) {
|
|
||||||
return will((const char *)topic, payload, qos, retain);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Publish a message to a topic using the specified QoS level. Returns true
|
// Publish a message to a topic using the specified QoS level. Returns true
|
||||||
// if the message was published, false otherwise.
|
// if the message was published, false otherwise.
|
||||||
// The topic must be stored in PROGMEM. It can either be a
|
|
||||||
// char*, or a __FlashStringHelper* (the result of the F() macro).
|
|
||||||
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
|
bool publish(const char *topic, const char *payload, uint8_t qos = 0);
|
||||||
bool publish(const char *topic, uint8_t *payload, uint8_t bLen, uint8_t qos = 0);
|
bool publish(const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos = 0);
|
||||||
bool publish(const __FlashStringHelper *topic, const char *payload, uint8_t qos = 0) {
|
|
||||||
return publish((const char *)topic, payload, qos);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a subscription to receive messages for a topic. Returns true if the
|
// Add a subscription to receive messages for a topic. Returns true if the
|
||||||
// subscription could be added or was already present, false otherwise.
|
// subscription could be added or was already present, false otherwise.
|
||||||
@ -183,6 +196,8 @@ class Adafruit_MQTT {
|
|||||||
// that subscribe should be called first for each topic that receives messages!
|
// that subscribe should be called first for each topic that receives messages!
|
||||||
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0);
|
Adafruit_MQTT_Subscribe *readSubscription(int16_t timeout=0);
|
||||||
|
|
||||||
|
void processPackets(int16_t timeout);
|
||||||
|
|
||||||
// Ping the server to ensure the connection is still alive.
|
// Ping the server to ensure the connection is still alive.
|
||||||
bool ping(uint8_t n = 1);
|
bool ping(uint8_t n = 1);
|
||||||
|
|
||||||
@ -196,15 +211,15 @@ class Adafruit_MQTT {
|
|||||||
virtual bool disconnectServer() = 0; // Subclasses need to fill this in!
|
virtual bool disconnectServer() = 0; // Subclasses need to fill this in!
|
||||||
|
|
||||||
// Send data to the server specified by the buffer and length of data.
|
// Send data to the server specified by the buffer and length of data.
|
||||||
virtual bool sendPacket(uint8_t *buffer, uint8_t len) = 0;
|
virtual bool sendPacket(uint8_t *buffer, uint16_t len) = 0;
|
||||||
|
|
||||||
// Read MQTT packet from the server. Will read up to maxlen bytes and store
|
// 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
|
// the data in the provided buffer. Waits up to the specified timeout (in
|
||||||
// milliseconds) for data to be available.
|
// milliseconds) for data to be available.
|
||||||
virtual uint16_t readPacket(uint8_t *buffer, uint8_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
|
// Read a full packet, keeping note of the correct length
|
||||||
uint16_t readFullPacket(uint8_t *buffer, uint16_t timeout);
|
uint16_t readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16_t timeout);
|
||||||
// Properly process packets until you get to one you want
|
// 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);
|
||||||
|
|
||||||
@ -229,7 +244,7 @@ class Adafruit_MQTT {
|
|||||||
// Functions to generate MQTT packets.
|
// Functions to generate MQTT packets.
|
||||||
uint8_t connectPacket(uint8_t *packet);
|
uint8_t connectPacket(uint8_t *packet);
|
||||||
uint8_t disconnectPacket(uint8_t *packet);
|
uint8_t disconnectPacket(uint8_t *packet);
|
||||||
uint8_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload, uint8_t bLen, uint8_t qos);
|
uint16_t publishPacket(uint8_t *packet, const char *topic, uint8_t *payload, uint16_t bLen, uint8_t qos);
|
||||||
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
|
uint8_t subscribePacket(uint8_t *packet, const char *topic, uint8_t qos);
|
||||||
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
|
uint8_t unsubscribePacket(uint8_t *packet, const char *topic);
|
||||||
uint8_t pingPacket(uint8_t *packet);
|
uint8_t pingPacket(uint8_t *packet);
|
||||||
@ -240,14 +255,13 @@ class Adafruit_MQTT {
|
|||||||
class Adafruit_MQTT_Publish {
|
class Adafruit_MQTT_Publish {
|
||||||
public:
|
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);
|
||||||
Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver, const __FlashStringHelper *feed, uint8_t qos = 0);
|
|
||||||
|
|
||||||
bool publish(const char *s);
|
bool publish(const char *s);
|
||||||
bool publish(double f, uint8_t precision=2); // Precision controls the minimum number of digits after decimal.
|
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.
|
// This might be ignored and a higher precision value sent.
|
||||||
bool publish(int32_t i);
|
bool publish(int32_t i);
|
||||||
bool publish(uint32_t i);
|
bool publish(uint32_t i);
|
||||||
bool publish(uint8_t *b, uint8_t bLen);
|
bool publish(uint8_t *b, uint16_t bLen);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -259,9 +273,11 @@ private:
|
|||||||
class Adafruit_MQTT_Subscribe {
|
class Adafruit_MQTT_Subscribe {
|
||||||
public:
|
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);
|
||||||
Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver, const __FlashStringHelper *feedname, uint8_t q=0);
|
|
||||||
|
|
||||||
void setCallback(SubscribeCallbackType callb);
|
void setCallback(SubscribeCallbackUInt32Type callb);
|
||||||
|
void setCallback(SubscribeCallbackDoubleType callb);
|
||||||
|
void setCallback(SubscribeCallbackBufferType callb);
|
||||||
|
void setCallback(AdafruitIO_MQTT *io, SubscribeCallbackIOType callb);
|
||||||
void removeCallback(void);
|
void removeCallback(void);
|
||||||
|
|
||||||
const char *topic;
|
const char *topic;
|
||||||
@ -270,9 +286,16 @@ class Adafruit_MQTT_Subscribe {
|
|||||||
uint8_t lastread[SUBSCRIPTIONDATALEN];
|
uint8_t lastread[SUBSCRIPTIONDATALEN];
|
||||||
// Number valid bytes in lastread. Limited to SUBSCRIPTIONDATALEN-1 to
|
// Number valid bytes in lastread. Limited to SUBSCRIPTIONDATALEN-1 to
|
||||||
// ensure nul terminating lastread.
|
// ensure nul terminating lastread.
|
||||||
uint8_t datalen;
|
uint16_t datalen;
|
||||||
|
|
||||||
|
SubscribeCallbackUInt32Type callback_uint32t;
|
||||||
|
SubscribeCallbackDoubleType callback_double;
|
||||||
|
SubscribeCallbackBufferType callback_buffer;
|
||||||
|
SubscribeCallbackIOType callback_io;
|
||||||
|
|
||||||
|
AdafruitIO_MQTT *io_mqtt;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SubscribeCallbackType callback;
|
|
||||||
Adafruit_MQTT *mqtt;
|
Adafruit_MQTT *mqtt;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,152 +0,0 @@
|
|||||||
// 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, uint8_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, uint8_t len) {
|
|
||||||
if (mqttclient.connected()) {
|
|
||||||
uint16_t ret = mqttclient.write(buffer, len);
|
|
||||||
DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
|
||||||
if (ret != len) {
|
|
||||||
DEBUG_PRINTLN("Failed to send complete packet.")
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
DEBUG_PRINTLN(F("Connection failed!"));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
uint32_t serverip;
|
|
||||||
Adafruit_CC3000 *cc3000;
|
|
||||||
Adafruit_CC3000_Client mqttclient;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
@ -25,7 +25,7 @@
|
|||||||
bool Adafruit_MQTT_Client::connectServer() {
|
bool Adafruit_MQTT_Client::connectServer() {
|
||||||
// Grab server name from flash and copy to buffer for name resolution.
|
// Grab server name from flash and copy to buffer for name resolution.
|
||||||
memset(buffer, 0, sizeof(buffer));
|
memset(buffer, 0, sizeof(buffer));
|
||||||
strcpy_P((char *)buffer, servername);
|
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).
|
// Connect and check for success (0 result).
|
||||||
int r = client->connect((char *)buffer, portnum);
|
int r = client->connect((char *)buffer, portnum);
|
||||||
@ -47,7 +47,7 @@ bool Adafruit_MQTT_Client::connected() {
|
|||||||
return client->connected();
|
return client->connected();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen,
|
uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
|
||||||
int16_t timeout) {
|
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;
|
uint16_t len = 0;
|
||||||
@ -74,17 +74,27 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint8_t maxlen,
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint8_t len) {
|
bool Adafruit_MQTT_Client::sendPacket(uint8_t *buffer, uint16_t len) {
|
||||||
if (client->connected()) {
|
uint16_t ret = 0;
|
||||||
uint16_t ret = client->write(buffer, len);
|
|
||||||
DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
while (len > 0) {
|
||||||
if (ret != len) {
|
if (client->connected()) {
|
||||||
DEBUG_PRINTLN("Failed to send complete packet.")
|
// send 250 bytes at most at a time, can adjust this later based on Client
|
||||||
|
|
||||||
|
uint16_t sendlen = len > 250 ? 250 : len;
|
||||||
|
//Serial.print("Sending: "); Serial.println(sendlen);
|
||||||
|
ret = client->write(buffer, sendlen);
|
||||||
|
DEBUG_PRINT(F("Client sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
||||||
|
len -= ret;
|
||||||
|
|
||||||
|
if (ret != sendlen) {
|
||||||
|
DEBUG_PRINTLN("Failed to send packet.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DEBUG_PRINTLN(F("Connection failed!"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
DEBUG_PRINTLN(F("Connection failed!"));
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ class Adafruit_MQTT_Client : public Adafruit_MQTT {
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port,
|
Adafruit_MQTT_Client(Client *client, const char *server, uint16_t port,
|
||||||
const char *user, const char *pass):
|
const char *user="", const char *pass=""):
|
||||||
Adafruit_MQTT(server, port, user, pass),
|
Adafruit_MQTT(server, port, user, pass),
|
||||||
client(client)
|
client(client)
|
||||||
{}
|
{}
|
||||||
@ -50,8 +50,8 @@ class Adafruit_MQTT_Client : public Adafruit_MQTT {
|
|||||||
bool connectServer();
|
bool connectServer();
|
||||||
bool disconnectServer();
|
bool disconnectServer();
|
||||||
bool connected();
|
bool connected();
|
||||||
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout);
|
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout);
|
||||||
bool sendPacket(uint8_t *buffer, uint8_t len);
|
bool sendPacket(uint8_t *buffer, uint16_t len);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Client* client;
|
Client* client;
|
||||||
|
@ -42,14 +42,14 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
Adafruit_MQTT_FONA(Adafruit_FONA *f, const char *server, uint16_t port,
|
||||||
const char *user, const char *pass):
|
const char *user="", const char *pass=""):
|
||||||
Adafruit_MQTT(server, port, user, pass),
|
Adafruit_MQTT(server, port, user, pass),
|
||||||
fona(f)
|
fona(f)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
bool connectServer() {
|
bool connectServer() {
|
||||||
char server[40];
|
char server[40];
|
||||||
strncpy_P(server, servername, 40);
|
strncpy(server, servername, 40);
|
||||||
#ifdef ADAFRUIT_SLEEPYDOG_H
|
#ifdef ADAFRUIT_SLEEPYDOG_H
|
||||||
Watchdog.reset();
|
Watchdog.reset();
|
||||||
#endif
|
#endif
|
||||||
@ -68,7 +68,7 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
|||||||
return fona->TCPconnected();
|
return fona->TCPconnected();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t readPacket(uint8_t *buffer, uint8_t maxlen, int16_t timeout) {
|
uint16_t readPacket(uint8_t *buffer, uint16_t maxlen, int16_t timeout) {
|
||||||
uint8_t *buffp = buffer;
|
uint8_t *buffp = buffer;
|
||||||
DEBUG_PRINTLN(F("Reading data.."));
|
DEBUG_PRINTLN(F("Reading data.."));
|
||||||
|
|
||||||
@ -117,13 +117,13 @@ class Adafruit_MQTT_FONA : public Adafruit_MQTT {
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sendPacket(uint8_t *buffer, uint8_t len) {
|
bool sendPacket(uint8_t *buffer, uint16_t len) {
|
||||||
DEBUG_PRINTLN(F("Writing packet"));
|
DEBUG_PRINTLN(F("Writing packet"));
|
||||||
if (fona->TCPconnected()) {
|
if (fona->TCPconnected()) {
|
||||||
boolean ret = fona->TCPsend((char *)buffer, len);
|
boolean ret = fona->TCPsend((char *)buffer, len);
|
||||||
//DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
//DEBUG_PRINT(F("sendPacket returned: ")); DEBUG_PRINTLN(ret);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
DEBUG_PRINTLN("Failed to send packet.")
|
DEBUG_PRINTLN("Failed to send packet.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
# Adafruit MQTT Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_MQTT_Library.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_MQTT_Library)
|
# Adafruit MQTT Library [![Build Status](https://travis-ci.org/adafruit/Adafruit_MQTT_Library.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_MQTT_Library)
|
||||||
|
|
||||||
Arduino library for MQTT support, including access to Adafruit IO. Works with
|
Arduino library for MQTT support, including access to Adafruit IO. Works with
|
||||||
the Adafruit CC3000, FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
|
the Adafruit FONA, Arduino Yun, ESP8266 Arduino platforms, and anything that supports
|
||||||
Arduino's Client interface (like Ethernet shield).
|
Arduino's Client interface (like Ethernet shield).
|
||||||
|
|
||||||
See included examples for how to use the library to access an MQTT service to
|
See included examples for how to use the library to access an MQTT service to
|
||||||
@ -11,10 +11,7 @@ spec but is intended to support enough for QoS 0 and 1 publishing.
|
|||||||
Depends on the following other libraries depending on the target platform:
|
Depends on the following other libraries depending on the target platform:
|
||||||
|
|
||||||
- [Adafruit SleepyDog](https://github.com/adafruit/Adafruit_SleepyDog), watchdog
|
- [Adafruit SleepyDog](https://github.com/adafruit/Adafruit_SleepyDog), watchdog
|
||||||
library used by FONA and CC3000 code for reliability.
|
library used by FONA 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
|
- [Adafruit FONA](https://github.com/adafruit/Adafruit_FONA_Library), required for
|
||||||
the FONA hardware.
|
the FONA hardware.
|
||||||
|
@ -0,0 +1,122 @@
|
|||||||
|
/***************************************************
|
||||||
|
Adafruit MQTT Library ESP8266 Adafruit IO Anonymous Time Query
|
||||||
|
|
||||||
|
Must use the latest version of ESP8266 Arduino from:
|
||||||
|
https://github.com/esp8266/Arduino
|
||||||
|
|
||||||
|
Works great with Adafruit's Huzzah ESP board & Feather
|
||||||
|
----> https://www.adafruit.com/product/2471
|
||||||
|
----> https://www.adafruit.com/products/2821
|
||||||
|
|
||||||
|
Adafruit invests time and resources providing this open source code,
|
||||||
|
please support Adafruit and open-source hardware by purchasing
|
||||||
|
products from Adafruit!
|
||||||
|
|
||||||
|
Written by Adafruit Industries.
|
||||||
|
MIT license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include "Adafruit_MQTT.h"
|
||||||
|
#include "Adafruit_MQTT_Client.h"
|
||||||
|
|
||||||
|
/************************* WiFi Access Point *********************************/
|
||||||
|
#define WLAN_SSID "network"
|
||||||
|
#define WLAN_PASS "password"
|
||||||
|
|
||||||
|
/************************* Adafruit.io Setup *********************************/
|
||||||
|
#define AIO_SERVER "io.adafruit.com"
|
||||||
|
#define AIO_SERVERPORT 8883
|
||||||
|
|
||||||
|
WiFiClientSecure client;
|
||||||
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT);
|
||||||
|
|
||||||
|
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
|
||||||
|
|
||||||
|
// set timezone offset from UTC
|
||||||
|
int timeZone = -4; // UTC - 4 eastern daylight time (nyc)
|
||||||
|
int interval = 4; // trigger every X hours
|
||||||
|
|
||||||
|
int last_min = -1;
|
||||||
|
|
||||||
|
void timecallback(uint32_t current) {
|
||||||
|
// adjust to local time zone
|
||||||
|
current += (timeZone * 60 * 60);
|
||||||
|
int curr_hour = (current / 60 / 60) % 24;
|
||||||
|
int curr_min = (current / 60 ) % 60;
|
||||||
|
int curr_sec = (current) % 60;
|
||||||
|
|
||||||
|
Serial.print("Time: ");
|
||||||
|
Serial.print(curr_hour); Serial.print(':');
|
||||||
|
Serial.print(curr_min); Serial.print(':');
|
||||||
|
Serial.println(curr_sec);
|
||||||
|
|
||||||
|
// only trigger on minute change
|
||||||
|
if(curr_min != last_min) {
|
||||||
|
last_min = curr_min;
|
||||||
|
|
||||||
|
Serial.println("This will print out every minute!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
Serial.print(F("\nAdafruit IO anonymous Time Demo"));
|
||||||
|
|
||||||
|
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(F("."));
|
||||||
|
}
|
||||||
|
Serial.println(F(" WiFi connected."));
|
||||||
|
|
||||||
|
timefeed.setCallback(timecallback);
|
||||||
|
mqtt.subscribe(&timefeed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// wait 10 seconds for subscription messages
|
||||||
|
// since we have no other tasks in this example.
|
||||||
|
mqtt.processPackets(10000);
|
||||||
|
|
||||||
|
// keep the connection alive
|
||||||
|
mqtt.ping();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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!");
|
||||||
|
}
|
155
examples/adafruitio_errors_esp8266/adafruitio_errors_esp8266.ino
Normal file
155
examples/adafruitio_errors_esp8266/adafruitio_errors_esp8266.ino
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
/***************************************************
|
||||||
|
Adafruit MQTT Library ESP8266 Example
|
||||||
|
|
||||||
|
Must use ESP8266 Arduino from:
|
||||||
|
https://github.com/esp8266/Arduino
|
||||||
|
|
||||||
|
Works great with Adafruit's Huzzah ESP board & Feather
|
||||||
|
----> https://www.adafruit.com/product/2471
|
||||||
|
----> https://www.adafruit.com/products/2821
|
||||||
|
|
||||||
|
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.
|
||||||
|
Error examples by Todd Treece for Adafruit Industries.
|
||||||
|
MIT license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#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 AIO_SERVER "io.adafruit.com"
|
||||||
|
#define AIO_SERVERPORT 1883 // 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!) ******************/
|
||||||
|
|
||||||
|
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||||
|
WiFiClient client;
|
||||||
|
// or... use WiFiFlientSecure 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, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
|
/****************************** Feeds ***************************************/
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
|
||||||
|
// Setup a feed called 'onoff' for subscribing to changes.
|
||||||
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
|
|
||||||
|
/*************************** Error Reporting *********************************/
|
||||||
|
|
||||||
|
Adafruit_MQTT_Subscribe errors = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/errors");
|
||||||
|
Adafruit_MQTT_Subscribe throttle = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/throttle");
|
||||||
|
|
||||||
|
/*************************** 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 setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
Serial.println(F("Adafruit MQTT 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());
|
||||||
|
|
||||||
|
// Setup MQTT subscription for onoff feed
|
||||||
|
mqtt.subscribe(&onoffbutton);
|
||||||
|
|
||||||
|
// Setup MQTT subscriptions for throttle & error messages
|
||||||
|
mqtt.subscribe(&throttle);
|
||||||
|
mqtt.subscribe(&errors);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// this is our 'wait for incoming subscription packets' busy subloop
|
||||||
|
// try to spend your time here
|
||||||
|
Adafruit_MQTT_Subscribe *subscription;
|
||||||
|
while ((subscription = mqtt.readSubscription(5000))) {
|
||||||
|
if (subscription == &onoffbutton) {
|
||||||
|
Serial.print(F("Got onoff: "));
|
||||||
|
Serial.println((char *)onoffbutton.lastread);
|
||||||
|
} else if(subscription == &errors) {
|
||||||
|
Serial.print(F("ERROR: "));
|
||||||
|
Serial.println((char *)errors.lastread);
|
||||||
|
} else if(subscription == &throttle) {
|
||||||
|
Serial.println((char *)throttle.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!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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!");
|
||||||
|
}
|
161
examples/adafruitio_secure_esp8266/adafruitio_secure_esp8266.ino
Normal file
161
examples/adafruitio_secure_esp8266/adafruitio_secure_esp8266.ino
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
/***************************************************
|
||||||
|
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
|
||||||
|
|
||||||
|
Must use the latest version of ESP8266 Arduino from:
|
||||||
|
https://github.com/esp8266/Arduino
|
||||||
|
|
||||||
|
Works great with Adafruit's Huzzah ESP board & Feather
|
||||||
|
----> https://www.adafruit.com/product/2471
|
||||||
|
----> https://www.adafruit.com/products/2821
|
||||||
|
|
||||||
|
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.
|
||||||
|
SSL/TLS additions by Todd Treece for Adafruit Industries.
|
||||||
|
MIT license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#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 AIO_SERVER "io.adafruit.com"
|
||||||
|
#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!) ******************/
|
||||||
|
|
||||||
|
// 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 SHA1 fingerprint. Current fingerprint can be verified via:
|
||||||
|
// echo | openssl s_client -connect io.adafruit.com:443 |& openssl x509 -fingerprint -noout
|
||||||
|
#define AIO_SSL_FINGERPRINT "77 00 54 2D DA E7 D8 03 27 31 23 99 EB 27 DB CB A5 4C 57 18"
|
||||||
|
|
||||||
|
/****************************** 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 ************************************/
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
|
// check the fingerprint of io.adafruit.com's SSL cert
|
||||||
|
verifyFingerprint();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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() {
|
||||||
|
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!");
|
||||||
|
}
|
122
examples/adafruitio_time_esp8266/adafruitio_time_esp8266.ino
Normal file
122
examples/adafruitio_time_esp8266/adafruitio_time_esp8266.ino
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/***************************************************
|
||||||
|
Adafruit MQTT Library ESP8266 Adafruit IO SSL/TLS example
|
||||||
|
|
||||||
|
Must use the latest version of ESP8266 Arduino from:
|
||||||
|
https://github.com/esp8266/Arduino
|
||||||
|
|
||||||
|
Works great with Adafruit's Huzzah ESP board & Feather
|
||||||
|
----> https://www.adafruit.com/product/2471
|
||||||
|
----> https://www.adafruit.com/products/2821
|
||||||
|
|
||||||
|
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.
|
||||||
|
Time additions by Todd Treece for Adafruit Industries.
|
||||||
|
MIT license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include "Adafruit_MQTT.h"
|
||||||
|
#include "Adafruit_MQTT_Client.h"
|
||||||
|
|
||||||
|
/************************* WiFi Access Point *********************************/
|
||||||
|
#define WLAN_SSID "network"
|
||||||
|
#define WLAN_PASS "password"
|
||||||
|
|
||||||
|
/************************* Adafruit.io Setup *********************************/
|
||||||
|
#define AIO_SERVER "io.adafruit.com"
|
||||||
|
#define AIO_SERVERPORT 8883
|
||||||
|
#define AIO_USERNAME "user"
|
||||||
|
#define AIO_KEY "key"
|
||||||
|
|
||||||
|
WiFiClientSecure client;
|
||||||
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
|
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
|
||||||
|
|
||||||
|
// set timezone offset from UTC
|
||||||
|
int timeZone = -4; // UTC - 4 eastern daylight time (nyc)
|
||||||
|
int interval = 4; // trigger every X hours
|
||||||
|
int hour = 0; // current hour
|
||||||
|
|
||||||
|
void timecallback(uint32_t current) {
|
||||||
|
|
||||||
|
// stash previous hour
|
||||||
|
int previous = hour;
|
||||||
|
|
||||||
|
// adjust to local time zone
|
||||||
|
current += (timeZone * 60 * 60);
|
||||||
|
|
||||||
|
// calculate current hour
|
||||||
|
hour = (current / 60 / 60) % 24;
|
||||||
|
|
||||||
|
// only trigger on interval
|
||||||
|
if((hour != previous) && (hour % interval) == 0) {
|
||||||
|
Serial.println("Run your code here");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
Serial.print(F("Adafruit IO Time Demo"));
|
||||||
|
|
||||||
|
WiFi.begin(WLAN_SSID, WLAN_PASS);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(F("."));
|
||||||
|
}
|
||||||
|
Serial.println(F(" WiFi connected."));
|
||||||
|
|
||||||
|
timefeed.setCallback(timecallback);
|
||||||
|
mqtt.subscribe(&timefeed);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// wait 10 seconds for subscription messages
|
||||||
|
// since we have no other tasks in this example.
|
||||||
|
mqtt.processPackets(10000);
|
||||||
|
|
||||||
|
// keep the connection alive
|
||||||
|
mqtt.ping();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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!");
|
||||||
|
}
|
@ -43,23 +43,14 @@ WiFiClient client;
|
|||||||
// or... use WiFiFlientSecure for SSL
|
// or... use WiFiFlientSecure for SSL
|
||||||
//WiFiClientSecure client;
|
//WiFiClientSecure client;
|
||||||
|
|
||||||
// Store the MQTT server, username, and password in flash memory.
|
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
/****************************** Feeds ***************************************/
|
/****************************** Feeds ***************************************/
|
||||||
|
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
|
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider");
|
||||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
|
||||||
const char SLIDER_FEED[] PROGMEM = AIO_USERNAME "/feeds/slider";
|
|
||||||
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, SLIDER_FEED);
|
|
||||||
|
|
||||||
/*************************** Sketch Code ************************************/
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
|
@ -40,20 +40,14 @@ WiFiClient client;
|
|||||||
// or... use WiFiFlientSecure for SSL
|
// or... use WiFiFlientSecure for SSL
|
||||||
//WiFiClientSecure client;
|
//WiFiClientSecure client;
|
||||||
|
|
||||||
// Store the MQTT server, username, and password in flash memory.
|
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = ARB_SERVER;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = ARB_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = ARB_PW;
|
|
||||||
|
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, ARB_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
Adafruit_MQTT_Client mqtt(&client, ARB_SERVER, ARB_SERVERPORT, ARB_USERNAME, ARB_PW);
|
||||||
|
|
||||||
/****************************** Feeds ***************************************/
|
/****************************** Feeds ***************************************/
|
||||||
|
|
||||||
// Setup a feed called 'arb_packet' for publishing.
|
// Setup a feed called 'arb_packet' for publishing.
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
const char ARB_FEED[] PROGMEM = "/feeds/arb_packet";
|
#define ARB_FEED "/feeds/arb_packet"
|
||||||
Adafruit_MQTT_Publish ap = Adafruit_MQTT_Publish(&mqtt, ARB_FEED);
|
Adafruit_MQTT_Publish ap = Adafruit_MQTT_Publish(&mqtt, ARB_FEED);
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,131 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,155 +0,0 @@
|
|||||||
/***************************************************
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Store the MQTT server, username, and password in flash memory.
|
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
// Setup the CC3000 MQTT class by passing in the CC3000 class and MQTT server and login details.
|
|
||||||
Adafruit_MQTT_CC3000 mqtt(&cc3000, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
|
||||||
|
|
||||||
// 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>
|
|
||||||
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
|
|
||||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
|
||||||
|
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
|
||||||
const char ONOFF_FEED[] PROGMEM = 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!");
|
|
||||||
}
|
|
@ -38,25 +38,17 @@ WiFiClient client;
|
|||||||
// or... use WiFiFlientSecure for SSL
|
// or... use WiFiFlientSecure for SSL
|
||||||
//WiFiClientSecure client;
|
//WiFiClientSecure client;
|
||||||
|
|
||||||
// Store the MQTT server, username, and password in flash memory.
|
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
/****************************** Feeds ***************************************/
|
/****************************** Feeds ***************************************/
|
||||||
|
|
||||||
// Setup a feed called 'photocell' for publishing.
|
// Setup a feed called 'photocell' for publishing.
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
|
||||||
|
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
// Setup a feed called 'onoff' for subscribing to changes.
|
||||||
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
|
||||||
|
|
||||||
/*************************** Sketch Code ************************************/
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
|
185
examples/mqtt_esp8266_callback/mqtt_esp8266_callback.ino
Normal file
185
examples/mqtt_esp8266_callback/mqtt_esp8266_callback.ino
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
/***************************************************
|
||||||
|
Adafruit MQTT Library ESP8266 Example
|
||||||
|
|
||||||
|
Must use ESP8266 Arduino from:
|
||||||
|
https://github.com/esp8266/Arduino
|
||||||
|
|
||||||
|
Works great with Adafruit's Huzzah ESP board:
|
||||||
|
----> https://www.adafruit.com/product/2471
|
||||||
|
|
||||||
|
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.
|
||||||
|
MIT license, all text above must be included in any redistribution
|
||||||
|
****************************************************/
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include "Adafruit_MQTT.h"
|
||||||
|
#include "Adafruit_MQTT_Client.h"
|
||||||
|
|
||||||
|
/************************* WiFi Access Point *********************************/
|
||||||
|
|
||||||
|
#define WLAN_SSID "network"
|
||||||
|
#define WLAN_PASS "password"
|
||||||
|
|
||||||
|
/************************* Adafruit.io Setup *********************************/
|
||||||
|
|
||||||
|
#define AIO_SERVER "io.adafruit.com"
|
||||||
|
#define AIO_SERVERPORT 1883
|
||||||
|
#define AIO_USERNAME "user"
|
||||||
|
#define AIO_KEY "key"
|
||||||
|
|
||||||
|
/************ Global State (you don't need to change this!) ******************/
|
||||||
|
|
||||||
|
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
|
||||||
|
WiFiClient 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_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
|
/****************************** Feeds ***************************************/
|
||||||
|
|
||||||
|
// Setup a feed called 'time' for subscribing to current time
|
||||||
|
Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, "time/seconds");
|
||||||
|
|
||||||
|
// Setup a feed called 'slider' for subscribing to changes on the slider
|
||||||
|
Adafruit_MQTT_Subscribe slider = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/slider", MQTT_QOS_1);
|
||||||
|
|
||||||
|
// Setup a feed called 'onoff' for subscribing to changes to the button
|
||||||
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff", MQTT_QOS_1);
|
||||||
|
|
||||||
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
|
int sec;
|
||||||
|
int min;
|
||||||
|
int hour;
|
||||||
|
|
||||||
|
int timeZone = -4; // utc-4 eastern daylight time (nyc)
|
||||||
|
|
||||||
|
void timecallback(uint32_t current) {
|
||||||
|
|
||||||
|
// adjust to local time zone
|
||||||
|
current += (timeZone * 60 * 60);
|
||||||
|
|
||||||
|
// calculate current time
|
||||||
|
sec = current % 60;
|
||||||
|
current /= 60;
|
||||||
|
min = current % 60;
|
||||||
|
current /= 60;
|
||||||
|
hour = current % 24;
|
||||||
|
|
||||||
|
// print hour
|
||||||
|
if(hour == 0 || hour == 12)
|
||||||
|
Serial.print("12");
|
||||||
|
if(hour < 12)
|
||||||
|
Serial.print(hour);
|
||||||
|
else
|
||||||
|
Serial.print(hour - 12);
|
||||||
|
|
||||||
|
// print mins
|
||||||
|
Serial.print(":");
|
||||||
|
if(min < 10) Serial.print("0");
|
||||||
|
Serial.print(min);
|
||||||
|
|
||||||
|
// print seconds
|
||||||
|
Serial.print(":");
|
||||||
|
if(sec < 10) Serial.print("0");
|
||||||
|
Serial.print(sec);
|
||||||
|
|
||||||
|
if(hour < 12)
|
||||||
|
Serial.println(" am");
|
||||||
|
else
|
||||||
|
Serial.println(" pm");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void slidercallback(double x) {
|
||||||
|
Serial.print("Hey we're in a slider callback, the slider value is: ");
|
||||||
|
Serial.println(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onoffcallback(char *data, uint16_t len) {
|
||||||
|
Serial.print("Hey we're in a onoff callback, the button value is: ");
|
||||||
|
Serial.println(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
|
Serial.println(F("Adafruit MQTT 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());
|
||||||
|
|
||||||
|
timefeed.setCallback(timecallback);
|
||||||
|
slider.setCallback(slidercallback);
|
||||||
|
onoffbutton.setCallback(onoffcallback);
|
||||||
|
|
||||||
|
// Setup MQTT subscription for time feed.
|
||||||
|
mqtt.subscribe(&timefeed);
|
||||||
|
mqtt.subscribe(&slider);
|
||||||
|
mqtt.subscribe(&onoffbutton);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// this is our 'wait for incoming subscription packets and callback em' busy subloop
|
||||||
|
// try to spend your time here:
|
||||||
|
mqtt.processPackets(10000);
|
||||||
|
|
||||||
|
// ping the server to keep the mqtt connection alive
|
||||||
|
// NOT required if you are publishing once every KEEPALIVE seconds
|
||||||
|
|
||||||
|
if(! mqtt.ping()) {
|
||||||
|
mqtt.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 10 seconds...");
|
||||||
|
mqtt.disconnect();
|
||||||
|
delay(10000); // wait 10 seconds
|
||||||
|
retries--;
|
||||||
|
if (retries == 0) {
|
||||||
|
// basically die and wait for WDT to reset me
|
||||||
|
while (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Serial.println("MQTT Connected!");
|
||||||
|
}
|
@ -41,17 +41,7 @@ byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
|||||||
//Set up the ethernet client
|
//Set up the ethernet client
|
||||||
EthernetClient client;
|
EthernetClient client;
|
||||||
|
|
||||||
// Store the MQTT server, client ID, username, and password in flash memory.
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
|
|
||||||
// was compiled (so this should be unique across multiple devices for a user,
|
|
||||||
// alternatively you can manually set this to a GUID or other random value).
|
|
||||||
const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
|
|
||||||
|
|
||||||
// You don't need to change anything below this line!
|
// You don't need to change anything below this line!
|
||||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||||
@ -61,12 +51,10 @@ Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, M
|
|||||||
|
|
||||||
// Setup a feed called 'photocell' for publishing.
|
// Setup a feed called 'photocell' for publishing.
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
|
||||||
|
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
// Setup a feed called 'onoff' for subscribing to changes.
|
||||||
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
|
||||||
|
|
||||||
/*************************** Sketch Code ************************************/
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
|
@ -25,8 +25,9 @@
|
|||||||
|
|
||||||
/*************************** FONA Pins ***********************************/
|
/*************************** FONA Pins ***********************************/
|
||||||
|
|
||||||
#define FONA_RX 2
|
// Default pins for Feather 32u4 FONA
|
||||||
#define FONA_TX 3
|
#define FONA_RX 9
|
||||||
|
#define FONA_TX 8
|
||||||
#define FONA_RST 4
|
#define FONA_RST 4
|
||||||
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
|
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
|
||||||
|
|
||||||
@ -52,14 +53,8 @@ Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
|
|||||||
|
|
||||||
/************ Global State (you don't need to change this!) ******************/
|
/************ Global State (you don't need to change this!) ******************/
|
||||||
|
|
||||||
// Store the MQTT server, username, and password in flash memory.
|
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.
|
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.
|
||||||
Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
// You don't need to change anything below this line!
|
// You don't need to change anything below this line!
|
||||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||||
@ -72,12 +67,10 @@ boolean FONAconnect(const __FlashStringHelper *apn, const __FlashStringHelper *u
|
|||||||
|
|
||||||
// Setup a feed called 'photocell' for publishing.
|
// Setup a feed called 'photocell' for publishing.
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
|
||||||
|
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
// Setup a feed called 'onoff' for subscribing to changes.
|
||||||
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
|
||||||
|
|
||||||
/*************************** Sketch Code ************************************/
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
@ -124,17 +117,6 @@ void loop() {
|
|||||||
// function definition further below.
|
// function definition further below.
|
||||||
MQTT_connect();
|
MQTT_connect();
|
||||||
|
|
||||||
Watchdog.reset();
|
|
||||||
|
|
||||||
// this is our 'wait for incoming subscription packets' busy subloop
|
|
||||||
Adafruit_MQTT_Subscribe *subscription;
|
|
||||||
while ((subscription = mqtt.readSubscription(5000))) {
|
|
||||||
if (subscription == &onoffbutton) {
|
|
||||||
Serial.print(F("Got: "));
|
|
||||||
Serial.println((char *)onoffbutton.lastread);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Watchdog.reset();
|
Watchdog.reset();
|
||||||
// Now we can publish stuff!
|
// Now we can publish stuff!
|
||||||
Serial.print(F("\nSending photocell val "));
|
Serial.print(F("\nSending photocell val "));
|
||||||
@ -148,6 +130,16 @@ void loop() {
|
|||||||
txfailures = 0;
|
txfailures = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Watchdog.reset();
|
||||||
|
// this is our 'wait for incoming subscription packets' busy subloop
|
||||||
|
Adafruit_MQTT_Subscribe *subscription;
|
||||||
|
while ((subscription = mqtt.readSubscription(5000))) {
|
||||||
|
if (subscription == &onoffbutton) {
|
||||||
|
Serial.print(F("Got: "));
|
||||||
|
Serial.println((char *)onoffbutton.lastread);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ping the server to keep the mqtt connection alive, only needed if we're not publishing
|
// ping the server to keep the mqtt connection alive, only needed if we're not publishing
|
||||||
//if(! mqtt.ping()) {
|
//if(! mqtt.ping()) {
|
||||||
// Serial.println(F("MQTT Ping failed."));
|
// Serial.println(F("MQTT Ping failed."));
|
||||||
|
@ -11,15 +11,13 @@
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include "Adafruit_MQTT.h"
|
#include "Adafruit_MQTT.h"
|
||||||
#include "Adafruit_MQTT_Client.h"
|
#include "Adafruit_MQTT_Client.h"
|
||||||
#include <Adafruit_WINC1500.h>
|
#include <WiFi101.h>
|
||||||
|
|
||||||
|
|
||||||
/************************* WiFI Setup *****************************/
|
/************************* WiFI Setup *****************************/
|
||||||
#define WINC_CS 8
|
#define WINC_CS 8
|
||||||
#define WINC_IRQ 7
|
#define WINC_IRQ 7
|
||||||
#define WINC_RST 4
|
#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 ssid[] = "yournetwork"; // your network SSID (name)
|
||||||
char pass[] = "yourpassword"; // your network password (use for WPA, or use as key for WEP)
|
char pass[] = "yourpassword"; // your network password (use for WPA, or use as key for WEP)
|
||||||
@ -31,25 +29,15 @@ int status = WL_IDLE_STATUS;
|
|||||||
|
|
||||||
#define AIO_SERVER "io.adafruit.com"
|
#define AIO_SERVER "io.adafruit.com"
|
||||||
#define AIO_SERVERPORT 1883
|
#define AIO_SERVERPORT 1883
|
||||||
#define AIO_USERNAME "adafruit2"
|
#define AIO_USERNAME "adafruitiousername"
|
||||||
#define AIO_KEY "3e148db54389a8b7c3efa9ddd2cd388317bcbf58"
|
#define AIO_KEY "adafruitiokey"
|
||||||
|
|
||||||
/************ Global State (you don't need to change this!) ******************/
|
/************ Global State (you don't need to change this!) ******************/
|
||||||
|
|
||||||
//Set up the wifi client
|
//Set up the wifi client
|
||||||
Adafruit_WINC1500Client client;
|
WiFiClient client;
|
||||||
|
|
||||||
// Store the MQTT server, client ID, username, and password in flash memory.
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
|
|
||||||
// was compiled (so this should be unique across multiple devices for a user,
|
|
||||||
// alternatively you can manually set this to a GUID or other random value).
|
|
||||||
const char MQTT_CLIENTID[] PROGMEM = __TIME__ AIO_USERNAME;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);
|
|
||||||
|
|
||||||
// You don't need to change anything below this line!
|
// You don't need to change anything below this line!
|
||||||
#define halt(s) { Serial.println(F( s )); while(1); }
|
#define halt(s) { Serial.println(F( s )); while(1); }
|
||||||
@ -58,18 +46,18 @@ Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, M
|
|||||||
|
|
||||||
// Setup a feed called 'photocell' for publishing.
|
// Setup a feed called 'photocell' for publishing.
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
|
||||||
|
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
// Setup a feed called 'onoff' for subscribing to changes.
|
||||||
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
|
||||||
|
|
||||||
/*************************** Sketch Code ************************************/
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
#define LEDPIN 13
|
#define LEDPIN 13
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
|
||||||
|
|
||||||
while (!Serial);
|
while (!Serial);
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
@ -18,11 +18,10 @@
|
|||||||
****************************************************/
|
****************************************************/
|
||||||
#include <Bridge.h>
|
#include <Bridge.h>
|
||||||
#include <Console.h>
|
#include <Console.h>
|
||||||
#include <YunClient.h>
|
#include <BridgeClient.h>
|
||||||
#include "Adafruit_MQTT.h"
|
#include "Adafruit_MQTT.h"
|
||||||
#include "Adafruit_MQTT_Client.h"
|
#include "Adafruit_MQTT_Client.h"
|
||||||
|
|
||||||
|
|
||||||
/************************* Adafruit.io Setup *********************************/
|
/************************* Adafruit.io Setup *********************************/
|
||||||
|
|
||||||
#define AIO_SERVER "io.adafruit.com"
|
#define AIO_SERVER "io.adafruit.com"
|
||||||
@ -30,30 +29,23 @@
|
|||||||
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
|
||||||
#define AIO_KEY "...your AIO key..."
|
#define AIO_KEY "...your AIO key..."
|
||||||
|
|
||||||
|
|
||||||
/************ Global State (you don't need to change this!) ******************/
|
/************ Global State (you don't need to change this!) ******************/
|
||||||
|
|
||||||
// Create a YunClient instance to communicate using the Yun's brighe & Linux OS.
|
// Create a BridgeClient instance to communicate using the Yun's bridge & Linux OS.
|
||||||
YunClient client;
|
BridgeClient client;
|
||||||
|
|
||||||
// Store the MQTT server, username, and password in flash memory.
|
|
||||||
// This is required for using the Adafruit MQTT library.
|
|
||||||
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
|
|
||||||
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
|
|
||||||
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
|
|
||||||
|
|
||||||
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
|
||||||
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
|
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
|
||||||
|
|
||||||
/****************************** Feeds ***************************************/
|
/****************************** Feeds ***************************************/
|
||||||
|
|
||||||
// Setup a feed called 'photocell' for publishing.
|
// Setup a feed called 'photocell' for publishing.
|
||||||
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
|
||||||
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/photocell";
|
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
|
||||||
Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
|
|
||||||
|
|
||||||
// Setup a feed called 'onoff' for subscribing to changes.
|
// Setup a feed called 'onoff' for subscribing to changes.
|
||||||
const char ONOFF_FEED[] PROGMEM = AIO_USERNAME "/feeds/onoff";
|
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
|
||||||
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, ONOFF_FEED);
|
|
||||||
|
|
||||||
/*************************** Sketch Code ************************************/
|
/*************************** Sketch Code ************************************/
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ Adafruit_MQTT_CC3000 KEYWORD1
|
|||||||
Adafruit_MQTT_FONA KEYWORD1
|
Adafruit_MQTT_FONA KEYWORD1
|
||||||
Adafruit_MQTT_Client KEYWORD1
|
Adafruit_MQTT_Client KEYWORD1
|
||||||
Adafruit_MQTT_Publish KEYWORD1
|
Adafruit_MQTT_Publish KEYWORD1
|
||||||
Adafruit_MQTT_Subscribe KEYWORD1
|
Adafruit_MQTT_Subscribe KEYWORD1
|
||||||
connect KEYWORD2
|
connect KEYWORD2
|
||||||
connectErrorString KEYWORD2
|
connectErrorString KEYWORD2
|
||||||
disconnect KEYWORD2
|
disconnect KEYWORD2
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
name=Adafruit MQTT Library
|
name=Adafruit MQTT Library
|
||||||
version=0.13.0
|
version=0.20.1
|
||||||
author=Adafruit
|
author=Adafruit
|
||||||
maintainer=Adafruit <info@adafruit.com>
|
maintainer=Adafruit <info@adafruit.com>
|
||||||
sentence=MQTT library that supports the CC3000, FONA, ESP8266, Yun, and generic Arduino Client hardware.
|
sentence=MQTT library that supports the FONA, ESP8266, Yun, and generic Arduino Client hardware.
|
||||||
paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
|
paragraph=Simple MQTT library that supports the bare minimum to publish and subscribe to topics.
|
||||||
category=Communication
|
category=Communication
|
||||||
url=https://github.com/adafruit/Adafruit_MQTT_Library
|
url=https://github.com/adafruit/Adafruit_MQTT_Library
|
||||||
|
Loading…
Reference in New Issue
Block a user