[Solved] Turn on 2 relays with V0 virtual button (raspberry-Node.js)

Hi guys, I created a sketch for raspberry with node.js, in this sketch I turn 2 relays via the blynk app widget button. my sketch works, only when i turn it on I turn on the relays automatically. I would like that when I start my sketch the relays remain off, how can I do it? thank you

var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio;

var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxx';

var blynk = new Blynk.Blynk(AUTH);


Relay1 = new Gpio(25, 'out');
Relay2 = new Gpio(24, 'out');

var v0 = new blynk.VirtualPin(0);

v0.on('write', function(param) {
   
  if (param == 1)  { 
   Relay1.writeSync(1); 
   Relay2.writeSync(1);
} else {
  Relay1.writeSync(0);
  Relay2.writeSync(0);
}

console.log('v0:', param[0]);

});

Are the relays active LOW or active HIGH?

Are the relays active HIGH

Are you 100% sure as GPIO 24 /25 should default as low and therefore your script should have the relays off when the Pi starts.

Does the virtual button work correctly once the relays have started from the ON position?

Is your button PUSH or SWITCH?

the button is in PUSH mode, when I click turn on, when I release turn off

PUSH is overrated, can’t understand why you would use that for a relay.

Dunno why they are ON at reboot but I guess a virtualWrite() in your script will turn them off.

I use it in push mode, so I have to energize a 220v relay

Do you have a relay switching a relay or just a single relay to switch?

I know you have 1 wired up to 2 pins.

the 2 relays 5v activate 2 220v relays with dual status

Try adding the following to the end of the script to turn off the relays on reboot:

blynk.on('connect', function() {
  console.log("Blynk ready.");
  blynk.virtualWrite(0, 0); // send LOW / OFF to virtual pin 0
});
blynk.on('disconnect', function() { console.log("DISCONNECT"); }); // connection to the server is dropped

does not work

    var Blynk = require('blynk-library');
    var Gpio = require('onoff').Gpio;

   var AUTH = 'xxxxxxxxxxxxxxxxxx';

var blynk = new Blynk.Blynk(AUTH);


Relay1 = new Gpio(25, 'out');
Relay2 = new Gpio(24, 'out');

var v0 = new blynk.VirtualPin(0);

v0.on('write', function(param) {
   
  if (param == 1)  { 
   Relay1.writeSync(1); 
   Relay2.writeSync(1);
   blynk.virtualWrite(20, 0);
} else {
  Relay1.writeSync(0);
  Relay2.writeSync(0);
  blynk.virtualWrite(20, 255);
}

console.log('v0:', param[0]);

});


blynk.on('connect', function() {
  console.log("Blynk ready.");
  blynk.virtualWrite(0, 0); // send LOW / OFF to virtual pin 0
});
blynk.on('disconnect', function() { console.log("DISCONNECT"); }); // connection to the server is dropped

the problem for me is here

Relay1 = nuovo Gpio (25, 'out'); 
Relay2 = nuovo Gpio (24, 'out');

I wanted to put something like that but it does not work

    Relay1 = nuovo Gpio (25, 'out' start off); 
    Relay2 = nuovo Gpio (24, 'out' start off);

Do you see “Blynk Ready” in the console?

Can you disconnect the secondary relay and put the button into switch mode. Then check the button ON starts the primary relay and button OFF stops it.

yes i see , the behavior does not change. the relay turns on when the sketch is launched. I make a video to see you too

Don’t you have access to YouTube?

no, I’m sorry. I can send it through Telegram. you can add @Jack_G_One in your telegram

@Jack1 I don’t use Telegram. Do you have an ftp client on your PC like FileZilla? How big is the video?

Ok so first observation is that when the PI reboots the Blynk button itself is going from OFF to ON, regardless of what might be happening with the relays. This suggests you have “sync” in your script. Please post your latest script to save me looking back through the thread. Meanwhile I will keep watching the video to see if I can spot anything else.

this is the sketch I’m using in the video

var Blynk = require('blynk-library');
var Gpio = require('onoff').Gpio;

var AUTH = 'xxxxxxxxxxxxxxxxxxxxxxxx';

var blynk = new Blynk.Blynk(AUTH);


Relay1 = new Gpio(25, 'out');
Relay2 = new Gpio(24, 'out');

var v0 = new blynk.VirtualPin(0);

v0.on('write', function(param) {
   
  if (param == 1)  { 
   Relay1.writeSync(1); 
   Relay2.writeSync(1);
   blynk.virtualWrite(20, 0);
} else {
  Relay1.writeSync(0);
  Relay2.writeSync(0);
  blynk.virtualWrite(20, 255);
}

console.log('v0:', param[0]);

});


blynk.on('connect', function() {
  console.log("Blynk ready.");
  blynk.virtualWrite(0, 0); // send LOW / OFF to virtual pin 0
});
blynk.on('disconnect', function() { console.log("DISCONNECT"); }); // connection to the server is dropped

And the button was booting to ON before we added this, right?

blynk.virtualWrite(0, 0); // send LOW / OFF to virtual pin 0