I would like to know if there is someone could help me with my project. I want to consecutively switch 4 relays with 1 switch using esp32. Can someone help me please. Send me some sample codes..thank you

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

ESP32 WIFI

#include <BlynkSimpleEsp32.h>

BlynkTimer timer;

// define the GPIO connected with Relays and switches
#define RelayPin1 23  //D23
#define RelayPin2 22  //D22
#define RelayPin3 21  //D21
#define RelayPin4 19  //D19
#define RelayPin5 18  //D18

#define SwitchPin1 13  //D13
#define SwitchPin2 12  //D12
#define SwitchPin3 14  //D14

#define wifiLed    2   //D2

#define VPIN_BUTTON_1    V1 
#define VPIN_BUTTON_2    V2
#define VPIN_BUTTON_3    V3 
#define VPIN_BUTTON_4    V4
#define VPIN_BUTTON_5    V5 

int toggleState_1 = 1; //Define integer to remember the toggle state for relay 1
int toggleState_2 = 1; //Define integer to remember the toggle state for relay 2
int toggleState_3 = 1; //Define integer to remember the toggle state for relay 3
int toggleState_4 = 1; //Define integer to remember the toggle state for relay 4
int toggleState_5 = 1; //Define integer to remember the toggle state for relay 5

int wifiFlag = 0;
int count = 0;

#define AUTH "Pp4ptemsukTdfJEDzUDjL-0TGVuWJAGo"                 // You should get Auth Token in the Blynk App.  
#define WIFI_SSID "mmmse_engineer"             //Enter Wifi Name
#define WIFI_PASS "mmsectqsatw"         //Enter wifi Password


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(RelayPin5, OUTPUT);
  
  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
 
  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, toggleState_1);
  digitalWrite(RelayPin2, toggleState_2);
  digitalWrite(RelayPin3, toggleState_3);
  digitalWrite(RelayPin4, toggleState_4);
  digitalWrite(RelayPin5, toggleState_5);
   

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  timer.setInterval(3000L, checkBlynkStatus); // check if Blynk server is connected every 3 seconds
  Blynk.config(AUTH);
}

void loop()
{  
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("WiFi Not Connected");
  }
  else
  {
    Serial.println("WiFi Connected");
    Blynk.run();
  }

  timer.run(); // Initiates SimpleTimer
  if (wifiFlag == 0)
    with_internet();
  else
    without_internet();
}

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
  Blynk.syncVirtual(VPIN_BUTTON_5);
}

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  digitalWrite(RelayPin1, toggleState_1);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  digitalWrite(RelayPin2, toggleState_2);
}

BLYNK_WRITE(VPIN_BUTTON_3) {
  toggleState_3 = param.asInt();
  digitalWrite(RelayPin3, toggleState_3);
}

BLYNK_WRITE(VPIN_BUTTON_4) {
  toggleState_4 = param.asInt();
  digitalWrite(RelayPin4, toggleState_4);
}

BLYNK_WRITE(VPIN_BUTTON_5) {
  toggleState_5 = param.asInt();
  digitalWrite(RelayPin5, toggleState_5);
}

void checkBlynkStatus() { // called every 3 seconds by SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    digitalWrite(wifiLed, LOW); //Turn off WiFi LED
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed, HIGH); //Turn on WiFi LED
  }
}


void FanSpeed()
{
  int val = 0;
  int count = 0;
  int state = 0;
  val = digitalRead(SwitchPin3);
  Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);
  if (val == 1){
    delay(50);
    val = digitalRead(SwitchPin3);
    if (val == 0){
    count = state + 1;
  }}
  else{
    delay(100);
  }
  switch(count){
    case 1: digitalWrite(RelayPin1, LOW);
            digitalWrite(RelayPin2, HIGH);
            digitalWrite(RelayPin3, HIGH);
            count = state;
    break;
    case 2: digitalWrite(RelayPin2, LOW);
            digitalWrite(RelayPin1, HIGH);
            digitalWrite(RelayPin3, HIGH);
            count = state;
    break;
    case 3: digitalWrite(RelayPin3, LOW);
            digitalWrite(RelayPin2, HIGH);
            digitalWrite(RelayPin1, HIGH);
            count = state;
    break;
    default:  digitalWrite(RelayPin1, HIGH);
              digitalWrite(RelayPin2, HIGH);
              digitalWrite(RelayPin3, HIGH);
              count = 0;
    break;
  }
  delay(200);
}

void PowerOn()
{
  if(toggleState_4 == 1){
    digitalWrite(RelayPin4, LOW);
    toggleState_4 = 0;
    Serial.println("Power On");
  }
  else{
    digitalWrite(RelayPin4, HIGH);
    toggleState_4 = 0;
    Serial.println("Device Off");
  }
  delay(100);
}

void OscillatorOn()
{
  if(toggleState_5 == 1){
    digitalWrite(RelayPin5, LOW);
    toggleState_5 = 0;
    Serial.println("Oscillator On");
  }
  else{
    digitalWrite(RelayPin5, HIGH);
    toggleState_5 = 0;
    Serial.println("Oscillator Off");
  }
  delay(100);
}


void with_internet()
{
  if (digitalRead(SwitchPin1) == LOW){
    delay(200);
    PowerOn();
    Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);
  }
  else if (digitalRead(SwitchPin2) == LOW){
    delay(200);
    OscillatorOn;
    Blynk.virtualWrite(VPIN_BUTTON_5, toggleState_5);
  }
  else if (digitalRead(SwitchPin3) == LOW){
    delay(200);
    FanSpeed();
  }
}

void without_internet()
{
  if (digitalRead(SwitchPin1) == LOW){
    delay(200);
    PowerOn();
  }
  else if (digitalRead(SwitchPin2) == LOW){
    delay(200);
    OscillatorOn;
  }
  else if (digitalRead(SwitchPin3) == LOW){
    delay(200);
    FanSpeed();
  }
}

It would be better if instead of putting everything in the topic title, you took some time to explain in words and (and pictures if that would help us) to explain exactly what it is that you are trying to achieve.
Some information about why the code you’ve posted doesn’t meet these goals would also help us to help you.

Pete.

2 Likes