Blynk automations with a multiplexer aren´t working

Hello everyone. I´m having trouble creating a code that will help me control 16 solenoid valves using an ESP32 and Blynk´s automations. Since I can only use 12 pins for digital outputs I decided to use a multiplexor in order to fit 14 of my valves and I followed some code instructions that I have found to create a code that would work with the multiplexor. However it doesn´t seem to do anything when I try to turn the valves on or off. I tried using #define to select the pins that I´m using and also writing them as “int”, and I also tried switching the HIGH and LOW statements with 1s and 0s and still nothing and I don´t really know what else could be wrong. I would really appreciate your help. Thanks in advance.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#include <BlynkSimpleEsp32.h>

#define m1 23
#define m2 22
#define m3 1
#define m4 3
#define m5 21

//Se incluye librería para el WiFi
#include <WiFi.h>

//Se define el código de Blynk
char auth[] ="";

//Se definen nombre de red y contraseña a la que se va a conectar el controlador
const char* ssid = "";
const char* pass = "";

//Se inicializa el temporizador
BlynkTimer timer;

//Se conectan los relé a los pines 32 y 33

int rele1 = 32;
int rele2 = 33;

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

//Se establecen las condiciones para que los relés se enciendan o apaguen

BLYNK_WRITE(V2)
 {
 if(param.asInt()==1)
  {
     digitalWrite(rele1, HIGH);
  }
  else
  {
     digitalWrite(rele1, LOW);
  }
 }

 BLYNK_WRITE(V3)
 {
 if(param.asInt()==1)
  {
     digitalWrite(rele2, HIGH);
  }
  else
  {
     digitalWrite(rele2, LOW);
  }
 }

 BLYNK_WRITE(V4)
 {
 if(param.asInt()==1)
  {
     digitalWrite(m2, LOW);
     digitalWrite(m3, LOW);
     digitalWrite(m4, LOW);
     digitalWrite(m5, LOW);
     digitalWrite(m1, HIGH);
  }
  else
  {
     digitalWrite(m1, LOW);
  }
 }

 BLYNK_WRITE(V5)
 {
 if(param.asInt()==1)
  {
     digitalWrite(m2, HIGH);
     digitalWrite(m3, LOW);
     digitalWrite(m4, LOW);
     digitalWrite(m5, LOW);
     digitalWrite(m1, HIGH);
  }
  else
  {
     digitalWrite(m1, LOW);
  }
 }

  BLYNK_WRITE(V6)
 {
 if(param.asInt()==1)
  {
     digitalWrite(m2, LOW);
     digitalWrite(m3, HIGH);
     digitalWrite(m4, LOW);
     digitalWrite(m5, LOW);
     digitalWrite(m1,HIGH);
  }
  else
  {
     digitalWrite(m1, LOW);
  }
 }

 BLYNK_WRITE(V7)
 {
 if(param.asInt()==1)
  {
     digitalWrite(m2, HIGH);
     digitalWrite(m3, HIGH);
     digitalWrite(m4, LOW);
     digitalWrite(m5, LOW);
     digitalWrite(m1, HIGH);
  }
  else
  {
     digitalWrite(m1, LOW);
  }
 }

  BLYNK_WRITE(V8)
 {
 if(param.asInt()==1)
  {
     digitalWrite(m2, LOW);
     digitalWrite(m3, LOW);
     digitalWrite(m4, HIGH);
     digitalWrite(m5, LOW);
     digitalWrite(m1,HIGH);
  }
  else
  {
     digitalWrite(m1, LOW);
  }
 }

  BLYNK_WRITE(V9)
 {
 if(param.asInt()==1)
  {
     digitalWrite(m2, HIGH);
     digitalWrite(m3, LOW);
     digitalWrite(m4, HIGH);
     digitalWrite(m5, LOW);
     digitalWrite(m1, HIGH);
  }
  else
  {
     digitalWrite(m1, LOW);
  }
 }
void setup() {

//Temporizador para verificar el estado de la conexión WiFi cada 1 segundo
timer.setInterval(5*60*1000, ConexionWifi);

Serial.begin(115200);

//Iniciar conexión Wifi
WiFi.begin(ssid, pass);

//Mostrar asteriscos mientras se realiza la conexión
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("*");
  }

  //Se anuncia cuando ya se haya conectado a la red WiFi y a la nube
  Serial.println("");
  Serial.println("Se ha conectado a la red WiFi");
  //Se inicia la conexión a la nube
  Blynk.begin(auth, ssid, pass);

  //Se establecen los pines del multiplexor como salida
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
  pinMode(m3, OUTPUT);
  pinMode(m4, OUTPUT);
  pinMode(m5, OUTPUT);
}

void ConexionWifi(){

//En caso de que el WiFi se haya desconectado volver a conectar
  if (WiFi.status() != WL_CONNECTED) {
    ESP.restart();
  }

  }

void loop() {

Blynk.run();
timer.run();

}

On a side note, so far I´m testing it with only 8 relays. m1 is my signal pin in the multiplexor and m2 m3 m4 m5 would be s0 s1 s2 s3

What type of multiplexor?

GPIOs 1 and 3 are a very bad choice of pins…

Using this type of board may be a better solution…

And if you wanted some additional pins for use as physical buttons to control your relays without Blynk, and/or LEDs to indicate the status of each relay then you could daisy-chain multiple MCP23017 boards together to give upto 128 additional GPIOs…

I notice that your V4 widget button is writing m2-5 LOW, which would normally turn output D1 on.

Then, your V5 widget button is writing m2 HIGH and m3-5 LOW. This would normally turn output D8 on.

V6 turns m2, m4 and m5 LOW and m3 HIGH, which would normally turn output D4 on.

This assumes that you were correct when you said “m2 m3 m4 m5 would be s0 s1 s2 s3”. Are these multiplexor outputs (D1, D8 & D4) the ones you were expecting to be controlling with these combinations?

Also your else statement in each of these BLYNK_WRITE commands won’t turn the corresponding output off, because you’re not writing anything to the m2-5 pins when you pull your signal pin LOW.

TBH, it’s usually far easier to use a library designed for your multiplexor chip to simplify these data pin combinations, otherwise it all gets very messy when you start dealing with lots of pins.

Also, using the BLYNK_WRITE_DEFAULT() function (as shown in the two examples above) makes your code much neater and simpler.

You should also be careful when using Blynk.syncAll() as this only works with datasterams where you’ve enabled the Sync with latest server value every time device connects to the cloud option in the advanced datastream options. It’s far better to either list the pins you want to sync, or use a for loop to cycle through those pins.

Pete.