-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (83 loc) · 3.17 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
/*********************************************
This ambient module example console.logs
ambient light and sound levels and whenever a
specified light or sound level trigger is met.
*********************************************/
var fs = require('fs');
var https = require('https');
var tessel = require('tessel');
var ambientlib = require('ambient-attx4');
var ambient = ambientlib.use(tessel.port['A']);
var camera = require('camera-vc0706').use(tessel.port['B']);
var notificationLED = tessel.led[1]; // Set up an LED to notify when we're taking a picture
var my_wifi = require('./wifi-control');
var send_notify = require('./send_notify');
ambient.on('ready', function() {
console.log("System will be start...");
notificationLED.high();
// Get points of light and sound data.
setInterval(function() {
ambient.getLightLevel(function(err, ldata) {
if (err) throw err;
ambient.getSoundLevel(function(err, sdata) {
if (err) throw err;
console.log("Light level:", ldata.toFixed(8), " ", "Sound Level:", sdata.toFixed(8));
});
})
}, 500); // The readings will happen every .5 seconds unless the trigger is hit
ambient.setLightTrigger(0.5);
// Set a light level trigger
// The trigger is a float between 0 and 1
ambient.on('light-trigger', function(data) {
console.log("Our light trigger was hit:", data);
// Clear the trigger so it stops firing
ambient.clearLightTrigger();
//After 1.5 seconds reset light trigger
setTimeout(function() {
ambient.setLightTrigger(0.5);
}, 1500);
});
// Set a sound level trigger
// The trigger is a float between 0 and 1
ambient.setSoundTrigger(0.2);
ambient.on('sound-trigger', function(data) {
console.log("Something happened with sound: ", data);
// Clear it
ambient.clearSoundTrigger();
//After 1.5 seconds reset sound trigger
setTimeout(function() {
ambient.setSoundTrigger(0.2);
// Take the picture
camera.takePicture(function(err, image) {
if (err) {
console.log('error taking image', err);
} else {
notificationLED.low();
var filename = 'picture-' + Math.floor(Date.now() * 1000) + '.jpg';
console.log('Picture as', filename, '...');
// saved picture
// process.sendfile(filename, image);
// var image = my_camera.picture();
var send = new send_notify('Baby is crying...', filename, image);
send.post();
notificationLED.high();
sleep(5);
}
});
}, 1500);
});
});
ambient.on('error', function(err) {
console.log(err)
});
function sleep(sec){
sec = sec * 1000;
var start = new Date().getTime();
while (true) {
if (new Date().getTime() - start > sec) {
break;
}
}
}