Skip to content

Instantly share code, notes, and snippets.

@abachman
Last active January 16, 2018 22:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abachman/14e73bc4f1e1aa31dddb23ab1d9c62ae to your computer and use it in GitHub Desktop.
Save abachman/14e73bc4f1e1aa31dddb23ab1d9c62ae to your computer and use it in GitHub Desktop.
Two Feed Subscriptions
#define IO_SERVER "io.adafruit.com"
#define IO_SERVERPORT 1883 // use 8883 for SSL
#define IO_USERNAME "$$$"
#define IO_KEY "$$$"
#define WIFI_SSID "$$$"
#define WIFI_PASS "$$$"
// indicator
#define PIN 15
//// WIFI + MQTT
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <adafruit_feather.h>
// WiFiClient client;
AdafruitTCP client;
Adafruit_MQTT_Client mqtt(&client, IO_SERVER, IO_SERVERPORT, IO_USERNAME, IO_USERNAME, IO_KEY);
//// AIO
Adafruit_MQTT_Subscribe feeda = Adafruit_MQTT_Subscribe(&mqtt, IO_USERNAME "/feeds/feed-a");
Adafruit_MQTT_Subscribe feedb = Adafruit_MQTT_Subscribe(&mqtt, IO_USERNAME "/feeds/feed-b");
//// ------------------------------------------- SETUP --------
void setup() {
// put your setup code here, to run once:
pinMode(PIN, OUTPUT);
Serial.begin(115200);
delay(10);
Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
Serial.println(); Serial.println();
while (!Feather.connected()) {
Serial.print("Attempting connection to ");
Serial.println(WIFI_SSID);
bool results = Feather.begin(WIFI_SSID, WIFI_PASS);
delay(2000);
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: "); Serial.println(Feather.localIP());
// Setup MQTT subscription for onoff & slider feed.
mqtt.subscribe(&feeda);
mqtt.subscribe(&feedb);
}
//// ------------------------------------------- LOOP ---------
void loop() {
Serial.println("--- Start Loop ---");
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))) {
Serial.println(" Subscribing... ");
if (subscription == &feeda) {
Serial.print(F("Got: "));
Serial.println((char *)feeda.lastread);
got_feeda();
}
if (subscription == &feedb) {
Serial.print(F("Got: "));
Serial.println((char *)feedb.lastread);
got_feedb();
}
}
// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
mqtt.disconnect();
}
blip(1);
}
//// ------------------------------------------- UTIL ---------
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
void blip(int c) {
for (int i=0; i<c; i++) {
digitalWrite(PIN, HIGH);
delay(100);
digitalWrite(PIN, LOW);
delay(100);
}
}
void got_feeda() {
blip(2);
}
void got_feedb() {
blip(4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment