GPIO activate when turn on

Hello friends,

I have an ESP32 connected on the 2.0 platform, and every time ESP is turned on, or restarted, the two GPIO I’m using are activated until ESP connects, is there any way to resolve this via software or hardware? That is, the GPIOs do not activate when powering on, or resetting the device.

Thanks

Can you post your whole sketch please ?

Hi John,


// 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 "TMPLHc_jQ6HO"
#define BLYNK_DEVICE_NAME "4 reles esp32"
#define BLYNK_AUTH_TOKEN "BBmmzNOqYKecgE3Kcv6HrE1rOI7a06bM"


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


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

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;




#define relay1_pin 0
#define relay2_pin 2


int relay1_state = 0;
int relay2_state = 0;




//------------------------------------------------------------------------------




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

  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);

  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, 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);
  //--------------------------------------------------------------------

}

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!
  
  listen_push_buttons();
}






void control_relay(int relay){
  //------------------------------------------------
  if(relay == 1){
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, relay1_state);
    Serial.print("Relay1 State = ");
    Serial.println(relay1_state);
    delay(50);
  }
  //------------------------------------------------
  else if(relay == 2){
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, relay2_state);
    delay(50);
  }

}

You should read this article

also if your relay is active low then replace 0 with 1

Your sketch is very strange, and I can’t work out whether this is because you’ve removed key parts of it (which you obviously have) or if its just very badly written,

What type of datastreams are you using, and how are they configured?

I suspect (and i think @John93 does too) that you have your relay logic reversed, nut without seeing your ACTUAL sketch and your datastream setup it’s impossible to tell.

Also, you should read this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Pete.

1 Like

Dear PeteKnight and John93,
first thanks for replying and helping, second, sorry I pasted in the wrong code, follow the correct code below, thanks.


// 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 "TMPLHc_jQ6HO"
#define BLYNK_DEVICE_NAME "4 reles esp32"
#define BLYNK_AUTH_TOKEN "BBmmzNOqYKecgE3Kcv6HrE1rOI7a06bM"


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


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

char auth[] = BLYNK_AUTH_TOKEN;

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

BlynkTimer timer;


#define button1_pin 23
#define button2_pin 22
#define button3_pin 33
#define button4_pin 32

#define relay1_pin 13
#define relay2_pin 12
#define relay3_pin 14
#define relay4_pin 27

int relay1_state = 0;
int relay2_state = 0;
int relay3_state = 0;
int relay4_state = 0;


//Change the virtual pins according the rooms
#define button1_vpin    V1
#define button2_vpin    V2
#define button3_vpin    V3 
#define button4_vpin    V4

//------------------------------------------------------------------------------
// This function is called every time the device is connected to the Blynk.Cloud
// Request the latest state from the server
BLYNK_CONNECTED() {
  Blynk.syncVirtual(button1_vpin);
  Blynk.syncVirtual(button2_vpin);
  Blynk.syncVirtual(button3_vpin);
  Blynk.syncVirtual(button4_vpin);
}

//--------------------------------------------------------------------------
// This function is called every time the Virtual Pin state change
//i.e when web push switch from Blynk App or Web Dashboard
BLYNK_WRITE(button1_vpin) {
  relay1_state = param.asInt();
  digitalWrite(relay1_pin, relay1_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button2_vpin) {
  relay2_state = param.asInt();
  digitalWrite(relay2_pin, relay2_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button3_vpin) {
  relay3_state = param.asInt();
  digitalWrite(relay3_pin, relay3_state);
}
//--------------------------------------------------------------------------
BLYNK_WRITE(button4_vpin) {
  relay4_state = param.asInt();
  digitalWrite(relay4_pin, relay4_state);
}
//--------------------------------------------------------------------------


void setup()
{
  // Debug console
  Serial.begin(115200);
  //--------------------------------------------------------------------
  pinMode(button1_pin, INPUT_PULLUP);
  pinMode(button2_pin, INPUT_PULLUP);
  pinMode(button3_pin, INPUT_PULLUP);
  pinMode(button4_pin, INPUT_PULLUP);
  //--------------------------------------------------------------------
  pinMode(relay1_pin, OUTPUT);
  pinMode(relay2_pin, OUTPUT);
  pinMode(relay3_pin, OUTPUT);
  pinMode(relay4_pin, OUTPUT);
  //--------------------------------------------------------------------
  //During Starting all Relays should TURN OFF
  digitalWrite(relay1_pin, HIGH);
  digitalWrite(relay2_pin, HIGH);
  digitalWrite(relay3_pin, HIGH);
  digitalWrite(relay4_pin, 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);
  //--------------------------------------------------------------------
  Blynk.virtualWrite(button1_vpin, relay1_state);
  Blynk.virtualWrite(button2_vpin, relay2_state);
  Blynk.virtualWrite(button3_vpin, relay3_state);
  Blynk.virtualWrite(button4_vpin, relay4_state);
  //--------------------------------------------------------------------
}

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!
  
  listen_push_buttons();
}

//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void listen_push_buttons(){
    //--------------------------------------------------------------------------
    if(digitalRead(button1_pin) == LOW){
      delay(200);
      control_relay(1);
      Blynk.virtualWrite(button1_vpin, relay1_state); //update button state
    }
    //--------------------------------------------------------------------------
    else if (digitalRead(button2_pin) == LOW){
      delay(200);
      control_relay(2);
      Blynk.virtualWrite(button2_vpin, relay2_state); //update button state
    }
    //--------------------------------------------------------------------------
    else if (digitalRead(button3_pin) == LOW){
      delay(200);
      control_relay(3);
      Blynk.virtualWrite(button3_vpin, relay3_state); //update button state
    }
    //--------------------------------------------------------------------------
    else if (digitalRead(button4_pin) == LOW){
      delay(200);
      control_relay(4);
      Blynk.virtualWrite(button4_vpin, relay4_state); //update button state
    }
    //--------------------------------------------------------------------------
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM




//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
void control_relay(int relay){
  //------------------------------------------------
  if(relay == 1){
    relay1_state = !relay1_state;
    digitalWrite(relay1_pin, relay1_state);
    Serial.print("Relay1 State = ");
    Serial.println(relay1_state);
    delay(50);
  }
  //------------------------------------------------
  else if(relay == 2){
    relay2_state = !relay2_state;
    digitalWrite(relay2_pin, relay2_state);
    delay(50);
  }
  //------------------------------------------------
  else if(relay == 3){
    relay3_state = !relay3_state;
    digitalWrite(relay3_pin, relay3_state);
    delay(50);
  }
  //------------------------------------------------
  else if(relay == 4){
    relay4_state = !relay4_state;
    digitalWrite(relay4_pin, relay4_state);
    delay(50);
  }
  //------------------------------------------------
}
//MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM

You appear to be using four GPIOs that are connected to relays, but you refer to “the two GPIO”.
That made sense with your original code, but not with your latest code.

I think you need to clarify EXACTLY what symptoms you are experiencing and which pins it is that you’re referring to.

You also need to start using the timer you are running, so that you can clean-up your void loop and get rid of those delays, as explained in the article I linked to before.

Pete

Check this example

Hello friend, the pins I mention (2) is because I’m only using two although the code is for 4, I only have one led and one buzzer, on gpio 13 and 12, as I’m just doing tests, I haven’t put anything on the other gpio , although they are programmed to activate relays.
I believe that Blynk is not the problem, but the ESP, which when turning on or restarting should send the pins down, this happened with ESP-01, I don’t know if it keeps happening with 12 and 32, I’ll try to check .

But it would be a big problem, assuming you are handling a machine, it will turn on for a few seconds at every restart, this is not desirable in any electronic circuit.

Yes, I’m going to study the timer library, that’s what it’s all about, studying and getting to know it in order to apply the tools.

Thank you for your help.

Thank you John93!

1 Like

Thank you!

This is a very useful topic, make sure to check it out

Relays come in two varieties - active LOW and active HIGH.
Some are capable of being changed from one to the other using a jumper.
Active LOW (relay coil energised when the GPIO is LOW) are the most common type.

The first thing you should do is to start by choosing the type of relay you are going to use, then structure your code and your datastreams accordingly and do some actual testing with this hardware and software setup.

Until you do this, and share data about the type of relay you are using, your datastream setup, your code and the results you are observing then we can’t really help.

Pete.

Dear friends ,

I am immensely grateful for the teachings and for sharing your knowledge with me.
PeteKnight , Thanks for your magnificent post on using the timer, a flawless gem that everyone should read before doing any projects here.

John93,

You don’t write much, but with the little you write you clarify everything, I used your indication and adapted the example you suggested, and the problem is solved.
Now the GPIOs don’t fire at every restart or when ESP is turned on, the problem was the delays, as you explained, the time it takes Blynk to connect to the ESP, it’s about 10 seconds, but if you use Delays, these will delay all the code, triggering some action even before the module connects, this is my conclusion, feel free to correct me if I’m wrong.
now that I have that sorted out, I’ll deal with the beautification only in the code and name changes like relay for example instead of ledpin, etc. and I’m going to assemble the hardware, as soon as I do that, I still hope, I’ll upload the project to my youtube channel, and I’ll publish the video link here.
Thanks.
Greetings!

Here the code:


/*************************************************************

  This example shows how to synchronize Button widget
  and physical button state.

  App project setup:
    Button widget attached to V2 (Switch mode)
 *************************************************************/

// 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 "TMPLHc_jQ6HO"
#define BLYNK_DEVICE_NAME "4 reles esp32"
#define BLYNK_AUTH_TOKEN "BBmmzNOqYKecgE3Kcv6HrE1rOI7a06bM"


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


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

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.

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

// Set your LED and physical button pins here
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 23;
const int ledPin4 = 27;

const int btnPin1 = 26;
const int btnPin2 = 25;
const int btnPin3 = 33;
const int btnPin4 = 32;

BlynkTimer timer;
void checkPhysicalButton();

int ledState1 = LOW;
int btnState1 = HIGH;
int ledState2 = LOW;
int btnState2 = HIGH;
int ledState3 = LOW;
int btnState3 = HIGH;
int ledState4 = LOW;
int btnState4 = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);
  Blynk.syncVirtual(V3);
  Blynk.syncVirtual(V4);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
  ledState1 = param.asInt();
  digitalWrite(ledPin1, ledState1);
}

BLYNK_WRITE(V2) {
  ledState2 = param.asInt();
  digitalWrite(ledPin2, ledState2);
}

BLYNK_WRITE(V3) {
  ledState3 = param.asInt();
  digitalWrite(ledPin3, ledState3);
}
BLYNK_WRITE(V4) {
  ledState4 = param.asInt();
  digitalWrite(ledPin4, ledState4);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnState1 != LOW) {

      // Toggle LED state
      ledState1 = !ledState1;
      digitalWrite(ledPin1, ledState1);

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

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

      // Toggle LED state
      ledState2 = !ledState2;
      digitalWrite(ledPin2, ledState2);

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

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

      // Toggle LED state
      ledState3 = !ledState3;
      digitalWrite(ledPin3, ledState3);

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

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

      // Toggle LED state
      ledState4 = !ledState4;
      digitalWrite(ledPin4, ledState4);

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

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);

  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, ledState1);

  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, ledState2);

  pinMode(ledPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(ledPin3, ledState3);

  pinMode(ledPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(ledPin4, ledState4);

  // Setup a function to be called every 100 ms
  timer.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  Blynk.run();
  timer.run();
  checkPhysicalButton();
}
1 Like

That explanation doesn’t make any sense.

It also seems that you’ve ignored the “keep your void loop clean” advice…

Pete.

That’s bad news, as the problem solved it, I figured that when I removed the delays and adapted the example that John93 suggested, for my needs, I thought everything was correct, but that’s why I asked them to correct me if I was wrong.
When you say, leave the void loop empty, do you mean to put nothing in it, just blynk.run? if so, where should i have put my timer, and where do the buttons function to make it work? sometimes an example is worth a thousand words, thank you if you can answer, thank you

If this line timer.setInterval(100L, checkPhysicalButton); is not necessary call it in The void loop?

The same with timer.run, no need placed in The void loop?

Thank in avançado for answer

Your void loop should look like this

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

Have you actually read and understood the document I linked to in post #5 ?

Pete.

Hi, yes, I read it superficially the other day, because it’s a long article and I tried to see what interested me most at the time, but you can be sure that I’ll devote a good part of my time to further reading, and I’ll see if I do one. pdf of the article to read offline, I’m sure I will understand.
I’m an electrical/electronics engineer, and I started with programming when I retired, I’m 68 years old but a lot of energy to spend, thank God, and if I cross paths with people like you on this path, I’m sure I’ll always improve.

yes you are right, there is no need to call the buttons function in the void loop, because it is already being called in the sketch outside the void, right?, I will remove it from void.
Thanks for sharing your time and your knowledge.

The checkPhysicalButton function is being called by a timer 10 times per second, so should not be in the void loop as well.

Also, as you are polling the physical buttons every 100ms you don’t need the debounce code, as the physical bounces much less time than this to subside. The debounce routine is only really needed of you attach interrupts to the pins rather than polling them with a timer.

Pete.

1 Like