socket.io-client
. Install packages sudo npm install rpi-gpio
and sudo npm install socket.io-client
. 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(); }); });
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}); }); });
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. <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>
Source: https://habr.com/ru/post/242409/
All Articles