Need advice for my project

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <DHT.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


char auth[] = "FlKPz9CWFecvdXHZHEfsioRxU5Oxb68K";
char ssid[] = "Pak Jasno";
char pass[] = "tirtalawu";

#define DHTPIN 15
#define DHTTYPE DHT11
#define treshold 30 

const int rly1Pin = 12;
const int rly2Pin = 13;
const int rly3Pin = 23;
const int rly4Pin = 19;
const int rly5Pin = 18;
const int rly6Pin = 5;
const int rly7Pin = 4;
const int rly8Pin = 2;

const int btn1Pin = 14;
const int btn2Pin = 27;
const int btn3Pin = 26;
const int btn4Pin = 25;
const int btn5Pin = 33;
const int btn6Pin = 32;
const int btn7Pin = 35;
const int btn8Pin = 34;

DHT dht(DHTPIN , DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void checkPhysicalButton();

int rly1State = LOW;
int rly2State = LOW;
int rly3State = LOW;
int rly4State = LOW;
int rly5State = LOW;
int rly6State = LOW;
int rly7State = LOW;
int rly8State = LOW;

int btn1State = HIGH;
int btn2State = HIGH;
int btn3State = HIGH;
int btn4State = HIGH;
int btn5State = HIGH;
int btn6State = HIGH;
int btn7State = HIGH;
int btn8State = HIGH;

BLYNK_CONNECTED() {

  Blynk.syncVirtual(V1, rly1State);
  Blynk.syncVirtual(V2, rly2State);
  Blynk.syncVirtual(V3, rly3State);
  Blynk.syncVirtual(V4, rly4State);
  Blynk.syncVirtual(V7, rly5State);
  Blynk.syncVirtual(V8, rly6State);
  Blynk.syncVirtual(V9, rly7State);
  Blynk.syncVirtual(V10,rly8State);
}
  BLYNK_WRITE(V1) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V2) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V3) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V4) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
   BLYNK_WRITE(V7) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V8) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V9) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V10) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
void checkPhysicalButton()
{
  if (digitalRead(btn1Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      rly1State = !rly1State;
      digitalWrite(rly1Pin, rly1State);

      // Update Button Widget
      Blynk.virtualWrite(V1, rly1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btn2Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      rly2State = !rly2State;
      digitalWrite(rly2Pin, rly2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, rly2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btn3Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      rly3State = !rly3State;
      digitalWrite(rly3Pin, rly3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, rly3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btn4Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      rly4State = !rly4State;
      digitalWrite(rly4Pin, rly4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
  if (digitalRead(btn5Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn5State != LOW) {

      // Toggle LED state
      rly5State = !rly5State;
      digitalWrite(rly5Pin, rly5State);

      // Update Button Widget
      Blynk.virtualWrite(V7, rly5State);
    }
    btn5State = LOW;
  } else {
    btn5State = HIGH;
  }

  if (digitalRead(btn6Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn6State != LOW) {

      // Toggle LED state
      rly6State = !rly6State;
      digitalWrite(rly6Pin, rly6State);

      // Update Button Widget
      Blynk.virtualWrite(V8, rly6State);
    }
    btn6State = LOW;
  } else {
    btn6State = HIGH;
  }
  if (digitalRead(btn7Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn7State != LOW) {

      // Toggle LED state
      rly7State = !rly7State;
      digitalWrite(rly7Pin, rly7State);

      // Update Button Widget
      Blynk.virtualWrite(V9, rly7State);
    }
    btn7State = LOW;
  } else {
    btn7State = HIGH;
  }

  if (digitalRead(btn8Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn8State != LOW) {

      // Toggle LED state
      rly8State = !rly8State;
      digitalWrite(rly8Pin, rly8State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    btn8State = LOW;
  } else {
    btn8State = HIGH;
  }
}

void setup()
{
  lcd.init();
  lcd.backlight();
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

   dht.begin();

  pinMode(rly1Pin, OUTPUT);
  pinMode(rly2Pin, OUTPUT);
  pinMode(rly3Pin, OUTPUT);
  pinMode(rly4Pin, OUTPUT);
  pinMode(rly5Pin, OUTPUT);
  pinMode(rly6Pin, OUTPUT);
  pinMode(rly7Pin, OUTPUT);
  pinMode(rly8Pin, OUTPUT);
 
  
  pinMode(btn1Pin, INPUT_PULLUP);
  pinMode(btn2Pin, INPUT_PULLUP);
  pinMode(btn3Pin, INPUT_PULLUP);
  pinMode(btn4Pin, INPUT_PULLUP);
  pinMode(btn5Pin, INPUT_PULLUP);
  pinMode(btn6Pin, INPUT_PULLUP);
  pinMode(btn7Pin, INPUT_PULLUP);
  pinMode(btn8Pin, INPUT_PULLUP);
  
  digitalWrite(rly1Pin, rly1State);
  digitalWrite(rly2Pin, rly2State);
  digitalWrite(rly3Pin, rly3State);
  digitalWrite(rly4Pin, rly4State);
  digitalWrite(rly5Pin, rly5State);
  digitalWrite(rly6Pin, rly6State);
  digitalWrite(rly7Pin, rly7State);
  digitalWrite(rly8Pin, rly8State);

  timer.setInterval(1000L, sendSensor);
  timer.setInterval(10L, checkPhysicalButton);
}

void loop()
{
   delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
}

With this program I am actually to control 8 relay using small push button, and then I changed my push button to 8 channel touch sensors but when I use the touch snesors, one ore the other relay turn on and off automatically randomly.

Please help
Thank you in advance

Regardless of any other problems with your code, you need to start by cleaning-up your void loop()
Read this:

Also, I know that the ESP32 has a much faster processor than most other boards, but you’re asking it to do 10 digital reads every 10 milliseconds as well as all of the other background Blynk processing associated with the Blynk.run command.

Reducing the scan rate for your buttons will probably be needed, or adding interrupts instead of using this repeated scanning method. Certainly for testing I think you should increase the scan rate substantially.

Pete.

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <DHT.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


char auth[] = "FlKPz9CWFecvdXHZHEfsioRxU5Oxb68K";
char ssid[] = "Pak Jasno";
char pass[] = "tirtalawu";

#define DHTPIN 15
#define DHTTYPE DHT11
#define treshold 30 

const int rly1Pin = 12;
const int rly2Pin = 13;
const int rly3Pin = 23;
const int rly4Pin = 19;
const int rly5Pin = 18;
const int rly6Pin = 5;
const int rly7Pin = 4;
const int rly8Pin = 2;

const int btn1Pin = 14;
const int btn2Pin = 27;
const int btn3Pin = 26;
const int btn4Pin = 25;
const int btn5Pin = 33;
const int btn6Pin = 32;
const int btn7Pin = 35;
const int btn8Pin = 34;

DHT dht(DHTPIN , DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void checkPhysicalButton();

int rly1State = LOW;
int rly2State = LOW;
int rly3State = LOW;
int rly4State = LOW;
int rly5State = LOW;
int rly6State = LOW;
int rly7State = LOW;
int rly8State = LOW;

int btn1State = HIGH;
int btn2State = HIGH;
int btn3State = HIGH;
int btn4State = HIGH;
int btn5State = HIGH;
int btn6State = HIGH;
int btn7State = HIGH;
int btn8State = HIGH;

BLYNK_CONNECTED() {

  Blynk.syncVirtual(V1, rly1State);
  Blynk.syncVirtual(V2, rly2State);
  Blynk.syncVirtual(V3, rly3State);
  Blynk.syncVirtual(V4, rly4State);
  Blynk.syncVirtual(V7, rly5State);
  Blynk.syncVirtual(V8, rly6State);
  Blynk.syncVirtual(V9, rly7State);
  Blynk.syncVirtual(V10,rly8State);
}
  BLYNK_WRITE(V1) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V2) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V3) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V4) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
   BLYNK_WRITE(V7) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V8) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V9) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V10) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
void checkPhysicalButton()
{
  if (digitalRead(btn1Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      rly1State = !rly1State;
      digitalWrite(rly1Pin, rly1State);

      // Update Button Widget
      Blynk.virtualWrite(V1, rly1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btn2Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      rly2State = !rly2State;
      digitalWrite(rly2Pin, rly2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, rly2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btn3Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      rly3State = !rly3State;
      digitalWrite(rly3Pin, rly3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, rly3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btn4Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      rly4State = !rly4State;
      digitalWrite(rly4Pin, rly4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
  if (digitalRead(btn5Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn5State != LOW) {

      // Toggle LED state
      rly5State = !rly5State;
      digitalWrite(rly5Pin, rly5State);

      // Update Button Widget
      Blynk.virtualWrite(V7, rly5State);
    }
    btn5State = LOW;
  } else {
    btn5State = HIGH;
  }

  if (digitalRead(btn6Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn6State != LOW) {

      // Toggle LED state
      rly6State = !rly6State;
      digitalWrite(rly6Pin, rly6State);

      // Update Button Widget
      Blynk.virtualWrite(V8, rly6State);
    }
    btn6State = LOW;
  } else {
    btn6State = HIGH;
  }
  if (digitalRead(btn7Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn7State != LOW) {

      // Toggle LED state
      rly7State = !rly7State;
      digitalWrite(rly7Pin, rly7State);

      // Update Button Widget
      Blynk.virtualWrite(V9, rly7State);
    }
    btn7State = LOW;
  } else {
    btn7State = HIGH;
  }

  if (digitalRead(btn8Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn8State != LOW) {

      // Toggle LED state
      rly8State = !rly8State;
      digitalWrite(rly8Pin, rly8State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    btn8State = LOW;
  } else {
    btn8State = HIGH;
  }
}

void setup()
{
  lcd.init();
  lcd.backlight();
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

   dht.begin();

  pinMode(rly1Pin, OUTPUT);
  pinMode(rly2Pin, OUTPUT);
  pinMode(rly3Pin, OUTPUT);
  pinMode(rly4Pin, OUTPUT);
  pinMode(rly5Pin, OUTPUT);
  pinMode(rly6Pin, OUTPUT);
  pinMode(rly7Pin, OUTPUT);
  pinMode(rly8Pin, OUTPUT);
 
  
  pinMode(btn1Pin, INPUT_PULLUP);
  pinMode(btn2Pin, INPUT_PULLUP);
  pinMode(btn3Pin, INPUT_PULLUP);
  pinMode(btn4Pin, INPUT_PULLUP);
  pinMode(btn5Pin, INPUT_PULLUP);
  pinMode(btn6Pin, INPUT_PULLUP);
  pinMode(btn7Pin, INPUT_PULLUP);
  pinMode(btn8Pin, INPUT_PULLUP);
  
  digitalWrite(rly1Pin, rly1State);
  digitalWrite(rly2Pin, rly2State);
  digitalWrite(rly3Pin, rly3State);
  digitalWrite(rly4Pin, rly4State);
  digitalWrite(rly5Pin, rly5State);
  digitalWrite(rly6Pin, rly6State);
  digitalWrite(rly7Pin, rly7State);
  digitalWrite(rly8Pin, rly8State);

  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, checkPhysicalButton);
}

void sendSensor()
{
   delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
}

void displayLCD()
{
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
}

void loop()  
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

Like this sir?

I’m reading this on an iPad, but I can’t see where you are calling your displayLCD() function.

Pete.

is it LCD or Liquid Crystal I2C?

Sorry, I don’t understand the question.

Pete.

my LCD is for displaying temp and humidity from dht11, and also displaying to blynk app

I still don’t really understand your response to my statement that:

Pete.

Nevermind, but can we get to the point

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <DHT.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


char auth[] = "FlKPz9CWFecvdXHZHEfsioRxU5Oxb68K";
char ssid[] = "Pak Jasno";
char pass[] = "tirtalawu";

#define DHTPIN 15
#define DHTTYPE DHT11
#define treshold 30 

const int rly1Pin = 12;
const int rly2Pin = 13;
const int rly3Pin = 23;
const int rly4Pin = 19;
const int rly5Pin = 18;
const int rly6Pin = 5;
const int rly7Pin = 4;
const int rly8Pin = 2;

const int btn1Pin = 14;
const int btn2Pin = 27;
const int btn3Pin = 26;
const int btn4Pin = 25;
const int btn5Pin = 33;
const int btn6Pin = 32;
const int btn7Pin = 35;
const int btn8Pin = 34;

DHT dht(DHTPIN , DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void checkPhysicalButton();

int rly1State = LOW;
int rly2State = LOW;
int rly3State = LOW;
int rly4State = LOW;
int rly5State = LOW;
int rly6State = LOW;
int rly7State = LOW;
int rly8State = LOW;

int btn1State = HIGH;
int btn2State = HIGH;
int btn3State = HIGH;
int btn4State = HIGH;
int btn5State = HIGH;
int btn6State = HIGH;
int btn7State = HIGH;
int btn8State = HIGH;

BLYNK_CONNECTED() {

  Blynk.syncVirtual(V1, rly1State);
  Blynk.syncVirtual(V2, rly2State);
  Blynk.syncVirtual(V3, rly3State);
  Blynk.syncVirtual(V4, rly4State);
  Blynk.syncVirtual(V7, rly5State);
  Blynk.syncVirtual(V8, rly6State);
  Blynk.syncVirtual(V9, rly7State);
  Blynk.syncVirtual(V10,rly8State);
}
  BLYNK_WRITE(V1) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V2) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V3) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V4) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
   BLYNK_WRITE(V7) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V8) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V9) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V10) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
void checkPhysicalButton()
{
  if (digitalRead(btn1Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      rly1State = !rly1State;
      digitalWrite(rly1Pin, rly1State);

      // Update Button Widget
      Blynk.virtualWrite(V1, rly1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btn2Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      rly2State = !rly2State;
      digitalWrite(rly2Pin, rly2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, rly2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btn3Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      rly3State = !rly3State;
      digitalWrite(rly3Pin, rly3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, rly3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btn4Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      rly4State = !rly4State;
      digitalWrite(rly4Pin, rly4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
  if (digitalRead(btn5Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn5State != LOW) {

      // Toggle LED state
      rly5State = !rly5State;
      digitalWrite(rly5Pin, rly5State);

      // Update Button Widget
      Blynk.virtualWrite(V7, rly5State);
    }
    btn5State = LOW;
  } else {
    btn5State = HIGH;
  }

  if (digitalRead(btn6Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn6State != LOW) {

      // Toggle LED state
      rly6State = !rly6State;
      digitalWrite(rly6Pin, rly6State);

      // Update Button Widget
      Blynk.virtualWrite(V8, rly6State);
    }
    btn6State = LOW;
  } else {
    btn6State = HIGH;
  }
  if (digitalRead(btn7Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn7State != LOW) {

      // Toggle LED state
      rly7State = !rly7State;
      digitalWrite(rly7Pin, rly7State);

      // Update Button Widget
      Blynk.virtualWrite(V9, rly7State);
    }
    btn7State = LOW;
  } else {
    btn7State = HIGH;
  }

  if (digitalRead(btn8Pin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn8State != LOW) {

      // Toggle LED state
      rly8State = !rly8State;
      digitalWrite(rly8Pin, rly8State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    btn8State = LOW;
  } else {
    btn8State = HIGH;
  }
}

void setup()
{
  lcd.init();
  lcd.backlight();
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

   dht.begin();

  pinMode(rly1Pin, OUTPUT);
  pinMode(rly2Pin, OUTPUT);
  pinMode(rly3Pin, OUTPUT);
  pinMode(rly4Pin, OUTPUT);
  pinMode(rly5Pin, OUTPUT);
  pinMode(rly6Pin, OUTPUT);
  pinMode(rly7Pin, OUTPUT);
  pinMode(rly8Pin, OUTPUT);
 
  
  pinMode(btn1Pin, INPUT_PULLUP);
  pinMode(btn2Pin, INPUT_PULLUP);
  pinMode(btn3Pin, INPUT_PULLUP);
  pinMode(btn4Pin, INPUT_PULLUP);
  pinMode(btn5Pin, INPUT_PULLUP);
  pinMode(btn6Pin, INPUT_PULLUP);
  pinMode(btn7Pin, INPUT_PULLUP);
  pinMode(btn8Pin, INPUT_PULLUP);
  
  digitalWrite(rly1Pin, rly1State);
  digitalWrite(rly2Pin, rly2State);
  digitalWrite(rly3Pin, rly3State);
  digitalWrite(rly4Pin, rly4State);
  digitalWrite(rly5Pin, rly5State);
  digitalWrite(rly6Pin, rly6State);
  digitalWrite(rly7Pin, rly7State);
  digitalWrite(rly8Pin, rly8State);

  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, checkPhysicalButton);
}

void sensor()
{
   delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
}

void loop()  
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

Like this sir?
Maybe :v

At least now you are calling the piece of code that was stranded in the displayLCD and never getting called.

Does it compile, and does it work correctly?

Pete.

No sir, I’m sorry, but after changing it now it wrorks properly. But still when I use Touch sensors just like before, it work randomly

I think you need to use different code if you want to use the touch sensor capabilities of the ESP32. I think the touchRead() function needs to be employed.

Pete.

Alright sir, I’ll try it. Thank you for the advice :smile:

#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32_SSL.h>
#include <DHT.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


char auth[] = "FlKPz9CWFecvdXHZHEfsioRxU5Oxb68K";
char ssid[] = "Pak Jasno";
char pass[] = "tirtalawu";

#define DHTPIN 15
#define DHTTYPE DHT11
int touch_value = 100;

const int rly1Pin = 12;
const int rly2Pin = 13;
const int rly3Pin = 23;
const int rly4Pin = 19;
const int rly5Pin = 18;
const int rly6Pin = 5;
const int rly7Pin = 4;
const int rly8Pin = 2;

const int touch1Pin = 14;
const int touch2Pin = 27;
const int touch3Pin = 26;
const int touch4Pin = 25;
const int touch5Pin = 33;
const int touch6Pin = 32;
const int touch7Pin = 35;
const int touch8Pin = 34;

DHT dht(DHTPIN , DHTTYPE);
BlynkTimer timer;


void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}


void checkPhysicalButton();

int rly1State = LOW;
int rly2State = LOW;
int rly3State = LOW;
int rly4State = LOW;
int rly5State = LOW;
int rly6State = LOW;
int rly7State = LOW;
int rly8State = LOW;

int touch1State = HIGH;
int touch2State = HIGH;
int touch3State = HIGH;
int touch4State = HIGH;
int touch5State = HIGH;
int touch6State = HIGH;
int touch7State = HIGH;
int touch8State = HIGH;

BLYNK_CONNECTED() {

  Blynk.syncVirtual(V1, rly1State);
  Blynk.syncVirtual(V2, rly2State);
  Blynk.syncVirtual(V3, rly3State);
  Blynk.syncVirtual(V4, rly4State);
  Blynk.syncVirtual(V7, rly5State);
  Blynk.syncVirtual(V8, rly6State);
  Blynk.syncVirtual(V9, rly7State);
  Blynk.syncVirtual(V10,rly8State);
}
  BLYNK_WRITE(V1) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V2) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V3) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V4) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}
   BLYNK_WRITE(V7) {
  rly1State = param.asInt();
  digitalWrite(rly1Pin, rly1State);
  }
  BLYNK_WRITE(V8) {
  rly2State = param.asInt();
  digitalWrite(rly2Pin, rly2State);
  }
  
   BLYNK_WRITE(V9) {
  rly3State = param.asInt();
  digitalWrite(rly3Pin, rly3State);
   }
   
   BLYNK_WRITE(V10) {
  rly4State = param.asInt();
  digitalWrite(rly4Pin, rly4State);
}


void checkPhysicalButton(){

  
  {
  touch_value = touchRead(touch1Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly1State;
      digitalWrite(rly1Pin, rly1State);

      // Update Button Widget
      Blynk.virtualWrite(V1, rly1State);
    }
    touch1State = LOW;
  } else {
    touch1State = HIGH;
  }


  {
  touch_value = touchRead(touch2Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly2State;
      digitalWrite(rly2Pin, rly2State);

      // Update Button Widget
      Blynk.virtualWrite(V2, rly2State);
    }
    touch2State = LOW;
  } else {
    touch2State = HIGH;
  }


  {
  touch_value = touchRead(touch3Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly3State;
      digitalWrite(rly3Pin, rly3State);

      // Update Button Widget
      Blynk.virtualWrite(V3, rly3State);
    }
    touch3State = LOW;
  } else {
    touch3State = HIGH;
  }


  {
  touch_value = touchRead(touch4Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly4State;
      digitalWrite(rly4Pin, rly4State);

      // Update Button Widget
      Blynk.virtualWrite(V4, rly4State);
    }
    touch4State = LOW;
  } else {
    touch4State = HIGH;
  }

  
   {
  touch_value = touchRead(touch5Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly5State;
      digitalWrite(rly5Pin, rly5State);

      // Update Button Widget
      Blynk.virtualWrite(V7, rly5State);
    }
    touch8State = LOW;
  } else {
    touch8State = HIGH;
  }

  
  {
  touch_value = touchRead(touch6Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly6State;
      digitalWrite(rly1Pin, rly6State);

      // Update Button Widget
      Blynk.virtualWrite(V8, rly6State);
    }
    touch6State = LOW;
  } else {
    touch6State = HIGH;
  }

  
  {
  touch_value = touchRead(touch7Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly7State;
      digitalWrite(rly7Pin, rly7State);

      // Update Button Widget
      Blynk.virtualWrite(V9, rly7State);
    }
    touch7State = LOW;
  } else {
    touch7State = HIGH;
  }


  {
  touch_value = touchRead(touch8Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
      rly1State = !rly8State;
      digitalWrite(rly8Pin, rly8State);

      // Update Button Widget
      Blynk.virtualWrite(V10, rly8State);
    }
    touch8State = LOW;
   }else{
    touch8State = HIGH;
  }

  
}

void setup()
{
  lcd.init();
  lcd.backlight();
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

   dht.begin();

  pinMode(rly1Pin, OUTPUT);
  pinMode(rly2Pin, OUTPUT);
  pinMode(rly3Pin, OUTPUT);
  pinMode(rly4Pin, OUTPUT);
  pinMode(rly5Pin, OUTPUT);
  pinMode(rly6Pin, OUTPUT);
  pinMode(rly7Pin, OUTPUT);
  pinMode(rly8Pin, OUTPUT);
 
  
  pinMode(touch1Pin, INPUT_PULLUP);
  pinMode(touch2Pin, INPUT_PULLUP);
  pinMode(touch3Pin, INPUT_PULLUP);
  pinMode(touch4Pin, INPUT_PULLUP);
  pinMode(touch5Pin, INPUT_PULLUP);
  pinMode(touch6Pin, INPUT_PULLUP);
  pinMode(touch7Pin, INPUT_PULLUP);
  pinMode(touch8Pin, INPUT_PULLUP);
  
  digitalWrite(rly1Pin, rly1State);
  digitalWrite(rly2Pin, rly2State);
  digitalWrite(rly3Pin, rly3State);
  digitalWrite(rly4Pin, rly4State);
  digitalWrite(rly5Pin, rly5State);
  digitalWrite(rly6Pin, rly6State);
  digitalWrite(rly7Pin, rly7State);
  digitalWrite(rly8Pin, rly8State);

  timer.setInterval(1000L, sendSensor);
  timer.setInterval(1000L, checkPhysicalButton);
}

void sensor()
{
   delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
}

void loop()  
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}

I already change it, but now I have an error code “else without if”

It would have been helpful if you’d have said where that error was, to save me compiling the code to find out.

  touch_value = touchRead(touch8Pin);
  Serial.println(touch_value);
  if (touch_value < 50)
  {
     rly1State = !rly8State;
     digitalWrite(rly8Pin, rly8State);
     // Update Button Widget
     Blynk.virtualWrite(V10, rly8State);
  } <------------------------------------------STRAY CLOSING BRACKET
    touch8State = LOW;
  }
  else
  {
    touch8State = HIGH;
  }

If you laid your code out better you’d be able to see where the problem lies.

Pete.

Thanks sir

Sir, another question now my relay working from 1 to 8 continuesly. Is it the treshold input?
even when my touch sensor is disconnect

I have no idea, as a great deal will depend on your physical layout.
Have you tried changing the threshold?

Pete.

I am a bit confused.

Are you using a touch sensor (e.g ttp226), or the esp32 pin touch sensor capability?