From 57885554f9fd2c776d5a1997f996421c34392697 Mon Sep 17 00:00:00 2001 From: Alec Moore Date: Sat, 3 Oct 2015 00:59:51 -0500 Subject: [PATCH 1/6] adding ethernet example It took more than a couple minutes to get some sample code going with ethernet, so I figured it would benefit everyone if there was at least one example out there. --- examples/mqtt_ethernet/mqtt_ethernet.ino | 144 +++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 examples/mqtt_ethernet/mqtt_ethernet.ino diff --git a/examples/mqtt_ethernet/mqtt_ethernet.ino b/examples/mqtt_ethernet/mqtt_ethernet.ino new file mode 100644 index 0000000..13987fe --- /dev/null +++ b/examples/mqtt_ethernet/mqtt_ethernet.ino @@ -0,0 +1,144 @@ +/*************************************************** + Adafruit MQTT Library Ethernet Example + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + Written by Alec Moore + Derived from the code written by Limor Fried/Ladyada for Adafruit Industries. + MIT license, all text above must be included in any redistribution + ****************************************************/ +#include +#include "Adafruit_MQTT.h" +#include "Adafruit_MQTT_Client.h" + +#include +#include +#include +#include + +/************************* Ethernet Client Setup *****************************/ +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; + +//Uncomment the following, and set to a valid ip if you don't have dhcp available. +//IPAddress iotIP (192, 168, 0, 42); +//Uncomment the following, and set to your preference if you don't have automatic dns. +//IPAddress dnsIP (8, 8, 8, 8); +//If you uncommented either of the above lines, make sure to change "Ethernet.begin(mac)" to "Ethernet.begin(mac, iotIP)" or "Ethernet.begin(mac, iotIP, dnsIP)" + + +/************************* Adafruit.io Setup *********************************/ + +#define AIO_SERVER "io.adafruit.com" +#define AIO_SERVERPORT 1883 +#define AIO_USERNAME "tapiralec" +#define AIO_KEY "f2983986545d28bdad80ba706b01bc1725417aab" + +/************ Global State (you don't need to change this!) ******************/ + +//Set up the ethernet client +EthernetClient client; + +// Store the MQTT server, client ID, username, and password in flash memory. +// 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! +#define halt(s) { Serial.println(F( s )); while(1); } + + +/****************************** Feeds ***************************************/ + +// Setup a feed called 'photocell' for publishing. +// Notice MQTT paths for AIO follow the form: /feeds/ +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 Client + Serial.print(F("\nInit the Client...")); + Ethernet.begin(mac); + delay(1000); //give the ethernet a second to initialize + + + 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(); + + // Try to ping the MQTT server + if (! mqtt.ping(3) ) { + // MQTT pings failed, let's reconnect by forcing a watchdog reset. + Serial.println("Ping fail! Resetting..."); + delay(10000); + } + + // 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!")); + } +} + +// 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)); + Serial.println("Retrying MQTT connection in 5 seconds..."); + mqtt.disconnect(); + delay(5000); // wait 5 seconds + } + Serial.println("MQTT Connected!"); +} From 478e2324d58f73474e842859287541eb9a19fe34 Mon Sep 17 00:00:00 2001 From: Alec Moore Date: Mon, 5 Oct 2015 19:17:32 -0500 Subject: [PATCH 2/6] removed username/aio key --- examples/mqtt_ethernet/mqtt_ethernet.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/mqtt_ethernet/mqtt_ethernet.ino b/examples/mqtt_ethernet/mqtt_ethernet.ino index 13987fe..bbfbee1 100644 --- a/examples/mqtt_ethernet/mqtt_ethernet.ino +++ b/examples/mqtt_ethernet/mqtt_ethernet.ino @@ -32,8 +32,8 @@ byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; #define AIO_SERVER "io.adafruit.com" #define AIO_SERVERPORT 1883 -#define AIO_USERNAME "tapiralec" -#define AIO_KEY "f2983986545d28bdad80ba706b01bc1725417aab" +#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!) ******************/ From 4ccf81e9236122d3a662e4bf4a4f8f62747e1ce2 Mon Sep 17 00:00:00 2001 From: Alec Moore Date: Mon, 5 Oct 2015 19:47:54 -0500 Subject: [PATCH 3/6] fixed up and functional removed unnecessary "}", and removed getFreeRam() which was dependent on "utility/debug.h" --- examples/mqtt_ethernet/mqtt_ethernet.ino | 4 ---- 1 file changed, 4 deletions(-) diff --git a/examples/mqtt_ethernet/mqtt_ethernet.ino b/examples/mqtt_ethernet/mqtt_ethernet.ino index bbfbee1..08ff0bd 100644 --- a/examples/mqtt_ethernet/mqtt_ethernet.ino +++ b/examples/mqtt_ethernet/mqtt_ethernet.ino @@ -74,8 +74,6 @@ void setup() { Serial.println(F("Adafruit MQTT demo")); - Serial.print(F("Free RAM: ")); Serial.println(getFreeRam(), DEC); - // Initialise the Client Serial.print(F("\nInit the Client...")); Ethernet.begin(mac); @@ -83,8 +81,6 @@ void setup() { mqtt.subscribe(&onoffbutton); - - } } uint32_t x=0; From 9e6b1519972c9e933e708b7cdaab37b8476f5b30 Mon Sep 17 00:00:00 2001 From: Alec Moore Date: Mon, 5 Oct 2015 20:13:18 -0500 Subject: [PATCH 4/6] triggering travis ci --- examples/mqtt_ethernet/mqtt_ethernet.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/mqtt_ethernet/mqtt_ethernet.ino b/examples/mqtt_ethernet/mqtt_ethernet.ino index 08ff0bd..c27f8a8 100644 --- a/examples/mqtt_ethernet/mqtt_ethernet.ino +++ b/examples/mqtt_ethernet/mqtt_ethernet.ino @@ -35,6 +35,7 @@ byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; #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!) ******************/ //Set up the ethernet client From 63229fbd1264cfe2ebe9a2978ddbfeb44b7a7745 Mon Sep 17 00:00:00 2001 From: Alec Moore Date: Tue, 6 Oct 2015 00:56:32 -0500 Subject: [PATCH 5/6] skip tests on due and esp8266 --- examples/mqtt_ethernet/.due.test.skip | 0 examples/mqtt_ethernet/.esp8266.test.skip | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 examples/mqtt_ethernet/.due.test.skip create mode 100644 examples/mqtt_ethernet/.esp8266.test.skip diff --git a/examples/mqtt_ethernet/.due.test.skip b/examples/mqtt_ethernet/.due.test.skip new file mode 100644 index 0000000..e69de29 diff --git a/examples/mqtt_ethernet/.esp8266.test.skip b/examples/mqtt_ethernet/.esp8266.test.skip new file mode 100644 index 0000000..e69de29 From 15713af468fe02581a767ff645b201c5497f1489 Mon Sep 17 00:00:00 2001 From: ladyada Date: Tue, 20 Oct 2015 23:51:03 -0400 Subject: [PATCH 6/6] switch to MQTT 3.1.1 by default --- Adafruit_MQTT.cpp | 6 ++++++ Adafruit_MQTT.h | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Adafruit_MQTT.cpp b/Adafruit_MQTT.cpp index 5558a65..c319384 100644 --- a/Adafruit_MQTT.cpp +++ b/Adafruit_MQTT.cpp @@ -287,7 +287,13 @@ uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) { p+=2; // fill in packet[1] last +#if (MQTT_PROTOCOL_LEVEL == 4) + p = stringprint_P(p, PSTR("MQTT")); +#elif (MQTT_PROTOCOL_LEVEL == 3) p = stringprint_P(p, PSTR("MQIsdp")); +#else + #error "No MQTT version selected!" +#endif p[0] = MQTT_PROTOCOL_LEVEL; p++; diff --git a/Adafruit_MQTT.h b/Adafruit_MQTT.h index e18110e..9d9fc71 100644 --- a/Adafruit_MQTT.h +++ b/Adafruit_MQTT.h @@ -45,7 +45,8 @@ #define DEBUG_PRINTBUFFER(buffer, len) {} #endif -#define MQTT_PROTOCOL_LEVEL 3 +// Use 3 (MQTT 3.0) or 4 (MQTT 3.1.1) +#define MQTT_PROTOCOL_LEVEL 4 #define MQTT_CTRL_CONNECT 0x01 #define MQTT_CTRL_CONNECTACK 0x02