from machine import Pin,I2C
import utime
import sys
import network
import ntptime
import wlan
import tm1637
import ds1307
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct
if(sys.platform=="esp8266"):
led = Pin(2, Pin.OUT)
I2C_SDA =4 # GPIO4(D2)
I2C_SCL =5 # GIPO5(D1)
i2c = I2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=10000) #initializing the I2C method for ESP8266
button_pin = 4 # GPIO4(D2)
elif(sys.platform=="esp32"):
led = Pin(2, Pin.OUT)
I2C_SDA =21 # GPIO21(I2C SDA)
I2C_SCL =22 # GPIO22(I2C SCL)
# Initialize I2C on GP16 (SDA) and GP17 (SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000) #initializing the I2C method for ESP32
button_pin = 35
elif(sys.platform=="rp2"):
led = Pin("LED", Pin.OUT)
I2C_SDA =16 # GPIO16(I2C SDA)
I2C_SCL =17 # GPIO17(I2C SCL)
# Initialize I2C on GP16 (SDA) and GP17 (SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=400000)
button_pin = 21
INTERVAL = 1000 # millisecond
"""
Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
(date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
(70*365+17)*24*60*60 = 2208988800 =>UTC0
28800 = 8*60*60
2208960000 = 2208988800-28800
"""
# Optional UTC+8 offset time (seconds), if not set, UTC0
UTC_OFFSET = 28800 # 8 * 60 * 60
NTP_DELTA = 2208988800 + UTC_OFFSET
def sync_time_with_retry():
led.on()
try:
print("Syncing with NTP server...")
ntptime.settime()
timer = utime.localtime(utime.time() + UTC_OFFSET)
rtc.datetime((timer[0], timer[1], timer[2], timer[6] + 1, timer[3], timer[4], timer[5], 0))
print("Time synchronized.")
except OSError as e:
# print("Error syncing time:", e)
if e.args[0] == 110: # ETIMEDOUT
print("ETIMEDOUT error during time sync. Retrying in 5 seconds...")
utime.sleep(5)
sync_time_with_retry() # Recursive call to retry
else:
print(f"An unexpected error occurred: {e}")
led.off()
# Function to handle button press interrupt (optional)
def button_press(pin):
global button_down
button_down = True
# Start Function
if __name__=="__main__":
print("Hello, " + sys.platform + "!")
# Define GPIO pins for push button
button_pin = Pin(button_pin, Pin.IN, Pin.PULL_UP) # Internal pull-up resistor
# Attach interrupt to the push button pin
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=button_press)
button_down = False
utime.sleep(0.1) # Wait for USB to become ready
tm = tm1637.TM1637(clk=Pin(2), dio=Pin(3))
rtc = ds1307.DS1307(i2c)
lcd = I2cLcd(i2c, 0x27, 2, 16)
print("Hello, Pi Pico!")
lcd.putstr("LCD , It Works!")
tm.scroll("CLOCK", delay=250)
now = (2018, 3, 24, 6, 13, 45, 21, 0)
rtc.datetime(now)
lcd.clear()
wlan.connect_wifi() # Connecting to WiFi Router
utime.sleep_ms(1000)
sync_time_with_retry() # Synchronize the time
lastTime = utime.ticks_ms()
while True:
try:
currTime = utime.ticks_ms()
if (currTime - lastTime > INTERVAL):
lastTime = currTime
led.toggle()
time_now=rtc.datetime()
print(time_now)
lcd.move_to(0, 0)
#lcd.clear()
lcd.putstr("{year:>04d}/{month:>02d}/{day:>02d}".format(year=time_now[0], month=time_now[1], day=time_now[2]))
lcd.move_to(0, 1)
lcd.putstr(" {HH:>02d}:{MM:>02d}:{SS:>02d}".format(HH=time_now[4], MM=time_now[5], SS=time_now[6]))
if time_now[6]%2==0:
tm.numbers(time_now[4], time_now[5], colon=True)
else:
tm.numbers(time_now[4], time_now[5], colon=False)
if button_down == True:
sync_time_with_retry() # Synchronize the time
button_down = False
utime.sleep_ms(100)
except KeyboardInterrupt:
print("Stop")
break