Skip to content

Instantly share code, notes, and snippets.

@MrTimcakes
Created June 4, 2016 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrTimcakes/450f26f868ef454d68309f005f4b1b63 to your computer and use it in GitHub Desktop.
Save MrTimcakes/450f26f868ef454d68309f005f4b1b63 to your computer and use it in GitHub Desktop.
Javascript Espruino Script to control WS2812b LEDs
var ESP8266 = require("ESP8266"),
Wifi = require("Wifi"),
curCol = '050505',
index = '<title>Brightness</title><script src="//cdnjs.cloudflare.com/ajax/libs/jscolor/2.0.4/jscolor.js"></script><form><label>Color: <input class="jscolor {onFineChange:\'update(this)\'}" value="00FF00" name="color"><br><input type="submit"><script>var xhttp = new XMLHttpRequest();var lastReq = 0;function update(jscolor) {if(lastReq+250 < Date.now()){lastReq=Date.now();xhttp.open("GET", "?color=" + jscolor, true);xhttp.send();}}</script>',
arr = new Uint8ClampedArray(111*3);
function onPageRequest(req, res) {
var a = url.parse(req.url, true);
if(a.search !== null){
if(a.search.indexOf("color")){
curCol = a.search.split('=')[1];
updateLEDs(hexToRgb(curCol));
}
}
if(a.pathname=="/"){
res.writeHead(200, {'Content-Type': 'text/HTML'});
res.end(index.replace('value=""', 'value="' + curCol + '"'));
} else if (a.pathname=="/hello") {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("Hello World");
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end("404: Page "+a.pathname+" not found");
}
}
function hexToRgb(hex) {
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return {"r":r, "g":g, "b":b};
}
function updateLEDs(color){
for (var i=0;i<arr.length;i+=3) {
arr[i ] = color.g;
arr[i+1] = color.r;
arr[i+2] = color.b;
}
ESP8266.neopixelWrite(NodeMCU.D1, arr);
}
function onInit(){
console.log(Wifi.getIP().ip);
require("http").createServer(onPageRequest).listen(80);
updateLEDs(hexToRgb(curCol));
}
onInit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment