Created
July 11, 2017 03:11
-
-
Save rodolfofadino/bb2d800c586343f1a2f1e4a579216076 to your computer and use it in GitHub Desktop.
Example Raspberry PI and Pingdom API
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 request = require('request'); | |
var gpio = require('rpi-gpio'); | |
gpio.setup(7, gpio.DIR_OUT,off7); | |
gpio.setup(11, gpio.DIR_OUT, off11); | |
var options = { | |
url: 'https://api.pingdom.com/api/2.0/checks', | |
headers: { | |
'User-Agent': 'rasp', | |
'App-Key':'xxxxxxxxxxxxxx', | |
'Authorization':'Basic xxxxxxxxxxxxxxxx' | |
} | |
}; | |
function errorLed(){ | |
gpio.setup(7, gpio.DIR_OUT, offGreen); | |
gpio.setup(11, gpio.DIR_OUT, onRed); | |
} | |
function okLed(){ | |
gpio.setup(7, gpio.DIR_OUT, onGreen); | |
gpio.setup(11, gpio.DIR_OUT, offRed); | |
} | |
function onGreen() { | |
gpio.write(7, true, function(err) { | |
if (err) throw err; | |
}); | |
} | |
function offGreen() { | |
gpio.write(7, false, function(err) { | |
if (err) throw err; | |
}); | |
} | |
function onRed() { | |
gpio.write(11, true, function(err) { | |
if (err) throw err; | |
}); | |
} | |
function offRed() { | |
gpio.write(11, false, function(err) { | |
if (err) throw err; | |
}); | |
} | |
function run(error, response, body) { | |
if (!error && response.statusCode == 200) { | |
var info = JSON.parse(body); | |
var total = 0; | |
for (var i = 0, len = info.checks.length; i < len; i++) { | |
var item =info.checks[i]; | |
if( item.status != 'up'){ | |
console.log('Erro'+ item.name); | |
total++; | |
} | |
} | |
if(total>0) | |
{ | |
errorLed(); | |
} | |
else | |
{ | |
okLed(); | |
} | |
} | |
} | |
setInterval(function(){ | |
request(options, run); | |
} | |
, 10000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment