Timer Widget not triggering action

I’m having trouble getting the Timer Widget to trigger an action, even though I know that the widget is returning a value in the correct time-frame. Can someone please let me know if they see anything wrong with my code, or let me know what my problem may be? It’s an ESP32 dev board, iPhone, and bluetooth connection.

#define BLYNK_PRINT Serial

#define BLYNK_USE_DIRECT_CONNECT

#include <BlynkSimpleEsp32_BLE.h>
#include <BLEDevice.h>
#include <BLEServer.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "x";

BlynkTimer timer;

WidgetLED virtualLED(V2);

#define   LEDPin  18


void setup()
{
  // Debug console
  Serial.begin(9600);
  
  Blynk.setDeviceName("Tote");
  Blynk.begin(auth);

  pinMode(LEDPin,OUTPUT);
}


BLYNK_WRITE(V5) {
  if (param.asInt() == 1) {
    Serial.print("It worked!");
    virtualLED.on();
    digitalWrite(LEDPin, HIGH);
  }
  else {
    virtualLED.off();
    digitalWrite(LEDPin, LOW);
  }
}


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

In the app I have the timer widget and a value display both attached to V5.

The value display shows a 1 when the timer widget is in range, but I’m still not getting any action out of my serial monitor, virtual LED, or actual LED.

Thanks!

What happens if you do this:

int x = param.asInt();
if (x==1){

Pete.

Hi Pete!

Thanks, but still no luck…

Do you have multiple devices in your project? If so, make sure that V5 is attached to the device with the same Auth code as used in your code.

If this isn’t the cause then try adding a serial.println(x); immediately after int x = param.asInt(); to see what value you’re getting through from your widget (if anything).

Pete.

No just the one device, I know it’s connected. And I tried your new serial command… still nothing. It’s still switching from a 0 to a 1 in the app in the extra value display I tied to v5, but doesn’t trigger anything else in the code.

Maybe try regenerating the Auth code?

Pete.

Thanks Pete. Redid the Auth code as well as wrote another little script to execute every couple of seconds based on the timer.run()

Still, neither one is letting me get that param.asInt

have you tried removing the display linked to V5, and using another Vpin to display the value? Maybe the display is causing issues.

the new displays reading rate should be set to push

BLYNK_WRITE(V5) {

int x = param.asInt();
if (x==1){
     Serial.print("It worked!");
    virtualLED.on();
      Blynk.virtualWrite(V6, x);
    digitalWrite(LEDPin, HIGH);
  }
  else {
    virtualLED.off();
    Blynk.virtualWrite(V6, x);
    digitalWrite(LEDPin, LOW);
  }
}

Hi Toro, thank you.

I tried your code but it is not giving anything in the value display now, leading me to believe that for some reason it still just doesn’t want to let me do anything with the param.asInt

Add in a button, also on V5, and test with it

I tried that… works with the button.

Or the button triggers the actions I should say.

Perhaps the timer is just not triggering on the same time you think it is? If I recall correctly, it is controlled by the Server’s time, with your phone’s timezone correction, not your phone’s internal time.

Do you have the RTC widget setup correctly with your timezone?

If you have the button (or another display widget) on the same vPin as the timer, then as the timer triggers you would see the result in the other widgets as well.

The timer is triggering correctly based on my phone’s time, as indicated by the extra widget which returns a 1 during the timer. It just won’t push that value anywhere so I can’t actually trigger any actions. I’m thinking you’re right and there is just some kind of internal blockage because I’m not synced with the server.

I would like to get the RTC widget set up, but I haven’t been able to get it to work with the BLE connection. Is this possible? If I can use the RTC then I have other options as well like the advanced timer.

Another option would be returning my phone’s time as a string that lets me do some logic, but I haven’t been able to find anything in the forums about that and may start another thread because I know it’s off topic.

I was really just hoping there was a simple reason that the timer returns a 1 at the correct time but won’t let me use that to do an action.

I do/did have the RTC widget set up with my timezone btw.

I am confused… it is or isn’t working? Actualy I guess you couldn’t tell without setting that up in the sketch as well… and yes, I think the BT/BLE limitations would apply there as well.

Very long shot… and again, probably requiring your ESP to be linked with the internet.

Have you tried the Time Input Widget… more on-device code required, but perhaps it might work… but again, that assumes you can get RTC synchronization in your code over BT/BLE :thinking:

So much depends on that BT/BLE limitation… but since your ESP32 has perfectly good WiFi, then why not just use it? It is a lot like pushing your car around because it is “greener” that way :stuck_out_tongue:

RTC isn’t working… whenever I reset the ESP the time starts over at 0 so there is no server connection as expected.

I have a poor shared wifi connection and am also hoping to share the device that I’m making so I don’t want to have to code in different people’s wifi credentials to have it work… but it seems like if I want time related actions I’m against the wall.

I have been playing with the Time Input Widget. It seemed promising, but to have it make actions it seems like it will rely on the RTC again. The example code doesn’t use RTC though, so I’m still wondering if there may be a workaround.

Do you know of a way to get the time from my phone and store it as a value? It must be in there somewhere or I wouldn’t be getting the limited functionality out of the timer… The fact that the timer switches from 0 to 1 at the right time is what keeps irking me.

Well, since the App MUST have WiFi in order for the App to login (to the Server) and then allow your device to work on BT/BLE anyhow, then simply setting up a hotspot on your phone is an option.

But the Blynk Sharing feature would require your project to be always active… thus the phone in proximity to the ESP32.

The alternative of them using their own phone and App means you would have to give them access to your account in order to run your project… otherwise, even with their own account, you need to keep changing the AUTH on the ESP32 for it to work on the active account.

I think you need to rethink what you are trying to accomplish here… and share it in better detail, else we can’t offer decent suggestions.

You’re right, my order of operations was incorrect. I would still have to program in their authentication token if I gave them one of these things to run on their own. And I also didn’t realize that I was using the local wifi despite connecting to the device with bluetooth… so thank you for that as well.

I think it’s time to try to go back to wifi and RTC and save myself the headache.

Thank you for your insights!

Many others forget that Blynk is NOT a standalone App… but a full IoT system, App ↔ Server ↔ Hardware, all interconnected.

I think the initial BT/BLE device connection option was, like the USB Serial link, just meant for the earlier Arduinos and “benchtop” development uses, before the WiFi based ESP8266 really came into its own.