Skip to content

Instantly share code, notes, and snippets.

@abachman
Created November 3, 2017 21:03
Show Gist options
  • Save abachman/cb6cf3b80032815205ced74c7376cfd0 to your computer and use it in GitHub Desktop.
Save abachman/cb6cf3b80032815205ced74c7376cfd0 to your computer and use it in GitHub Desktop.
#define WIFI_SSID "your wifi SSID"
#define WIFI_PASS "your wifi password"
// this chunk of code can be copied straight from your IO account
#define IO_USERNAME "your IO username"
#define IO_KEY "your IO key"
/// Adafruit IO setup
#include <AdafruitIO_WiFi.h>
#include "config.h"
// config.h includes #define statements for each of these values:
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
/*
* setup IO feed handlers. since these are read-only in this sketch, you'll have to
* create them at https://io.adafruit.com/feeds
*/
AdafruitIO_Feed *red = io.feed("red");
AdafruitIO_Feed *green = io.feed("green");
AdafruitIO_Feed *blue = io.feed("blue");
/// neopixel setup
#include <Adafruit_NeoPixel.h>
#define NEO_DATA_PIN 14
Adafruit_NeoPixel strip = Adafruit_NeoPixel(7, NEO_DATA_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
// built in LED as a simple activity light
pinMode(0, OUTPUT);
/// Start neopixels
strip.begin();
strip.setBrightness(128); // global brightness, only need to set once
setColor(0, 0, 0); // all pixels are 'off'(0) to start with
/// Connecting Adafruit IO
// attach message handler functions
red->onMessage(handleRed);
green->onMessage(handleGreen);
blue->onMessage(handleBlue);
// connect to io.adafruit.com
Serial.println("connecting");
io.connect();
Serial.println("io.connect() called");
// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
}
void loop() {
io.run();
digitalWrite(0, HIGH);
delay(500);
digitalWrite(0, LOW);
delay(500);
}
// globals so they can be changed in IO handlers
int gr = 0, gg = 0, gb = 0;
void handleRed(AdafruitIO_Data *data) {
handleColor('r', data->toInt());
}
void handleGreen(AdafruitIO_Data *data) {
handleColor('g', data->toInt());
}
void handleBlue(AdafruitIO_Data *data) {
handleColor('b', data->toInt());
}
void handleColor(char c, int val) {
// print out the received light value
Serial.print("received <- ");
Serial.print(c);
Serial.print(' ');
Serial.println(val);
// set global color values and refresh
if (val >= 0 && val <= 255) {
switch(c) {
case 'r':
gr = val;
break;
case 'g':
gg = val;
break;
case 'b':
gb = val;
break;
}
}
refreshPixels();
}
void refreshPixels() {
setColor(gr, gg, gb);
}
// Set all pixels in the strip to the same color. Values should be in the range [0, 255]
void setColor(int r, int g, int b) {
// turn NP on
for(int i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
}
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment