# Rui Santos & Sara Santos - Random Nerd Tutorials
# Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-ds1307-rtc-micropython/
from machine import I2C, Pin
import time
import onewire,ds18x20
import urtc
import ssd1306
days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
# Initialize RTC (connected to I2C)
i2c = I2C(0, sda = Pin(20), scl = Pin(21), freq=400000)
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
rtc = urtc.DS1307(i2c)
# Set the current time using a specified time tuple
# Time tupple: (year, month, day, day of week, hour, minute, seconds, milliseconds)
#initial_time = (2024, 1, 30, 1, 12, 30, 0, 0)
# Or get the local time from the system
initial_time_tuple = time.localtime() #tuple (microPython)
initial_time_seconds = time.mktime(initial_time_tuple) # local time in seconds
# Convert to tuple compatible with the library
initial_time = urtc.seconds2tuple(initial_time_seconds)
# Sync the RTC
rtc.datetime(initial_time)
onewire_pin = Pin(22)
ds = ds18x20.DS18X20(onewire.OneWire(onewire_pin))
roms = ds.scan()
print(roms)
oled.fill(0)
oled.show()
oled.text('Hello', 0, 0)
oled.show()
time.sleep(1)
while True:
current_datetime = rtc.datetime()
ds.convert_temp()
for rom in roms:
temp = ds.read_temp(rom)
oled.fill(0)
oled.text('Current', 0, 0)
oled.text('Date:'+ str(current_datetime.year) + ":" + str(current_datetime.month)+ ":" + str(current_datetime.day), 0, 10)
oled.text('Time:'+ str(current_datetime.hour) + ":" + str(current_datetime.minute)+ ":" + str(current_datetime.second), 0, 20)
oled.text('Week:' + days_of_week[current_datetime.weekday], 0, 30)
oled.text('Temp:' + str(int(temp)) + 'C', 0, 40)
oled.show()
print('Current date and time:')
print('Date:'+ str(current_datetime.year) + ":" + str(current_datetime.month)+ ":" + str(current_datetime.day))
print('Time:'+ str(current_datetime.hour) + ":" + str(current_datetime.minute)+ ":" + str(current_datetime.second))
print('Day of the Week:', days_of_week[current_datetime.weekday])
print('Temp:' + str(int(temp)) + 'C')
time.sleep(1)