Activate relay by app and automatically

Hi!

Thanks for the help, I’m using the old version.
I’m a beginner in programming, I’ll be changing the delay.

Okay, the easiest way is using eventor widget.
Have you ever tried it ?

no, can you show me an example?

Read this :

Hi!

To use the event widget will I need to use virtual pins? I say this because to activate the relay by the button I’m using D5, and for soil moisture the A0, how can I make the change?

Check this out :

Hi!

the button with the virtual pin is already working, how do I send the value of A0 to a gauge or labeled value through V2?

[Unformatted code removed by moderator]

You would use

Blynk.virtualWrite(V2, valoranalog);

Try this :

void sendSensor2()
{
  int sensorvalue=analogRead(sensorpin);
  outputvalue=map(sensorvalue,0,1023,0,100);

  if(outputvalue>74)
  {
       Serial.println("water your plant");
       Serial.print(outputvalue);
       
  }
  else if(outputvalue<45)
  {
       Serial.println("soil is wet enough to water");
       Serial.print(outputvalue);
       

}
Blynk.virtualWrite(V5, outputvalue);
}

@Bruno_Smicaluk please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Pete.

I tried both ways but the gauge is still not getting any value. I’ll send the entire code, maybe it’s a syntax error.

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


char ssid[] = "";       
char pass[] = "";
char auth[] = ""; 
BlynkTimer timer;
boolean stateled=0;
boolean prevStateled=0;

void setup()
{
  // Debug console
  Serial.begin(115200);
  pinMode(14,OUTPUT);  
  pinMode(A0, INPUT);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);

}

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)
  {
    digitalWrite(14,HIGH);
  }
  else{
    digitalWrite(14,LOW);
  }
}

BLYNK_WRITE(V2)
{
  int valoranalogico = param.asInt();
  valoranalogico = analogRead(A0);
  Blynk.virtualWrite(V2, valoranalogico);
}


void loop()
{
  if (Blynk.connected())
  {
    Blynk.run();
  }
   timer.run();    
  }
      
 
BLYNK_CONNECTED()
{
  Blynk.syncAll();
  
}```

Assign gauge to V5 and change this

To V5 instead of V2

You can’t use port 8442 with blynk-cloud.com
It needs to be port 8080 or port 80.

Sharing information about what you see in your serial monitor, and the online status in the Blynk legacy app would be useful.

Pete.

No it’s a coding error. You need to use a timer to read the analog pin at a certain interval and then take that reading and send it to the virtual pin.

Take a look at this example. You would follow the same format.

void myTimerEvent()
{
  int valoranalogico = param.asInt();
  valoranalogico = analogRead(A0);
  Blynk.virtualWrite(V2, valoranalogico);
}

and add this to your setup()

timer.setInterval(1000L, myTimerEvent);

A value appeared in the gauge that does not change, I put a labeled value and verified that it is counting the seconds.

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

char ssid[] = "";       
char pass[] = "";
char auth[] = ""; 
BlynkTimer timer;
boolean stateled=0;
boolean prevStateled=0;

void myTimerEvent()
{
  Blynk.virtualWrite(V2, millis() / 1000);
}

void setup()
{
  Serial.begin(115200);
  pinMode(14,OUTPUT);  
  pinMode(A0, INPUT);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
}

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)
  {
    digitalWrite(14,HIGH);
  }
  else{
    digitalWrite(14,LOW);
  }
}

BLYNK_WRITE(V2)
{ 
  int valoranalogico = param.asInt();
  valoranalogico = analogRead(A0);
  Blynk.virtualWrite(V2, valoranalogico);
  timer.setInterval(1000L, myTimerEvent);
}

void loop()
{ 
   Blynk.run();
   timer.run();    
  }```

This code will ONLY be called when the value of the widget attached to V2 changes in the app. You should not be putting code that needs to be called on a timed basis inside a BLYNK_WRITE(vPin) callback function.
That code should be inside a function such as your myTimerEvent function, and this line of code…

should only be present inside your void setup (which it already is) so this duplicate setInterval definition needs to be deleted.

Pete.

I think you need to take another look at what I had posted, and review the example a little more as well. The example is more to give you an idea of how to use a timer to pass data to a virtual pin, not to copy for your application.

The code snippet I posted would replace that part of the example, and is more specific to what you need.

Once you get the data displaying properly on the gauge, then just add in your code for the button.

It is already running. I also fixed the event widget to fire according to humidity. However, when soil moisture is below 40 the relay should be activated, but it increases to 1540 every second and the relay is deactivated, what is the solution?

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

char ssid[] = "";       
char pass[] = "";
char auth[] = ""; 
BlynkTimer timer;
boolean stateled=0;
boolean prevStateled=0;
int valoranalogico;
int porcento;

void myTimerEvent()
{
  Blynk.virtualWrite(V2, millis() / 1000);
  valoranalogico = analogRead(A0);
  porcento = map(valoranalogico, 1023, 0, 0, 100);
  Blynk.virtualWrite(V2, porcento);
}

void setup()
{
  Serial.begin(115200);
  pinMode(14,OUTPUT);  
  pinMode(A0, INPUT);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
}

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)
  {
    digitalWrite(14,HIGH);
  }
  else{
    digitalWrite(14,LOW);
  }
}

void loop()
{ 
   Blynk.run();
   timer.run();    
  }```

Unless you share what you’ve set-up in Eventor then we can only see part of the picture.
My advice would be not to use Eventor at all, and do it in your sketch.

Pete.

Capturar158 Uploading: Capturar158.JPG…