With my Sonoff's, I want to turn on/off my lamps.
I believe, for that it is not necessary to store mqtt messages at the mqtt-broker.
Hence, I connect to the broker with connect(clean_session=False) ... but this doesn't work.
But, when I do connect(session=True), it works at least for many hours (but it seems to be unstable, because after one day, the connection is lost. I assume because my router reboots each night and Wlan-connection is lost ... but this is another story.)
Following, the code for establishing mqtt-connection:
Code: Select all
class Mqtt:
global status_flag
def __init__(self, client_id, broker_ip, broker_port, topic):
self.connect_with_broker(client_id, broker_ip, broker_port, topic)
def connect_with_broker(self, client_id, broker_ip, broker_port, topic):
self.c = MQTTClient(client_id, broker_ip, broker_port)
self.c.set_last_will("last_will", client_id + " said good-bye")
self.c.set_callback(self.analyse_messages)
print("Trying to establish connection to MQTT-Broker ...") # <-- this message I always see
if not self.c.connect(clean_session=True): #<-- True works, False doesn't work
print("Successfully established connection with MQTT-broker") <-- in case of True, I see this, but never in case of false
self.c.subscribe(topic)
Code: Select all
persistence true
persistence_location /var/lib/mosquitto