RP2040 updating RTC from httpserver

Hi everyone, I am using raspberry pi pico with Arduino ide. I want to make http request to server then I want to take date and time from servers response. With this date and time, I want to update RTC. I am using this libraryexample . Here is my code ;


// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include <Timezone_Generic.h>             // https://github.com/khoih-prog/Timezone_Generic

// Example set at 05:00:00 Fri 21 Jan 2022 UTC or 00:00:00 Fri 21 Jan 2022 EST
datetime_t currTime = { 2022, 06, 07,05, 17, 30, 36 };
//////////////////////////////////////////
char buf[32];
void setup()
{
  Serial.begin(115200);
  delay(200);
  // Start the RTC
  rtc_init();
  rtc_set_datetime(DateTime);
}
void displayTime(){
  // Display time from RTC
  rtc_get_datetime(&currTime);
  DateTime now = DateTime(currTime);
  time_t utc = now.get_time_t();
  sprintf(buf,"%.2d-%.2d-%.2d %.2d:%.2d:%.2d",year(utc),month(utc),day(utc),hour(utc),minute(utc),second(utc));
  Serial.println(buf);

}
void loop()
{ 
  displayTime();
}
EthernetClient      client;
EthernetHttpClient  httpClient(client, serverAddress, port);
String path = "/tryingtoget/time";
httpClient.get(path);
String response = httpClient.responseBody();
StaticJsonDocument<200> doc;
deserializeJson(doc, response);
String date = (const char*)doc["date"];

After this htt request code, I am parsing date to int16_t. And then I am trying to use those values inside datetime_t currTime like this ;

  currTime = { i_year, i_month, i_date, i_hour, i_minute, i_second };

But I can't define currTime inside function or something. I always need to identify this currTime top of the code. So is there a way to update my RTC ?

I have no clue what your problem is.

But: The RP2040 has no network hardware on-chip. It might help to tell us what hardware you're using (wiring diagram).

Above code shows a sketch and an excerpt of some other code. Post the complete code you're working with!

Thank you. Sorry for late reply. Turns out, I could set the datetime in function with this code:

unsigned long epoch = 1654860535;
void setup()
{
  Serial.begin(115200);
  while (!Serial && millis() < 5000);

  delay(200);

  // Start the RTC
  rtc_init();
  Serial.println(epoch);
  time_t epoch_t = epoch;
  rtc_set_datetime(DateTime((uint32_t)epoch));
}
void displayTime()
{
  rtc_get_datetime(&currTime);

  // Display time from RTC
  DateTime now = DateTime(currTime);

  time_t utc = now.get_time_t();
  sprintf(buf,"%.2d-%.2d-%.2d %.2d:%.2d:%.2d",year(utc),month(utc),day(utc),hour(utc),minute(utc),second(utc));
  Serial.println(buf);
}
void loop()
{ 
  displayTime();
}

but I need to use epoch time, I couldn't find the set the datetime with char array , example (15:12:35 211/07/2022). This code use epoch time unsigned long epoch = 1654860535; which is equal , 2022 11:28:55 AM .

DateTime has several constructors defined, you don't have to use the one which uses the epoch input.

For example there is one that uses interger values for all data:
DateTime(year, month, day, hour, minute, second);

Parsing a string value to get these values is rather painful and error prone, so if you use constants anyway, use above constructor.

thank you.

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