import paho.mqtt.client as mqtt

# MQTT Broker Details
broker = "broker.hivemq.com"  # Use your broker's URL if using a different broker
port = 1883  # Default port for non-secure MQTT (8883 for TLS)
temperature_topic = "fleet/temperature"
humidity_topic = "fleet/humidity"
gps_topic = "fleet/gps"

# Callback when message is received
def on_message(client, userdata, msg):
    print(f"Received message: {msg.payload.decode()} on topic {msg.topic}")

# Setup MQTT client
client = mqtt.Client()  # Create a new MQTT client instance
client.on_message = on_message  # Set the callback function for messages

# Connect to MQTT broker
client.connect(broker, port, 60)  # Connect to the broker at the specified port

# Subscribe to multiple topics
client.subscribe(temperature_topic)
client.subscribe(humidity_topic)
client.subscribe(gps_topic)

# Print confirmation
print(f"Subscribed to topics: {temperature_topic}, {humidity_topic}, {gps_topic}")

# Loop forever and wait for messages
client.loop_forever()  # This will keep the script running and process incoming messages
