forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·71 lines (51 loc) · 1.74 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
'use strict';
const Motion = require('motion').Stream,
Camera = require('./lib/camera'),
AdafruitIO = require('adafruit-io');
AdafruitIO.CLI.getConfigPath().then(path => {
require('dotenv').config({silent: true, path: path});
const env = process.env;
const aio = new AdafruitIO(
env.AIO_CLIENT_USER,
env.AIO_CLIENT_KEY,
{
host: env.AIO_CLIENT_HOST || 'io.adafruit.com',
port: env.AIO_CLIENT_PORT || 80,
success: ready,
failure: error
}
);
function ready() {
const feed = aio.Feeds.writable(env.AIO_CAMFEED || 'picam'),
camera_options = {};
let camera = false;
if(env.CAM_VFLIP === 'true' || env.CAM_VFLIP === '1')
camera_options.vflip = true;
if(env.CAM_HFLIP === 'true' || env.CAM_HFLIP === '1')
camera_options.hflip = true;
if(env.MOTION === 'true' || env.MOTION === '1') {
const motion = new Motion({
threshold: env.MOTION_THRESH ? parseInt(env.MOTION_THRESH) : 0x15,
minChange: env.MOTION_MINCHANGE ? parseInt(env.MOTION_MINCHANGE) : 10,
minimumMotion: env.MOTION_MINSECONDS ? parseInt(env.MOTION_MINSECONDS) : 1,
prebuffer: 0,
postbuffer: 0
});
camera_options.timelapse = env.CAM_RATE ? parseInt(env.CAM_RATE) * 1000 : 2000;
camera = new Camera(camera_options);
camera.pipe(motion);
return motion.on('data', (img) => {
feed.write(img.toString('base64'));
});
}
camera_options.timelapse = env.CAM_RATE ? parseInt(env.CAM_RATE) * 1000 : 5000;
camera = new Camera(camera_options);
return camera.on('data', (img) => {
feed.write(img.toString('base64'));
});
}
function error(err) {
console.error(err);
process.exit(1);
}
});