📜 ⬆️ ⬇️

LED control over the Internet using RaspberryPi

In our time, the usual thing connected to the Internet, is beginning to become commonplace. Even the concept of “Internet of Things” (Internet of Things, IoT) has appeared. But how to approach this peculiar Internet to a beginner is not always clear, because although there are many articles on this topic, everyone would like the article to be easy to play and to understand something very close and pleasant for the reader.

Therefore, we will try to connect the simplest to the Internet - an LED taken from a broken optical mouse. We will turn on and off the LED through a page on the Internet, control the frequency of its flickering.

For our experiment will require




We collect the scheme


The scheme of connecting the LED to the Internet will be as follows:
')


So, we will collect all the details on the table and solder our simple scheme. From the mouse we get a LED and a resistor of 220 Ohms. The resistor is needed to limit the current, it is small and can barely be seen at the end of the wire. To connect wires with GPIO, I use the connectors I have asked for when repairing computers.



Preparing software


On the server and the “raspberry” should be node.js and npm (node ​​package manager). We install according to this instruction .

In order for node.js to work with GPIO, you need to install the rpi-gpio module. And to connect to the RPi server, you will need a socket.io-client . Install packages sudo npm install rpi-gpio and sudo npm install socket.io-client .

Script for node.js on Raspberry Pi:
 var socket = require('socket.io-client')('vpssite.domain:3141'); var gpio = require('rpi-gpio'); var fs = require('fs'); // hack due to error fs.exists = require('path').exists; var async = require('async'); // pin GPIO4 var pin = 7; // current fps var piFps = 0; var currentValue = false; var timemanager; var set0 = function(err, results) { if (err) console.log(err); console.log('Pin ' + pin + ' closed'); directWrite(pin, false, function() { clearTimeout(timemanager); }); }; var blinkexec = function() { delayedWrite(7, true, function() { delayedWrite(7, false, blinkexec) }); }; var blink = function(err, results) { if (err) console.log(err); console.log('Pin ' + pin + ' blinking'); blinkexec(); }; function directWrite(pin, value, callback) { return gpio.write(pin, value, callback); } function delayedWrite(pin, value, callback) { var delay = Math.round(1000 / piFps / 2); clearTimeout(timemanager); timemanager = setTimeout(function() { directWrite(pin, value, callback); }, delay); } var release = function() { console.log('Writes complete, pause then unexport pins'); setTimeout(function() { gpio.destroy(function() { console.log('Closed pins, now exit'); return process.exit(0); }); }, 500); }; socket.on('connect', function() { console.log('connected'); socket.on('setfps', function(data) { console.log(data); if (data.fps > 0) { piFps = data.fps; gpio.setup(pin, gpio.DIR_OUT, blink); } else { gpio.setup(pin, gpio.DIR_OUT, set0); } }); socket.on('disconnect', function() { console.log('disconnect'); release(); }); }); 



The following code on the server creates a server on port 3141 and takes the setfps command and sends it on to all browsers and Raspberry Pi.

Script for node.js on vps
 var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); var allClients = []; var count = 0; var fpsPi = 0; server.listen(3141); server.on('error', function(e) { if (e.code == 'EADDRINUSE') { console.log('Address in use, exit...'); process.exit(); } }); app.get('/', function (req, res) { res.send('Fps is ' + fpsPi); console.log('requested / - show ' + fpsPi); }); function getDate() { var datas = new Date(); return datas.getHours() + ':' + datas.getMinutes() + ':' + datas.getSeconds() + '.' + datas.getMilliseconds() } function consolelog(msg) { console.log(getDate() + ' ' + msg); } io.on('connection', function (socket) { io.emit('setfps', {fps: fpsPi}); // browser subscribes to listen to the station socket.on('subscribe', function(data) { socket.json.emit('subscribed', {fps: fpsPi}); io.emit('setfps', {fps: fpsPi}); }); // disconnect on error. Browser will reconnect socket.on('error', function() { socket.disconnect(); }); // client disconnects socket.on('disconnect', function() { consolelog('Client disconnected.'); }); // save to RPi and browsers new fps socket.on('setfps', function(fps){ fpsPi = fps; consolelog('setfps ' + fpsPi); io.emit('setfps', {fps: fpsPi}); }); }); 



Start work


Run the scripts as follows. On RaspberryPi - with root rights: sudo nodejs led.js , and on the server - just add to crontab * * * * * cd /var/www/apps; node server.js >> cron_rpi.log * * * * * cd /var/www/apps; node server.js >> cron_rpi.log , it's not so beautiful, but always the server will be running and we can forget about it.

On the page of your site we include the code for the jquery-ui and socket.io slider from our server. When receiving a signal from the node.js server, the slider sets the current fps value and vice versa - when moving the slider, we send a new fps value to the server, which the server then sends to all clients in browsers and Raspberry Pi.

Code posted on the site page
 <script src="http://vpssite.domain:3141/socket.io/socket.io.js"></script> <script> function setSlided(val) { if (val == 0) { $('#freq').html(''); $('#freq2').hide(); } else { $('#freq').html(val); $('#freq2').show(); } } $(function() { //   nodejs   var socket = io.connect('http://bk-it.ru:3141'); socket.emit('subscribe'); //      fps socket.on('subscribed', function(data) { if (!data.error) { setSlided(data.fps); $("#loading").hide(); $("#slider").slider({ min: 0, max: 20, value: data.fps, slide: function(event, ui) { $("#slider").slider({ disabled: true }); socket.emit('setfps', ui.value); //setSlided(ui.value); } }); } }); socket.on('setfps', function(data) { if (!data.error) { setSlided(data.fps); $("#slider").slider({value: data.fps}); $("#slider").slider({ disabled: false}); } }); }); </script> <h1>,   </h1> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <div class="panel panel-info"> <div class="panel-heading">   </div> <div class="panel-body"> <div class="row"> <div class="col-md-6"> <div style="height:40px;position:relative;top:12px;"> <div id="slider"><div id="loading" style="position:relative;top:-4px;"> <img src="/img/loading.gif" alt=""></div></div> </div> </div> <div class="col-md-6"> <div class="well-sm">  <b id="freq"></b> <span id="freq2" style="display:none;">/</span></div> </div> </div> </div> <div class="panel-footer">     ,   ? </div> </div> 



Work results


And here is a video describing what happened:



That's all, the LED is connected to the Internet. Now from any device with the Internet, we can manage its flicker. The scope for further creativity is great. It is possible to connect a module-relay to the terminals and turn on or off any equipment with a button on your website. The modules for the Raspberry Pi are sold a lot, so the flight of fancy is almost unlimited, especially if there is an enthusiastic eight-year fan of electronics.

Useful links that helped the experiment


Documentation Socket.io ;
Node.js documentation ;
Rpi-gpio package ;
Documentation for jquery-ui slider ;
GPIO pinout on Raspberry Pi ;
The respawn module to automatically start the nodejs application (but I chose crontab) .

You can take the code from rpi-led on github.

Source: https://habr.com/ru/post/242409/


All Articles