├── rpi_neopixel_stripled.png ├── package.json ├── README.md └── neopixel_server.js /rpi_neopixel_stripled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/crazycoder1999/NeoPixelRpiWebServer/HEAD/rpi_neopixel_stripled.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "neopixelwebserver", 3 | "version": "1.0.0", 4 | "description": "A webserver built with express, to control single led on neopixel throught a rest api", 5 | "main": "neopixel_server.js", 6 | "dependencies": { 7 | "express": "^4.15.4", 8 | "rpi-ws281x-native": "^0.8.2" 9 | }, 10 | "devDependencies": {}, 11 | "scripts": { 12 | "test": "echo \"Error: no test specified\" && exit 1" 13 | }, 14 | "keywords": [ 15 | "neopixel", 16 | "raspberrypi", 17 | "pizero" 18 | ], 19 | "author": "Andrea De Gaetano", 20 | "license": "ISC" 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NeoPixelRpiWebServer 2 | This project helps anyone to control LEDS on a NeoPixel Led Strip, thanks to 2 simple HTTP Get / API: 3 | - http://ip:8083/switchAllOff? will switch off all the leds 4 | - http://ip:8083/changeLed?ledId=[0-7]&red=[0-255]&green=[0-255]&blue=[0-255]&brightness=[0-100] will change the ledId selected with the color (red,green,blue) and the brightness passed. 5 | - http://ip:8083/changeLedInRange?from=[0-7]&to=[0-7]&red=[0-255]&green=[0-255]&blue=[0-255]&brightness=[0-100] will change the ledId in the range [from,to] with the color provided. 6 | 7 | This allows easy integrations with other applications running on the RPI. 8 | 9 | The project works on Raspberry Pi Boards. 10 | It must be run as root: sudo node neopixel_server.js. 11 | The server listen on all interface: it's easy to change the code in order to run the server only on localhost; search and replace the code: 12 | 13 | --> app.listen(8083); 14 | 15 | with 16 | 17 | --> app.listen(8083,'localhost'); 18 | 19 | # Schematics 20 | Configuration tested with a Raspberry Pi Zero W 21 | ![Alt text](rpi_neopixel_stripled.png?raw=true "Schematic") 22 | 23 | -------------------------------------------------------------------------------- /neopixel_server.js: -------------------------------------------------------------------------------- 1 | var ws281x = require('rpi-ws281x-native'); 2 | var express = require('express'); 3 | const defaultBrightness = 10; 4 | const redColor = rgb2Int(255,0,0); 5 | var ledIdInit = 0; 6 | var initTimes = 4 * 8; 7 | var app = express(); 8 | 9 | var NUM_LEDS = parseInt(process.argv[2], 10) || 8, 10 | pixelData = new Uint32Array(NUM_LEDS); 11 | 12 | ws281x.init(NUM_LEDS); 13 | 14 | // ---- trap the SIGINT and reset before exit 15 | process.on('SIGINT', function () { 16 | ws281x.reset(); 17 | process.nextTick(function () { process.exit(0); }); 18 | }); 19 | 20 | app.get('/switchAllOff', function (req, res) { 21 | switchAllLedOff(); 22 | res.type("application/json"); 23 | res.send('{"status":"ok"}'); 24 | }); 25 | 26 | app.get('/changeLedInRange',function (req,res){ 27 | var from = parseInt(req.query.from); 28 | var to = parseInt(req.query.to); 29 | var red = req.query.red; 30 | var green = req.query.green; 31 | var blue = req.query.blue; 32 | var brightness = parseInt(req.query.brightness); 33 | if( 34 | ( from === undefined || from < 0 || from > NUM_LEDS-1 ) || 35 | ( to === undefined || to < 0 || to > NUM_LEDS-1 ) || 36 | ( from >= to ) || 37 | ( red === undefined || red < 0 || red > 255 ) || 38 | ( green === undefined || green < 0 || green > 255 ) || 39 | ( blue === undefined || blue < 0 || blue > 255 ) || 40 | ( brightness === undefined || brightness < 0 || brightness > 100 )) { 41 | res.send("{}"); 42 | return; 43 | } 44 | 45 | ws281x.setBrightness(brightness); 46 | for(var ledId = from;ledId<=to;ledId++) { 47 | var color = rgb2Int(red,green,blue); 48 | pixelData[ledId] = color; 49 | } 50 | ws281x.render(pixelData); 51 | res.type("application/json"); 52 | res.send('{"status": "ok"}'); 53 | return; 54 | }); 55 | 56 | app.get('/changeLed',function (req,res){ 57 | var ledId = parseInt(req.query.ledId); 58 | var red = req.query.red; 59 | var green = req.query.green; 60 | var blue = req.query.blue; 61 | var brightness = parseInt(req.query.brightness); 62 | if( ( ledId===undefined || ledId < 0 || ledId > NUM_LEDS-1 ) || 63 | ( red === undefined || red < 0 || red > 255 ) || 64 | ( green === undefined || green < 0 || green > 255 ) || 65 | ( blue === undefined || blue < 0 || blue > 255 ) || 66 | ( brightness === undefined || brightness < 0 || brightness > 100 )) { 67 | res.send("{}"); 68 | return; 69 | } 70 | 71 | var conf = new Object(); 72 | conf.ledId = ledId; 73 | conf.color = rgb2Int(red,green,blue); 74 | conf.brightness = brightness; 75 | console.log("LedId " + ledId); 76 | console.log("Color " + red + ";" + green + ";"+blue ); 77 | console.log("Brightness " + brightness); 78 | res.type("application/json"); 79 | res.send(JSON.stringify(conf)); 80 | changeLed(ledId,conf.color,conf.brightness); 81 | return; 82 | }); 83 | 84 | 85 | defBrightness(); 86 | 87 | setTimeout(startupSequence, 200); 88 | 89 | function startupSequence() { 90 | changeLed(ledIdInit,redColor,30); 91 | ledIdInit++; 92 | if ( ledIdInit > NUM_LEDS ){ 93 | ledIdInit = 0; 94 | switchAllLedOff(); 95 | } 96 | initTimes--; 97 | if(initTimes === 0) { 98 | switchAllLedOff(); 99 | changeLed(7,redColor,20); 100 | //app.listen(8083,'localhost'); 101 | app.listen(8083); 102 | } else 103 | setTimeout(startupSequence, 200); 104 | } 105 | 106 | function changeLed(ledId,color,brightness) { 107 | ws281x.setBrightness(brightness); 108 | pixelData[ledId] = color; 109 | ws281x.render(pixelData); 110 | } 111 | 112 | 113 | function switchAllLedOff() { 114 | ws281x.setBrightness(0); 115 | var noColor = rgb2Int(0,0,0); 116 | for (var i = 0; i < NUM_LEDS; i++) { 117 | pixelData[i] = noColor; 118 | } 119 | 120 | ws281x.render(pixelData); 121 | } 122 | 123 | 124 | function defBrightness(){ 125 | ws281x.setBrightness(defaultBrightness); 126 | } 127 | 128 | function rgb2Int(r, g, b) { 129 | return ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff); 130 | } 131 | --------------------------------------------------------------------------------