Hi
I used a simple from library to run relay module with D7 output and a push button on Blynk app.
when I connect the USB cable to the ESP card and run the project on Blynk app the D7 getting active and make the relay module to be energized while the bush button on OFF mode .I also changd the output from 1 to 0 and still same.
please see screenshot
The first thing to do is to work-out if your relay is activated when a LOW signal or a HIGH signal is applied to it. Most relay boards that you buy on the internet seem to be active LOW.
You could add a pinMode statement followed by a a digitalWrite statement in your void setup, to set your relay pin to the correct status. I would also suggest using virtual pins and modifying your code to change the status of your relay when the virtual pin state changes.
Hi
With is modification I’m getting now at connecting the ESP to power bank it is switched ON then OFF
please see the code and advice if I miss something
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
BLYNK_CONNECTED()
{
// if your relay is HIGH triggering
if (digitalRead(2) == 1) Blynk.virtualWrite(V7, digitalRead(2));
else Blynk.syncVirtual(V7);
/* uncomment this section if your relay is LOW triggering
* if (digitalRead(2) == 0) Blynk.virtualWrite(V2, digitalRead(2));
else Blynk.syncVirtual(V2);
*/
}
BLYNK_WRITE(V7)
{
int pinData = param.asInt();
digitalWrite(2, pinData);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH); // if your relay is HIGH triggering
// digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
//Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
}
void loop()
{
Blynk.run();
}```
Sorry,I didn’t got your point
I want the relay to be OFF at startup and to be ON when push the button ON Blynk app.
I dont want from relay to be energized once plug the cable to power bank.
Okay let’s take a few steps back…
Are you using a bare relay, or one on a board with some other components?
If it’s the latter, how are the connecting pins on the board labelled?
How are you wiring these pins up to power the relay?
Nothing connects to V7 other than the switch widget. V7 is a Virtual pin - it doesn’t physically exist, so you can’t connect a relay to it.
When the status of the widget attached to pin Vt changes, it triggers the BL6NK_WRITE(V7) callback function.
In that function you have code which writes a value to GPIO2, and as I said earlier, GPIO2 is the pin that’s labelled D4 on your NodeMCU.
So In1 on your relay should connect to pin D4 on your NodeMCU.
You still need to know if your relay activates on a HIGH or LOW signal. If connecting In1 to GND energises the relay then it’s active LOW. If connecting In1 to VCC energises the relay then it’s active HIGH.
the relay still ON and OFF at the moment of plugging the USB cable to PC or power bank even I did the changes as described and hope it is correct ,please see the new code and advise if there is any correction needed
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
BLYNK_CONNECTED()
{
// if your relay is HIGH triggering
if (digitalRead(2) == 0) Blynk.virtualWrite(V7, digitalRead(2));
else Blynk.syncVirtual(V7);
/* uncomment this section if your relay is LOW triggering
* if (digitalRead(2) == 0) Blynk.virtualWrite(V2, digitalRead(2));
else Blynk.syncVirtual(V2);
*/
}
BLYNK_WRITE(V7)
{
int pinData = param.asInt();
digitalWrite(2, pinData);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH); // if your relay is HIGH triggering
// digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
//Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
}
void loop()
{
Blynk.run();
}
Thank you Pete, that’s very helpful excel sheet ,it is work without blinking in the powering up but since I’m new in coding I think I have destroyed the whole code after embedding the code of triggering to the code of water level because the Blynk app now controlling only the push button but not readig the rest of the code for water level measurement , please advise what is the mistake in the code below in order to execute the overall steps
#define TRIGGER D0
#define ECHO D2
// NodeMCU Pin D0 > TRIGGER | Pin D2 > ECHO
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = ""; //
char pass[] = ";//
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
BLYNK_CONNECTED()
{
// if your relay is HIGH triggering
if (digitalRead(5) == 0) Blynk.virtualWrite(V7, digitalRead(5));
else Blynk.syncVirtual(V7);
/* uncomment this section if your relay is LOW triggering
* if (digitalRead(2) == 0) Blynk.virtualWrite(V7, digitalRead(5));
else Blynk.syncVirtual(V7);
*/
}
BLYNK_WRITE(V7)
{
int pinData = param.asInt();
digitalWrite(5, pinData);
}
void setup()
{
// Debug console
Serial.begin(115200);
pinMode(5, OUTPUT);
digitalWrite(5, HIGH); // if your relay is HIGH triggering
// digitalWrite(5, HIGH); // uncomment this if your relay is LOW triggering
pinMode(5, OUTPUT);
digitalWrite(5, HIGH); // if your relay is HIGH triggering
// digitalWrite(2, HIGH); // uncomment this if your relay is LOW triggering
//Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
}
void loop() {
long duration, distance;
digitalWrite(TRIGGER, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
duration = pulseIn(ECHO, HIGH);
distance = (duration/2) / 29.1;
if (distance <=100) {
Blynk.virtualWrite(V0, 80);
}
else {
Blynk.virtualWrite(V0, 0);
}
if (distance <= 75) {
Blynk.virtualWrite(V1, 100);
}
else {
Blynk.virtualWrite(V1, 0);
}
if (distance <= 50) {
Blynk.virtualWrite(V2, 100);
}
else {
Blynk.virtualWrite(V2, 0);
}
if (distance <= 25) {
Blynk.virtualWrite(V3, 100);
}
else {
Blynk.virtualWrite(V3, 0);
}
if (distance <= 5) {
Blynk.virtualWrite(V4, 100);
}
else {
Blynk.virtualWrite(V4, 0);
}
Serial.print(distance);
Serial.println("Centimeter:");
Blynk.virtualWrite(V5, distance);
delay(100);
Blynk.run();
}
You need to read this and follow the example of how to use a timer to call a function - which in this case will take readings from your sensor and display the results in Blynk.
It’s also not a good idea to mix the way that you refer to your pins. I would encourage you to stop using the “D” numbers that are screen-printed on your NodeMCU and instead use GPIO numbers. That way it’s easy to see when you’ve referenced the same pin twice for two different uses.
I changed it as per your advice and it is work perfectly
thank you for your time and help
still one more question how can I make the distance between the the water top level read 100 % in the Gauge of the Blynk app when the distance is around 20 cm between the HCR04 and watewr level when it is in maximum level.
shall I post this question here or in another post ??
Keep it in this topic.
The series of if statements that were in your void loop are where the percentage full numbers come from for each distance.
At the moment, they don’t make much sense, as the options are 0%, 75% and 100%
You should start by working out what distance reading you want to translate into which percentages, and how many steps you want 0, 25, 50, 75, 100 for example, or 0, 10, 20, 30, 40 etc.
You could show the level in the gauge widget without having to use if statements.
Just set the values in the gauge widget to match the values of full and empty. You should have them reversed in the widget, that is the larger value (tank empty) first, and the smaller value (tank full) second. This would make it so the smaller value (tank full) would show the gauge as full.