Last active
November 3, 2022 06:54
-
-
Save neosarchizo/43d31f6bbb9d70355e8e124980d60795 to your computer and use it in GitHub Desktop.
MicroPython - ESP32 WiFi NTP 사용하기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from network import WLAN, STA_IF | |
from time import time | |
from machine import SoftI2C, Pin, RTC, Timer | |
from ssd1306 import SSD1306_I2C | |
import ntptime | |
SERVER = 'broker.mqttdashboard.com' | |
PORT = 1883 | |
CLIENT_ID = 'esp32' | |
TOPIC = 'devicemart' | |
week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] | |
# MON ~ SUN : 0 ~ 6 | |
time_pads = ['', '', ''] | |
i2c = SoftI2C(sda=Pin(13), scl=Pin(14)) | |
display = SSD1306_I2C(128, 64, i2c, addr=0x3C) | |
rtc = RTC() | |
wlan = WLAN(STA_IF) | |
wlan.active(True) | |
start_time = time() | |
if not wlan.isconnected(): | |
print('Connecting to network...') | |
wlan.connect('devicemart', 'devicemart1') | |
while not wlan.isconnected(): | |
if time() - start_time > 15: | |
print('Connecting WiFi Timeout!') | |
quit() | |
config = wlan.ifconfig() | |
print(config) | |
ntptime.settime() | |
def on_timeout(_): | |
datetime = rtc.datetime() | |
display.fill(0) | |
display.text('DeviceMart', 0, 0) | |
display.text('RTC Clock', 0, 15) | |
display.text( | |
str(datetime[0]) + | |
'-' + | |
str(datetime[1]) + | |
'-' + | |
str(datetime[2]) + | |
' ' + | |
week[datetime[3]], | |
0, | |
40 | |
) | |
for i in range(4, 7): # 4 ~ 6 : Hours Mins Secs | |
if datetime[i] < 10: | |
time_pads[i - 4] = '0' | |
else: | |
time_pads[i - 4] = '' | |
display.text( | |
time_pads[0] + | |
str(datetime[4]) + | |
':' + | |
time_pads[1] + | |
str(datetime[5]) + | |
':' + | |
time_pads[2] + | |
str(datetime[6]), | |
0, | |
55 | |
) | |
display.show() | |
timer = Timer(-1) | |
timer.init(period=300, mode=Timer.PERIODIC, callback=on_timeout) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment