Skip to content

Instantly share code, notes, and snippets.

@vestige
Created January 26, 2025 07:24
Show Gist options
  • Save vestige/e46b331fee6910f07cd27dce9f8f52c0 to your computer and use it in GitHub Desktop.
Save vestige/e46b331fee6910f07cd27dce9f8f52c0 to your computer and use it in GitHub Desktop.
import time
import network
import ntptime
def connect_to_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('接続待ち...')
time.sleep(1)
if wlan.status() != 3:
raise RuntimeError('ネットワーク接続失敗')
else:
print('接続完了')
status = wlan.ifconfig()
print('IPアドレス = ' + status[0])
ssid = "imaizumi-G-accord"
password = "40714075"
connect_to_wifi(ssid, password)
ntptime.host = "time.cloudflare.com"
try:
ntptime.settime()
except:
print("時間の同期に失敗しました。")
raise
tm = time.localtime(time.time() + 9 * 60 * 60)
print("{0}/{1:02d}/{2:02d} {3:02d}:{4:02d}:{5:02d}".format(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment