sw

micropython은 time.time대신 ntptime.time으로 timestamp를 찍자 (raspberry pico2w, esp32)

mvsw 2025. 9. 3. 23:09

 

당연히 세팅하기전에 체크했어야되는 부분인데...

 

놀랍게도 Edge Device가 인터넷에 연결되어 있더라도 time sink가 안맞는 경우가 생긴다. 

 

뭐 이런 경우가 있나 싶은데 

 

 

지금 25년 9월 3일 오후 9시 56분 KST 기준인데 timestamp가 안맞아도 너무 안맞는다. 

 

해당 제품은 raspberry pi pico 2w 이고 인터넷에 검색을 해보니 

 

https://stackoverflow.com/questions/73059598/how-to-have-the-pico-w-set-correct-time-on-boot

 

How to have the Pico W set correct time on boot

When running on batteries the Pico W's clock/utime starts at 2021-01-01 00:00:00 by default. At every boot it obviously should: Get the current time on its own, likely off the internet. Set it's

stackoverflow.com

 

어떤 사람은 2021-01-01로 세팅 되어 있다고 하더라 

 

 

micropython의 time.time()으로 만들어지는 timestamp는 기준이 디바이스 마다 다른거 같다.

- 그냥 x86_64에서 할때는 1970년 1월 1일 기준이었는데....

 

그래서 비교 체험

 

왼쪽은 지금 날라오는 raspberry pi pico 2w 

오른쪽은 지금 찍어본 ESP-32D

 

뭐야 왜 달라....

 

왼쪽에 있는건 심지어 1970 기준인데 default가 진짜 2021-01-01인거 같은데?

 

그럼 이걸 어떻게 해결해야 되나?

 

해결방법은...

 

time.time()을 사용하는게 아니라 

 

network가 연결된 상태에서 ntptime 을 이용한

 

ntpime.time()을 사용하는것이다.

 

지금 집에 raspberry pi pico 2w가 없어서 ESP-32D를 기준으로 보면

 

# conn.py

import time
import json
import ntptime
import network

#WiFi 설정 
ssid = "<SSID>"
password = "<PW>"

# WiFi 연결
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# 
while not wlan.isconnected():
    
    print("Waiting for WiFi connection...")
    time.sleep(1)

print("WiFi connected:", wlan.ifconfig(), time.localtime())
while(True):
    print(time.time(), time.gmtime(time.time()))
    print(ntptime.time(), time.gmtime(ntptime.time()))
    time.sleep(1)

 

위 코드를 그냥 실행하면 얼추 결과를 볼 수 있는데

 

MPY: soft reboot
WiFi connected: ('---.---.---.---', '255.255.255.0', '---.---.---.---', '---.---.---.---')
810255742 (2025, 9, 3, 23, 2, 22, 2, 246)
810223350 (2025, 9, 3, 14, 2, 31, 2, 246)
810255746 (2025, 9, 3, 23, 2, 26, 2, 246)
810223352 (2025, 9, 3, 14, 2, 32, 2, 246)
810255748 (2025, 9, 3, 23, 2, 28, 2, 246)
810223354 (2025, 9, 3, 14, 2, 34, 2, 246)
810255750 (2025, 9, 3, 23, 2, 30, 2, 246)
810223355 (2025, 9, 3, 14, 2, 36, 2, 246)
810255751 (2025, 9, 3, 23, 2, 31, 2, 246)

 

이렇게 나온다. 

 

응? 시간이 맞춰져 있는데?

 

이거는 Thonny IDE가 자동으로 board의 시간을 연결된 PC의 시간과 동기화 해주기 때문이다. 

(라고 누군가 말하더라 )

 

따라서 차이를 보고 싶다면 Thonny로 코드 넣고 reset 버튼을 눌러서 테스트 해봐야 된다. 

 

이렇게 하면

 

entry 0x400805b0
Waiting for WiFi connection...
Waiting for WiFi connection...
Waiting for WiFi connection...
Waiting for WiFi connection...
Waiting for WiFi connection...
WiFi connected: ('---,---,---,---', '255.255.255.0', '---,---,---,---, '---.---.---.---') (2000, 1, 1, 0, 0, 5, 5, 1)
5 (2000, 1, 1, 0, 0, 5, 5, 1)
810223295 (2025, 9, 3, 14, 1, 35, 2, 246)
6 (2000, 1, 1, 0, 0, 6, 5, 1)
810223296 (2025, 9, 3, 14, 1, 36, 2, 246)
7 (2000, 1, 1, 0, 0, 7, 5, 1)
810223297 (2025, 9, 3, 14, 1, 37, 2, 246)
8 (2000, 1, 1, 0, 0, 8, 5, 1)
810223298 (2025, 9, 3, 14, 1, 38, 2, 246)
9 (2000, 1, 1, 0, 0, 9, 5, 1)
810223299 (2025, 9, 3, 14, 1, 39, 2, 246)

 

 

이런 모습을 볼 수 있다. 

 

아 그런데 이미 현장에 코드 가져다 넣어버렸는데 어떻게 바꾸지...

 

다음에는 OTA를 세팅해봐야겠다....