Skip to content

Instantly share code, notes, and snippets.

@shawngrimes
Last active July 14, 2016 21:56
Show Gist options
  • Save shawngrimes/e2184330fb1a7f56720ad6c7c3b576cf to your computer and use it in GitHub Desktop.
Save shawngrimes/e2184330fb1a7f56720ad6c7c3b576cf to your computer and use it in GitHub Desktop.
//This is a basic program to report the value of a photoresistor to the Adafruit.io service.
//These libraries are needed to talk to the Internet and the Adafruit service
#include <YunClient.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
int photoPin=A0; //Variable to store pin of photoresistor
float photoValue=0; //Variable to store last known value of photoresistor
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "username"
#define AIO_KEY "key"
#define LIGHT_FEED_NAME "light"
//////////////////////////////////
//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 publishing values.
const char MY_LIGHT_FEED[] PROGMEM = AIO_USERNAME "/feeds/" LIGHT_FEED_NAME;
Adafruit_MQTT_Publish myLightFeed = Adafruit_MQTT_Publish(&mqtt, MY_LIGHT_FEED);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Bridge.begin();
}
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). See the MQTT_connect
// function definition further below.
MQTT_connect();
photoValue=analogRead(photoPin); //Read the value of the potentiometer pin
Serial.print("Analog Read: "); //Output to serial console
Serial.println(photoValue); //Output photoValue to serial console and add a new line at the end
myLightFeed.publish(photoValue); //Send the value to Adafruit.io
// ping the server to keep the mqtt connection alive
if (! mqtt.ping()) {
Serial.println(F("MQTT Ping failed."));
}
delay(30000); //Wait 30 seconds before looping
}
// 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