Prevent gpio from toggling at power up

Hi,

I’ve a relay driven by a RPI3 to open a garage door so it’s important that it doesn’t get actuated when power is missing to the board (active LOW) or at power up. To prevent such possible incidence, I added the line dtoverlay=gpio-poweroff,gpiopin=17,active_low in /boot/config.txt.

The following test py code works:

#!/bin/bash
relayPin=17
gpio -g mode $relayPin out
gpio -g write $relayPin 0
sleep 1
gpio -g write $relayPin 1

I’m using a json test code to interface with Blynk:

var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio;
//var relayPin = new Gpio(27, 'out');

var AUTH = '****';
var blynk = new Blynk.Blynk(AUTH);

However, Blynk crushes when the button is clicked in the app with the following msg:

node index.js
OnOff mode
Connecting to: blynk-cloud.com 8441
SSL authorization…
Connected
Authorized

fs.js:786
return binding.writeBuffer(fd, buffer, offset, length, position);
                      ^

Error: EBUSY: resource busy or locked, write
at Error (native)
at Object.fs.writeSync (fs.js:786:20)
at Object.fs.writeFileSync (fs.js:1343:24)
at new Gpio (/home/pi/sketches/blink/node_modules/onoff/onoff.js:84:8)
at process (/home/pi/sketches/blink/node_modules/blynk-library/blynk-node.js:366:19)
at Blynk.onReceive (/home/pi/sketches/blink/node_modules/blynk-library/blynk.js:469:29)
at .<anonymous> (/home/pi/sketches/blink/node_modules/blynk-library/blynk.js:548:50)
at emitOne (events.js:96:13)
at emit (events.js:188:7)
at TLSSocket.<anonymous> (/home/pi/sketches/blink/node_modules/blynk-library/blynk-node.js:212:16)

However, if I comment the line mentioned above in the /boot/config.txt, Blynk works.

Am I doing some wrong (which is most likely the case) or is there a work around?
TIA

Which Engineer is responsible for active LOW relays?

1 Like

Chinese :grinning: devise

It’s been working fine for me for the last couple of years controlled by an Arduino Yun but because the latter is practically obsolete, I moved on to RPi. So, I’m basically crawling the learning curve for this device.

I guess I could buy an activate HIGH relays board, add an inverter chip or just add a simple NPN transistor but I’d like to learn why Blynk is crushing. I can imagine because the pin is been reserved at power up so, conflicting with the code.

So, at the end I removed the line added in /boot/config.txt and added a link in crontab to a py file which forces the relays GPIOs pins to HIGH @powerup.

hi Emilio. I have the same problem. Im now to programing so can you make a example step by step on how to do this.
thank you

@mat_pav Actually, I don’t remember what I had for lunch today so, remembering what I did in January, is quite a challenge for my memory :wink:

Even though this’ not Blynk related, if I was to redo it now I’d do this way [not tested and not sure if is the best way to go]

Create a py file (initRelays.py) with the following code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

#set array for relay pins
relayPins = [17,18,22,23]

#set output mode for all relay pins 
GPIO.setup(relayPins, GPIO.OUT)
 
#set relays GPIO to HIGH (off)
for x in relayPins:
	GPIO.output(x, GPIO.HIGH)

Then edit your crontan (crontab –e) and add on the last line

@reboot python /path/to your file/initRelays.py

reboot

If you’re having a problem with your Blynk code, post it and someone will definitely help you.

Good luck.

Its not working. Maybe i did somthing wrong . The code is this:

var Blynk = require(‘blynk-library’);
var Gpio = require(‘onoff’).Gpio;
var led = new Gpio(11, ‘out’);

var AUTH = ‘8cbf3576575e342cfb755bdb64877ed27’;

var blynk = new Blynk.Blynk(AUTH);

var v0 = new blynk.VirtualPin(0);

v0.on(‘write’, function(param) {

if (param[0] == ‘1’) {
led.writeSync(1);
} else {
led.writeSync(0);
}

console.log(‘Odklenjeno’, param[0]);

});

How can i check that the script initrekay.py is running. The crontab entry is below:

daemon’s notion of time and timezones.

Output of the crontab jobs (including errors) is sent through
email to the user the crontab file belongs to (unless redirected).

For example, you can run a backup of all your user accounts
at 5 a.m every week with:
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ For more information see the manual pages of crontab(5) and cron(8)

m h dom mon dow command
@reboot python /home/pi/projekti/initRelays.py

First check if the file initRelays.py is working by monitoring the output voltage of the pins when running the script manually (with a voltmeter or a LED through a limiting resistor):

python /home/pi/projekti/initRelays.py

then flip the outputs by editing the file
from

#set relays GPIO to HIGH (off)
for x in relayPins:
	GPIO.output(x, GPIO.HIGH)

To

#set relays GPIO to LOW (on)
for x in relayPins:
	GPIO.output(x, GPIO.LOW)

Run again the script

python /home/pi/projekti/initRelays.py

and monitor the voltage again. You should see a change, if you do, repeat the above steps by rebooting the board instead of running the script manually.

EDIT:
As for the jnode script, you may want to run this test https://www.w3schools.com/nodejs/nodejs_raspberrypi_blinking_led.asp

Thanks for your time. I ran a test. I connected the releyboard to 1 pin and LED to another.

When i run the HIGH script the relays turn of and the LED lights up. With the LOW script is the oposit.
But when i run the .JS file is all the same. I deleted the crontab enty for the test and run the HIGHT and LOW script manualy. After i run the .py script i run the .js script to se if there is any change. But its all the same. The relay turns on when runing the .js script. What does the crontab enry do?

One more thing. In checked my relay board and it is a LOW LEVEL TRIGGER. Do you think i should go with the board which has jumpers to regulate the TRIGGER option betwen high and low? 5v board has 0-1.5V LOW 3-5V HIGH , 12V board same low but 5-12V High. Which should i buy?

Runs python /home/pi/projekti/initRelays.py at power up/rebooting.

Which should i buy?

You may want to take a look here

Ok. But does it mean that when the initRalays is run at startup the in sets the pin mode to HIGH or LOW for every command utilizing GPIO in any js which is run after the initrelays

The purpose of initRelays script is to set the output pins to HIGH/LOW at one point of time, in this case, only at startup. Obviously, if in your change the pins outputs using any other command after booting then, you’re changing the initRelays script’s output.

What are you actually trying to do?

I would like to open the garage and the main door of the house. The problem is that if the PI reboots the relays get power-on and open the doors. I need the script to boot without triggering the relays. They should trigger only after the command from the phone app.

Then that script should do the trick. You may also look into this

Its not working. the script is running at startup but when i run the above .js script it trigers the relay at startup. I tried to put the below blynk code insid emy script but its not working. Im new to programing so i dont know what to write. To meke it work. Can you help me?

BLYNK_CONNECTED() {
Blynk.syncAll();
}

//here handlers for sync command
BLYNK_WRITE(V0) {

}

You asked me to show you how I solved my problem and so I did. I suggest you to start a new thread and post your Blynk code with a detailed explanation of what is not working.