2022-11-27 01:56:04 +00:00
|
|
|
// npm module for Raspberri Pi GPIO
|
2022-11-27 00:45:18 +00:00
|
|
|
var gpio = require('rpi-gpio');
|
2022-11-27 01:56:04 +00:00
|
|
|
// Module for importing environment variables
|
2022-11-27 01:50:20 +00:00
|
|
|
var dotenv = require('dotenv').config();
|
2022-11-27 02:40:51 +00:00
|
|
|
// Module for working with files
|
|
|
|
var fs = require('fs');
|
2022-11-27 01:50:20 +00:00
|
|
|
|
2022-11-27 03:06:42 +00:00
|
|
|
// Write the current env vars to console
|
|
|
|
console.log('Environment variables:');
|
|
|
|
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`);
|
|
|
|
|
2022-11-27 01:56:04 +00:00
|
|
|
// Set up GPIO 4 (pysical pin 7) as output, then call cycleAuger()
|
2022-11-27 00:49:07 +00:00
|
|
|
gpio.setup(7, gpio.DIR_OUT, cycleAuger);
|
2022-11-27 00:33:33 +00:00
|
|
|
|
2022-11-27 03:14:18 +00:00
|
|
|
// TODO Move functions to another file for cleaner code
|
|
|
|
// TODO Add logic for other sensors
|
|
|
|
|
2022-11-27 01:56:04 +00:00
|
|
|
// Turns the auger on (Pin 7 high)
|
2022-11-27 00:44:31 +00:00
|
|
|
function augerOn(err) {
|
|
|
|
if (err) throw err;
|
|
|
|
gpio.write(7, true, function(err) {
|
|
|
|
if (err) throw err;
|
2022-11-27 03:02:25 +00:00
|
|
|
if (process.env.DEBUG == "true") console.log('Auger turned on.');
|
2022-11-27 00:44:31 +00:00
|
|
|
});
|
2022-11-27 00:33:33 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 01:56:04 +00:00
|
|
|
// Turns the auger off (pin 7 low)
|
2022-11-27 00:44:31 +00:00
|
|
|
function augerOff(err) {
|
|
|
|
if (err) throw err;
|
|
|
|
gpio.write(7, false, function(err) {
|
|
|
|
if (err) throw err;
|
2022-11-27 03:02:25 +00:00
|
|
|
if (process.env.DEBUG == "true") console.log('Auger turned off.');
|
2022-11-27 00:44:31 +00:00
|
|
|
});
|
2022-11-27 00:33:33 +00:00
|
|
|
}
|
2022-11-27 00:44:31 +00:00
|
|
|
|
2022-11-27 01:56:04 +00:00
|
|
|
// Sleeps for any given milliseconds, call with await
|
2022-11-27 00:33:33 +00:00
|
|
|
function sleep(ms) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(resolve, ms);
|
2022-11-27 03:02:25 +00:00
|
|
|
if (process.env.DEBUG == "true") console.log(`Slept for ${ms}ms`);
|
2022-11-27 00:33:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-27 01:56:04 +00:00
|
|
|
// Main function, turns the auger on, sleeps for the time given in environment variables, then turns the auger off, sleeps, repeats.
|
2022-11-27 00:49:07 +00:00
|
|
|
async function cycleAuger(err) {
|
|
|
|
if (err) throw err;
|
2022-11-27 02:40:51 +00:00
|
|
|
// Check for pause file existing, then sleep for preset time, then run the function again.
|
|
|
|
if (fs.existsSync('./pause')) {
|
|
|
|
console.log('Paused...');
|
|
|
|
await sleep(process.env.PAUSETIME);
|
|
|
|
cycleAuger();
|
2022-11-27 02:55:23 +00:00
|
|
|
return;
|
2022-11-27 02:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for reload file existing, then reload environment variables, then delete the file.
|
|
|
|
if (fs.existsSync('./reload')) {
|
2022-11-27 02:52:17 +00:00
|
|
|
var dotenv = require('dotenv');
|
2022-11-27 02:40:51 +00:00
|
|
|
dotenv.config({ override: true })
|
|
|
|
fs.unlink('./reload', (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Deleted reload file.');
|
|
|
|
});
|
|
|
|
console.log('Reloaded environment variables.');
|
2022-11-27 03:06:42 +00:00
|
|
|
console.log(`ONTIME=${process.env.ONTIME}\nOFFTIME=${process.env.OFFTIME}\nPAUSETIME=${process.env.PAUSETIME}\nDEBUG=${process.env.DEBUG}`);
|
2022-11-27 02:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for quit file existing, then delete it, then quit the program
|
|
|
|
if (fs.existsSync('./quit')) {
|
|
|
|
fs.unlink('./quit', (err) => {
|
|
|
|
if (err) throw err;
|
|
|
|
console.log('Removed quit file.');
|
|
|
|
});
|
|
|
|
console.log('Quitting.');
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If none of the above checks are true, cycle the auger on and off, then repeat
|
2022-11-27 00:33:33 +00:00
|
|
|
augerOn();
|
2022-11-27 01:50:20 +00:00
|
|
|
await sleep(process.env.ONTIME);
|
2022-11-27 00:33:33 +00:00
|
|
|
augerOff();
|
2022-11-27 01:50:20 +00:00
|
|
|
await sleep(process.env.OFFTIME);
|
2022-11-27 00:39:33 +00:00
|
|
|
cycleAuger();
|
2022-11-27 00:49:07 +00:00
|
|
|
}
|