Synchronise RTC using mobile phone

Hello. I am working on a project using the AMB82 which is connected to an internal RTC (3231). While that's working fine, the issue is how to synchronise the RTC based on the local time (e.g. timezone different to the original RTC location). I have been able to set up the AMB82 as a wifi AP and can connect my phone to it and was thinking that I can 'get' the phone's date and time through HTML or Javascript?? Any thoughts if that's possible, or any other way (without any app on the phone, of course)? Thanks for any pointers.

What is that? Please post a link.

Could you enable the AP (Hotspot) on your phone and allow this AMB82 thing to connect to the internet through your phone? If so, it could get the time from NTP servers and use this to update the RTC.

Maybe this board

Hi, yes, that's correct. It's a great product!

➜ Developing a captive portal on the device that would expose fields to fill in the date and time would probably work well. On the ESP platform you have WiFiManager that could be a source of inspiration.

Hi, and thanks for the suggestion. However, there's no UI on the board - it's inside a waterproof enclosure, so the only access is through a mobile phone via wifi.

Hi - yes, that's what I've been experimenting with. That got me connected OK, but not sure how to get to the next step of having a web page appear on the phone, so I can get the date and time sent to the processor. I assume it's possible to host a simple web page on the AMB82, or some other automatic method (ideally) to synchronise the local RTC with the phone's date and time.

I don't know the AMB82. When you say

do you mean the WifiManager library or the concept of captive portal ?

The wifimanager library.

The example I've modified slightly is here: WiFi – Set up WiFi AP Mode – Realtek IoT/Wi-Fi MCU Solutions

I don't know if it's compatible with your board

Have you tested with the basic example

Then I went onto this example, which works well at sending data: WiFi – Simple Http Server to Receive Data – Realtek IoT/Wi-Fi MCU Solutions

Thanks. I haven't tries that one, but generally you get a warning, but it still works. Will this provide the time and date of the connected client (mobile phone)?

OK, looks similar to what I've been using, and I can get the connection. It's the next step - how to get the date and time.

What I am envisioning is a simple web page appears on the phone and there's a 'synchro' button that then sends the date and time to the AMB82.

I didn't imply there was....

How do you update its firmware?

That library can embed custom HTML so you could do something like this

#include <WiFiManager.h>
WiFiManager wm;
WiFiManagerParameter custom_date_param("date", "Enter Date (YYYY-MM-DD)", "", 11);
WiFiManagerParameter custom_time_param("time", "Enter Time (HH:MM)", "", 6);

void saveDateTimeCallback() {
  Serial.println("[CALLBACK] Form submitted!");
  Serial.printf("Received Date: %s\n", custom_date_param.getValue());
  Serial.printf("Received Time: %s\n", custom_time_param.getValue());
  wm.stopConfigPortal();
}

void setup() {
  Serial.begin(115200);
  while (!Serial) yield();
  Serial.println("\nStarting");

  WiFi.mode(WIFI_AP);

  wm.setConfigPortalBlocking(true);
  wm.addParameter(&custom_date_param);
  wm.addParameter(&custom_time_param);
  wm.setSaveParamsCallback(saveDateTimeCallback);

  // Only show the "setup" page and "exit" button
  std::vector<const char *> menu = {"param", "exit"};
  wm.setMenu(menu);

  // add a small JavaScript for auto-filling the fields with the client's date & time
  wm.setCustomHeadElement("<script>"
                          "function setDateTime() {"
                          "  var now = new Date();"
                          "  var date = now.toISOString().split('T')[0];"
                          "  var time = now.toTimeString().split(' ')[0].substring(0, 5);"
                          "  document.getElementById('date').value = date;"
                          "  document.getElementById('time').value = time;"
                          "}"
                          "window.onload = setDateTime;"
                          "</script>");

  // Start the configuration portal with custom SSID
  wm.startConfigPortal("SetDateTimeAP");
}

void loop() {}

Typed here, mind typos. This should create an access point named SetDateTimeAP and when you select this access point on your phone or computer, a web page should appear with a setup and cancel button and if you click on the setup you should see two textfields, one for the time, one for the date and those will auto-fill based on your client's date & time. so you just save that and the call back in the ESP code will be called where you can capture the strings, parse them and set the info in the RTC.

In my code I just print that out and ends the captive portal session.

Wifi manager offers also an OTA option.

Hi, I have to take it back to the office, remove it from the enclosure. It's normally going to be out in the field, doing monitoring.

Thanks for this, I shall study it. Many places where the monitoring is done do not have internet, so OTA is not going to be practical for updating firmware. Anyway, the items are moved around often.