Skip to content

Instantly share code, notes, and snippets.

@francisbrito
Last active August 29, 2015 14:09
Show Gist options
  • Save francisbrito/6f95eb4cfa2114d6400e to your computer and use it in GitHub Desktop.
Save francisbrito/6f95eb4cfa2114d6400e to your computer and use it in GitHub Desktop.
Simple script for uploading serial data from Arduino to a web server.
var SerialPort = require('serialport').SerialPort;
var split = require('split2'),
through = require('through2'),
request = require('request'),
JSONStream = require('JSONStream');
var SERVER_URL = 'http://localhost:3000';
var DEVICE_FILE = '/dev/ttyACM0';
var arduino = new SerialPort(DEVICE_FILE, {
baudrate: 9600
});
arduino
// Split input into comma-separated lines.
.pipe(split())
// Parse lines into JSON.
.pipe(through.obj(function (csv, enc, done) {
var measures = csv.split(',');
this.push({
measures:
measures.map(parseFloat),
uploadedAt: new Date()
});
done();
}))
// Stringify JSON.
.pipe(JSONStream.stringify())
// Post each JSON object into the server.
on('data', function (read) {
request.post({
body: read,
url: SERVER_URL
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment