-->
Page 1 of 1

Struggling to read a push button input on a ESP-01 module

PostPosted: Sun Jul 10, 2016 11:19 pm
by Dirka
Hello Guys and Girls

I'm new here - first post woohoo. Anyways, i've been getting my feet wet a bit with the ESP-01 and with the Blynk app. Just exploring at this stage and what I've gotten up to so far is to get a DHT22 linked up to the ESP-01 and to get the data through the Blynk app and to trend it using the Blynk app.

I was inspired by This Project so I decided that I wanted to try my hand at getting an internet connected relay module also using the Blynk app. So first off, what am I doing and what am I using:


Processor: ESP-01 Module
Breadboard Power Supply Module
Relay Module

The relay module activates the relay when a low input is given to the trigger so I had to write my code a bit in reverse.

For my push button input I followed the same wiring in the mentioned project except that I utilized a 12k resistor instead of a 1k resistor (Link to wiring picture). I also initially tried to us GPIO 0 as my button input and GPIO 2 as my relay trigger output. Unfortunately I couldn't get any response using GPIO 0 as my button input so I switched it around so that GPIO 2 is my button input and GPIO 0 is my relay trigger output.

My code as follows:

Code: Select all#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

char auth[] = "********************************************************";

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

int       Output_1             = 0;
int       Input_1              = 2;
WidgetLED Virtual_LED_1        = 1;

int       Button_State         = 0;
int       Prev_Button_State    = 0;
int       Relay_State          = 0;
int       Virtual_LED_State    = 0;
int       Virtual_Button_State = 0;

int       Time_Interval        = 1000;
int       Previous_Millis      = 0;

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

void setup()
{
  Blynk.begin(auth, "**************", "************");

  while (Blynk.connect() == false)            //  Go into a while program until Blynk has connected to prevent wrongly reported switch values
  {
    yield();                                  //  Do (almost) nothing -- yield to allow ESP8266 background functions
  }

  pinMode(Output_1, OUTPUT);
  pinMode(Input_1, INPUT);
  digitalWrite(Input_1, HIGH);                //  Have to write this pin high before entering the void loop otherwise this pin will be grounded
}

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

void loop()
{
  Blynk.run(); // Initiates Blynk
  Button_State = digitalRead(Input_1);

  if((Button_State == LOW ) || (Virtual_Button_State == HIGH))       //((Button_State == HIGH && Prev_Button_State == 0) || (Virtual_Button_State == HIGH))
  {
    Relay_State = 1;
    Prev_Button_State = 1;
  }
 
  if((Button_State == LOW ) || (Virtual_Button_State == LOW))
  {
    Relay_State = 0;
    Prev_Button_State = 0;
  }

  /*   
  if(millis() - Previous_Millis > Time_Interval)
  {
    Previous_Millis = millis();
  }
  */
   
  //*******************************************************************************************************

  if(Relay_State == 1)
  {
    digitalWrite(Output_1, LOW);      //  Relay module triggers high with a LOW input
  }

  if(Relay_State == 0)
  {
    digitalWrite(Output_1, HIGH);
  }

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

  Virtual_LED_State = digitalRead(Output_1);

  if(Virtual_LED_State == LOW)
  {
    Virtual_LED_1.on();
  }

  else
  {
    Virtual_LED_1.off();
  }
}

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

BLYNK_WRITE(V0)
{
  Virtual_Button_State = param.asInt();         // Get the state of the VButton
}

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


Everything works the way it should when I utilise only the app to control the relay output. But for the life of me I can't figure out how to use the push button input. Using the above mentioned wiring I know that I'm forcing my input from a high to a low so I should look for a low input on my input pin.

When I activate the relay using the app and then push the button, then whilst the button is engaged, the relay is disengaged. I figured out that since the Virtual_Button_State value was still high that it bypassed my press of the button:

Code: Select allif((Button_State == LOW ) || (Virtual_Button_State == LOW))


So I've removed the " || (Virtual_Button_State == LOW))" from my if statement to try and see whether I could get the system to work with only the push button but nothing. I only want to focus now on getting the push button to work, so I'm hoping that you guys and girls can give some pointers. Thank you in advance.

Regards
Dirk

Re: Struggling to read a push button input on a ESP-01 modul

PostPosted: Mon Jul 11, 2016 8:03 am
by martinayotte
You have 2 times "if((Button_State == LOW )", one bringing the "Relay_State = 1;" and the other "Relay_State = 0;", so this last one is always winning and your relay stays at 0.

Re: Struggling to read a push button input on a ESP-01 modul

PostPosted: Tue Jul 12, 2016 12:36 am
by Dirka
Hello Martinayotte

Thank you for the input. I've been working tonight to try and resolve the problem but I'm not quite there yet. I'll post another post if I have a follow-up question or if I get the code working then I will paste the code. Thanks again.

Regards
Dirk