1

I'm running an MQTT Broker on my Windows machine using Mosquitto broker. I have setup a client on the same Windows machine through the Paho MQTT python library. Another client is setup using the same python library on a Linux machine that I have.

So, when I publish a file from the Windows client, it reaches the Linux client easily without any issues. However, when I tried to publish a file from my Linux client, it does not reach my Windows client. Later I noticed that if the file size is greater than 50KB, the file does not transfer from the linux machine. The log from the broker does not display the "Publish message recieved" if the filesize exceeds 50KB.

The code for Windows machine to publish:

import paho.mqtt.client as mqtt
import time


client = mqtt.Client("P2")
client.on_message=on_message
print("COnnecting")
client.connect("localhost")
print("Connected")
f = open("D:/Downloads/test.txt",'rb')
imagestring = f.read()
byteArray = bytearray(imagestring)



print("Publishing")

client.publish("photo", byteArray)

The code for Linux machine to subscribe:

import paho.mqtt.client as mqtt
import time


def on_message(mosq, obj, msg):
  with open('test.txt', 'wb') as fd:
    fd.write(msg.payload)
    print("message topic=",msg.topic)

client = mqtt.Client("P1")
client.on_message=on_message
print("COnnecting")
client.connect("192.168.1.115")

client.loop_start()

print("Subbing")
client.subscribe("photo")

time.sleep(1000)

client.loop_stop()

Code for Windows machine to subscribe:

import paho.mqtt.client as mqtt
import time


def on_message(mosq, obj, msg):
  with open('test.txt', 'wb') as fd:
    fd.write(msg.payload)
    print("message topic=",msg.topic)

client = mqtt.Client("P2")
client.on_message=on_message
print("COnnecting")
client.connect("localhost")

client.loop_start()

print("Subbing")
client.subscribe("photo")

time.sleep(1000)

client.loop_stop()

Code for Linux machine to publish:

import paho.mqtt.client as mqtt
import time

client = mqtt.Client("P1")
print("COnnecting")
client.connect("192.168.1.115")
print("Connected")
f = open("/opt/plcnext/test.txt","rb")
imagestring = f.read()
byteArray = bytearray(imagestring)



print("Publishing")

client.publish("photo", byteArray)

Is there any restriction from the Linux machine side? or am I doing something wrong here?

1 Answer 1

1

Your publishing code needs to run the client loop as well otherwise it will only send 1 TCP/IP packet which will limit the sending size to what ever the MTU is on the network.

Adding the same timed period with the client loop running will work, but a better solution would be to use the single shot wrappers included in the Paho library that will ensure that the message is fully sent before closing the connection. The docs can be found here

import paho.mqtt.publish as publish

f = open("/opt/plcnext/test.txt","rb")
imagestring = f.read()
byteArray = bytearray(imagestring)

publish.single("paho/test/single", byteArray, hostname="192.168.1.115")
1
  • Tried both and both worked like a charm! I think ill stick with the publish library method to reduce the number of lines in my code. Thank you! Commented Jul 8, 2021 at 6:36

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.