import time
from umqtt.simple import MQTTClient
from machine import Pin
# MQTT Settings
MQTT_BROKER = "broker.hivemq.com"
MQTT_TOPIC_SOS = "alert/SOS"
MQTT_TOPIC_UNAUTHORIZED = "alert/unauthorized"
MQTT_TOPIC_MOVE = "move/robot"
# Setup MQTT client
client = MQTTClient("parking_system", MQTT_BROKER)
client.connect()
# Setup Button and LED for simulation
button_sos = Pin(2, Pin.IN) # Button for SOS alert
button_unauthorized = Pin(3, Pin.IN) # Button for Unauthorized alert
led = Pin(4, Pin.OUT) # LED to visualize actions
def send_sos_alert():
client.publish(MQTT_TOPIC_SOS, "SOS alert at location (2,3)")
print("SOS alert sent.")
def send_unauthorized_alert():
client.publish(MQTT_TOPIC_UNAUTHORIZED, "Unauthorized access at location (1,1)")
print("Unauthorized access alert sent.")
def send_move_command(x, y):
move_message = f"Move to ({x},{y})"
client.publish(MQTT_TOPIC_MOVE, move_message)
print("Move command sent:", move_message)
# Main loop for the Parking System
while True:
if button_sos.value() == 0: # SOS alert triggered by button press
led.value(1)
send_sos_alert()
time.sleep(1)
led.value(0)
if button_unauthorized.value() == 0: # Unauthorized alert triggered by button press
led.value(1)
send_unauthorized_alert()
time.sleep(1)
led.value(0)
# Example: Send a move command to nurse robot periodically
send_move_command(2, 3)
time.sleep(10) # Wait for 10 seconds before sending another move command