As already stated in another post i am struggling with writing to grove relays connected to digital ports with node.js. I decided to debug the code to see what the grove package acutally does during a read or write from a digital port.
During debugging I came accross this part of SPDTRelay.js:
SPDTRelay.prototype.on = function () {
this.board.pinMode(this.board.OUTPUT)
var write = this.board.writeBytes(commands.dWrite.concat([this.pin, 1, commands.unused]))
if (write) {
return true
} else {
return false
}
}
Besides the fact that the function name “on” is a bit misleading since .on is usually used for the on condition to watch a sensor, the statement “this.board.pinMode(this.board.OUTPUT)” looks wrong to me. I believe the first paramenter for pinMode must be the pin. If I debug further into the function I end up in grovepi.js at this function:
GrovePi.prototype.pinMode = function(pin, mode) {
var isOperative = this.checkStatus()
if (!isOperative)
return false
if (mode == this.OUTPUT) {
return this.writeBytes(commands.pMode.concat([pin, 1, commands.unused]))
} else if (mode == this.INPUT) {
return this.writeBytes(commands.pMode.concat([pin, 0, commands.unused]))
} else {
this.debug(‘Unknown pin mode’)
}
}
The function expects two parameters, a pin and the mode:
GrovePi.prototype.pinMode = function(pin, mode)
but SPDTRelay.js, and most other digital sensor/actor functions you provide, only pass one parameter. The INPUT/OUTPUT parameter as the first argument.
This is why in my debugger I run into the else case “this.debug(‘Unknown pin mode’)” and no pinMode setting is done.
As alreday elaborated in my other post I’m struggling with writing to digital ports so I adapted my code to do a manual pinMode set before turning my relay on:
board.pinMode(2,board.OUTPUT)
Doing this improves my problem of not being able to write to digital ports after a reboot of my raspberryPi.
I don’t know enough yet about node.js to finally call this a Bug, but debugging the GrovePi code certainly looks like it is one. Having a quick peek into the other digital sensors/actors it seems that you have the same pinMode setting pattern all over the place. Both in the provided GrovePi/Software/NodeJS/sensors and in the npm package node-grovepi. So if this is a bug you might want to check the other sensors as well.
I’m still not sure what relevance the pinMode acutally has. In my case, setting the pinMode correctly made a big differendce tackling the problems I have.