wifi + ntp (micropython + jupyter notebook)
プログラムの書き方
MicroPython の ESP32 API リファレンスのネットワーキング を参照する. wifi への接続部分をそのまま使えば良い.
また, NTP に関連する部分は, MicroPython のPython 標準ライブラリとマイクロライブラリの utime 時間関連の関数 を参照する.
プログラム例
1 import network
2 import ntptime
3 import utime
4
5 def do_connect():
6 import network
7 wlan = network.WLAN(network.STA_IF)
8 wlan.active(True)
9 if not wlan.isconnected():
10 print('connecting to network...')
11 wlan.connect('YOUR_SSID', 'YOUR_PASSWD')
12 while not wlan.isconnected():
13 pass
14 print('network config:', wlan.ifconfig())
15
16 def get_jst():
17 tm = utime.localtime(utime.time()) # UTC now
18 jst = str(tm[0])+'/'+str(tm[1])+'/'+str(tm[2])+' '+str((tm[3]+9)%24)+':'+str(tm[ 18 4])+':'+str(tm[5])
19 return jst
20
21 do_connect()
22
23 ntptime.settime() # setup time by remote NTP server
24 tt = get_jst()
25 print(tt)

参照
- NTP 部分については <URL:https://beta-notes.way-nifty.com/blog/2020/03/post-c320f1.html> を参照した.