Trying to obtain initial values from Blynk app

Hi, I’m trying to create a program that runs a stepper motor at a preset time, I have setup the time on the template in the Blynk app and I am trying to use Blynk.syncAll() or Blynk.syncVirtual(V1,V2,V3) but it always comes back blank or zero.
can someone help me see what I’m doing wrong?

Thanks,

// Blynk and wifi code omitted. It connects to wifi and Blynk

#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"
//#include <AccelStepper.h>
#include <BlynkSimpleEsp32.h>
#include <Stepper.h>

// Stepper Motor Pins 
#define IN1 25
#define IN2 26
#define IN3 27
#define IN4 14
#define STEPS_PER_REV 2048  // Adjust according to your motor
const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
bool manualControl = false;
unsigned long duration = 0;
unsigned long startTime1 = 0;
const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = -18000;
const int daylightOffset_sec = 3600;
const char *time_zone = "EST5EDT,M3.2.0,M11.1.0";  
struct tm timeinfo;
int hour = 0;
int minutes = 0;
int seconds = 0;
int day = 0;

#define START_TIME_VPIN1 V1
#define DURATION_VPIN V2
#define MANUAL_START_VPIN V3

BLYNK_CONNECTED() {
  // Request Blynk server to re-send latest values for all pins
  printLocalTime();
  Blynk.syncAll();
  Blynk.syncVirtual(V1,V2,V3);
  Serial.println(" Start Time: " + startTime1);
  Serial.println(" Duration:   " + duration);
}
// first feeding
BLYNK_WRITE(START_TIME_VPIN1) {
  int startTime1 = param.asInt(); // Start time in seconds since midnight
}

BLYNK_WRITE(DURATION_VPIN) {
  duration = param.asInt();  // Convert to milliseconds
}

BLYNK_WRITE(MANUAL_START_VPIN) {
  if (param.asInt() == 1) {
    manualControl = true;
    Serial.println("Manual Start Triggered!");
    myStepper.step(stepsPerRevolution);  
  } else {
    manualControl = false;
    Serial.println("Manual Stop Triggered!");
  }
}

void printLocalTime() {
//  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("No time available (yet)");
    return;
  }
}
void timeavailable(struct timeval *t) {
  Serial.println("Got time adjustment from NTP!");
  printLocalTime();

}
void setup() {
  Serial.begin(9600);
  myStepper.setSpeed(15);
  Blynk.begin(BLYNK_AUTH_TOKEN, WIFI_SSID, WIFI_PASSWORD);
  esp_sntp_servermode_dhcp(1);  // (optional)

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" CONNECTED");

  // set notification call-back function
  sntp_set_time_sync_notification_cb(timeavailable);

  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);

 }
void loop() {
  Blynk.run();
  delay(1000);
  printLocalTime();  // it will take some time to sync time :)
  Serial.println(" Start Time: " + startTime1);
  Serial.println(" Duration:   " + duration);
// always blank

}

serial console (I had some serial.println() for time removed them)
11:57:38.293 → Thursday, March 06 2025 11:57:43
11:57:38.334 → Start Time:
11:57:38.334 → uration:
11:57:38.334 → Thursday, March 06 2025 11:57:43
attached screenshot from app.

thanks in advance,

Sam

You need to read the documentation for the Time Input widget…

I’d recommend that you don’t use Blynk.syncAll()

You shouldn’t use delays either, and certainly not in your void loop.

Pete.

Thanks,

I read the documentation before I posted.
the problem was that I defined a variable inside:
BLYNK_WRITE(START_TIME_VPIN1) {
int startTime1 = param.asInt(); // Start time in seconds since midnight
}
removed the int and the value appeared.
Everywhere I read, all the examples have delays inside the loop.

thanks for your help

Really? Would you like to share some of those?

You should read this…

Pete.

1 Like

Seriously dude?
I asked for help and not only did you not help, all you did is critiqued. do a search on your own.
you “recommended” I don’t use Blynk.syncAll() but you don’t say why or even provide an alternative.
you say I shouldn’t use delays but no reason why or no alternative either.

/beginsarcasm
THANKS FOR YOUR “HELP”
/endsarcasm

You’re welcome!

Pete.