from machine import Pin, ADC, PWM
import time
import network
import urequests as requests
WIFI_NAME = "Wokwi-GUEST"
WIFI_PASSWORD = ""
myChannelNumber = 2496875
myApiKey = "19OUIMOVIV7R68VA"
server = "api.thingspeak.com"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_NAME, WIFI_PASSWORD)
while not wifi.isconnected():
time.sleep(0.5)
print("Connected to WiFi")
lcd = None # Assume you have a similar library for LCD
servo = PWM(Pin(17), freq=50) # Assuming 50Hz frequency
def map_value(x, in_min, in_max, out_min, out_max):
return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
def update_thingspeak(data):
url = "https://api.thingspeak.com/update?api_key={}&field1={}".format(myApiKey, data)
response = requests.get(url)
if response.status_code == 200:
print("Data update successful!")
else:
print("Data update error")
def main():
available_slots = 2
ldr_pin = ADC(Pin(33))
ldr_pin.atten(ADC.ATTN_11DB)
pwm_pin = PWM(Pin(32), freq=5000, duty=0)
while True:
x = Pin(5, Pin.IN).value()
y = Pin(25, Pin.IN).value()
slot1 = Pin(12, Pin.IN).value()
slot2 = Pin(14, Pin.IN).value()
lcd.print("Slots available:")
if x:
servo.duty(77) # Equivalent to 90 degrees
time.sleep(2)
else:
available_slots -= 1
servo.duty(90)
if y:
servo.duty(77)
time.sleep(2)
else:
servo.duty(90)
available_slots -= 1
if slot1:
lcd.print("1")
elif slot2:
lcd.print("1")
elif slot1 and slot2:
lcd.print("0")
available_slots = 0
else:
lcd.print("2")
available_slots = 2
update_thingspeak(available_slots)
ldr_val = ldr_pin.read()
a = map_value(ldr_val, 0, 4095, 0, 1023)
pwm_pin.duty(a - 1)
time.sleep(0.1)
if __name__ == "__main__":
main()