When nodemcu restart its toggles relay by its self

i uploaded the code for dht sensor plus two channel relay in esp32, when its restart and connect with blynk relay toggles by its self, and then i have to push button in blynk app two times to synk the state. its happend with both board esp32 and 8266.

here is the code.
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "DHT.h"

char auth[] = "           ";
char ssid[] = "    ";
char pass[] = "    ";

#define DHTTYPE DHT11
#define DHTPIN 14

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, t);
  Blynk.virtualWrite(V6, h);
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  dht.begin();
  timer.setInterval(1000L, sendSensor);
  Blynk.begin(auth, ssid, pass);
}

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

@muhammad8khan 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:
```

First of all, I’d recommend that you use virtual pins in the app to control your relays, rather than digital pins
You will then need to a a pinMode statement in your void setup to define the relay pins as being used for OUTPUT, and add BLYNK_WRITE(Vpin) callback functions for your two virtual pins that control the relays.
These callbacks will switch the relays using digitalWrite commands.

Then you have a choice to make - do you want to synchronise your relays with the app at startup/reconnection, or do you want to set your relays to a particular state (off maybe?) and synchronise the app with your relays?

If it’s the former then add a BLYNK_CONNECTED callback and do a Blynk.syncViortual for each of yourt two virtual pins within that callback.

If it’;s the latter then set the relays to the desired state in your void setup (after the pinMode statements) and do a Blynk.virtualWrite(vpin) for each of the two virtual pins. This needs to come after the blynk connection is established.

There are plenty of examples of how to do this on the forum, as well as in the Sketch Builder examples.

Pete,

thanks pete,
actually i am not very well code writer and know little about it,
could u please help me to re write my code with virtual pins plus sync relays with app status.

There really are lots of examples of thermostat code on the forum (I assume that this is what you are wanting to do, if not then it’s probably a good starting point anyway), so you’d be better-off looking at that rather than trying to re-invent the wheel.

Pete.