Last active
February 6, 2017 16:31
-
-
Save meaku/742965652e639266db068bc21122ad0a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"use strict"; | |
//hardware modules | |
const Raspi = require("raspi-io"); | |
const five = require("johnny-five"); | |
const board = new five.Board({ | |
io: new Raspi() | |
}); | |
//networking modules | |
const http = require("http"); | |
const sockjs = require("sockjs"); | |
const wsClients = []; | |
let currentTemperature = null; | |
function wsBroadcast(msg) { | |
wsClients.forEach(client => { | |
client.write(JSON.stringify(msg)); | |
}); | |
} | |
const wsServer = sockjs.createServer(); | |
wsServer.on("connection", (conn) => { | |
wsClients.push(conn); | |
}); | |
//gets called on HTTP requests | |
function onRequest(req, res) { | |
res.end(JSON.stringify({ | |
temperature: currentTemperature | |
})); | |
} | |
const httpServer = http.createServer(onRequest).listen(8080); | |
wsServer.installHandlers(httpServer, { prefix: "/stream" }); | |
board.on("ready", function() { | |
const multi = new five.Multi({ | |
controller: "MPL3115A2", | |
elevation: 492 | |
}); | |
multi.on("change", function() { | |
currentTemperature = this.temperature.celsius; | |
console.log(this.temperature.celsius, this.barometer.pressure, this.altimeter.meters); | |
wsBroadcast({ | |
temperature: this.temperature.celsius, | |
ts: Date.now() | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment