I am doing an IoT project. My IoT device (hardware with Arduino) is measuring some temperature information every minutes. And sending these data to the server. Hardware has a display. That shows the temperature value and the time. I want to sync this time with the server time. I can't use NTP as I am not working with UDP. Can I use HTTP to syn time? any suggestions please


Use NTP. It worked for the computers of 1985, it can work on today's small devices.

Extremely likely that a NTP daemon or library already exists. If not, writing basic SNTP code is possible with any IP stack. A good reason to understand UDP on your network.

HTTP Date header, if a web server has accurate time, provides one second precision. openntpd uses this, but only as a sanity check constraint. Warning that the date format isn't very machine readable. Easy enough with say Python, but if you had Python you could just use ntplib...

| improve this answer | |

If your Arduino device is simply working with HTTP requests, like sending a simple HTTP GET request to the server with the gathered sensor data, you could either:

  • Return the server time information within the GET request body and use the data to periodically sync the time of the arduino device.
    • For example call: GET http://yourserver/sensordatacollector/record?temperature=22&humidity=45
    • Use the response datetime values that are returned
  • Or implement a separate HTTP endpoint to the server, which provides the time information. This time endpoint is periodically called by the Arduino device, which the syncs the time of the device.
    • For example call: GET http://yourserver/sensordatacolletor/datetime
    • use the response datetime values to sync the Arduino

Also see https://arduinojson.org/v6/example/http-client/ for reference.

Edit: If possible, you could also take advantage of already open APIs that serve datetime information, like: http://worldtimeapi.org/

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.