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:
pnpm mikro env set WIFI_SSID YourNetworkName --no-secret
pnpm mikro env set WIFI_PASSPHRASEnpx mikro env set WIFI_SSID YourNetworkName --no-secret
npx mikro env set WIFI_PASSPHRASEyarn mikro env set WIFI_SSID YourNetworkName --no-secret
yarn mikro env set WIFI_PASSPHRASEbunx mikro env set WIFI_SSID YourNetworkName --no-secret
bunx mikro env set WIFI_PASSPHRASECode
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
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.WiFi first. Time sync requires an internet connection. The example connects to WiFi and checks the result before attempting SNTP.
Timezone string. The
timezoneoption uses POSIX TZ format.CET-1CEST,M3.5.0,M10.5.0/3means Central European Time (UTC+1), with daylight saving (CEST) starting the last Sunday of March and ending the last Sunday of October.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 astop()function to cancel background sync.
Create project
pnpm create mikro --template sntpnpm create mikro -- --template sntpyarn create mikro --template sntpbun create mikro --template sntpRun it
pnpm install
pnpm mikro flash # only needed once per board
pnpm mikro devnpm install
npx mikro flash # only needed once per board
npx mikro devyarn install
yarn mikro flash # only needed once per board
yarn mikro devbun install
bunx mikro flash # only needed once per board
bunx mikro devThe time jumps from near-epoch to the correct wall-clock time after the sync completes.