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.

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
Ioan Kats
  • 151
  • 5

1 Answers1

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!

Ioan Kats
  • 151
  • 5