Last active
July 15, 2016 02:09
-
-
Save shawngrimes/fe6d824b0d903311a013d8eef243d6fb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <YunClient.h> | |
#include <Adafruit_MQTT.h> | |
#include <Adafruit_MQTT_Client.h> | |
int ledPin = 9; | |
/************************* Adafruit.io Setup *********************************/ | |
#define AIO_SERVER "io.adafruit.com" | |
#define AIO_SERVERPORT 1883 | |
#define AIO_USERNAME "username" | |
#define AIO_KEY "key" | |
#define LED_FEED_NAME "lamp" | |
////////////////////////////////// | |
//Don't change anything in here | |
// Create a YunClient instance to communicate using the Yun's brighe & Linux OS. | |
YunClient 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. | |
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD); | |
/////////////////////////////////// | |
//OK, now you can change stuff below here | |
/////////////////////////////////// | |
// Setup a feed for subscribing to changes. | |
const char MY_LED_FEED[] PROGMEM = AIO_USERNAME "/feeds/" LED_FEED_NAME; | |
Adafruit_MQTT_Subscribe myLedFeed = Adafruit_MQTT_Subscribe(&mqtt, MY_LED_FEED); | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
Bridge.begin(); | |
pinMode(ledPin,OUTPUT); //Set ledPin to OUTPUT | |
// Setup MQTT subscription for lamp feed. | |
mqtt.subscribe(&myLedFeed); | |
} | |
void loop() { | |
Serial.println("running..."); | |
// Ensure the connection to the MQTT server is alive (this will make the first | |
// connection and automatically reconnect when disconnected). | |
MQTT_connect(); | |
//***** BEGIN IMPORTANT SECTION ****** | |
// this section of code will check the lamp feed on Adafruit.io | |
Adafruit_MQTT_Subscribe *subscription; | |
while ((subscription = mqtt.readSubscription(1000))) { | |
//You might have multiple subscriptions in one sketch, | |
//this code checks to see if the subscription returning | |
//information is the myLedFeed subscription | |
if (subscription == &myLedFeed) { | |
char *value = (char *)myLedFeed.lastread; //This line gets the last value from the feed | |
Serial.print("Received: "); //Print received to the serial console | |
Serial.println(value); //Print the value that came in from the feed | |
int current = atoi(value); //this line converts whatever came in from the feed into an integer | |
if(current==1){ //this line compares the value from the feed to the number 1 | |
digitalWrite(ledPin,HIGH); //if there is a match (e.g. if the feed had the number 1 in it), then we turn the led on | |
}else{ | |
digitalWrite(ledPin,LOW); //if it was something else, we are assuming a 0, then turn the led off | |
} | |
} | |
} | |
//***** END IMPORTANT SECTION ****** | |
// ping the server to keep the mqtt connection alive | |
if (! mqtt.ping()) { | |
Serial.println(F("MQTT Ping failed.")); | |
} | |
delay(30000); //Wait 30 seconds before checking again | |
} | |
// 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