# Hardware and connections used:
# LCD GND Pin to Raspberry Pi Pico GND
# LCD VCC Pin to Raspberry Pi Pico VBUS
# (Note: VBUS is only to be used as power for the screen.
# It can't be used as power for the entire circuit if there are other components interfaced.)
# LCD SDA Pin to Raspberry Pi Pico GPIO Pin 0
# LCD SCL Pin to Raspberry Pi Pico GPIO Pin 1
# modules
from machine import I2C, Pin # since I2C communication would be used, I2C class is imported
from time import sleep
import utime
import urequests
# very important
# this module needs to be saved in the Raspberry Pi Pico in order for the LCD I2C to be used
from pico_i2c_lcd import I2cLcd
# creating an I2C object, specifying the data (SDA) and clock (SCL) pins used in the Raspberry Pi Pico
# any SDA and SCL pins in the Raspberry Pi Pico can be used (check documentation for SDA and SCL pins)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
# getting I2C address
I2C_ADDR = i2c.scan()[0]
# creating an LCD object using the I2C address and specifying number of rows and columns in the LCD
# LCD number of rows = 2, number of columns = 16
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# init button
button = Pin(26, Pin.IN, Pin.PULL_DOWN)
led = Pin(7,Pin.OUT)
# Function to get weather data
def get_weather():
response = urequests.get('https://api.open-meteo.com/v1/dwd-icon?latitude=12.9719&longitude=77.5937&hourly=temperature_2m&forecast_days=1')
weather_data = response.json()
print(weather_data)
return weather_data['weather'][0]['description']
# Function to start timer
def start_timer(minutes):
end_time = utime.time() + minutes * 60
while utime.time() < end_time:
if button.value() == 1: # If button is pressed, break the loop
break
remaining_time = end_time - utime.time()
lcd.putstr('Time left: {} seconds'.format(remaining_time))
utime.sleep(1)
# Main loop
while True:
print("entered loop")
if button.value(): # If button is pressed, start work mode
print("button pressed")
led.toggle()
for i in range(4): # Repeat work mode 4 times
lcd.write('Work time')
start_timer(25)
buzzer.beep() # Beep the buzzer
lcd.putstr('Rest time')
start_timer(5)
buzzer.beep() # Beep the buzzer
else: # If not in work mode, display time and weather
lcd.putstr('Time: {}'.format(utime.localtime()))
lcd.putstr('Weather: {}'.format(get_weather()))
utime.sleep(60) # Update every minute