Einzelwerte aus einer Structure holen

Im Beispiel SimpleTime von der ESP32 library wird mit wenig code die Uhrzeit mit NTP syncronisiert und läuft dann im ESP32 RTC autark weiter.

#include <WiFi.h>
#include "time.h"

const char* ssid       = "YOUR_SSID";
const char* password   = "YOUR_PASS";

const char* ntpServer = "pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;

void printLocalTime()
{
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

void setup()
{
  Serial.begin(115200);
  
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  printLocalTime();
}

Die Funktion getLocalTime(&timeInfo) lädt die Daten (anscheinend mit sprint() in eine Structure, die gedruckt werden kann.

Wie kann man aber die Zeitdaten einzeln von der Structure in Variabeln bekommen?

Suche in time.h nach etwas wie (aus Time.h):

typedef struct  { 
  uint8_t Second; 
  uint8_t Minute; 
  uint8_t Hour; 
  uint8_t Wday;   // day of week, sunday is day 1
  uint8_t Day;
  uint8_t Month; 
  uint8_t Year;   // offset from 1970; 
} tmElements_t, TimeElements, *tmElementsPtr_t;

Mit dieser Struktur wäre die Antwort:

uint8_t stunde = timeinfo.Hour;

Probier es mal. Wenn es nicht funktioniert, tickt Deine Bibliothek anders oder ich falsch :slight_smile:

Guckst du die Definition der struct tm in deiner Library time.h an.

Hättest du eine

struct beispiel { int a; int b; } meinbeispiel ;

Könntest du z.B. schreiben

 meinbeispiel.a = 100;
  meinbeispiel.b = 0;
  Serial.print(meinbeispiel.a);Serial.print("/"); Serial.println(meinbeispiel.b);

Das problemchen ist, dass der ESP32 Ordner ganz vielen time.h Dateien beinhaltet.
In einer davon habe ich eine Struktur gefunden, die ich jetzt als Kommentar im SimpleTime Beispiel hinzugefügt habe:

// ...

void printLocalTime()
{
  struct tm timeinfo;
  /*
struct tm //as defined in time.h
{
  int tm_sec;
  int tm_min;
  int tm_hour;
  int tm_mday;
  int tm_mon;
  int tm_year;
  int tm_wday;
  int tm_yday;
  int tm_isdst;
}
 */  
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
}

/...

von der angezeigten Struktur verstehe ich den Bezug zu "%A, %B %d %Y %H:%M:%S" immer noch nicht. Kann mir einer das erklären?

verstehe ich den Bezug zu "%A, %B %d %Y %H:%M:%S" immer noch nicht

Den brauchst du auch nicht zu verstehen.
Jedenfalls nicht in Verbindung mit Serial.print.

WennSerial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");keinen Fehler liefert,
    [color=red]call of overloaded 'print(tm*, const char [])' is ambiguous[/color] (mindestens)
macht es jedenfalls nichts vernünftiges, behaupte ich mal.

Dass ein ESP32 kein richtiger Arduino ist, weiss ich selber. Aber diese Syntax passt dann doch nicht zur Arduino - IDE, finde ich.

michael_x:
Den brauchst du auch nicht zu verstehen.
Jedenfalls nicht in Verbindung mit Serial.print.

WennSerial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");keinen Fehler liefert,
   [color=red]call of overloaded 'print(tm*, const char [])' is ambiguous[/color] (mindestens)
macht es jedenfalls nichts vernünftiges, behaupte ich mal.

Dass ein ESP32 kein richtiger Arduino ist, weiss ich selber. Aber diese Syntax passt dann doch nicht zur Arduino - IDE, finde ich.

Das muss man für einen ESP32 board z.B. LoLin32 kompilieren, sonst kann es natürlich nicht funktionieren.

Es druckt ein schön formatiertes Datum / Uhrzeit : "Saturday, March 31 2018 14:05:55"

Die Einzelzeitdaten habe ich herausgefunden.

#include <WiFi.h>
#include "time.h"

const char* ssid       = "SSID";
const char* password   = "PASS";

const char* ntpServer = "de.pool.ntp.org";
const long  gmtOffset_sec = 3600;
const int   daylightOffset_sec = 3600;
int second;
int minute;
int hour;
int day;
int month;
int year;
int weekday;
long current;
struct tm timeinfo;
  /*
struct tm
{
int    tm_sec;   //   Seconds [0,60]. 
int    tm_min;   //   Minutes [0,59]. 
int    tm_hour;  //   Hour [0,23]. 
int    tm_mday;  //   Day of month [1,31]. 
int    tm_mon;   //   Month of year [0,11]. 
int    tm_year;  //   Years since 1900. 
int    tm_wday;  //   Day of week [0,6] (Sunday =0). 
int    tm_yday;  //   Day of year [0,365]. 
int    tm_isdst; //   Daylight Savings flag. 
}
 */  

void printLocalTime()
{

  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return;
  }
  Serial.println(&timeinfo, "%A, %d %B %Y %H:%M:%S");
}

void setup()
{
  Serial.begin(115200);
  
  //connect to WiFi
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
  Serial.println(" CONNECTED");
  
  //init and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  printLocalTime();

  //disconnect WiFi as it's no longer needed
  WiFi.disconnect(true);
  WiFi.mode(WIFI_OFF);
}

void loop()
{
  delay(1000);
  
  printLocalTime();
  second = timeinfo.tm_sec;
  minute = timeinfo.tm_min;
  hour = timeinfo.tm_hour;
  day = timeinfo.tm_mday;
  month = timeinfo.tm_mon + 1;
  year = timeinfo.tm_year + 1900;
  weekday = timeinfo.tm_wday +1;
  Serial.print("Time from variables:  ");
  Serial.print(day);
  Serial.print(".");
  Serial.print(month);
  Serial.print(".");
  Serial.print(year);
  Serial.print(" --- ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

Das muss man für einen ESP32 board z.B. LoLin32 kompilieren, sonst kann es natürlich nicht funktionieren

Hab ich schon befürchtet. :wink:

Klar gibt es strftime
Ich wundere mich eher, wofür alles die Arduino IDE missbraucht wird.

Aber es ging ja eigentlich um ganz Grundsätzliches (Einzelwerte aus einer struct holen)