Skip to Content

← All functionalities

Textile monitoring (weaving)ESP32 PLCNTPResilience / OTA

Keeping industrial timestamps honest with NTP plus a hardware RTC

An NTP-synced hardware RTC is what separates usable production data from garbage: a timestamp that resets to 1970 after every power cut makes daily log files and traceability IDs worthless. This example for the ESP32 PLC fetches time from pool.ntp.org with retries and writes it into the PLC's battery-backed RTC chip. The pattern runs in a real textile machine monitoring deployment where log filenames and message IDs all depend on it.

NTP sets the clock, the RTC keeps it

The ESP32's internal time dies with every reboot, and factory networks are not always up when the PLC powers on. So NTP is used only to set the hardware RTC chip — which keeps ticking on battery through reboots and outages. On a cold start without network, the firmware simply reads the last valid time from the chip and carries on with correct timestamps.

Retries and a last-sync diagnostic

getLocalTime() is polled up to ten times before the firmware gives up, because a congested plant network routinely drops the first NTP packets and a single attempt would fail far too often. Every successful sync records its epoch in a dedicated variable that both the embedded web server and the MQTT status commands expose — one glance at that value tells you whether a PLC's clock is fresh or has been free-running for weeks.

Periodic resync against drift

RTC crystals drift a few seconds per month, which sounds harmless until you try to correlate a stop event on one machine with a quality defect logged on another. A 12-hour resync timer keeps correcting the chip for as long as the network is available, so drift never accumulates. Timezone and daylight-saving offsets are handled by configTime, which means the daily log file rotation flips at real local midnight, not at some UTC-shifted hour.

A snippet from the implementation

Straight from the example as deployed on the ESP32 PLC — copy it freely:

void setup() {
  Serial.begin(115200);

  RTC.begin();                           // the chip time is already usable at boot

  checkWiFi();
  uint32_t t0 = millis();                // initial WiFi wait (max 15 s)
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < 15000) delay(500);

  if (!syncRTC())
    Serial.println("No NTP at startup: using previous hardware RTC time");
}

The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.

Frequently asked questions

What if the factory network has no internet access?

Point the firmware at an internal NTP server instead of pool.ntp.org — Node-RED hosts or any Linux server can serve NTP on the LAN. The code only needs the hostname changed.

Why not just use the ESP32 internal clock after one NTP sync?

The internal clock is lost on every reset and brownout. The hardware RTC chip on the PLC is battery-backed, so timestamps stay valid even after a weekend power-down.

How do I know a PLC clock has gone stale?

The firmware stores the timestamp of the last successful sync. Publish it in the periodic MQTT snapshot or show it on the status page; an old value means the device has not reached NTP for a while.

Related functionalities