Skip to content

Instantly share code, notes, and snippets.

@jkeefe
Last active August 29, 2015 13:56
Show Gist options
  • Save jkeefe/9244098 to your computer and use it in GitHub Desktop.
Save jkeefe/9244098 to your computer and use it in GitHub Desktop.
/*
* The Chartbeat LED Bar
* (over Bluetooth)
*
* This is the Desktop/Processing Code
*
* For information about the hardware and other
* code for this project
* see: http://johnkeefe.net/the-charbeat-led-bar
*
* Includes example code from
* http://processing.org/reference/JSONObject_getJSONArray_.html
*
* John Keefe
* john@johnkeefe.net
* February 2014
*/
// import serial library
import processing.serial.*;
Serial port;
// set up our varialbes
JSONObject json;
int lastMillis = 0;
int LEDs_to_light = 0;
int current_people = 0;
// URL of the JSON file with the Chartbeat data we need.
// Info on the Chartbeat API:
// https://chartbeat.com/docs/api/explore/
// Enter your own API key where the xxxxxxx is and site where the example site is
String data_url = "http://api.chartbeat.com/live/quickstats/v3/?apikey=xxxxxxxx&host=www.example.com";
// what number of visitors should be the top of the led bar?
int top_of_scale = 300;
// Number of LEDs in strip
int total_LEDs = 60;
// Number of people to trigger the "alarm"
int first_alarm = 1000;
void setup() {
// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
// Also there can be several ids on each line, separated by a space
// like:
// this/is/tty.zero this/is/tty.one this/is/tty.two
// this/is/tty.three
println(Serial.list());
// Open the port that the Arduino board is connected to (in my case #6)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[6], 9600);
}
void draw() {
// check to see if 60 seconds (60000 mills) have passed
// or if it's the first time through (lastMills = 0)
if(millis()>lastMillis+60000 || lastMillis == 0)
{
lastMillis=millis(); //update the last time we did anything
// load the json from the chartbeat API using the URL above
json = loadJSONObject(data_url);
// get the "people" attribute
// nabbing it as a string because I'm not doing any math and just
// have to pass it to the Arduino as a string anyway
current_people = json.getInt("people");
// if the people is less than the scale ...
if (current_people <= top_of_scale) {
// calculate LEDs to light up
LEDs_to_light = total_LEDs * current_people / top_of_scale;
} else {
// otherwise just light all of the LEDs
LEDs_to_light = total_LEDs;
}
// write data to the screen
println("Chartbeat reports " + current_people + " ppl on site. Lighting " +
LEDs_to_light + " of " + total_LEDs + " LEDs");
// Have we crossed the alerm threshold?
if (current_people > first_alarm) {
// tell Arduino to turn 'em all a pretty purple
// using RGB 128,0,128
port.write("c128,0,128");
} else {
// otherwise just light up the number of
// total LEDs calculated
port.write("p" + LEDs_to_light);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment