-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathntpclient_test1.py
50 lines (43 loc) · 1.64 KB
/
ntpclient_test1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
from machine import RTC, Pin
import uasyncio as asyncio
import utime
import ntpclient
async def test1_square(pin):
rtc = RTC()
while True:
if sys.platform == 'esp32':
# We want maximum precision, so we let await get us within 2ms,
# then use a blocking utime.sleep_us() to strike. Every now and
# the something will interfere ... meh.
delay = (1000000 - rtc.datetime()[7]) // 1000
if delay > 4:
await asyncio.sleep_ms(delay - 2)
delay = (1000000 - rtc.datetime()[7])
if delay > 0:
utime.sleep_us(delay)
elif sys.platform == 'esp8266':
# ESP8266 only has milliseconds, so no point in the above.
delay = (1000 - rtc.datetime()[7])
if delay > 0:
await asyncio.sleep_ms(delay)
else:
raise RuntimeError("unsupported platform '{}'".format(sys.platform))
# Turn the pin on and print the current time on REPL
pin.value(1)
now = utime.localtime()
print("{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}".format(*now))
# Wait for 100ms
if sys.platform == 'esp32':
delay = (100000 - rtc.datetime()[7]) // 1000
elif sys.platform == 'esp8266':
delay = (100 - rtc.datetime()[7])
if delay > 0:
await asyncio.sleep_ms(delay)
# Turn the pin off
pin.value(0)
def run(pps = None, **kwargs):
pps_pin = Pin(pps, mode = Pin.OUT)
client = ntpclient.ntpclient(**kwargs)
asyncio.create_task(test1_square(pps_pin))
asyncio.run_until_complete()