I have a project publishing MQTT messages from an ESP to a broker running on a Pi, and client running on the Pi. Messages are reliably getting from the ESP to the broker, as I can observe them using mosquitto_sub
, however the client only receives them sporadically. I have tried setting QOS to 1 and 2, but it hasn't resolved. Wondering if anyone can help me spot the issue.
Here is the code on the ESP (micropython) - this is effectively working fine:
from umqtt.simple import MQTTClient
broker_ip = "[IP]"
client_name = "[client]"
user = "[user]"
password = "[password]"
def connect_publish(broker, client, topic, message, user, password):
print("Creating client object...")
client = MQTTClient(client_id=client_name,
server=broker_ip,
user=user,
password=password,
keepalive=60)
print("Connecting to server...")
client.connect()
print("Publishing message")
client.publish(topic = topic, msg = str(message), qos = 1)
print("Published", message, "to", topic)
print("Disconecting from server")
client.disconnect()
[function to connect to wifi]
[initialize sensor]
while True:
if [sensor_trigger]:
connect_publish(broker = broker_ip,
client = client_name,
topic = b"sensor",
message = "on",
user = user,
password = password)
Code on the client - running on the Pi (python):
#!/usr/bin/env python3
import paho.mqtt.client as paho
import time
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected with result code " + str(rc))
else:
print("Failed to subscribe, code:", rc)
client.subscribe("sensor", qos = 1)
def on_message(client, userdata, msg):
print(msg.topic+" "+ msg.payload.decode())
if msg.payload.decode() == "on":
if [some further conditions defined in variables below]:
[do something]
#Initialize the variables for MQTT
BROKER = '[IP]'
#uname and password for mqtt client stored on pi: /etc/mosquitto/passwd
uname = '[user]'
pwd = '[password]'
#Initialize all the paho functions
client = paho.Client('[name]')
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(password = pwd, username = uname)
client.connect(host = BROKER)
client.loop_start()
[initialize some more variables]
while True:
[update some variables]
time.sleep(0.1)
Essentially I reliably see the messages mosquitto_sub
on the broker, but don't see the print statements (nor do i see the outcomes of the commands) in the on_message
function running on the client - even with qos = 1
. I tried 2, made no difference.
Thanks to the comments above.
Setting the logging options to all:
Add: log_type all
to in /etc/mosquitto/mosquitto.conf
Then looking at the logs:
sudo less /var/log/mosquitto/mosquitto.log
([shift] + [g] to get to the end of the file).
I saw that I had two clients connecting with the same credentials. This was causing on each new connection to disconnect the old one. The result was that messages would only get delivered if the correct client happened to be connected.
Thanks again for pointing me to the full debugging option in the logs.