Created
February 25, 2017 23:13
-
-
Save ericterpstra/818c08a3cf633fc938f3f9f158df1aaa to your computer and use it in GitHub Desktop.
Lola Feeder Script
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
const RaspiCam = require('raspicam'); | |
const axios = require('axios'); | |
const schedule = require('node-schedule'); | |
const fs = require('fs'); | |
const moment = require('moment'); | |
const rfDevice = require('./RF_Thing'); | |
const tweeter = require('./tweets'); | |
// Set times to turn lamp on and off | |
const LAMP_ON_CRONTAB = "0 5,20 * * *"; | |
const LAMP_OFF_CRONTAB = "0 8,23 * * *"; | |
// Use 'node-schedule' library to turn lamp ON at specified times. | |
let lampOn = schedule.scheduleJob(LAMP_ON_CRONTAB, function() { | |
rfDevice.rfCommand(rfDevice.LAMP, 'on'); | |
}); | |
// Use 'node-schedule' library to turn lamp OFF at specified times. | |
let lampOff = schedule.scheduleJob(LAMP_OFF_CRONTAB, function() { | |
rfDevice.rfCommand(rfDevice.LAMP, 'off'); | |
}); | |
// Instantiate a Raspberry Pi camera lib instance | |
const cam = RaspiCam({ | |
mode: 'photo', | |
output: 'rpicam.jpg', | |
width: 600, | |
height: 600, | |
quality: 80, | |
timeout: 2000 | |
}); | |
// Log when a photo is taken | |
cam.on('start', function(err, ts) { | |
console.log('Photo snapped at ' + ts); | |
}); | |
// When a photo capture is complete... | |
cam.on('stop', function(err, ts) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
// Send a GET request to the Lola_Detector.py service | |
axios.get('http://127.0.0.1:5000/lola') | |
.then(function(response){ | |
let data = response.data; | |
// Check the lola probability in the result. | |
if ( response.data.lola > 0.993 ) { | |
console.log('Lola FOUND!! Activating Feeder.') | |
// Turn ON the Feeder | |
rfDevice.rfCommand(rfDevice.FEEDER, 'on'); | |
// Send a request to IFTTT Maker rule that sends me a push notification | |
axios.get('https://maker.ifttt.com/trigger/lola_feeder_triggered/with/key/<mysecrectkey>'); | |
let now = moment().format('YYYY-MM-DD_HHmm'); | |
// Copy the image to a separate folder for archival purposes. | |
fs.createReadStream('rpicam.jpg').pipe(fs.createWriteStream(`./snaps/${now}.jpg`)); | |
// Wait 60 seconds to do more stuff. | |
setTimeout(function() { | |
// Tweet the picture | |
tweeter('./rpicam.jpg'); | |
console.log('Turning off Feeder.') | |
// Turn off the Feeder | |
rfDevice.rfCommand(rfDevice.FEEDER, 'off'); | |
// Set the wait time to 1.5 hours. | |
let hours = 1000 * 60 * 60 * 1.5; | |
// Prepare the whole thing to start over in 1.5 hours. | |
camRestart(hours); | |
}, 60000); | |
} else { | |
console.log(' No Lola '); | |
// If no lola, wait a couple seconds and try again. | |
camRestart(2000); | |
// TODO: instead of constantly snapping photos every 2 seconds, use a PIR motion sensor to kick of a photo snapping session. | |
} | |
}) | |
.catch(function(err){ | |
console.log(err.message); | |
}); | |
}); | |
// Stop the camera | |
cam.on('exit', function(ts) { | |
cam.stop(); | |
}); | |
// This function kicks off the photo snap & analyze cycle after <timeout> ms | |
function camRestart(timeout) { | |
console.log('camRestart. Timeout: ' + timeout/1000 + ' seconds.'); | |
setTimeout( function() { | |
cam.start(); | |
}, timeout) | |
} | |
// Start the app when the script loads for the first time. | |
camRestart(1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment