BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

[SOLVED] Looking for advice (on a touch sensor based relay control with nodemcu)

Hello Blynk team ,
You guys have done a wonderful thing by creating Blynk … hats off .

I am currently working on a touch sensor based relay control with nodemcu . Now i am planning to integrate Blynk to the program that i have made. With real time reflection of what state( relay on or off ) is it on the Blynk app ?
Actually with virtual pins i have made one but doesn’t work well .
If i can do that , what is procedure? Please help me .
Thank you in advance.

You posted to a topic that is well over a year old :wink: So I moved your question to your own topic.

And as for your question… we need much more information on what you have and what you are trying to do.

Please post your existing code here so we can see it, otherwise we are just guessing.

Paste the code and explain what is not working very well including Serial Monitor output etc.

#define D0 16
#define D1 5 
#define D2 4 

#define D4 2
#define D5 14 
#define D6 12  
#define D7 13 
#define D8 15 
#define D9 3 
#define D10 1 
#define BLYNK_PRINT Serial  // comment this out to disable prints to save place
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
bool rele;




char auth[] = "**************************";
char ssid[] = "******";
char pass[] = "**************";
char server[] = "xxx.xxx.x.xxx";


void setup()
  {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass, server);
  pinMode(D2, OUTPUT);
  pinMode(D4, OUTPUT);
  pinMode(D5, OUTPUT);
  pinMode(D8, OUTPUT);
  pinMode(D1, INPUT);
  pinMode(D0, INPUT);
  pinMode(D6, INPUT);
  pinMode(D7, INPUT);
  }


  BLYNK_WRITE(V0)
  {
    rele = param.asInt();
    digitalWrite(D2, rele);
  }

  BLYNK_WRITE(V1)
  {
    rele = param.asInt();
    digitalWrite(D4, rele);
  }

  BLYNK_WRITE(V2)
  {
    rele = param.asInt();
    digitalWrite(D5, rele);
  }

  BLYNK_WRITE(V3)
  {
    rele = param.asInt();
    digitalWrite(D8, rele);
  }

  void loop()
  {
    if(digitalRead(D1))
    {
      rele = !rele;
      digitalWrite(D2, rele);
      Blynk.virtualWrite(V0, rele);
      delay (100);
    }

    if(digitalRead(D0))
    {
      rele = !rele;
      digitalWrite(D4, rele);
      Blynk.virtualWrite(V1, rele);
      delay (100);
    }

     if(digitalRead(D6))
    {
      rele = !rele;
      digitalWrite(D5, rele);
      Blynk.virtualWrite(V2, rele);
      delay (100);
    }

     if(digitalRead(D7))
    {
      rele = !rele;
      digitalWrite(D8, rele);
      Blynk.virtualWrite(V3, rele);
      delay (100);
    }
    Blynk.run();
  }
/////////////////////////////////////////////////

With this i am actually able to control all the 4 relays , but when is use touch sensor to turn on the relays, one or the other turn on automatically randomly , and cannot turn off all the relays using touch. But with Blynk app its possible to turn off all the relays. And when i keep holding the touch sensor , the relays will start clicking instead of keeping its state. In serial monitor everything seems good. Please help me.

Thank you in advance.

@Madhukesh I am looking at the following code in your loop()

if(digitalRead(D1))
{
  rele = !rele;
  digitalWrite(D2, rele);
  Blynk.virtualWrite(V0, rele);
  delay (100);
} 

The rele = !rele; suggests to me that you are trying to toggle the relays with the touch sensors but the if statement will prevent this. The if is checking for HIGH status (OFF, on an active LOW relay) but once the relay has switched ON the touch sensor will have no way of ever going OFF.

You would need an if / else statement BUT with a relayState variable to only trigger the relays when the state (touch or not touch sensor) changes. Or the relays will be clicking away like mad.

1 Like

Hello,

I am trying to integrate existing touch based relay program with Blynk, with real time update on Blynk app and also to work the relays from Blynk as well. Here is th program made using only Blynk. The down side is when the touch sensor is held continuously the relay starts to latch and unlatch continuously. so i made a program without Blynk and that works well, now i want that to be integrated to Blynk. I tried using Blynk Virtual , Blynk Write , but unable to get the expected result. can any one help ?

#define RELE D7
#define BUTTON D1
#define BLYNK_PRINT Serial  // comment this out to disable prints to save place
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
bool rele;



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


void setup()
  {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(RELE, OUTPUT);
  pinMode(BUTTON, INPUT);
  }


  BLYNK_WRITE(V0)
  {
    rele = param.asInt();
    digitalWrite(RELE, rele);
  }

  void loop()
  {
    if(digitalRead(BUTTON))
    {
      rele = !rele;
      digitalWrite(RELE, rele);
      Blynk.virtualWrite(V0, rele);
      delay (600);
    }
    Blynk.run();
  }
///////////////////////////////////////////////////////////////////

Touch program that needs to be integrated with Blynk… Help Please !!!

#define TouchSensor 9 
 
int relay = 2; 

boolean currentState = LOW;
boolean lastState = LOW;
boolean RelayState = LOW;
 
void setup() {
  Serial.begin(9600);
  pinMode(relay, OUTPUT);  
  pinMode(TouchSensor, INPUT);
}
 
void loop() {
  currentState = digitalRead(TouchSensor);
    if (currentState == HIGH && lastState == LOW){
    Serial.println("pressed");
    delay(1);
    
    if (RelayState == HIGH){
      digitalWrite(relay, LOW);
      RelayState = LOW;
    } else {
      digitalWrite(relay, HIGH);
      RelayState = HIGH;
    }
  }
  lastState = currentState;
}

It looks like you have already taken parts out of this example… but you need to move your button / relay function out of the void loop()… go back and re-examine how the timer works.

https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=More%2FSync%2FSyncPhysicalButton

As for the rapid cycling back and forth, part of that might be reduced by a proper timed function, but you can also put in a logic latch that determines if the touch sensor (or button) has been released (since last time) before running the relay function. I.e:

Timer Function says run button function

If button/touch shows released, then carry on and run relay function
   if not released (AKA still being held down) then ignore relay function

Loop around to Timer Function

Relay Function toggles relay open or closed depending on present state.
1 Like

Thank you for the reply…
I am putting the touch function on loop because when the server goes down we cannot also use the physical touch as well. is there any better way of doing it ?
Thank you in advance…

@Madhukesh the normal recommendation is to use BlynkTimer and despite the name it’s nothing to do with Blynk so it will work when you don’t have access to the server.

However, simple, very fast, routines like digitalRead() and digitalWrite() are generally OK in Blynk’s loop() as long as they are covered by “button state” routines i.e. they are not repeating hundreds of time a second.

1 Like

@Madhukesh study the following sketch:

/* TouchSensor.ino (not Boolean operator)  https://community.blynk.cc/t/looking-for-advice-on-a-touch-sensor-based-relay-control-with-nodemcu/15686/6

 Based on WeMos and internal pullup resistor on D3, GPIO 0

 TODO:
   1. Add button debounce code, study also ISR (interrupt service routines) 
   2. Add sync
   3. Add connection management
   4. Add timed events with BlynkTimer

*/   

#define BLYNK_PRINT Serial  // comment this out to disable prints to save place
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int relay;  // doesn't need to be global but will probably as the sketch develops

char auth[]   = "xxxx";
char ssid[]   = "xxxx";
char pass[]   = "xxxx";
char server[] = "blynk-cloud.com";

#define TouchSensor 0  // D3 has pullup resistor on a WeMos
 
int LED = 2;           // D4 BUILTIN_LED but also physical pin for relays

boolean currentState = LOW;
boolean lastState = LOW;
boolean RelayState = LOW;
bool booted = false;

BLYNK_WRITE(V9)
{
  relay = param.asInt();
  digitalWrite(LED, !relay);
  Serial.println(relay);  // 1 is ON, 0 is OFF
}
 
void setup() 
{
  Serial.begin(115200);
  Serial.println();
  pinMode(LED, OUTPUT);   
  digitalWrite(LED, HIGH);  // LED / Relay OFF on reboot

  pinMode(TouchSensor, INPUT_PULLUP);  
  Blynk.begin(auth, ssid, pass, server);
}              
 
void loop() {
  currentState = digitalRead(TouchSensor);
  if(booted == true)    // booted is hack to cover pin status at first boot
  {
    if (currentState == HIGH && lastState == LOW) 
    {
      Serial.println("Pressed");
      delay(1);
      
      if (RelayState == HIGH)
      {
        digitalWrite(LED, LOW);
        RelayState = LOW;
        Serial.println("ON");
        Blynk.virtualWrite(V0, 1); // could perhaps enclose this with if(Blynk_connected())
      } else 
      {
        digitalWrite(LED, HIGH);
        RelayState = HIGH;
        Serial.println("OFF");
        Blynk.virtualWrite(V0, 0); // could perhaps enclose this with if(Blynk_connected())
      }
    }
  }
  else
  {
    booted = true; // hack to cover pin status at first boot
    Serial.println("Booted");
  }
  lastState = currentState;
  Blynk.run();
}
2 Likes

As stated, your sketch would keep running if disconnected anyhow (within limitations), regardless of where your touch function resided, in the void loop() or being called by a timer.

It is just considered good Blynk practice to keep the void loop() as clear as possible, unless you are a skilled programmer or you are lucky and only placed fast functions in there :wink:

Check out Avoiding the void in this article, it covers the timer option as well:

Hello Costas !!

I went through the code, and everything seems right . What happens is when we switch on the relay with touch sensor , it reflects on the Blynk app (expected). here comes the problem… now when we try to switch on or off the relay using the touch , it takes two presses to either on or off depending on what we did in the Blynk app. ((Basically if the relay is turned on or off by Blynk and we need to turn on or off the relay takes two touches to sync with whats being done on Blynk app…))
I guess this is the problem alone with the touch problem not the Blynk part , not sure… :neutral_face:

I’ll leave that for you to work out.

1 Like

Madhukesh, would you post the full code please?