"""
Real-time clock using MicroPython
https://www.hackster.io/kamaluddinkhan/creating-a-real-time-clock-on-encropi-using-micropython-176fe4
"""
from machine import Pin, I2C, SPI, UART
import utime
import sys
import ntptime
import ds1307
import st7789
import wlan
from sync_time import sync_time_NTP as sync_time
# import vga1_8x16 as font1
# import vga1_16x16 as font2
import vga1_16x32 as font
INTERVAL = 1000 # millisecond
I2C_FREQ = 400000
DISP_WIDTH = 240
DISP_HEIGHT = 240
#sec min hour week day month year
CurrentTime = b'\x00\x50\x14\x05\x21\x06\x22' #00:00:10 friday 03/03/2022
week = ["Sun","Mon","Tues","Wed","Thur","Fri","Sat"];
if(sys.platform=="esp32"):
led = Pin(2, Pin.OUT)
disp_sck = 18 # default SCK of SPI(1)
disp_mosi = 23 # default MOSI of SPI(1)
disp_miso = 19 # not use
disp_cs = 5
disp_dc = 17
disp_rst = 16
disp_blk = 4
# Initialize SPI communication
spi = SPI(2, baudrate=40000000, polarity=1, phase=0, bits=8, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=None)
I2C_SDA = 21 # GPIO21
I2C_SCL = 22 # GPIO22
# Initialize I2C on GP16 (SDA) and GP17 (SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=I2C_FREQ) #initializing the I2C method for ESP32
elif(sys.platform=="rp2"):
led = Pin("LED", Pin.OUT)
disp_sck = 6 # default SCK of SPI(0)
disp_mosi = 7 # default MOSI of SPI(0)
disp_miso = 4 # not use
disp_cs = 5
disp_rst = 2
disp_dc = 1
disp_blk = 3
# 初始化 SPI
spi = SPI(0, baudrate=24000000, polarity=0, phase=0, bits=8, sck=Pin(disp_sck), mosi=Pin(disp_mosi), miso=None)
I2C_SDA = 16 # GPIO16
I2C_SCL = 17 # GPIO17
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=I2C_FREQ)
def info():
# tft.init()
utime.sleep(0.2)
tft.text(font,"RP2040 RTC", 15,20)
tft.fill_rect(15, 60, 210,10, st7789.RED)
tft.text(font,"Real Time", 15,70,st7789.YELLOW)
tft.text(font,"Clock", 15,100,st7789.YELLOW)
tft.fill_rect(15, 140, 210, 10, st7789.BLUE)
utime.sleep(2)
tft.fill(0) #clear screen
# Start Function
if __name__ == "__main__":
print("Hello, " + sys.platform + "!")
tft = st7789.ST7789(spi,
DISP_WIDTH, DISP_HEIGHT,
reset=Pin(disp_rst, Pin.OUT),
cs=Pin(disp_cs, Pin.OUT),
dc=Pin(disp_dc, Pin.OUT),
backlight=Pin(disp_blk, Pin.OUT),
rotation=0)
print(st7789.__name__, tft.width, "x", tft.height)
info()
rtc = ds1307.DS1307(i2c)
now = (2018, 3, 24, 6, 13, 45, 21, 0)
rtc.datetime(now)
# ========== CONNECT WIFI ==========
tft.text(font,"Connecting to", 15,16)
tft.text(font," WiFi Router", 15,56)
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
tft.fill(0) #clear screen
# ========== NTP TIME SYNC ==========
print("Syncing time from NTP...")
timer = sync_time() # Synchronize the time
rtc.datetime((timer[0], timer[1], timer[2], timer[6] + 1, timer[3], timer[4], timer[5], 0))
lastTime = utime.ticks_ms()
# ========== MAIN LOOP ==========
try:
while True:
currTime = utime.ticks_ms()
if (currTime - lastTime > INTERVAL):
lastTime = currTime
time_now = rtc.datetime()
# time_now = utime.localtime(utime.time() + UTC_OFFSET)
year = "{:04d}".format(time_now[0])
month = "{:02d}".format(time_now[1])
day = "{:02d}".format(time_now[2])
hour = "{:02d}".format(time_now[4])
minute = "{:02d}".format(time_now[5])
second = "{:02d}".format(time_now[6])
# SERIAL PRINT
str_date = year + ":" + month + ":" + day
str_time = hour + ":" + minute + ":" + second
print("Date: " + str_date, end='')
print("; Time: " + str_time)
# DISPLAY
tft.text(font,"Date: ", 15,16,st7789.WHITE)
tft.text(font,str_date, 15,56,st7789.WHITE)
tft.fill_rect(15, 96, 210,10, st7789.RED)
tft.text(font,"Time: ", 15,112,st7789.WHITE)
tft.text(font,str_time, 15,152,st7789.YELLOW)
tft.fill_rect(15, 190, 210, 10, st7789.BLUE)
utime.sleep_ms(100)
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
print("Stop")
Loading
pi-pico-w
pi-pico-w
Loading
st7789
st7789