Blynk timer/delay soil moisture rethink?

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…

I was just saying to use the variable val, instead of having it returned to the function.

If you are looking to display the value from the sensor on the app, you would just add in a virtual write.

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"
     // Send val to virtual pin 5 on BLYNK
     Blynk.virtualWrite(V5, val);

     });  // END
} //you are missing this

Should work just the same on a local server provided you have it set-up correctly.

yes that would be handy using more then one sensor…
do they work independent from each other ?
Or do timers wait for the one before it… or would i work if the first interval is longer then the one before it? :thinking:

correctly i dont know? :smirk:
but it works and it restarts… for now haha… i still need to google and check how certificates and what not works?

i didn’t know that this would work as easy as adding a dht22 sensor virtual write…
so check if it works on the d1mini…
and then make it bigger for the UNO WiFi R3 ATmega328P (add four sensors and so on :stuck_out_tongue:
Thanks a million btw :wink:

You’re using a hardware device that isn’t capable of multi-tasking, so it can only perform one operation at any one time.
The Blynk/Simple timer is a non-blocking command, but the functions that you call with it can block the execution of your code. Blocking the code execution is not a good thing when using Blynk, as the Blynk library needs processor time to maintain communication with the server.

Because of this, you should try to avoid calling timed functions in a what where they will coincide with each other. Setting-up two timers that both have an interval of 2000 ms will mean that both timers try to execute at virtually the same time, and this can cause problems.

In the example I gave, both timers will coincide with each other, as 60000 is divisible by 2000.
There are several threads about different techniques for preventing timers from coinciding and if you’re having problems with missed readings, or Blynk disconnections then you should search for them and study the discussions on the subject.

Pete.