I am trying to use socket.io to transfer data from a node.js script to an HTML webpage. The node.js script is executing a function (that uses a BeagleBone Black sensor) and returning a certain value. I want to transfer this value via a socket to the HTML page. I then want the HTML page to display this value. I have been able to get the webpage to log everything on Chrome's Javascript console. I don't understand though why the data is not being printed on the actual page.
// Install socket.io: terminal, goto /var/lib/cloud9 and enter: npm install socket.io
var app = require('http').createServer(handler);
var io = require('socket.io').listen(app);
var fs = require('fs');
var b = require('bonescript');
var fulldata = 0;
// count how many pulses
var pulses = 0;
// track the state of the pulse pin
var lastflowpinstate;
//time between pulses
var lastflowratetimer = 0;
// used to calculate a flow rate
var flowrate;
// Interrupt is called once a millisecond, looks for any pulses
var value = 0;
var liters = 0;
app.listen(8090);
/* socket.io options go here
io.set('log level', 2); // reduce logging - set 1 for warn, 2 for info, 3 for debug
io.set('browser client minification', true); // send minified client
io.set('browser client etag', true); // apply etag caching logic based on version number
*/
console.log('Server running on: http://' + getIPAddress() + ':8090');
var Sensor = "P8_19";
b.pinMode('P8_19', b.INPUT);
console.log(b.digitalRead('P8_19'));
function handler (req, res) {
if (req.url == "/favicon.ico"){ // handle requests for favico.ico
res.writeHead(200, {'Content-Type': 'image/x-icon'} );
res.end();
//console.log('favicon requested');
return;
}
fs.readFile('SensorHTML.html', // load html file
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function(socket) {
setInterval(function() {
//doSomething();
b.pinMode('P8_19', b.INPUT);
value = b.digitalRead('P8_19');
if (value == lastflowpinstate)
{
lastflowratetimer++;
//return; // nothing changed!
}
if (value == '1')
{
//low to high transition!
pulses++;
}
lastflowpinstate = value;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
liters = pulses / (450);
fulldata += liters;
JSON.stringify(fulldata);
//send temperature reading out to connected clients
socket.emit('water', {'water': fulldata});
}, 500);
//function doSomething() {
});
// Get server IP address on LAN
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
And the HTML file:
<html>
<head>
<!-- include bootstrap, jquery for easy div manipulation -->
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="node_modules/socket.io-client/dist/socket.io.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<h1>SmartWater</h1>
<th>Flow (liters)</th>
<script>
var socket = io.connect('http://192.168.7.2:8090/'); //enter the IP of your beaglebone and port you are using in app.js
socket.on('water', function(data) {
//$('#').html(data.full_data);
$('#water').html(data.fulldata);
console.log(data.fulldata);
});
</script>
<div id="water"></div>
</body>
</html>
Any help or insight would be appreciated.
io.on('connection', function() {...}), notio.sockets.on('connection', function () {...});setInterval()goes forever even on closed sockets. You will need to stop the interval timer when the socket is closed.JSON.stringify(fulldata). Remove that line.