import network
import urequests
import machine
import time

ssid = 'Wokwi-GUEST'
password = ''

station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)

while not station.isconnected():
    pass

print('Connection successful')
print(station.ifconfig())

def check_time():
    flask_url = 'https://25c972eb-ea66-40a5-870a-bf40da16f219-00-38osnrh9ghhn0.janeway.replit.dev/isInTime'  # Replace with your Flask server IP address
    response = urequests.get(flask_url)
    result = response.json()
    response.close()
    return result['status']

def control_servo(action):
    servo_pin = machine.Pin(2)  # pin 2
    pwm = machine.PWM(servo_pin, freq=50)

    if action == 'open':
        pwm.duty(40)  # odprt kot
        print("Servo opened")
    elif action == 'closed':
        pwm.duty(115)  # zaprt kot
        print("Servo closed")

    time.sleep(1)
    pwm.deinit()

while True:
    status = check_time()
    control_servo(status)
    time.sleep(10)  # 10 check