MQTT publisher: import network, time from machine import Pin, UART from umqtt.simple import MQTTClient #connect pico W to internet ssid = "ITEK 2nd" password = "2nd_Semester_F23v" wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) while not wlan.isconnected(): print('waiting to connect:') time.sleep(1) ip = wlan.ifconfig()[0] gpsModule = UART(1, baudrate=9600, tx=Pin(4), rx=Pin(5), timeout=500) #init #MQTT parameters broker = 'test.mosquitto.org' client_id = 'eaaa_pico_w_gps' topic = "eaaa_gps_data" def mqtt_connect(): client = MQTTClient(client_id, broker, keepalive=3600) client.connect() print('Connected to %s MQTT Broker' % broker) return client #initialise an MQTT client client = mqtt_connect() #publish GPS data via the MQTT client while True: buff = gpsModule.readline() if buff: buff = buff.decode() buff_array = buff.split(',') if buff_array[0] == '$GNGGA': client.publish(topic, buff) MQTT Subscriber: # Receiving messages via MQTT import random import time from paho.mqtt import client as mqtt_client # Define broker parameters broker= 'test.mosquitto.org' #use external broker m_port = 1883 topic = "eaaa_gps_data" client_id = f'eaaa-mqtt-{random.randint(0, 1000)}' #genetate client ID with prefix def connect_mqtt() -> mqtt_client: def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %d\n", rc) client = mqtt_client.Client(client_id) # Set Connecting Client ID # client.username_pw_set(username, password) - note: Password not configures in broker client.on_connect = on_connect client.connect(broker,m_port) return client def subscribe(client: mqtt_client): def on_message(client, userdata, msg): print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic") client.subscribe(topic) client.on_message = on_message def run(): client = connect_mqtt() subscribe(client) client.loop_forever() if __name__ == '__main__': run()