Created
July 16, 2016 00:29
-
-
Save tothandras/bd83ca41eee9c960e525129815598826 to your computer and use it in GitHub Desktop.
Tessel 2 - Enlightener
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
'use strict' | |
const ambientlib = require('ambient-attx4') | |
function ambientSensor (port) { | |
return new Promise((resolve, reject) => { | |
const ambient = ambientlib.use(port) | |
ambient.on('ready', () => { | |
console.log('connected to ambient module') | |
resolve(ambient) | |
}) | |
ambient.on('error', (err) => { | |
console.error('error connecting to ambient module', err) | |
reject(err) | |
}) | |
}) | |
} | |
module.exports = ambientSensor |
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
'use strict' | |
const tessel = require('tessel') | |
const http = require('http') | |
const ws = require('nodejs-websocket') | |
const ambientSensor = require('./ambient')(tessel.port.B) | |
const sockets = new Set() | |
const measurements = [] | |
ambientSensor.then((sensor) => { | |
setInterval(() => { | |
sensor.getLightLevel((err, lightlevel) => { | |
if (err) { | |
return console.error(err) | |
} | |
sockets.forEach((socket) => socket.send(JSON.stringify({ lightlevel }))) | |
}) | |
}, 100) | |
}) | |
const server = http.createServer((request, response) => { | |
response.writeHead(200, { 'Content-Type': 'text/html' }) | |
response.end(` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Andras's Tessel</title> | |
<style> | |
html { | |
background: #000; | |
} | |
html, body { | |
height: 100%; | |
margin: 0; | |
} | |
body { | |
display: flex; | |
align-items: center; | |
} | |
#current { | |
text-align: center; | |
width: 100%; | |
margin: 0; | |
padding: 0; | |
} | |
</style> | |
</head> | |
<body> | |
<h1 id="current"></h1> | |
<script> | |
var ws = new WebSocket('ws://' + location.host + ':81') | |
var body = document.body | |
var current = document.getElementById('current') | |
ws.onmessage = function onmessage (message) { | |
try { | |
var data = JSON.parse(message.data) | |
var lightlevel = data.lightlevel | |
current.innerHTML = lightlevel | |
body.style.background = 'rgba(255, 255, 255,' + lightlevel * 10 + ')' | |
} catch (ex) { | |
console.error(ex) | |
} | |
} | |
</script> | |
</body> | |
</html> | |
`) | |
}) | |
const wsServer = ws.createServer((socket) => { | |
sockets.add(socket) | |
socket.on('close', () => sockets.delete(socket)) | |
}) | |
server.listen(80, () => { | |
console.log('Server is listening on http://192.168.1.101/') | |
}) | |
wsServer.listen(81) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment