BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Blynk timer/delay soil moisture rethink?

Okay both answers where really great…

i just cant get my head around this part of the code

{

    digitalWrite(soilPower, HIGH);//turn D7 "On"
    delay(10);//wait 10 milliseconds 
    val = analogRead(soilPin);//Read the SIG value form sensor 
    digitalWrite(soilPower, LOW);//turn D7 "Off"
    return val;//send current moisture value
}

i want to check if i can rewrite this without the delay… maybe make it a void?

1 Like

I don’t think a delay of 10 milliseconds will give you any problems. Especially if you are only reading the sensor every couple of seconds.

The other option is to use a timeout


digitalWrite(soilPower, HIGH);//turn D7 "On"
     timer.setTimeout(10L, []() {  // Lambda Timer Function
     val = analogRead(soilPin);//Read the SIG value form sensor 
     digitalWrite(soilPower, LOW);//turn D7 "Off"
     return val;//send current moisture value
    });  // END
1 Like

problem is there will be minimal of 4 soil sensors working on the shield… so 4 times the delays? plus my control of the relays should keep working to?

1 Like

then go with option 2

will that not interfere with the connection to the blynk local server and such?

No it is non-blocking.

It is a very useful function, You should keep it saved for future use. Works with both setTimeout (run once), and setInterval (run continuously)

okay great…

would that interfere with the timer in my second code? (the one starting with UnoCorn add?

Not sure what your are referencing, but most likely not.

With the timer in this (now working version) ?

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;

char auth[] = "***********************";       // BlynkServer WasRechts account

char ssid[] = "###########";
char pass[] = "****************";
char server[] = "000.000.0.00";
int port = 0000;

//UnoCorn add
//Serial 1 naar Serial zonder 1
#define EspSerial Serial

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(0, 1); // RX, TX

// Your ESP8266 baud rate:
//UnoCorn add
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

//Smart Plant add
  // set water relays and water pump
int Pump = 7;     // Pomp 12volt
int Valve1 = 3;   // Valve 1
int Valve2 = 4;   // Valve 2
int Valve3 = 5;   // Valve 3
int Valve4 = 6;   // Valve 4

// Vpin sAFE? add
int v1update = 0;
int lastv1update = 0;
int buttonState = 0;

// VPIN NEW ADD
// This function will be called every time Slider or?and?Button? Widget
// in Blynk app writes values to the 
// Virtual Pin 1

BLYNK_WRITE(V1)
{
  
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  if (pinValue == 1) { 
    v1update = 1;
            
  }
  if (pinValue == 0) {
    v1update = 0;
            
  }
  
  if (v1update != lastv1update) {
    
    if (v1update == 1) {
//  vAlve1 & pomp AAN
    digitalWrite(Pump, HIGH); // Turn Pump ON.
    digitalWrite(Valve1, HIGH); // Turn Valve 1 ON.
    
  }
  
if (v1update == 0) {
//  vAlve1 & pomp UIT
    digitalWrite(Pump, LOW); // Turn Pump OFF.
    digitalWrite(Valve1, LOW); // Turn Valve 1 OFF.
  }
}

lastv1update = v1update;

}

//  Virtual Pin 2

BLYNK_WRITE(V2)
{
  
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  if (pinValue == 1) { 
    v1update = 1;
            
  }
  if (pinValue == 0) {
    v1update = 0;
            
  }
  
  if (v1update != lastv1update) {
    
    if (v1update == 1) {
//  vAlve2 & pomp AAN
    digitalWrite(Pump, HIGH); // Turn Pump ON.
    digitalWrite(Valve2, HIGH); // Turn Valve 2 ON
    
  }
  if (v1update == 0) {
//  vAlve2 & pomp UIT
    digitalWrite(Pump, LOW); // Turn Pump OFF.
    digitalWrite(Valve2, LOW); // Turn Valve 2 OFF.
  }
}

lastv1update = v1update;

}

BLYNK_WRITE(V3)
{
  
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  if (pinValue == 1) { 
    v1update = 1;
            
  }
  if (pinValue == 0) {
    v1update = 0;
            
  }
  
  if (v1update != lastv1update) {
    
    if (v1update == 1) {
//  vAlve3 & pomp AAN
    digitalWrite(Pump, HIGH); // Turn Pump ON.
    digitalWrite(Valve3, HIGH); // Turn Valve 3 ON.
    
  }
  if (v1update == 0) {
//  vAlve3 & pomp UIT
    digitalWrite(Pump, LOW); // Turn Pump OFF.
    digitalWrite(Valve3, LOW); // Turn Valve 3 OFF.
  }
}

lastv1update = v1update;

}

BLYNK_WRITE(V4)
{
  
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  // You can also use:
  // String i = param.asStr();
  // double d = param.asDouble();
  if (pinValue == 1) { 
    v1update = 1;
            
  }
  if (pinValue == 0) {
    v1update = 0;
            
  }
  
  if (v1update != lastv1update) {
    
    if (v1update == 1) {
//  vAlve4 & pomp AAN
    digitalWrite(Pump, HIGH); // Turn Pump ON.
    digitalWrite(Valve4, HIGH); // Turn Valve 4 ON.
  }
  if (v1update == 0) {
//  vAlve4 & pomp UIT
    digitalWrite(Pump, LOW); // Turn Pump OFF.
    digitalWrite(Valve4, LOW); // Turn Valve 4 OFF.
  }
}

lastv1update = v1update;

}

void setup()
{
  // Debug console
  //UnoCorn add
  //Serial.begin(9600);

  //delay(10);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

   //Smart PLant add
  pinMode(Pump, OUTPUT); // Pump als output
  pinMode(Valve1, OUTPUT); // Valve 1 als output
  pinMode(Valve2, OUTPUT); // Valve 2 als output
  pinMode(Valve3, OUTPUT); // Valve 3 als output
  pinMode(Valve4, OUTPUT); // Valve 4 als  

  Blynk.begin(auth, wifi, ssid, pass, server, port );
}

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

because there is allready a timer and timer.run in this code should i call one of them timer2.run? or something simular… ??

1 Like

No, one instance of Blynk timer can support 16 individual timers.

Pete.

1 Like

great :wink:
and how do i make a difference between the different timers in code then?

the name? or how?

What is it that you’re trying to achieve?

If you just want to set-up some timers that do things every so often then there no reason to identify them, just do them like this…

timer.setInterval(60000L,function1); //call function 1 every 60 sec
timer.setInterval(2000L,function2); //call function 2 every 2 sec

If you need to delete or start/stop a timer then you do this by obtaining the timer ID and referencing this. Is this something you need to do?

Pete.

1 Like

This may help.

https://playground.arduino.cc/Code/SimpleTimer/

1 Like

When i try your part of code


digitalWrite(soilPower, HIGH);//turn D7 "On"
     timer.setTimeout(10L, []() {  // Lambda Timer Function
     val = analogRead(soilPin);//Read the SIG value form sensor 
     digitalWrite(soilPower, LOW);//turn D7 "Off"
     return val;//send current moisture value
    });  // END

i get this error?

exit status 1
expected ‘)’ before ‘}’ token

or when i try ) before } and then ;
i get this error?

exit status 1
expected primary-expression before ‘)’ token

post the whole function. It could be a missing ) or } elsewhere

and eventualy this?

invalid user-defined conversion from ‘myTimerEvent()::__lambda0’ to ‘timer_callback {aka void (*)()}’ [-fpermissive]

What? :stuck_out_tongue:

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer; // Announcing the timer


char auth[] = "00000000000000000";   // Blynk_Server "hostname.try"
char ssid[] = "##########";
char pass[] = "**************";


//    Soil Moisture add
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.

void myTimerEvent()
{

digitalWrite(soilPower, HIGH);//turn D7 "On"
     timer.setTimeout(10L, []() {  // Lambda Timer Function
     val = analogRead(soilPin);//Read the SIG value form sensor 
     digitalWrite(soilPower, LOW);//turn D7 "Off"
     return val;//send current moisture value
    });  // END


void setup()
{
  // Debug console
  Serial.begin(115200);

  pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
  digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the

  //hostname add
  Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
  WiFi.hostname("Moisture.try");
  Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());

  Blynk.begin(auth, ssid, pass, IPAddress(***, ***, ***, ***), ****);

  timer.setInterval(1000L, myTimerEvent);

}


      
  void loop()
  {
    Blynk.run();
    timer.run(); // running timer every second
  }

You need a closing bracket on the myTimerEvent function.

Plus I would probably ditch the return, and just use the global variable.


/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer; // Announcing the timer


char auth[] = "00000000000000000";   // Blynk_Server "hostname.try"
char ssid[] = "##########";
char pass[] = "**************";


//    Soil Moisture add
int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = 7;//Variable for Soil moisture Power
//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.

void myTimerEvent()
{

digitalWrite(soilPower, HIGH);//turn D7 "On"
     timer.setTimeout(10L, []() {  // Lambda Timer Function
     val = analogRead(soilPin);//Read the SIG value form sensor 
     digitalWrite(soilPower, LOW);//turn D7 "Off"
     });  // END
} //you are missing this

void setup()
{
  // Debug console
  Serial.begin(115200);

  pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
  digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the

  //hostname add
  Serial.printf("Default hostname: %s\n", WiFi.hostname().c_str());
  WiFi.hostname("Moisture.try");
  Serial.printf("New hostname: %s\n", WiFi.hostname().c_str());

  Blynk.begin(auth, ssid, pass, IPAddress(***, ***, ***, ***), ****);

  timer.setInterval(1000L, myTimerEvent); // running timer every second

}

void loop()
  {
    Blynk.run();
    timer.run(); // running timer 
  }

ah okay great… THANKS :wink: i’ll try it asap

Okay great that worked :slight_smile: (the second time haha)

how would you use a global variant here?

i would like to use a virtual pin so i can it working on my Blynk local server and app on phone…