import network
import time
import urequests
from machine import Pin, ADC, SoftI2C
from utime import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# Connect to Wi-Fi
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Initialize I2C for the LCD
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
i2c_addresses = i2c.scan()
print("I2C addresses found:", i2c_addresses)
if not i2c_addresses:
raise Exception("No I2C devices found")
lcd_address = i2c_addresses[0]
lcd = I2cLcd(i2c, lcd_address, 2, 16)
# Initialize the potentiometer
potentiometer = ADC(Pin(34))
potentiometer.atten(ADC.ATTN_11DB)
# Function to read potentiometer and display value on LCD
def display_potentiometer_value():
while True:
pot_value = potentiometer.read()
Hour = (pot_value / 4095.0) * 24
lcd.clear()
lcd.putstr("Set hours {:.0f}h".format(Hour))
sleep(0.5)
# Call the function to start displaying the potentiometer value
display_potentiometer_value()