Skip to content

SNTP ​

Connect to WiFi and synchronize the device clock via NTP, so new Date() returns real wall-clock time. Uses the wifi and sntp APIs.

Hardware ​

  • Any ESP32 board with WiFi
  • USB cable
  • A WiFi network with internet access

Setup ​

Set your WiFi credentials as environment variables:

sh
pnpm mikro env set WIFI_SSID YourNetworkName --no-secret
pnpm mikro env set WIFI_PASSPHRASE
sh
npx mikro env set WIFI_SSID YourNetworkName --no-secret
npx mikro env set WIFI_PASSPHRASE
sh
yarn mikro env set WIFI_SSID YourNetworkName --no-secret
yarn mikro env set WIFI_PASSPHRASE
sh
bunx mikro env set WIFI_SSID YourNetworkName --no-secret
bunx mikro env set WIFI_PASSPHRASE

Code ​

ts
import {
env
} from 'mikro/env'
import {
sleep
} from 'mikro/sleep'
import {
sntp
} from 'mikro/sntp'
import {
wifi
} from 'mikro/wifi'
await
sleep
(2000)
console
.
log
('Current time at startup: %s', new
Date
())
const
ssid
=
env
.
require
('WIFI_SSID')
const
passphrase
=
env
.
require
('WIFI_PASSPHRASE')
// Connect to WiFi
console
.
log
(`Connecting to ${
ssid
}...`)
const
connectResult
= await
wifi
.
connect
({
ssid
,
passphrase
})
if (!
connectResult
.
ok
) {
console
.
error
('WiFi connect failed:',
connectResult
.
error
)
} else {
console
.
log
('Connected! IP: %s',
connectResult
.
value
.
ip
)
// One-shot time sync
console
.
log
('Syncing time...')
const
syncResult
= await
sntp
.
sync
({
timezone
: 'CET-1CEST,M3.5.0,M10.5.0/3'})
if (!
syncResult
.
ok
) {
console
.
error
('SNTP sync failed:',
syncResult
.
error
)
} else {
console
.
log
('Time synced: %s',
syncResult
.
value
.
time
)
console
.
log
('Current time: %s', new
Date
())
} }

Walkthrough ​

  1. Before sync. new Date() returns a timestamp near epoch 0 at startup, since the ESP32 has no battery-backed clock. The initial log shows this.

  2. WiFi first. Time sync requires an internet connection. The example connects to WiFi and checks the result before attempting SNTP.

  3. Timezone string. The timezone option uses POSIX TZ format. CET-1CEST,M3.5.0,M10.5.0/3 means Central European Time (UTC+1), with daylight saving (CEST) starting the last Sunday of March and ending the last Sunday of October.

  4. One-shot vs. background. sntp.sync() does a single time sync by default. Pass {background: true} to keep re-syncing periodically. The returned object includes a stop() function to cancel background sync.

Create project ​

sh
pnpm create mikro --template sntp
sh
npm create mikro -- --template sntp
sh
yarn create mikro --template sntp
sh
bun create mikro --template sntp

Run it ​

sh
pnpm install
pnpm mikro flash  # only needed once per board
pnpm mikro dev
sh
npm install
npx mikro flash  # only needed once per board
npx mikro dev
sh
yarn install
yarn mikro flash  # only needed once per board
yarn mikro dev
sh
bun install
bunx mikro flash  # only needed once per board
bunx mikro dev

The time jumps from near-epoch to the correct wall-clock time after the sync completes.

View source on GitHub

Mikro.js is built with AI assistance, code and docs alike. Read the AI disclosure.