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

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