Relays off if not connected to the server

hello
i want to Before connecting to the internet and blynk server all of my relays to be off and When connecting to the Internet, take new commands from the server.
Like my previous code then me use

void setup() {

    pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, HIGH);
  
    pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY2, HIGH);

////////////////////////////////////////////////////////////////////////////

in first viod setup but in new blynk code i don’t know Where in the code should I enter these statements so that the relays are off by default and only turn on when connected to the site server?

it’s my code i use right now :

#define BLYNK_TEMPLATE_ID "TMPLlo99-8WE"
#define BLYNK_DEVICE_NAME "google assisstant"
#define BLYNK_AUTH_TOKEN "#############"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "#########";
char pass[] = "#########";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

Should be in the void setup.

i use these in the void setup but When I disconnect the power and connect it again, the relays stay on until they connect to the server and turn off automatically. I want the relays to be off during operation until connecting to the modem

Post the updated sketch please. The one you’re currently using.

Put your PinMode and digitalWrite commands at the begging of your void setup, before Blynk.begin

Pete.

this is my old sketch whitout blynk :

/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <WiFiManager.h>

#define RELAY 0 // relay connected to  GPIO0
#define RELAY2 2

const char *ssid = "###########"; //Enter your WIFI ssid 
const char *password = "###########"; //Enter your WIFI password 


/* Set these to your desired credentials. */

String stat_str = "--";
String stat_str2 = "--";

ESP8266WebServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
   connected to this access point to see it.
*/
void handleRoot() { 
  server.send(200, "text/html", "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head><body bgcolor=\"#cfde2c\"><div style='text-align:center; height:96%; border:10px groove blue; border-radius: 5px;'><strong></strong><h1><i><a style=color:#ff0005; font-size:830%>Shayan Ro0m</a></i></h1></strong><br><br><form action=\"/RELAY_on\" method=\"get\" id=\"form1\"></form><button type=\"submit\" form=\"form1\" value=\"On\"style=\"color:green; background-color:white; font-size:190%; width:230px; margin-left:20px;\" onclick=\'this.form.submit();'>MAHTABI_ON</button><br><br><form action=\"/RELAY_off\" method=\"get\" id=\"form2\"></form><button type=\"submit\" form=\"form2\" value=\"Off\"style=\"color:red; background-color:white; font-size:190%; width:230px; margin-left:20px;\" onclick=\'this.form.submit();'>MAHTABI_OFF</button><h2><a style=color:#ffffff; font-size:330%> MAHTABI_STAT: "+stat_str+"</a><h2><h2><h2><form action=\"/RELAY_on2\" method=\"get\" id=\"form3\"></form><button type=\"submit\" form=\"form3\" value=\"On\"style=\"color:green; background-color:white; font-size:130%; width:210px; margin-left:20px;\" onclick=\'this.form.submit();'>LOSR_ON</button><br><br><form action=\"/RELAY_off2\" method=\"get\" id=\"form4\"></form><button type=\"submit\" form=\"form4\" value=\"Off\"style=\"color:red; background-color:white; font-size:130%; width:210px; margin-left:20px;\" onclick=\'this.form.submit();'>LOSR_OFF</button></h2><a style=color:#ffffff; font-size:830%>LOSR_STAT: "+stat_str2+"</a></h2></body></html>"); 
} 

void handleSave() { 
  if (server.arg("pass") != "") { 
    Serial.println(server.arg("pass")); 
  } 
} 

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 9);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 0, 0);



void setup() {

    pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, HIGH);
  
    pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY2, HIGH);
  



// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet)) {
  Serial.println("STA Failed to configure");
}
  
  
  EEPROM.begin(4096);
  int stat = EEPROM.read(0);
  digitalWrite(RELAY, stat);
  if(stat==0)stat_str="OFF";
  if(stat==1)stat_str="ON";

    EEPROM.begin(4096);
  int stat2 = EEPROM.read(0);
  digitalWrite(RELAY2, stat2);
  if(stat2==0)stat_str2="OFF";
  if(stat2==1)stat_str2="ON";
  
  delay(80000); 
  Serial.begin(115200); 
  Serial.println(); 

   WiFiManager wifiManager;

   wifiManager.autoConnect("Shayan_Home", "password333");
  
  Serial.println("stat: ");
  Serial.println(stat);
  
  Serial.println("stat2: ");
  Serial.println(stat2);
  
  Serial.print("Configuring access point..."); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
  }



  Serial.println(""); 
  Serial.println("WiFi connected"); 
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP()); 
  
  server.on("/", handleRoot);
  server.on ("/save", handleSave); 
  server.begin();
  Serial.println("HTTP server started");
  
    server.on("/RELAY_on", []() { 
    digitalWrite(RELAY, 1); 
    Serial.println("on"); 

    EEPROM.write(0,1);//addr,data
    EEPROM.commit();
    stat_str="ON";
    
    handleRoot(); 
  }); 
  server.on("/RELAY_off", []() { 
    digitalWrite(RELAY, 0); 
    Serial.println("off"); 

    EEPROM.write(0,0);//addr,data
    EEPROM.commit();
    stat_str="OFF";
    
    handleRoot(); 
  }); 

//////////////////////////////////////2

    server.on("/RELAY_on2", []() { 
    digitalWrite(RELAY2, 1); 
    Serial.println("on"); 

    EEPROM.write(0,1);//addr,data
    EEPROM.commit();
    stat_str2="ON";
    
    handleRoot(); 
  }); 
  server.on("/RELAY_off2", []() { 
    digitalWrite(RELAY2, 0); 
    Serial.println("off"); 

    EEPROM.write(0,0);//addr,data
    EEPROM.commit();
    stat_str2="OFF";
    
    handleRoot(); 
  }); 
  
}

void loop() {
  server.handleClient();
}

my currently code then I want you to help me fix it :

Blockquote

Your void setup should look like this

void setup()
{
  // Debug console
  Serial.begin(115200);

  pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, HIGH);
  
    pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY2, HIGH);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

I did this, but the relays are still on at the beginning

  This is a simple demo of sending and receiving some data.
  Be sure to check out other examples!
 *************************************************************/

// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "#########"
#define BLYNK_DEVICE_NAME "google assisstant"
#define BLYNK_AUTH_TOKEN "#########"
#define RELAY 0 // relay connected to  GPIO0
#define RELAY2 2


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "########";
char pass[] = "######";

BlynkTimer timer;

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
  // Set incoming value from pin V0 to a variable
  int value = param.asInt();

  // Update state
  Blynk.virtualWrite(V1, value);
}

// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
  // Change Web Link Button message to "Congratulations!"
  Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
  Blynk.setProperty(V3, "onImageUrl",  "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
  Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}

// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, HIGH);
  
    pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY2, HIGH);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{
  Blynk.run();
  timer.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
}

First of all, I think you should read this

1 Like

I have read this before, can you solve the problem in this last code that I sent? So that 2 relays are off at the beginning and execute commands after connecting?

There is no way you read that before as it shows you that GPIO is held high at boot, and GPIO2, which are the two GPIO you are using for your relays.
Switch them to some of the safe pins shown in that link and your problem will probably be gone.

i use esp8266-01s this have 2 pin

Well you are going to have a hard time because both of those pins require to be high at boot.

It’s a very bad choice. I’d suggest using esp32 or nodemcu instead.

i use my old sketch and worked fine .I just want to know why it doesn’t work in the new code with blynk.

this is my old sketch : its work with pin 0 and 2 and esp8266 esp01s why doesn’t work with blynk !!!

/* Create a WiFi access point and provide a web server on it. */

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <WiFiManager.h>

#define RELAY 0 // relay connected to  GPIO0
#define RELAY2 2

const char *ssid = "#######"; //Enter your WIFI ssid 
const char *password = "###"; //Enter your WIFI password 


/* Set these to your desired credentials. */

String stat_str = "--";
String stat_str2 = "--";

ESP8266WebServer server(80);

/* Just a little test message.  Go to http://192.168.4.1 in a web browser
   connected to this access point to see it.
*/
void handleRoot() { 
  server.send(200, "text/html", "<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head><body bgcolor=\"#cfde2c\"><div style='text-align:center; height:96%; border:10px groove blue; border-radius: 5px;'><strong></strong><h1><i><a style=color:#ff0005; font-size:830%>Shayan Ro0m</a></i></h1></strong><br><br><form action=\"/RELAY_on\" method=\"get\" id=\"form1\"></form><button type=\"submit\" form=\"form1\" value=\"On\"style=\"color:green; background-color:white; font-size:190%; width:230px; margin-left:20px;\" onclick=\'this.form.submit();'>MAHTABI_ON</button><br><br><form action=\"/RELAY_off\" method=\"get\" id=\"form2\"></form><button type=\"submit\" form=\"form2\" value=\"Off\"style=\"color:red; background-color:white; font-size:190%; width:230px; margin-left:20px;\" onclick=\'this.form.submit();'>MAHTABI_OFF</button><h2><a style=color:#ffffff; font-size:330%> MAHTABI_STAT: "+stat_str+"</a><h2><h2><h2><form action=\"/RELAY_on2\" method=\"get\" id=\"form3\"></form><button type=\"submit\" form=\"form3\" value=\"On\"style=\"color:green; background-color:white; font-size:130%; width:210px; margin-left:20px;\" onclick=\'this.form.submit();'>LOSR_ON</button><br><br><form action=\"/RELAY_off2\" method=\"get\" id=\"form4\"></form><button type=\"submit\" form=\"form4\" value=\"Off\"style=\"color:red; background-color:white; font-size:130%; width:210px; margin-left:20px;\" onclick=\'this.form.submit();'>LOSR_OFF</button></h2><a style=color:#ffffff; font-size:830%>LOSR_STAT: "+stat_str2+"</a></h2></body></html>"); 
} 

void handleSave() { 
  if (server.arg("pass") != "") { 
    Serial.println(server.arg("pass")); 
  } 
} 

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 9);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 0, 0);



void setup() {

    pinMode(RELAY, OUTPUT); 
  digitalWrite(RELAY, HIGH);
  
    pinMode(RELAY2, OUTPUT); 
  digitalWrite(RELAY2, HIGH);
  



// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet)) {
  Serial.println("STA Failed to configure");
}
  
  
  EEPROM.begin(4096);
  int stat = EEPROM.read(0);
  digitalWrite(RELAY, stat);
  if(stat==0)stat_str="OFF";
  if(stat==1)stat_str="ON";

    EEPROM.begin(4096);
  int stat2 = EEPROM.read(0);
  digitalWrite(RELAY2, stat2);
  if(stat2==0)stat_str2="OFF";
  if(stat2==1)stat_str2="ON";
  
  delay(80000); 
  Serial.begin(115200); 
  Serial.println(); 

   WiFiManager wifiManager;

   wifiManager.autoConnect("Shayan_Home", "password333");
  
  Serial.println("stat: ");
  Serial.println(stat);
  
  Serial.println("stat2: ");
  Serial.println(stat2);
  
  Serial.print("Configuring access point..."); 
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
  }



  Serial.println(""); 
  Serial.println("WiFi connected"); 
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP()); 
  
  server.on("/", handleRoot);
  server.on ("/save", handleSave); 
  server.begin();
  Serial.println("HTTP server started");
  
    server.on("/RELAY_on", []() { 
    digitalWrite(RELAY, 1); 
    Serial.println("on"); 

    EEPROM.write(0,1);//addr,data
    EEPROM.commit();
    stat_str="ON";
    
    handleRoot(); 
  }); 
  server.on("/RELAY_off", []() { 
    digitalWrite(RELAY, 0); 
    Serial.println("off"); 

    EEPROM.write(0,0);//addr,data
    EEPROM.commit();
    stat_str="OFF";
    
    handleRoot(); 
  }); 

//////////////////////////////////////2

    server.on("/RELAY_on2", []() { 
    digitalWrite(RELAY2, 1); 
    Serial.println("on"); 

    EEPROM.write(0,1);//addr,data
    EEPROM.commit();
    stat_str2="ON";
    
    handleRoot(); 
  }); 
  server.on("/RELAY_off2", []() { 
    digitalWrite(RELAY2, 0); 
    Serial.println("off"); 

    EEPROM.write(0,0);//addr,data
    EEPROM.commit();
    stat_str2="OFF";
    
    handleRoot(); 
  }); 
  
}

void loop() {
  server.handleClient();
}

@Shayan_Gr do you have digital datastreams set-up in Blynk for pins GPIO0 and GPIO2 ?

Pete.

yes

Okay, that’s your problem.

You should use virtual pins instead…

Pete.