Enable Sensor via Button in App

Hi!

My ESP32 is connected to my local blynk server via GSM.
To save some data volume, sensor values are only transferred all x milliseconds using a blynk timer.

Now I have a sensor (gyroscope) that I only need in some situations and therefore want to have the ability to manually turn it on, for example by using a button in the app.

Is it possible to set up a button, which triggers the transfer of sensor data for X seconds, while the rest of the time the sensor data is neither read nor transferred?

Thanks!

It is possible to enable/disable Blynk Timers within your sketch.
BlynkTimer is based on SimpleTimer, so reading the SimpleTimer documentation will show you how to do this.

The thing to look out for is that timers are enabled and disabled by referencing their timer ID, which are assigned dynamically throughout the process. to ensure that you are referencing the correct timer you need to assign variables to hold these timer IDs and use these variables in your enable/disable commands. That means that you may have code which looks something like this

int check_connections_timer_id;
const int check_connection_millis = 30000;

void loop()
{
  check_connections_timer_id = timer.setInterval(check_connection_millis, Check_Connections); 
  timer.disable(check_connections_timer_id);

and then at some point you can enable that timer again…

timer.enable(check_connections_timer_id);

The problem is that there is a bug in the SoftwareSerial code when you try to work with that timer that has ID=0. The simplest way is therefore to ensure that the very first timer declared in your void setup is a sacrificial timer that will never actually be used.
That can be done like this…

timer.setInterval(3600000, [] () {}); // Dummy timer in slot zero to overcome bug in the simple timer library when timer 0 is disabled

I thought that I’d done some tests to prove that this issue only existed when using SimpleTimer rather than BlynkTimer, but I’m told that this issue does still exist in BlynkTimer, so it’s safest to simply add this dummy sacrificial timer in there.

This timer issue has been discussed at length in other topics if you want to read more, but I’d certainly recommend googling the SimpleTimer documentation and reading it.

Pete.