2

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):
   print(msg)

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

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

client_id = "python-mqtt-1"

client = MQTTClient(client_id, '0.0.0.0', user="guest", password="guest", port=1883)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic="python.mqtt")

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

    time.sleep(100)

This is the docker compose for the rabbitmq

services:
  rabbitmq:
    image: rabbitmq:3.9-management
    container_name: rabbitmq
    ports:
      - 1883:1883
      - 5672:5672
      - 15672:15672
    volumes:
      - ./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 = '0.0.0.0'
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!")
        else:
            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 :
            break
        time.sleep(0.1)
        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}`")
        else:
            print(f"Failed to send message to topic {topic}")
        msg_count += 1


def run():
    client = connect_mqtt()
    client.loop_start()
    publish(client)


if __name__ == '__main__':
    run()

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

7
  • 1
    It means you have the wrong port number for the MQTT broker or the broker isn't actually running.
    – romkey
    May 2 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
    May 2 at 16:38
  • 1
    Edit the question to actually show what the code and what you've tried.
    – hardillb
    May 2 at 17:17
  • Are you not trying to use TLS (aka SSL) on a connection which does not require it, or vice-versa?
    – jcaron
    May 2 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. May 3 at 19:52

1 Answer 1

3

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, privacy policy and cookie policy

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