Created
November 14, 2017 15:48
-
-
Save abachman/8090f5be630fce02e603d7874cd50e0c to your computer and use it in GitHub Desktop.
working example of Adafruit IO + MQTT + Ethernet Featherwing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************** | |
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 <SPI.h> | |
// using version 0.17.0 | |
#include "Adafruit_MQTT.h" | |
#include "Adafruit_MQTT_Client.h" | |
// version 1.0.4 | |
#include <Ethernet2.h> | |
#include <EthernetClient.h> | |
#include <Dns.h> | |
#include <Dhcp.h> | |
/************************* 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 "your IO username" | |
#define AIO_KEY "your IO key" | |
/************ Global State (you don't need to change this!) ******************/ | |
//Set up the ethernet client | |
EthernetClient client; | |
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY); | |
#define halt(s) { Serial.println(F( s )); while(1); } | |
/****************************** Feeds ***************************************/ | |
// Notice MQTT topics for Adafruit IO follow the form: <username>/feeds/<feed_key> | |
Adafruit_MQTT_Publish counter = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/counter"); | |
/*************************** Sketch Code ************************************/ | |
// I'm using the Feather M0 Proto | |
#define BUILTIN_LED 13 | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(F("Adafruit MQTT demo")); | |
pinMode(BUILTIN_LED, OUTPUT); | |
// Initialise the Client | |
Serial.print(F("\nInit the Client...")); | |
Ethernet.begin(mac); | |
delay(1000); //give the ethernet a second to initialize | |
} | |
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! | |
digitalWrite(BUILTIN_LED, HIGH); | |
Serial.print(F("\nSending counter val ")); | |
Serial.print(x); | |
Serial.print(F("... ")); | |
if (! counter.publish(x++)) { | |
Serial.println(F("Failed to publish")); | |
} else { | |
Serial.println(F("OK!")); | |
} | |
// ping the server to keep the mqtt connection alive | |
if(! mqtt.ping()) { | |
mqtt.disconnect(); | |
} | |
digitalWrite(BUILTIN_LED, LOW); | |
// don't publish too fast | |
delay(4000); | |
} | |
// 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!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment