Blynk timer/delay soil moisture rethink?

Hello everbody,

i am trying to get this code for arduino with delays changed in sonething that works without delay but with timer… timer.interval/timerTimeout…

/*  Soil Mositure Basic Example
    This sketch was written by SparkFun Electronics
    Joel Bartlett 
    August 31, 2015

    Basic skecth to print out soil moisture values to the Serial Monitor 

    Released under the MIT License(http://opensource.org/licenses/MIT)
*/

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 setup() 
{
  Serial.begin(9600);   // open serial over USB

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

void loop() 
{
Serial.print("Soil Moisture = ");    
//get soil moisture value from the function below and print it
Serial.println(readSoil());

//This 1 second timefrme is used so you can test the sensor and see it change in real-time.
//For in-plant applications, you will want to take readings much less frequently.
delay(1000);//take a reading every second
}
//This is a function used to get the soil moisture content
int readSoil()
{

    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
}

my setup is a "UNO+WiFi R3 ATmega328P+ESP8266 (32Mb memory), USB-TTL CH340G"from aliexpress

and a " Arduino Automatic Smart Plant Watering Kit" not the version on aliexpress, without the oled screen…

My code so far is…

// UnoCorn add
//#define BLYNK_PRINT Serial


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

Does anybody have an idea how to start? I got the sensor working on Uno with the above code… I just dont know where to begin with the read of analog with timer and not delays…

this BlynkTimer example sounds logical and then… euhm??

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  

who would have a smart idea how to? or how best to?

kind regards,

I think this document has some good examples of how to use Blynk timer instead of delays…

Pete.

Just put in your setup

timer.setInterval(60000L, myTimerEvent); //every 60 sec
1 Like

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