Skip to content

Instantly share code, notes, and snippets.

@tylor
Last active November 1, 2016 00:40
Show Gist options
  • Save tylor/a1b8969d17553c9a4ae1876f2afa415a to your computer and use it in GitHub Desktop.
Save tylor/a1b8969d17553c9a4ae1876f2afa415a to your computer and use it in GitHub Desktop.
Pumpkin Patch Code
/**
* Handy commands for setting everything up:
*
* Get Particle access token:
* $ curl https://api.particle.io/oauth/token -u particle:particle -d grant_type=password -d username={Your Particle email address} -d password={Your Particle password}
*
* Get list of devices from Particle:
* $ curl "https://api.particle.io/v1/devices?access_token={Your Particle access token}"
*
* Set pumpkin device to red:
* $ curl "https://api.particle.io/v1/devices/{Your device Particle ID}/colour" -d arg="255000000" -d access_token={Your Particle access token}
*
* Set pumpkin 0 to red:
* $ curl "http://pumpkin-patch-instance.herokuapp.com/?pumpkin=0&colour=ff0000"
*
* To set up on Heroku:
* $ heroku create pumpkin-patch-instance
* $ git push heroku master
* $ heroku config:set PARTICLE_ACCESS_TOKEN={Your Particle access token}
*/
var app = require('express')();
var request = require('request');
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
var pumpkins = [
// ... The Particle Device IDs for your pumpkins.
];
app.get('/', function(req, res, next) {
var r = parseInt(req.query.colour.substring(0,2), 16);
var g = parseInt(req.query.colour.substring(2,4), 16);
var b = parseInt(req.query.colour.substring(4,6), 16);
var decColour = new Array(3 + 1 - r.toString().length).join('0') + r
+ new Array(3 + 1 - g.toString().length).join('0') + g
+ new Array(3 + 1 - b.toString().length).join('0') + b;
request.post('https://api.particle.io/v1/devices/' + pumpkins[parseInt(req.query.pumpkin)] + '/colour', {
form: {
access_token: process.env.PARTICLE_ACCESS_TOKEN,
arg: decColour,
}
},
function(err, data) {
res.status(data.statusCode).send(data.body);
});
});
app.listen(process.env.PORT || 3000);
{
"name": "pumpkin-patch",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"express": "^4.14.0",
"request": "^2.76.0"
}
}
int RED = D0;
int GREEN = D1;
int BLUE = D2;
int countdown = 0;
void setColour(String colour) {
analogWrite(RED, 255 - colour.substring(0, 3).toInt() );
analogWrite(GREEN, 255 - colour.substring(3, 6).toInt() );
analogWrite(BLUE, 255 - colour.substring(6, 9).toInt() );
}
int cloudColour(String colour) {
countdown = 30;
setColour(colour);
Particle.publish("colour", colour);
return 1;
}
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Particle.function("colour", cloudColour);
}
void loop() {
if (countdown > 1) {
countdown -= 1;
delay(1000);
}
else {
setColour("255000000");
delay(750);
setColour("000255000");
delay(750);
setColour("000000255");
delay(750);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment