eco-server
Version:
A data driven, UDP streamed Arduino robot for the 2014 Robot Challenge in Vienna, Austria.
39 lines (34 loc) • 1.4 kB
JavaScript
var net = require('net'),
client = require('dgram').createSocket("udp4");
// Mock sensors
var mockSensors = [
{ sensor: 'temperature', maxValue: 30, unit: 'C' },
{ sensor: 'distance', maxValue: 10, unit: 'm' },
{ sensor: 'light', maxValue: 15, unit: 'Ω' },
{ sensor: 'air_quality', maxValue: 10, unit: 'R' },
{ sensor: 'compass', maxValue: 360, unit: 'd' },
{ sensor: 'range1', maxValue: 400, unit: 'cm' },
{ sensor: 'range2', maxValue: 400, unit: 'cm' },
{ sensor: 'range3', maxValue: 400, unit: 'cm' },
{ sensor: 'range4', maxValue: 400, unit: 'cm' },
{ sensor: 'range5', maxValue: 400, unit: 'cm' },
{ sensor: 'range6', maxValue: 400, unit: 'cm' },
{ sensor: 'range7', maxValue: 400, unit: 'cm' },
{ sensor: 'range8', maxValue: 400, unit: 'cm' },
{ sensor: 'carbon', maxValue: 1000, unit: 'ppm' }
];
sendRandomData(client);
// send random streams of data
function sendRandomData(client) {
var time = Math.floor(Math.random() * 1000);
setTimeout(function() {
var sensor = mockSensors[Math.floor(Math.random() * mockSensors.length)];
sensor.value = Math.floor(Math.random() * sensor.maxValue);
sensor.date = Date.now();
var message = new Buffer(JSON.stringify(sensor, ["sensor", "value", "unit", "date"]) + "|");
client.send(message, 0, message.length, 5432, "localhost", function(err, bytes) {
//client.close();
});
sendRandomData(client);
}, time);
};