Skip to content

Instantly share code, notes, and snippets.

@zwigby
Last active April 19, 2016 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwigby/2d6b2b28e157d632f875 to your computer and use it in GitHub Desktop.
Save zwigby/2d6b2b28e157d632f875 to your computer and use it in GitHub Desktop.
Twitter Notification Code for Arudino and Adafruit Feather Huzzah using Structure and NeoPixel Ring
/**
* Code to light a set of Neopixels when a Losant command comes in.
*
* Copyright (c) 2016 Losant. All rights reserved.
* http://www.losant.com
*/
#include <ESP8266WiFi.h>
#include <Losant.h>
#include <Adafruit_NeoPixel.h>
// WiFi credentials.
const char* WIFI_SSID = "your wifi ssid here";
const char* WIFI_PASS = "your wifi pass here";
// Losant credentials.
const char* DEVICE_ID = "your device id here";
const char* ACCESS_KEY = "your access key here";
const char* ACCESS_SECRET = "your access secret here";
const int STRIP_PIN = 13;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, STRIP_PIN, NEO_RGBW + NEO_KHZ800);
bool runningAnimation = false;
uint8_t animationOffset = 0;
uint32_t prevTime;
uint8_t animationLoop = 0;
// Colors
uint32_t infraRed = strip.Color(73, 255, 92, 50);
uint32_t cobaltBlue = strip.Color(79, 40, 255, 50);
uint32_t defaultColor = strip.Color(0, 0, 0, 255);
uint32_t animationColor = cobaltBlue;
uint8_t defaultBrightness = 255;
// For an unsecure connection to Losant.
WiFiClientSecure wifiClient;
LosantDevice device(DEVICE_ID);
void connect() {
// Connect to Wifi.
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Connect to Losant.
Serial.println();
Serial.print("Connecting to Losant...");
device.connectSecure(wifiClient, ACCESS_KEY, ACCESS_SECRET);
while(!device.connected()) {
delay(500);
Serial.print(".");
}
Serial.println("Connected!");
}
void startAnimation() {
prevTime = millis();
runningAnimation = true;
animationLoop = 0;
}
// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
Serial.print("Command received: ");
Serial.println(command->name);
if(strcmp(command->name, "newMention") == 0) {
animationColor = cobaltBlue;
startAnimation();
}
if(strcmp(command->name, "newFollower") == 0) {
animationColor = infraRed;
startAnimation();
}
}
void pulseAnimation() {
uint8_t i;
for(i = 0; i < 12; i++) {
if((animationLoop % 2 == 0 && animationOffset > i) || (animationLoop % 2 == 1 && animationOffset < i)) {
strip.setPixelColor(i, animationColor);
} else {
strip.setPixelColor(i, defaultColor);
}
}
strip.show();
animationOffset++;
if(animationOffset == 12) {
animationOffset = 0;
animationLoop++;
delay(7200);
}
delay(100);
}
void checkAnimation() {
uint32_t t;
uint8_t i;
// check for running animation
t = millis();
if((t - prevTime) > 9600) {
for(i = 0; i < 12; i++) strip.setPixelColor(i, defaultColor);
strip.show();
prevTime = t;
runningAnimation = false;
Serial.println("Animation Complete!");
}
if(runningAnimation) {
pulseAnimation();
}
}
void setup() {
uint8_t i;
Serial.begin(115200);
delay(100);
// Register the command handler to be called when a command is received
// from the Losant platform.
device.onCommand(&handleCommand);
connect();
// Setup Neopixel Strip
strip.begin();
for(i = 0; i < 12; i++) strip.setPixelColor(i, defaultColor);
strip.setBrightness(defaultBrightness);
strip.show();
}
void loop() {
bool toReconnect = false;
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Disconnected from WiFi");
toReconnect = true;
}
if(!device.connected()) {
Serial.println("Disconnected from Losant");
Serial.println(device.mqttClient.state());
toReconnect = true;
}
if(toReconnect) {
connect();
}
device.loop();
if(runningAnimation) {
checkAnimation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment