Please be kind as i am guessing this is probably easy for most of you.
I'm an old Basic/Cobol programmer from the 70's and struggling learning C++
My problem... I am trying to extract the date/time from a function in the Samples/esp32/Time/time sample sketch.
I know this should be easy but everything I try doesn't give the results I am expecting.
Line 21...... Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
displays the returned date and time as expected.
but lines 27,28,29
day == String((&timeinfo, "%A"));
month == String((&timeinfo, "%B"));
year == String((&timeinfo, "%Y"));
dosnt return anything, the variables day,month and year still hold the initial values.
How do I get the values returned into the strings.
I have tried all sorts but just end up with %A or nothing.
( I need to be able to tag the date and time to the end of a string I push out to call me bot in another sketch
String url = "https://api.callmebot.com/whatsapp.php?phone=" + telephoneNumber + "&apikey=" + whatsappAPIKey + "&text=" + urlEncode("** Laser Stopped ! ** ") +count + date + time;
#include <WiFi.h>
#include "time.h"
#include "esp_sntp.h"
const char *ssid = "Paul2";
const char *password = "abcdeabcde";
const char *ntpServer1 = "pool.ntp.org";
const char *ntpServer2 = "time.nist.gov";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = 0;
String day = "Tuesday";
String month = "December";
String year = "1965";
const char *time_zone = "CET-1CEST,M3.5.0,M10.5.0/3"; // TimeZone rule for Europe/Rome including daylight adjustment rules (optional)
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("No time available (yet)");
return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.println(&timeinfo, "%A");
Serial.print( "Day before ");
Serial.println(day);
day == String((&timeinfo, "%A"));
month == String((&timeinfo, "%B"));
year == String((&timeinfo, "%Y"));
Serial.print( "Day ");
Serial.print(day);
Serial.print( " Month ");
Serial.print(month);
Serial.print( " year ");
Serial.println(year);
}
// Callback function (gets called when time adjusts via NTP)
void timeavailable(struct timeval *t) {
Serial.println("Got time adjustment from NTP!");
printLocalTime();
}
void setup() {
Serial.begin(115200);
// First step is to configure WiFi STA and connect in order to get the current time and date.
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
/**
* NTP server address could be acquired via DHCP,
*
* NOTE: This call should be made BEFORE esp32 acquires IP address via DHCP,
* otherwise SNTP option 42 would be rejected by default.
* NOTE: configTime() function call if made AFTER DHCP-client run
* will OVERRIDE acquired NTP server address
*/
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);
/**
* This will set configured ntp servers and constant TimeZone/daylightOffset
* should be OK if your time zone does not need to adjust daylightOffset twice a year,
* in such a case time adjustment won't be handled automagically.
*/
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer1, ntpServer2);
/**
* A more convenient approach to handle TimeZones with daylightOffset
* would be to specify a environment variable with TimeZone definition including daylight adjustmnet rules.
* A list of rules for your zone could be obtained from https://github.com/esp8266/Arduino/blob/master/cores/esp8266/TZ.h
*/
//configTzTime(time_zone, ntpServer1, ntpServer2);
}
void loop() {
delay(5000);
printLocalTime(); // it will take some time to sync time :)
}