Created
February 14, 2017 11:58
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////// | |
// 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