-
Notifications
You must be signed in to change notification settings - Fork 0
/
beagleboneAlertSystem.js
73 lines (58 loc) · 1.88 KB
/
beagleboneAlertSystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var https = require('https');
var bones = require('bonescript');
var send_stream = function(device, apikey, data){
var stream = '{"protocol":"v2","device":"'+device+'","at":"now","data":'+data+'}'
var options = {
host: 'api.carriots.com',
path: '/streams',
method: 'POST',
headers: {'User-Agent': 'NodeJS-Carriots',
'Content-Type' : 'application/vnd.carriots.api.v2+json;q=7',
'Accept': 'application/vnd.carriots.api.v2+json;q=7',
'Content-Length': stream.length,
'Carriots.apikey': apikey}
};
var req = https.request(options, function(response) {
console.log('STATUS: ' + response.statusCode);
//console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.write(stream);
req.end();
};
var main = function(){
var gpiopin = "P9_33";
var on = "ON"
var off = "OFF"
var device = 'your device id_developer here'
var apikey = 'your apikey here'
var data = ''
var lights = off
var new_lights = lights
setInterval(read_analog_pin, 500);
function read_analog_pin() {
bones.analogRead(gpiopin, get_status);
function get_status(x) {
var n_status = x.value * 1000
if (n_status > 2.0) {
new_lights = off
} else {
new_lights = on
}
console.log(new_lights);
if (lights != new_lights){
lights = new_lights
data = '{"light": "'+ new_lights +'"}'
console.log(data);
send_stream(device, apikey, data);
}
}
}
}
main()