Esp32 wifi and hardware problem

Hi

I using blynk legacy for my project. My project is esp32 control 2 channel 5V relay module on off via 2 mode (switch button dan blynk app)

My problem is, after turn on power for esp32, switch button dont work unless esp32 connect to wifi first.

My second problem is, here at my place wifi connect is unstable. Esp32 wont reconnect to same wifi if disconnect. I nned to turn off and turn on back power if i want esp32 to connect to wifi.

Here is my code

#define BLYNK_AUTH_TOKEN "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"                   
//
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

//server status led
#define BlynkLED 2

//buttons and led pins
#define btn1 26
#define btn2 27

#define relay1 18
#define relay2 19

//led states
bool relay1State = false;
bool relay2State = false;

//server state | flag for online/Offline Mode
bool onlineMode = false;

//Wifi config
char ssid[] = "XXXXXXXXXX"; //nama wifi
char pass[] = "XXXXXXXXXX"; //password wifi
bool ledStatus = false;
WidgetLED led1(V8);
#define BLYNK_GREEN     "#23C48E"
#define BLYNK_BLUE      "#04C0F8"
#define BLYNK_YELLOW    "#ED9D00"
#define BLYNK_RED       "#D3435C"
#define BLYNK_DARK_BLUE "#5F7CD8"
BlynkTimer timer;


void setup() {
  Serial.begin(115200);

  timer.setInterval(2000L, isServerConnected);

  pinMode(BlynkLED, OUTPUT);

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  pinMode(btn1, INPUT_PULLUP);
  pinMode(btn2, INPUT_PULLUP);

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, IPAddress(XXX, XXX, XXX, XXX), XXXX);

  Blynk.virtualWrite(V1, LOW);
  Blynk.virtualWrite(V2, LOW);
  led1.on();
  timer.setInterval(1000L, blinkLedWidget);
}

void loop()
{
  //Blynk.run();
  timer.run();

  if (onlineMode) {
    Blynk.run();
    whenOnline(); //handles online functionalities
  } else {
    whenOffline(); //handles offline functionalities
  }
}

void whenOnline() {
  if (digitalRead(btn1) == LOW) {
    relay1State = !relay1State;
    updateLEDs();
    delay(50);
    while (digitalRead(btn1) == LOW);

    updateBlynkServer();
  }

  if (digitalRead(btn2) == LOW) {
    relay2State = !relay2State;
    updateLEDs();
    delay(50);
    while (digitalRead(btn2) == LOW);

    updateBlynkServer();
  }
}

void whenOffline() {
  if (digitalRead(btn1) == LOW) {
    relay1State = !relay1State;
    updateLEDs();
    delay(50);
    while (digitalRead(btn1) == LOW);
  }

  if (digitalRead(btn2) == LOW) {
    relay2State = !relay2State;
    updateLEDs();
    delay(50);
    while (digitalRead(btn2) == LOW);
  }
}

void updateLEDs() {
  digitalWrite(relay1, relay1State);
  digitalWrite(relay2, relay2State);
}

void updateBlynkServer() {
  Blynk.virtualWrite(V1, relay1State);
  Blynk.virtualWrite(V2, relay2State);
}


BLYNK_WRITE(V1) {
  relay1State = param.asInt();
  Serial.println(relay1State);
  updateLEDs();
}
BLYNK_WRITE(V2) {
  relay2State = param.asInt();
  Serial.println(relay2State);
  updateLEDs();
}

//
void isServerConnected() {
  bool isConnected = Blynk.connected();
  if (isConnected == true) {
    onlineMode = true;
    digitalWrite(BlynkLED, HIGH);
    Serial.println("Connected");
  }
  if (isConnected == false) {
    if (onlineMode == true) {
      onlineMode = false;
    }
    Serial.println("Not Connected");
    digitalWrite(BlynkLED, LOW);
  }
}

void blinkLedWidget()
{
  if (ledStatus) {
    led1.setColor(BLYNK_YELLOW);
    Serial.println("LED on V8: yellow");
    ledStatus = false;
  } else {
    led1.setColor(BLYNK_BLUE);
    Serial.println("LED on V8: blue");
    ledStatus = true;
  }
}

@Anas_Muhammad Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Ok…already done

Blynk.begin is a blocking function, so if the device can’t connect to either your WiFi or the Blynk server all code execution will halt at that point. This means that all of your online/offline code is irrelevant, and it won’t be executed because the sketch will be halted in your void setup.

You need to be manually managing your WiFi connection and using Blynk.config and Blynk.connect, along with a timed routine to periodically check if your device isn’t connected to either WiFi or Blynk and to attempt a reconnection.
The frequency of this reconnection attempt process, and the timeout period that you choose for the Blynk.connect command need to be adjusted to give you minimal disruption to your physical button functionality.

Also, you need to stop polling your button pins each time your void loop is executed, either by doing this via a BlynkTimer or using interrupts.

Here’s an example of how Blynk.config with the optional timeout and Blynk.begin can be used…

As you’re using Blynk Legacy you need to be running version 0.6.1 of the Blynk library, and preferably be running version 0.41.17 of local server if you want to avoid the Log4j2 Java vulnerability.

Pete.