Activate relay by app and automatically

Hello guys! I’m using google translator to translate to english, sorry if any part gets weird

I’m starting to use Blynk and I’m having a problem in my college project with WeMos , my goal is to build an automatic irrigation system, I want it to work in two ways, the first is that it be activated through a button (ja got it), and another is that it works automatically. This automatic way is based on soil moisture, the problem is that if it’s automatic, I can’t activate the button through the app, I’d like it to work both ways, so when I’m not at home and without internet, the activation of the system happens alone, and if I am, turning on the button it works. My code below.
Capturar

Hey there.

First don’t use delay in the void loop, try to keep it clean as possible, check this :
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Second are you using the old blynk or the new one ?

Greetings.

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();    
  }```