From 170cbb7792bba11b70bccf64eb8529ef6dd59578 Mon Sep 17 00:00:00 2001 From: Skylar Grant Date: Sun, 18 Aug 2024 12:31:29 -0400 Subject: [PATCH] Restructure logic and test --- README.md | 20 +++---- src/custom_modules/VoidGPIO.js | 67 +++++++++++++++++++++++ src/main.js | 98 +--------------------------------- src/python/gpio_interface.py | 2 +- 4 files changed, 80 insertions(+), 107 deletions(-) create mode 100644 src/custom_modules/VoidGPIO.js diff --git a/README.md b/README.md index 259ba7f..0c24994 100644 --- a/README.md +++ b/README.md @@ -41,16 +41,16 @@ This project seeks to replace the OEM control panel on a Lennox Winslow PS40 pel # GPIO Three GPIO pins are used along with a common ground to control three relays, supplying 120VAC power to the igniter and combustion blower when appropriate, and supplying power to the auger motor in pulses. Two more GPIO pins are used to detect open/closed status of a temperature-controlled snap switch and a vacuum switch. Another temperature-controlled snap switch is used to supply power to the convection motor when the pellet stove has reached a suitable temperature. A final temperature-controlled snap switch us used to interrupt the circuit for the auger motor to shut the stove off when an over-temperature condition is met. I will be utilizing a OneWire DS18B20 temperature sensor to detect the temperature of air exiting the stove vents. -| Pi Pin | Function | Direction | Wire Color | -| ------:| -------- | --------- | ---------- | -7 | Auger Relay | Out | Blue -13 | Igniter Relay | Out | Blue/White -15 | Combustion Blower Relay | Out | Orange -16 | Proof of Fire Switch | In | Orange/White -18 | OneWire Temp Sensor | In | Brown -22 | Vacuum Switch | In | Brown/White -4 | +5VDC for Switches | N/A | Green -6 | GND for Relays | N/A | Green/White +| Board Pin | BCM Pin | Function | Direction | Wire Color | +| ------:| -------- | -------- | --------- | ---------- | +7 | 4 | Auger Relay | Out | Blue +13 | 27 | Igniter Relay | Out | Blue/White +15 | 22 | Exhaust Relay | Out | Orange +16 | 23 | Proof of Fire Switch | In | Orange/White +~~18~~ | ~~24~~ | ~~OneWire Temp Sensor~~ | ~~In~~ | ~~Brown~~ +22 | 25 | Vacuum Switch | In | Brown/White +4 | N/A | +5VDC for Switches | N/A | Green +6 | N/A | GND for Relays | N/A | Green/White # Schematics ## The Current Setup diff --git a/src/custom_modules/VoidGPIO.js b/src/custom_modules/VoidGPIO.js new file mode 100644 index 0000000..49b0164 --- /dev/null +++ b/src/custom_modules/VoidGPIO.js @@ -0,0 +1,67 @@ +const { exec } = require('child_process'); + +const pins = [ + { key: 'igniter', board: 13, bcm: 27, mode: 'OUT' }, + { key: 'exhaust', board: 15, bcm: 22, mode: 'OUT' }, + { key: 'auger', board: 7, bcm: 4, mode: 'OUT' }, + { key: 'pof', board: 16, bcm: 23, mode: 'IN' }, + { key: 'vacuum', board: 22, bcm: 25, mode: 'IN' } +]; + +pins.forEach(pin => { + console.log(`Key: ${pin.key}, Board: ${pin.board}, BCM: ${pin.bcm}, Mode: ${pin.mode}`); +}); + + +// Function to toggle a pin on-then-off +function togglePin(pin, callback) { + exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => { + if (error) { + return callback(error); + } + if (stderr) { + return callback(new Error(stderr)); + } + console.log(`Successfully toggled pin ${pin}`); + }); +} + +// Function to sense a pin state +function sensePin(pin, callback) { + exec(`python3 src/python/gpio_interface.py sense ${pin}`, (error, stdout, stderr) => { + if (error) { + console.error(`Error sensing pin ${pin}: ${error.message}`); + return callback(error); + } + if (stderr) { + console.error(`Stderr while sensing pin ${pin}: ${stderr}`); + return callback(new Error(stderr)); + } + console.log(`Pin ${pin} state: ${stdout.trim()}`); + callback(stdout.trim(), null); + }); +} + +// Toggle pins sequentially and then sense pin states +function debugInit() { + pins.forEach(async (pin) => { + switch (pin.mode) { + case 'OUT': + togglePin(pin.board, err => { + if (err) throw err; + }); + // Wait 1000ms before toggling again. + setTimeout(togglePin(pin.board, err => { + if (err) throw err; + }), 1000); + break; + case 'IN': + sensePin(pin.board, (state, err) => { + if (err) throw err; + console.log(`${pin.key} state: ${state}`); + }); + default: + break; + } + }); +} \ No newline at end of file diff --git a/src/main.js b/src/main.js index 9160dd4..f4cbf35 100644 --- a/src/main.js +++ b/src/main.js @@ -1,102 +1,8 @@ -const { exec } = require('child_process'); +// Import modules +import gpio from './custom_modules/VoidGPIO.js'; // List of pins for toggling const togglePins = [7, 13]; // List of pins for sensing const sensePins = [16, 22]; -// Function to toggle a pin on-then-off -function togglePin(pin, callback) { - exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => { - if (error) { - console.error(`Error toggling pin ${pin}: ${error.message}`); - return callback(error); - } - if (stderr) { - console.error(`Stderr while toggling pin ${pin}: ${stderr}`); - return callback(new Error(stderr)); - } - console.log(`Successfully toggled pin ${pin}`); - // Wait for 1 second before turning it off - setTimeout(() => { - exec(`python3 src/python/gpio_interface.py toggle ${pin}`, (error, stdout, stderr) => { - if (error) { - console.error(`Error turning off pin ${pin}: ${error.message}`); - return callback(error); - } - if (stderr) { - console.error(`Stderr while turning off pin ${pin}: ${stderr}`); - return callback(new Error(stderr)); - } - console.log(`Successfully turned off pin ${pin}`); - callback(null); - }); - }, 1000); // 1-second delay to ensure the pin is toggled - }); -} - -// Function to sense a pin state -function sensePin(pin, callback) { - exec(`python3 src/python/gpio_interface.py sense ${pin}`, (error, stdout, stderr) => { - if (error) { - console.error(`Error sensing pin ${pin}: ${error.message}`); - return callback(error); - } - if (stderr) { - console.error(`Stderr while sensing pin ${pin}: ${stderr}`); - return callback(new Error(stderr)); - } - console.log(`Pin ${pin} state: ${stdout.trim()}`); - callback(null); - }); -} - -// Toggle pins sequentially and then sense pin states -function controlPins() { - let index = 0; - - // Toggle all pins - function toggleNextPin() { - if (index >= togglePins.length) { - console.log('All toggle pins processed.'); - // Proceed to sensing pins - senseNextPin(); - } else { - togglePin(togglePins[index], (err) => { - if (err) { - process.exit(1); // Exit with error - } else { - index++; - toggleNextPin(); - } - }); - } - } - - // Sense all pins - function senseNextPin() { - index = 0; - function sensePinNext() { - if (index >= sensePins.length) { - console.log('All sense pins processed.'); - process.exit(0); // Exit successfully - } else { - sensePin(sensePins[index], (err) => { - if (err) { - process.exit(1); // Exit with error - } else { - index++; - sensePinNext(); - } - }); - } - } - sensePinNext(); - } - - // Start with toggling pins - toggleNextPin(); -} - -// Start the process -controlPins(); diff --git a/src/python/gpio_interface.py b/src/python/gpio_interface.py index e22e12a..01703d3 100644 --- a/src/python/gpio_interface.py +++ b/src/python/gpio_interface.py @@ -2,7 +2,7 @@ import RPi.GPIO as GPIO import sys import time -# Initialize GPIO +# Initialize GPIO using Board mode for pin numbering GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False)