Created
February 14, 2021 10:34
-
-
Save andrewn/17f2f7dc78c06a9fc4444fa783b3c810 to your computer and use it in GitHub Desktop.
Corona Ampel-Status for Espruino and LEDs
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
var config = { | |
"ssid": "WIFI SSID", | |
"password": "WIFI PASSWORD", | |
"dataURL": "http://ampel-status.netlify.app/.netlify/functions/hello_fetch", | |
"timeoutMs": 60 * 1000, | |
"numLEDs": 3, | |
}; | |
var errors = []; | |
var currentErrorIndex = 0; | |
var wifiState = null; | |
function logError(err) { | |
errors[currentErrorIndex++ % 10] = err; | |
} | |
var state = "init" | |
var existingTimeoutId = null; | |
var colours = { | |
"green": [ 25, 0, 0 ], | |
"yellow": [ 6, 29, 0 ], | |
"red": [ 0, 25, 0 ] | |
}; | |
var statusNames = [ | |
"basic_reproduction_number", | |
"incidence_new_infections", | |
"icu_occupancy_rate" | |
]; | |
function testColours() { | |
updateLEDs( | |
[].concat(colours.green, colours.yellow, colours.green) | |
); | |
} | |
const OFF = [0, 0, 0]; | |
const WHITE = [25, 25, 25]; | |
const BLUE = [0, 0, 25]; | |
const wifi = require('Wifi'); | |
function connectToWifi() { | |
console.log('connectToWifi'); | |
updateLEDs([].concat(BLUE, OFF, OFF)); | |
state = "connectToWifi"; | |
wifi.connect(config.ssid, {password: config.password}, function(e) { | |
console.log('... starting'); | |
state = "startConnectToWifi"; | |
if (e) { | |
console.log('error during connect:',e); | |
logError(e); | |
state = "connectToWifiError"; | |
updateLEDs([].concat(OFF, OFF, BLUE)); | |
wifi.disconnect(); | |
connectToWifi(); | |
} else { | |
console.log('connected to', config.ssid); | |
state = "connected"; | |
updateLEDs([].concat(OFF, BLUE, BLUE)); | |
wifi.stopAP(); | |
//wifi.save(); | |
fetchStatus(); | |
} | |
}); | |
} | |
require("Wifi").on('disconnected', connectToWifi); | |
function logWifi() { | |
require("Wifi").getDetails(console.log); | |
} | |
function parseIndicators(statuses) { | |
console.log('parseIndicators', statuses); | |
var updates = []; | |
statusNames.forEach( | |
name => { | |
updates = updates.concat(colours[statuses[name].color]); | |
} | |
); | |
return updates; | |
} | |
var rgb = new Uint8ClampedArray([0, 0, 0, 255, 255, 255, 0, 0, 0]); | |
function updateLEDs(updates) { | |
console.log('->', updates); | |
for (var i = 0, len = rgb.length; i < len; i++) { | |
rgb[i] = updates[i]; | |
} | |
console.log('will write', rgb); | |
require("neopixel").write(D13, rgb); | |
} | |
function fetchStatus() { | |
console.log("Fetching", config.dataURL); | |
state = "fetchStatus"; | |
var body = ""; | |
require('http').get(config.dataURL, function(res) { | |
res.on('data', function(data) { | |
body += data; | |
}); | |
res.on('error', function(e) { | |
logError(e); | |
}); | |
res.on('close', function() { | |
console.log("Connection closed"); | |
console.log("body", body); | |
var data = JSON.parse(body); | |
var updates = parseIndicators(data.indicators); | |
updateLEDs(updates); | |
if (existingTimeoutId) { | |
clearTimeout(existingTimeoutId); | |
} | |
existingTimeoutId = setTimeout(fetchStatus, config.timeoutMs); | |
}); | |
}); | |
} | |
function main() { | |
connectToWifi(); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment