Mostly times it works, but in some devices after a time operating it receive messages from topic, but it can't publish.
Actually in the code the script run the publish line but dosen't generate any error, and when I'm listening the topic doesn't reach any message.
Some code:
class Mqtt:
def __init__(self, base_id):
self.base_id = base_id
# client settings
self.client = mqtt.Client(client_id=self.base_id, clean_session=True, userdata=None,
protocol=mqtt.MQTTv31)
self.client.username_pw_set(username=MQTT_USER, password=MQTT_PASS)
self.mqtt_web_conected = False
# connection calllbacks
self.client._on_connect_ = self.on_connect
self.client._on_disconnect_ = self.on_disconnect
self.connected_once = False
def connect(self):
# connect to broker
# @params void
# @return void
try:
self.client.connect(host=MQTT_HOST, port=MQTT_PORT, keepalive=KEEP_ALIVE, bind_address='')
self.client.loop_start()
self.mqtt_web_conected = True
last_will_topic = STREAMS_PUBLISH_TOPIC + "/" + self.base_id
value = get_hex_timestamp() + b'\x17\x09\x90'
value = base64.encodebytes(value)
self.client.will_set(topic=last_will_topic, payload=value, qos=2, retain=False)
except Exception as e:
print(e)
self.mqtt_web_conected = False
def publish_payload(self, value, topic):
# publish a payload
# @params value: bytes
# @return 1 or 0 based if msg was sent or not
# encodes msg in base 64
value = base64.encodebytes(value)
topic = topic + self.base_id
result, mid = self.client.publish(topic, value, qos=2)
return result
Has anyone seem an error like this?