Skip to content

Instantly share code, notes, and snippets.

@natevw
Created June 15, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natevw/004b0892c29ff0f72e1f4c716176d8da to your computer and use it in GitHub Desktop.
Save natevw/004b0892c29ff0f72e1f4c716176d8da to your computer and use it in GitHub Desktop.
A quick little script showing someone a general outline of bridging a hardware SPI device to an HTTP api via node.js. It's fairly terse, but I tried to provide a few documentation and project links for further exploration.
var SPI_DEV = "/dev/spidev0.0", // hardware will be connected to pins matching a certain Linux SPI device
HTTP_PORT = 0, // if you leave 0, a port gets randomly assigned
POLL_INTERVAL = 1e3; // equivalent to 1 second, set to `20` for 20 milliseconds
// this global variable will store the values
var most_recent_value = null;
var spiInstance = require('pi-spi').initialize(SPI_DEV); // this sets up the SPI device for use in node.js
// then we register a timeout that will get called repeatedly at an approximate interval
setInterval(function () { // this function gets called every POLL_INTERVAL milliseconds
// inside the timeout callback function, we call the SPI "instance" read method to sipmly read a 4-byte response
// this here is where you might need to do a "spiInstance.transfer" instead and write a command or something so the device knows what you want it to do.
spiInstance.read(4, function (e,d) { // this function is a "callback", and will get run when the read completes
// `e` will contain an Error object if something went wrong
// otherwise on success, `e` will be null, and `d` a buffer
if (e) console.error(e);
else most_recent_value = d.readFloatLE(0); // see https://nodejs.org/api/buffer.html#buffer_buf_readfloatle_offset_noassert
// for more complicated data structures (here we assume the SPI device just sends a single 4-byte float number whenever it is talked to) you might look into https://github.com/natevw/struct-fu or similar.
// you might also want to encapsulate the device logic into a separate module to keep the code clean and/or allow others to reuse it. see https://github.com/natevw/node-nrf for a fairly complicated example of that.
});
}, POLL_INTERVAL);
// this is a really barebones HTTP server that just "barfs out" the `most_recent_value` for anyone to see
// see https://nodejs.org/api/http.html for node.js server method documentation
require('http').createServer(function (req, res) {
// normally you might look at `req` to see what method/path/etc. it has
// (for significant amounts of HTTP processing you might use the Express library to help)
// in our case we just respond to any sort of request with a simple response.
res.writeHead(200, {'Content-Type': "text/plain"});
res.write("Most recent value is: ")
res.end( most_recent_value.toFixed(2) ); // this writes a bit more data and then finishes
}).listen(HTTP_PORT, function () {
console.log("HTTP server listening on", this.address().port);
}).on('error', function (e) {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment