Latching input to ESP12 which latches in Blynk - possible maybe?

Hello, hope someone may be able to help me?

I have an ESP12 which using the following code below works very well. However, is there a way that I could alter the script so that I could pulse the input once (D0) and the blynk app will notify with “Dads Up” and stay like that until the esp12 input is pulsed a second time and it says “Dads gone to bed”, then repeat cycle. Does this sound do-able? Is it the code I have to change or some other setting in Blynk? Many thanks in advance.


#define BLYNK_PRINT Serial

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

char auth[] = "qZyN0qCrl585pcUy-N6tS**********************";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "BTHub5-12";
char pass[] = "*************";

SimpleTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (1).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000);

int RelayInput = D0; // Reed sensor

int flag = 0; 

void setup()
  // Debug console
  Blynk.begin(auth, ssid, pass);
 pinMode(RelayInput, INPUT_PULLUP); 


void loop()
{;; // Initiates BlynkTimer   


void sensorvalue1()

if((digitalRead(RelayInput) == LOW) && ( flag == 0  ))
  Blynk.virtualWrite(V2,0 ); //turns off the led
  Blynk.virtualWrite(V3,255 ); // turns on the led
 Blynk.notify("Dad's gone to bed."); 
  flag = 1; 

if((digitalRead(RelayInput) == HIGH) && ( flag == 1) )
  Blynk.virtualWrite(V2,255 ); // turns on the led
  Blynk.virtualWrite(V3,0 ); // turns off the led 
  Blynk.notify("Dads up."); 
  flag = 0; 

Have I upset anyone?:thinking:

At first glance, it appears that your code already does what you want it to.
Maybe you should explain what it currently achieves, and why that is different to what you want it to achieve.


Hi Peter, at the moment the code works as it should, ie when D0 goes low, blynk indicates this via notifications to my phone and tells me “dad has gone to bed”, D0 is still held low. When D0 goes high, again blynk pushes notifications “dads up” - D0 is still high. What I would rather have if it is at all possible is, when D0 is triggered low for say a second and then goes high, it pushes to my phone and says “dads gone to bed”. Next trigger of D0 for a second, it pushes notification “dads up”. Until the next time D0 is triggered it pushes “dads gone to bed” and so on. The reason is as currently, D0 is triggered by a relay to ground, and I’m not keen for the relay being on and getting rather warm.

Kind regards


“dads gone to bed” that sounds fishy :stuck_out_tongue_winking_eye::stuck_out_tongue_winking_eye:

Personally, I’d use a CHANGE interrupt, and maybe a bit of denouncing code if you get multiple interrupt triggers when the reed switch actuated.


I can see a situation where the two may get out of sync. That is “dads gone to bed”, but in fact he is up. As when the device first starts you will need to assign an initial state (either in bed or up), since it has no feedback as to what dad is actually doing at the time of start-up. So if the device resets (power outage, etc.) and dad is not where he should be based off of the start-up condition, you may end up out of sync. Or if he happens to move while in bed, and creates a false trigger (not sure how you are actually detecting if he is in bed or not).

One solution might be to rethink the use of a relay as you triggering device, and keep the code as it is. Maybe use an IR Proximity Sensor, or a “SoftPot”, or a pressure sensitive mat/switch, etc…

As to what you are currently asking, there is an example on the Sketch Builder that may be a good starting place. It uses a push button for toggling a relay/LED.

1 Like

Hi Toro_Blanco

Many thanks for your input. It goes like this: when my wifes father goes to bed, he switches the house alarm onto part set. So the esp is being triggered on D0 with the relay from the alarm. If or when he gets up and his intention is to go downstairs, he will switch the alarm off, which in turn switches off the relay which then opens D0 from ground and is pulled high. Its he fact that having the relay on all night getting warm is whats bothering me. I have had a look at the change interupt that Pete mentioned - I need to have another look at it to get me head around it… I shall now have a look at the example that you have suggested. Thank you


So wouldn’t this relay be turned on regardless of what is attached to it (your ESP or nothing at all). It sounds like this is part of the alarm system, and as such should be designed so that it can handle being on for long periods of time.

If it is a relay that you added, what is the triggering voltage? Maybe you can switch the type of relay so that long periods of activation do generate heat. I use THESE in some of my projects, and while they do require the use of some type of board (protoboard, or custom), they do not generate any heat.

Afternoon Toro_Blanco

I’ve had a look at these, of course either SS relay or opto- isolator is the way forward. I would love to understand interrupts, but its a concept I cannot seem to understand, shall keep looking at them though.



I now understand how an interrupt works, damn that youtube is good. I have now got an opto isolator 4N35 in circuit and it is working exactly as I want it too. Many thanks for your help.