Problem with virtual pins and if command (ESP 8266)

Hello, I am new to the world of blynk.

The question is that I am creating an automation project and I have 2 problems with nodemcu 8266 AMICA + blynk Local server (rasberry pi):

Problem 1:
When I turn on the nodemcu the 9 relays turn ON, I can’t find a way that when the system starts they all start off in OFF.

Problem 2:

I need when I press a button1 in my blynk app through the virtual pin V1, turn on R1, R8 and R3 … when the virtual pin V1 goes to 0, R1, R8 and R3 should be turned OFF.

When I press another button2 of the APP, through the virtual pn V2 (virtual pin V2 becomes 1), turn on R2, R8 and R4.

When I turn off this button2, V2 would go to 0 and therefore R2, R8 and R4 to off.

A similar case would be for a third button3 of the APP, through virtual pin V3, being at 1 it should turn on R1, R8 and R4 .
And if V3 goes to 0 R1, R8 and R4 should turn off.

Using the button 4 of the app and the virtual pin V4, when it goes to 1, it should turn on R5 and R8.
And if V4 is 0, R5 and R8 should go OFF

With the button 5 of the app and the virtual pin V5, when it goes to 1, it should turn on R6 and R8.
And if V5 = 0, R6 and R8 should go OFF.

and this logic would not be happening and I can not find the problem or error why it does not happen

Thank you

Sorry my english

#include <ESP8266WiFi.h>

#include <DNSServer.h>
#include <ESP8266WebServer.h >
#include <SPI.h>
#include <WiFiManager.h>

#include <BlynkSimpleEsp8266.h>

char auth[] ="";

char blynk_token[]="";

const int R1=D1; //Electrovalvula 1
const int R2=D2; //Electrovalvula 2
const int R3=D3; //Electrovalvula 3
const int R4=D4; //Electrovalvula 4
const int R5=D5; //Electrovalvula 5
const int R6=D6; //Electrovalvula 6
const int R7=D7; //Luces pileta
const int R8=D8; //Bomba de agua vulcano
const int R9=D0; //Bomba PH/acido

float phValue; 
int pinValue0 ;
int pinValue1 ;
int pinValue2 ;
int pinValue3 ;
int pinValue4 ;
int pinValue5 ;

 const int analogInPin = A0;
 int sensorValue = 0;
 unsigned long int avgValue;
 float b;
 int buf[10], temp;


{ pinValue1 = param.asInt();  

{ pinValue2 = param.asInt();  

{ pinValue3 = param.asInt();  

{  pinValue4 = param.asInt();  

{  pinValue5 = param.asInt();  

void setup(){

pinMode (R1,OUTPUT);
pinMode (R2,OUTPUT);
pinMode (R3,OUTPUT);
pinMode (R4,OUTPUT);
pinMode (R5,OUTPUT);
pinMode (R6,OUTPUT);
pinMode (R7,OUTPUT);
pinMode (R8,OUTPUT);
pinMode (R9,OUTPUT);

  // Creamos una instancia de la clase WiFiManager
WiFiManager wifiManager;
  // Descomentar para resetear configuración

 WiFiManagerParameter custom_blynk_token("Blynk", "blynk token", blynk_token, 34); 


  // Cremos AP y portal cautivo
Serial.println("Ya estás conectado");

  //Blynk.begin (auth)



void Filtrado(){
digitalWrite (R1, HIGH); 
digitalWrite (R3, HIGH);
digitalWrite (R8, HIGH);

void Retrolavado(){
digitalWrite (R2, HIGH); 
digitalWrite (R4, HIGH);
digitalWrite (R8, HIGH);

void Enjuague(){
digitalWrite (R1, HIGH); 
digitalWrite (R4, HIGH);
digitalWrite (R8, HIGH);

void Recirculado(){
digitalWrite (R5, HIGH); 
digitalWrite (R8, HIGH);

void Vaciado(){
digitalWrite (R6, HIGH); 
digitalWrite (R8, HIGH);

void loop() {

if (pinValue1 == 1) {

if (pinValue2 == 1) {

if (pinValue3 == 1) {

if (pinValue4 == 1) {

if (pinValue5 == 1) {

if (pinValue0==1 && phValue>7,2) {
digitalWrite(R9, LOW);  // prueba con R4
else {
digitalWrite(R9, HIGH);  //prueba con r4

 for (int i = 0; i < 10; i++) {
 buf[i] = analogRead(analogInPin);
for (int i = 0; i < 9; i++)
for (int j = i + 1; j < 10; j++)
if (buf[i] > buf[j])
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;}}
avgValue = 0;
for (int i = 2; i < 8; i++)
avgValue += buf[i];
float pHVol = (float)avgValue * 5 / 1023/10;
float phValue = -5.70 * pHVol + 21.34;
Serial.print("sensor = ");
Serial.print("Voltaje = ");

Serial.print("pv1 = ");

Serial.print("pv2 = ");

Serial.print("pv3 = ");

Serial.print("pv4 = ");

Serial.print("pv5 = ");


You can’t use 9 relays successfully with a NodeMCU, there aren’t enough suitable pins. You should read this:

You should use an ESP32 instead, or a port expander on the NodeMCU.

Your void loop isn’t suitable for use with Blynk. You should read this…

Add virtualWrite commands to your void setup to set the status that you want.

Read this…