#-----------------------------------------------------------------------#
# Objectives: #
# - Blink External LEDs #
# - Get and print RTC-Based System time on LCD #
# #
# Author: Drop Bos #
# Created Date: May-02-2025 #
# Updated Date: May-04-2025 #
# #
# License: Apache-2.0 #
# #
# Copyright © 2025. Drop Bos. All Rights Reserved. #
# ----------------------------------------------------------------------#
from lcd1602 import LCD
from ds1307 import DS1307
from ntptime import settime
from network import WLAN, STA_IF
from machine import Pin, RTC, ADC, I2C
from utime import sleep, time, localtime
# import _thread # multi-threaded related import - comment out for now
print("Initializing project...")
print("...")
# 1. set up external leds
led_one = Pin(5, Pin.OUT)
led_two = Pin(9, Pin.OUT)
led_three = Pin(13, Pin.OUT)
# 2. setup external lcd
# download lcd1602 library from the link below and upload:
# https://toptechboy.com/lcd1602-display-library-for-micropython-and-the-raspberry-pi-pico-w/
lcd = LCD(scl=Pin(7), sda=Pin(6))
# 3. setup system time
# download ds1307 library from the link below and upload:
# https://github.com/peter-l5/DS1307/blob/main/ds1307.py
password = ""
ssid = "Wokwi-GUEST"
no_of_retries = 5
set_system_manually = False
print("...")
print("Initializing project...")
print("...")
# a. set up the system time
if set_system_manually:
print("Setting up system time manually ...")
# i. set up real time clock
rtc = RTC() # independent clock: keeps track of the date and time.
# print(rtc.datetime()) # not correct
# so set a specific time (local or UTC time); e.g. 2025/1/23 10:13:50;
# format (year, month, day, weekday, hours, minutes, seconds, subseconds)
# day-of-week value is ignored
rtc.datetime((2025, 05, 04, 0, 10, 13, 50, 0))
# ii. confirm the set system (clock) time twice before and after 5 seconds sleep
print({"rtc" : rtc.datetime() })
sleep(5)
print({"rtc" : rtc.datetime() })
else:
print("Setting up system time using ntptime ...")
print("...")
# use ntptime (network time) to set the system time
# i. connect to wifi network
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
sleep(1)
print("Connecting to Wi-Fi")
print("---------------------")
print("Connected to Wi-Fi!")
print("---------------------")
# ii. connect to ntp server set the system local time
for index in range(no_of_retries):
try:
settime()
except(OSError) as error:
if error or (error.args[0] == 110) : # 110 = ETIMEDOUT
print("Retrying")
sleep(1)
print("Completed.")
# iii. confirm system time
sys_time = localtime()
#print({"sys-time" : sys_time})
# b. set up system clock for display on LCD
i2c = I2C(0, scl = Pin(1), sda = Pin(0), freq = 100000) # default values: scl=Pin(9), sda=Pin(8), freq=400000
sys_clock = DS1307(i2c)
print("...")
def separator():
print("--------------------")
def external_led_display(led_one=None, led_two=None, led_three=None):
# while True: # multi-threaded related code - comment out for now
ready_to_stop = 7
stop_go = 1
ready_to_stop_sleep_interval = 7
# ready to stop
print("-- Ready to stop --")
for index in range(ready_to_stop):
led_one.on()
sleep(1)
led_one.off()
sleep(1)
# stop
print("-- Stop --")
for index in range(stop_go):
led_two.on()
sleep(ready_to_stop_sleep_interval)
led_two.off()
sleep(1)
# go
print("-- Go --")
for index in range(stop_go):
led_three.on()
sleep(ready_to_stop_sleep_interval)
led_three.off()
sleep(1)
separator()
def system_time_display(sensor=None, seconds=None):
#while True: # multi-threaded related code - comment out for now
for index in range(3):
sys_time_now = sys_clock.datetime
now = sys_time_now
#now_in_string = f"{now[0]:0}-{now[1]:02}-{now[2]:02} {now[3]:02}:{now[4]:02}:{now[5]:02}"
#sensor.write(0, 0, now_in_string)
#sleep(seconds)
#sensor.clear()
#sleep(seconds)
sensor.write(0, 0, f"Date: {now[0]:0}-{now[1]:02}-{now[2]:02}")
sleep(seconds)
sensor.clear()
sleep(seconds)
sensor.write(0, 0, f"Time: {now[3]:02}:{now[4]:02}:{now[5]:02}")
sleep(seconds)
sensor.clear()
sleep(seconds)
while True:
external_led_display(led_one=led_one, led_two=led_two, led_three=led_three)
system_time_display(sensor=lcd, seconds=0.5)
# multi-threaded version/code - comment out for now
#_thread.start_new_thread(external_led_display, (led_one, led_two, led_three))
#system_time_display(sensor=lcd, seconds=0.5)