# main.py — Smart Parking Monitor
# MicroPython ESP32 | Wokwi Free (tanpa library eksternal)
import uasyncio as asyncio
import network
import time
from config import WIFI_SSID, WIFI_PASS
from parking import ParkingMonitor
from firebase import FirebaseClient
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if wlan.isconnected():
return True
print("Connecting WiFi...")
wlan.connect(WIFI_SSID, WIFI_PASS)
for _ in range(20):
if wlan.isconnected():
print("WiFi OK:", wlan.ifconfig()[0])
return True
time.sleep(1)
print("WiFi gagal — mode offline")
return False
async def task_sensor(monitor):
while True:
monitor.read_all_sensors()
monitor.update_led()
monitor.update_lcd()
await asyncio.sleep_ms(300)
async def task_buttons(monitor):
while True:
monitor.read_buttons()
await asyncio.sleep_ms(50)
async def task_firebase(monitor, fb):
while True:
await asyncio.sleep(5)
data = monitor.get_state_dict()
ok = fb.patch("parking", data)
print("Firebase:", "OK" if ok else "GAGAL")
if ok:
fb.post("history", {
"occupied" : data["occupied"],
"freeSlots": data["freeSlots"],
"ts" : data["uptime_s"],
})
async def main():
wifi_ok = connect_wifi()
monitor = ParkingMonitor()
monitor.lcd_clear()
monitor.lcd_print("Smart Parking", row=0)
monitor.lcd_print("MicroPython IoT", row=1)
time.sleep(2)
fb = FirebaseClient() if wifi_ok else None
tasks = [
asyncio.create_task(task_sensor(monitor)),
asyncio.create_task(task_buttons(monitor)),
]
if fb:
tasks.append(asyncio.create_task(task_firebase(monitor, fb)))
await asyncio.gather(*tasks)
asyncio.run(main())
{
"version": 1,
"author": "Smart Parking MicroPython",
"editor": "wokwi",
"parts": [
{
"type": "board-esp32-devkit-c-v4",
"id": "esp",
"top": 80,
"left": 120,
"attrs": { "env": "micropython-20231227-v1.22.0" }
},
{
"type": "wokwi-hc-sr04",
"id": "sr1",
"top": -60,
"left": 420,
"attrs": { "distance": "25" }
},
{
"type": "wokwi-hc-sr04",
"id": "sr2",
"top": 60,
"left": 420,
"attrs": { "distance": "25" }
},
{
"type": "wokwi-hc-sr04",
"id": "sr3",
"top": 180,
"left": 420,
"attrs": { "distance": "25" }
},
{ "type": "wokwi-lcd1602", "id": "lcd", "top": 339.43, "left": 198.4, "attrs": {} },
{ "type": "wokwi-led", "id": "ledR", "top": -60, "left": -80, "attrs": { "color": "red" } },
{ "type": "wokwi-led", "id": "ledY", "top": 30, "left": -80, "attrs": { "color": "yellow" } },
{ "type": "wokwi-led", "id": "ledG", "top": 120, "left": -80, "attrs": { "color": "green" } },
{
"type": "wokwi-pushbutton",
"id": "btn1",
"top": 460,
"left": 80,
"attrs": { "color": "green" }
},
{
"type": "wokwi-pushbutton",
"id": "btn2",
"top": 460,
"left": 200,
"attrs": { "color": "yellow" }
},
{
"type": "wokwi-pushbutton",
"id": "btn3",
"top": 460,
"left": 320,
"attrs": { "color": "red" }
},
{ "type": "wokwi-resistor", "id": "r1", "top": -40, "left": -40, "attrs": { "value": "220" } },
{ "type": "wokwi-resistor", "id": "r2", "top": 50, "left": -40, "attrs": { "value": "220" } },
{ "type": "wokwi-resistor", "id": "r3", "top": 140, "left": -40, "attrs": { "value": "220" } }
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "esp:5", "sr1:TRIG", "blue", [] ],
[ "esp:18", "sr1:ECHO", "green", [] ],
[ "esp:19", "sr2:TRIG", "blue", [] ],
[ "esp:21", "sr2:ECHO", "green", [] ],
[ "esp:22", "sr3:TRIG", "blue", [] ],
[ "esp:23", "sr3:ECHO", "green", [] ],
[ "sr1:VCC", "esp:3V3", "red", [] ],
[ "sr1:GND", "esp:GND", "black", [] ],
[ "sr2:VCC", "esp:3V3", "red", [] ],
[ "sr2:GND", "esp:GND", "black", [] ],
[ "sr3:VCC", "esp:3V3", "red", [] ],
[ "sr3:GND", "esp:GND", "black", [] ],
[ "esp:25", "r1:1", "orange", [] ],
[ "r1:2", "ledR:A", "orange", [] ],
[ "ledR:K", "esp:GND", "black", [] ],
[ "esp:26", "r2:1", "yellow", [] ],
[ "r2:2", "ledY:A", "yellow", [] ],
[ "ledY:K", "esp:GND", "black", [] ],
[ "esp:27", "r3:1", "lime", [] ],
[ "r3:2", "ledG:A", "lime", [] ],
[ "ledG:K", "esp:GND", "black", [] ],
[ "esp:13", "lcd:RS", "purple", [] ],
[ "esp:12", "lcd:E", "purple", [] ],
[ "esp:14", "lcd:D4", "blue", [] ],
[ "esp:2", "lcd:D5", "blue", [] ],
[ "esp:4", "lcd:D6", "blue", [] ],
[ "esp:15", "lcd:D7", "blue", [] ],
[ "lcd:VSS", "esp:GND", "black", [] ],
[ "lcd:VDD", "esp:3V3", "red", [] ],
[ "lcd:V0", "esp:GND", "black", [] ],
[ "lcd:A", "esp:3V3", "red", [] ],
[ "lcd:K", "esp:GND", "black", [] ],
[ "lcd:RW", "esp:GND", "black", [] ],
[ "esp:32", "btn1:1.l", "white", [] ],
[ "btn1:2.l", "esp:GND", "black", [] ],
[ "esp:33", "btn2:1.l", "white", [] ],
[ "btn2:2.l", "esp:GND", "black", [] ],
[ "esp:34", "btn3:1.l", "white", [] ],
[ "btn3:2.l", "esp:GND", "black", [] ]
],
"dependencies": {}
}