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.
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.
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.
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 !!!
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.
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.
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.
/* 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();
}
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
Check out Avoiding the void in this article, it covers the timer option as well:
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…