Last active
November 6, 2015 12:43
-
-
Save Bouhnosaure/be592052db735f390f0c to your computer and use it in GitHub Desktop.
require node-restler et node-serialport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var serialport = require("serialport"); | |
var SerialPort = serialport.SerialPort; | |
var sp = new SerialPort("/dev/ttyACM0", { baudrate: 9600, parser: serialport.parsers.readline("\n\r") }, false); | |
var rest = require('restler'); | |
var callback = function (error) { | |
if ( error ) { | |
console.log('failed to open: '+error); | |
console.log('retry in 2 seconds...'); | |
setTimeout(function() { | |
sp.open(callback); | |
}, 2000); | |
} else { | |
console.log('open'); | |
sp.on('data', function(c_data) { | |
console.log('data received: ' + c_data); | |
c_data = c_data.split('|'); | |
rest.post('http://couchdb.ovh/api/mesures', {data: { ip: getIPAddress(), brightness: c_data[0], temperature: c_data[1], moisture: c_data[2] }}); | |
}); | |
} | |
}; | |
sp.open(callback); | |
sp.on('close', callback); | |
sp.on('error', callback); | |
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'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment