I have two LoPy4 Dev boards. I am using this tutorial in order to (successfully) communicate with the two boards. After receiving data in LoRa communication I am trying to also publish this data in an MQTT Broker. I have followed the related MQTT tutorial which is provided by Pycom, here (also placed mqqt.py file under lib/ folder) but every time that I am trying to run the code I get OSError: [Errno 104] ECONNRESET.

Has anyone faced any similar issues ?

This is the main file that I am using on the LoPy:

from mqtt import MQTTClient
# from mqtt import MQTTClient_lib as MQTTClient
from network import WLAN
import machine
import time

def sub_cb(topic, msg):

wlan = WLAN(mode=WLAN.STA)
wlan.connect("XXXXX", auth=(WLAN.WPA2, "XXXXXXXXXXXXXX"), timeout=5000)

while not wlan.isconnected():  
print("Connected to WiFi\n")

client_id = "python-mqtt-1"

client = MQTTClient(client_id, '', user="guest", password="guest", port=1883)

while True:
    x = 100
    while x > 0:
        client.publish(topic="python.mqtt", msg=str(x))
        x -= 1


This is the docker compose for the rabbitmq

    image: rabbitmq:3.9-management
    container_name: rabbitmq
      - 1883:1883
      - 5672:5672
      - 15672:15672
      - ./conf/myrabbit.conf:/etc/rabbitmq/myrabbit.conf
    command: '/bin/bash -c "rabbitmq-plugins enable rabbitmq_mqtt; rabbitmq-server"'

I also created a python script that publishes MQTT messages to the same broker

import random
import time

from paho.mqtt import client as mqtt_client

broker = ''
port = 1883
topic = "python.mqtt"
# generate client ID with pub prefix randomly
client_id = f'python-mqtt-{random.randint(0, 1000)}'

def connect_mqtt():
    def on_connect(client, userdata, flags, rc):
        if rc == 0:
            print("Connected to MQTT Broker!")
            print("Failed to connect, return code %d\n", rc)

    client = mqtt_client.Client(client_id)
    client.username_pw_set('guest', 'guest')
    client.on_connect = on_connect
    client.connect(broker, port)
    return client

def publish(client):
    msg_count = 0
    while True:
        if msg_count == 50 :
        msg = f"messages: {msg_count}"
        result = client.publish(topic, msg)
        # result: [0, 1]
        status = result[0]
        if status == 0:
            print(f"Send `{msg}` to topic `{topic}`")
            print(f"Failed to send message to topic {topic}")
        msg_count += 1

def run():
    client = connect_mqtt()

if __name__ == '__main__':

When uploading the code to the LoPy I keep getting ECONNRESET, however the python script publishes successfully on the broker.

  • 1
    It means you have the wrong port number for the MQTT broker or the broker isn't actually running.
    – romkey
    Commented May 2, 2022 at 16:30
  • Hello, thanks for the response, I do not think that this is the case. I also created an mqqt publisher in python and I was successfully able to publish to the MQTT broker, which means that both the broker is running and I have the right port.
    – Ioan Kats
    Commented May 2, 2022 at 16:38
  • 1
    Edit the question to actually show what the code and what you've tried.
    – hardillb
    Commented May 2, 2022 at 17:17
  • Are you not trying to use TLS (aka SSL) on a connection which does not require it, or vice-versa?
    – jcaron
    Commented May 2, 2022 at 20:11
  • Figure out how to log in the broker and see the broker logs. You're doing something that the broker does not like and are getting kicked out. Also, try connecting to the broker with a client on a PC and see if that works ok. Commented May 3, 2022 at 19:52

1 Answer 1


I was quite naive. I was using localhost on the LoPy instead of the broker IP. Replacing that fixed the issue. Thanks for all the answers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.