Passing elements of time.h's struct tm to a client.println() statement [SOLVED]

I am wanting to printout the current processor time on a webpage that shows other queried data from the ESP32 processor. The issue is
I cannot define the integers hour, min, and sec outside the void printLocalTime() without generating a compiler error. But I need to reference those integers in a manner similar to:

StackString<50> LString = StackString<50>(" Current Pump Cycle [minutes] =  ");
LString.append(current_pump_cycle_min);
client.println(LString.c_str());
void printLocalTime() {    // this function monitors current time of day to initiate 
                           // pump counter reset at midnight
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  // Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");

  int hour = timeinfo.tm_hour;   
  int min  = timeinfo.tm_min;    
  int sec  = timeinfo.tm_sec;   
  
  if (hour == hour_trigger && min == min_trigger && sec == sec_trigger) {
    Serial.println("Reset counters...");
    counter_reset = true;
    delay(1000);    
  }

If there is a much more efficient straightforward way to add a processor's current time to a webpage I am all ears.
thanks...

Did the code you posted generate the error? If so, please post the complete compiler error listing. If not, post that and also the code that does.

Ok, I moved the integer definitions hour, min, sec out of void printLocalTime() and placed them in my constants.h file. After reading the entire error message I realized I had used "hour" in a previous declaration so I changed integers descriptors to time_hour, time_min, & time_sec to avoid that conflict. But I am still getting an error about 'timeinfo' was not declared in this scope:

WARNING: library StackString claims to run on avr architecture(s) and may be incompatible with your current board which runs on esp32 architecture(s).
In file included from C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\Pump_House_Monitor_ver8L.ino:19:
C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\constants.h:61:17: error: 'timeinfo' was not declared in this scope
 boolean counter_reset = false;
                 ^~~~~~~~
C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\constants.h:61:17: note: suggested alternative: 'time'
 boolean counter_reset = false;
                 ^~~~~~~~
                 time
C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\constants.h:62:17: error: 'timeinfo' was not declared in this scope
 
                 ^       
C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\constants.h:62:17: note: suggested alternative: 'time_min'
 
                 ^       
                 time_min
C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\constants.h:63:17: error: 'timeinfo' was not declared in this scope
 boolean bldg_alm_ON = false;
                 ^~~~~~~~
C:\Users\Ed\IDE 2.0 Sketches-Libraries\Pump_House_Monitor_ver8L\constants.h:63:17: note: suggested alternative: 'time_sec'
 boolean bldg_alm_ON = false;
                 ^~~~~~~~
                 time_sec
Multiple libraries were found for "WiFi.h"
  Used: C:\Users\Ed\AppData\Local\Arduino15\packages\esp32\hardware\esp32\2.0.9\libraries\WiFi
  Not used: C:\Users\Ed\IDE 2.0 Sketches-Libraries\libraries\WiFiNINA
exit status 1

Compilation error: 'timeinfo' was not declared in this scope

added constants.h segment containing timeinfo :

int hour_trigger = 23;        // hour to reset pump counters to zero
int min_trigger = 59;         // minute            (23 : 59 : 59)
int sec_trigger = 59;         // second             HR   MIN  SEC

int time_hour = timeinfo.tm_hour;   // tm structure element "hour" from time.h
int time_min  = timeinfo.tm_min;    //                     "minute"  "    "
int time_sec  = timeinfo.tm_sec;   //                      "second"
  

boolean counter_reset = false;

If you look at your printLocalTime() function, you will notice a declaration

struct tm timeinfo;

but in your constants.h file, you don't declare that anywhere. Any variables declared inside a function are only visible to that function.

Aha!!! I did not know this. So much to still learn obviously....lol

thank you very much.... :+1: :+1: :+1:

cppCopy code

int current_pump_cycle_min = 10; // Example value, replace with your actual variable
client.print("Current Pump Cycle [minutes] = ");
client.println(current_pump_cycle_min);

Replace current_pump_cycle_min with your actual variable representing the pump cycle duration in minutes. This code will print the message and the value to the client.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.