Hi guys, I’ve been trying to figure this out too. I think I’m close with these two files using ‘pinctrl’ instead of ‘/sys/class/gpio/’ but I can’t figure out what I’m missing.
Based on the console log it looks like there’s an issue in my node_helper file, possible with the socketNotification functions because I’m not seeing any of my test messages.
And based on the log files there may be something missing from the package.json file because sometimes I get this error “Error: Cannot find module ‘node helper’ MODULE_NOT_FOUND” but other times I don’t. And I tried running this fix but couldn’t get it to work.
[https://forum.magicmirror.builders/user/jorickjuh](link url)
Other than that I’m not seeing any other errors and module does appear to be working but it’s just not processing any button presses. I’ll keep trying but I’ll post my progress so far on here in case someone can see what I’m missing.
test file:
Module.register("MMM-PinToggleTest", {
defaults: {
pin: 19,
modulesToToggle: ["clock", "calendar"],
interval: 1000
},
//consol.log("MMM-PinToggleTest is starting...");
start: function() {
Log.info("MMM-PinToggleTest is starting, sending CONFIG...");
consol.log("MMM-PinToggleTest is starting, sending CONFIG...");
this.sendSocketNotification("CONFIG", {
interval: this.config.interval,
pin: this.config.pin
});
},
start: function() {
console.log("MMM-PinToggleTest started");
this.previousPinState = null;
// Request initial pin check from the helper
this.sendSocketNotification("CONFIG", this.config);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "PIN_STATE") {
const pinState = payload;
console.log(`Received pin state: ${pinState}`); // Log received state
// Toggle modules based on pin state
if (pinState !== this.previousPinState) {
this.previousPinState = pinState;
this.toggleModules(pinState === 1);
}
}
},
toggleModules: function(show) {
console.log(`Toggling modules to ${show ? "show" : "hide"}`);
this.config.modulesToToggle.forEach(moduleName => {
const modules = MM.getModules().withClass(moduleName);
modules.forEach(module => {
if (show) {
module.show(1000); // Show module with fade-in effect
} else {
module.hide(1000); // Hide module with fade-out effect
}
});
});
},
getDom: function() {
const wrapper = document.createElement("div");
wrapper.innerHTML = "<small>MMM-PinToggleTest</small>";
return wrapper;
}
});
node_helper:
const NodeHelper = require("node_helper");
const exec = require("child_process").exec;
//const rpio = require("rpio");
//const Gpio = require("onoff").Gpio;
module.exports = NodeHelper.create({
start: function() {
console.log("node_helper for MMM-PinToggleTest has started."); // Confirm helper initialization
this.previousPinState = null;
this.lastChangeTime = 0;
this.debounceDelay = 200;
},
socketNotificationReceived: function(notification, payload) {
if (notification === "CONFIG") {
console.log("Received CONFIG in node_helper:", payload); // Log receipt of configuration
this.config = payload;
this.schedulePinCheck();
}
},
schedulePinCheck: function() {
console.log(`Setting up pin check every ${this.config.interval} ms`); // Confirmation log
setInterval(() => {
this.checkPinState();
}, this.config.interval);
},
checkPinState: function() {
const pinNumber = this.config.pin;
exec(`pinctrl ${pinNumber}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error reading GPIO pin ${pinNumber}: ${stderr}`);
return;
}
const pinState = parseInt(stdout.trim(), 10);
const now = Date.now();
console.log(`Current pin state for GPIO ${pinNumber}: ${pinState}`);
if (pinState !== this.previousPinState && (now - this.lastChangeTime > this.debounceDelay)) {
this.previousPinState = pinState;
this.lastChangeTime = now;
console.log(`Pin state changed to ${pinState} at ${new Date(now).toISOString()}`);
this.sendSocketNotification("PIN_STATE", pinState);
// Small delay to avoid multiple triggers
//setTimeout(() => {}, 100);
}
});
}
});