client.publish("topic", payload,qos=2) not publishing data onto the broker whereas the same is working for qos=0 and 1.I have not defined persistence, it is set to default value.
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
from random import *
import time
MQTT_IP='192.168.0.23'
MQTT_PORT=1883
global client
import datetime
def on_connect(client, userdata, flags, rc,qos):
if rc==0:
print("MQTT CONNECTION ESTABLISHED")
print(str(client)+'\n'+str(userdata))
def on_message(client, userdata, msg):
print("Message arrived from "+str(client))
print(msg.topic+" "+str(msg.payload))
def MQTT_CONNECTION():
global client
global MQTT_CLIENT_CONNECTED
try:
print("IN mqtt connection")
client = mqtt.Client()
client.connect(MQTT_IP,MQTT_PORT)
client.on_connect = on_connect
client.on_message = on_message
MQTT_CLIENT_CONNECTED=True
except Exception as error:
print("ERROR IN MQTT CONNECTION",error)
MQTT_CLIENT_CONNECTED=False
def publish():
global client
client.publish("1/MB/EM/3/21/IB",2,qos=2,retain=True)
if __name__=="__main__":
MQTT_CONNECTION()
publish()
I have removed all other functionality, for you all to understand better.
You need to start the MQTT client network loop. This is to handle the multi step handshake for QOS 2 messaging.
The python Paho client has 3 ways to run the loop.
client.loop_start()
client.loop_forever()
client.loop()
at regular intervalsWhich one you choose depends on what else your code is doing, but I suspect that starting the loop on a background thread is probably the best.