Commit ff2db225 authored by Arnaud WATLET's avatar Arnaud WATLET
Browse files

fixing connection to broker on reboot

Showing with 24 additions and 7 deletions
+24 -7
......@@ -20,8 +20,18 @@ def on_message(client, userdata, message):
mqtt_client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_listener', clean_session=False) # create new instance
print('now this')
print('connecting to broker')
mqtt_client.connect(MQTT_CONTROL_CONFIG['hostname'])
trials = 0
trials_max = 10
while trials < trials_max:
try:
mqtt_client.connect(MQTT_CONTROL_CONFIG['hostname'])
trials = trials_max
except:
print('new trial')
time.sleep(2)
trials+=1
print('Subscribing to topic', MQTT_CONTROL_CONFIG['ctrl_topic'])
mqtt_client.subscribe(MQTT_CONTROL_CONFIG['ctrl_topic'], MQTT_CONTROL_CONFIG['qos'])
mqtt_client.on_message = on_message
......
......@@ -51,8 +51,11 @@ class MQTTHandler(logging.Handler):
cleanly.
"""
msg = self.format(record)
publish.single(self.topic, msg, self.qos, self.retain,
try:
publish.single(self.topic, msg, self.qos, self.retain,
hostname=self.hostname, port=self.port,
client_id=self.client_id, keepalive=self.keepalive,
will=self.will, auth=self.auth, tls=self.tls,
protocol=self.protocol, transport=self.transport)
except Exception as e:
print(e)
......@@ -709,8 +709,11 @@ class OhmPi(object):
self.exec_logger.debug(reply)
reply = bytes(reply, 'utf-8')
socket.send(reply)
except zmq.Again:
time.sleep(.1)
except zmq.ZMQError as e:
if e.errno == zmq.EAGAIN:
pass # no message was ready (yet!)
else:
traceback.print_exc()
def measure(self, cmd_id=None):
"""Run the sequence in a separate thread. Can be stopped by 'OhmPi.stop()'.
......
#!bin/bash
source ./ohmpy/bin/activate
python ohmpi.py
cd /home/pi/OhmPi
source "/home/pi/OhmPi/ohmpy/bin/activate"
python --version
python /home/pi/OhmPi/ohmpi.py
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment