Hi,
I am working on some "mailbox security system" and trying to capture the image when someone opens my mailbox. I'm using Tessel2 + USB Camera + Magnetic Switch. I followed the example in README file. It works great for the first time, but not after that.
Here is my code:
const camera = new av.Camera();
const board = new five.Board({
io: new Tessel()
});
board.on('ready', () => {
const door = new five.Switch({
pin: 'a2',
invert: true
});
door.on('open', () => {
console.log(`--> Door is open....`);
captureAndStartUpload();
});
door.on('close', () => {
console.log(`--> Door is closed`);
});
});
function captureAndUpload() {
console.log(`--> Started capturing....`);
const fileName = 'captured-via-data-event.jpg';
const filePath = path.join(__dirname, fileName);
let writable = fs.createWriteStream(filePath);
camera.capture().pipe(writable);
writable.on('finish', () => {
console.log(`--> File saved....`);
console.log(chalk.green(`--> Uploading to gDrive`));
uploadFiles.uploadToGDrive(fileName)
.then(fileData => {
console.log(chalk.green('--> File created: ', fileData.name, ' with URL: ', fileData.webViewLink));
})
.catch(err => console.log(chalk.red(err)));
});
}
Not sure if I'm doing something wrong here. May be I'm not closing the camera stream?
Please advice.
Many thanks in advance!