Skip to content

Instantly share code, notes, and snippets.

@jkuip
Created February 14, 2017 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkuip/cb1e4e5b24b118407e8db19ff52304d7 to your computer and use it in GitHub Desktop.
Save jkuip/cb1e4e5b24b118407e8db19ff52304d7 to your computer and use it in GitHub Desktop.
Data Visualization using Particle, Firebase and P5js: https://www.youtube.com/watch?v=puBpA1LmGCs
///////////////////////////////////
// PARTICLE / WIRING CODE //
///////////////////////////////////
int photoResistor = A0;
int power = A5;
int lightValue;
void setup() {
pinMode(photoResistor,INPUT);
pinMode(power,OUTPUT);
digitalWrite(power,HIGH);
}
void loop() {
lightValue = analogRead(photoResistor);
Particle.publish("light", String(lightValue));
delay(2000);
}
///////////////////////////////////
// P5.js code //
///////////////////////////////////
// Initialize Firebase
var config = {
apiKey: "YOURAPIKEY",
authDomain: "YOURAUTHDOMAIN",
databaseURL: "YOURDATABASEURL",
storageBucket: "YOURSTORAGEBUCKET",
messagingSenderId: "YOURMESSAGINGSENDERID"
};
firebase.initializeApp(config);
var database = firebase.database();
var counter = 0;
function setup() {
createCanvas(1440, 900);
background(255);
}
function draw() {
// do nothing
}
database.ref('light').limitToLast(80).on('child_added', function(snapshot)
{
var data = snapshot.val();
var normalizedLight = map(data.light, 0, 800, 0, 255);
print(normalizedLight);
// draw a graph
noStroke();
fill(normalizedLight);
rect(counter * 5, 0, 5, height);
fill(255);
ellipse(width/2, height/2, 200, 200);
fill(normalizedLight);
textSize(72);
textAlign(CENTER);
text(data.light, width/2, height/2);
if(counter < 160)
{
counter++;
} else {
background(255);
counter = 0;
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment