Last active
June 19, 2016 11:36
-
-
Save xperimental/95f68c8a80f9423edd3538de8e7301e1 to your computer and use it in GitHub Desktop.
Script to show NetAtmo weather on Onion Omega OLED Display
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
{ | |
"clientId": "", | |
"clientSecret": "", | |
"username": "", | |
"password": "" | |
} |
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
var https = require('https'), | |
fs = require('fs'), | |
querystring = require('querystring'), | |
oledAddon = require("/usr/bin/oled-exp-addon"); | |
function writeLine(text) { | |
var trimmed = text.substring(0, 21); | |
if (trimmed.length < 21) { | |
trimmed += "\\n"; | |
} | |
oledAddon.write(trimmed); | |
} | |
function updateModule(module) { | |
var date = new Date(module.dashboard_data.time_utc * 1000); | |
writeLine(module.module_name + ' @ ' + date.toLocaleTimeString()); | |
writeLine(' ' + module.dashboard_data.Temperature + ' C - ' + module.dashboard_data.Humidity + ' %'); | |
} | |
function updateDevice(device) { | |
var date = new Date(device.dashboard_data.time_utc * 1000); | |
writeLine(device.module_name + ' @ ' + date.toLocaleTimeString()); | |
writeLine(' ' + device.dashboard_data.Temperature + ' C - ' + device.dashboard_data.Humidity + ' %'); | |
writeLine(' ' + device.dashboard_data.CO2 + ' ppm'); | |
for (var i = 0; i < device.modules.length; i++) { | |
updateModule(device.modules[i]); | |
} | |
} | |
function invert(callback) { | |
var invertFile = '/tmp/weather-invert'; | |
fs.exists(invertFile, function(exists) { | |
if (exists) { | |
fs.unlink(invertFile); | |
} else { | |
fs.closeSync(fs.openSync(invertFile, 'w')); | |
} | |
callback(exists ? 1 : 0); | |
}); | |
} | |
function updateDisplay(devices) { | |
oledAddon.init(); | |
invert(function(mode) { | |
oledAddon.setDisplayMode(mode); | |
}); | |
oledAddon.write('--- N e t A t m o ---'); | |
for (var i = 0; i < devices.length; i++) { | |
updateDevice(devices[i]); | |
} | |
} | |
function getStationsData(token) { | |
console.log('getting data for ' + token); | |
https.get('https://api.netatmo.com/api/getstationsdata?access_token=' + token, function(res) { | |
console.log('status: ' + res.statusCode); | |
if (res.statusCode === 200) { | |
var body = ""; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
var data = JSON.parse(body); | |
if (data) { | |
if (data.body) { | |
if (data.body.devices) { | |
updateDisplay(data.body.devices); | |
} | |
} | |
} | |
}); | |
} else { | |
console.log('error getting data: ' + res.statusCode); | |
} | |
}); | |
} | |
function authenticate(data, success) { | |
// Build the post string from an object | |
var post_data = querystring.stringify({ | |
"client_id": data.clientId, | |
"client_secret": data.clientSecret, | |
"username": data.username, | |
"password": data.password, | |
"grant_type": "password", | |
"scope": "read_station" | |
}); | |
console.log(post_data); | |
// An object of options to indicate where to post to | |
var post_options = { | |
host: 'api.netatmo.com', | |
port: '443', | |
path: '/oauth2/token', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
} | |
}; | |
console.log('authenticating ' + data.username); | |
var req = https.request(post_options, function(res) { | |
if (res.statusCode === 200) { | |
var body = ""; | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
var data = JSON.parse(body); | |
if (data) { | |
var token = data.access_token; | |
if (token) { | |
console.log('token: ' + token) | |
success(token); | |
} | |
} | |
}); | |
} else { | |
console.log('error authenticating: ' + res.statusCode); | |
} | |
}); | |
req.write(post_data); | |
req.end(); | |
} | |
fs.readFile('weather-config.json', function(err, contents) { | |
if (err) throw err; | |
var data = JSON.parse(contents); | |
console.log('username: ' + data.username); | |
authenticate(data, function(token) { | |
getStationsData(token); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment