Skip to content

Instantly share code, notes, and snippets.

@vestige
Created April 27, 2025 07:08
Show Gist options
  • Save vestige/ff5c11e6eec248694a074e9c9b22b03a to your computer and use it in GitHub Desktop.
Save vestige/ff5c11e6eec248694a074e9c9b22b03a to your computer and use it in GitHub Desktop.
import time
import network
import ntptime
#Wi-Fiへの接続
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])
# 今日はその月で何回目の曜日かを調べる関数
def get_NthDayOfWeek(day):
# 日付を7で割ることで何回目の曜日かを計算
nth_day_of_week = (day - 1) // 7 + 1
return nth_day_of_week
# 自宅Wi-FiのSSIDとパスワードを入力
ssid = 'YOUR NETWORK SSID'
password = 'YOUR NETWORK PASSWORD'
# Wi-Fiに接続
connect_to_wifi(ssid, password)
# NTPサーバーとして"time.cloudflare.com"を指定
ntptime.host = "time.cloudflare.com"
# 時間の同期を試みる
try:
# NTPサーバーから取得した時刻でPico WのRTCを同期
ntptime.settime()
except:
print("時間の同期に失敗しました。")
raise
# 世界標準時に9時間加算し日本時間を算出
tm = time.localtime(time.time() + 9 * 60 * 60)
# 曜日を日本語で表示するためのリスト
week_days_list = ["月", "火", "水", "木", "金", "土", "日"]
# 現在の曜日の日本語名を取得
day_of_week = week_days_list[tm[6]]
# 今日はその月で何回目の曜日かを調べる
nth_day_of_week = get_NthDayOfWeek(tm[2])
# 結果を表示
print(f"{tm[0]}年{tm[1]}月{tm[2]}日は、その月で{nth_day_of_week}回目の{day_of_week}曜日です。")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment