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): ...@@ -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 mqtt_client = mqtt.Client(f'ohmpi_{OHMPI_CONFIG["id"]}_listener', clean_session=False) # create new instance
print('now this')
print('connecting to broker') 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']) print('Subscribing to topic', MQTT_CONTROL_CONFIG['ctrl_topic'])
mqtt_client.subscribe(MQTT_CONTROL_CONFIG['ctrl_topic'], MQTT_CONTROL_CONFIG['qos']) mqtt_client.subscribe(MQTT_CONTROL_CONFIG['ctrl_topic'], MQTT_CONTROL_CONFIG['qos'])
mqtt_client.on_message = on_message mqtt_client.on_message = on_message
......
...@@ -51,8 +51,11 @@ class MQTTHandler(logging.Handler): ...@@ -51,8 +51,11 @@ class MQTTHandler(logging.Handler):
cleanly. cleanly.
""" """
msg = self.format(record) 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, hostname=self.hostname, port=self.port,
client_id=self.client_id, keepalive=self.keepalive, client_id=self.client_id, keepalive=self.keepalive,
will=self.will, auth=self.auth, tls=self.tls, will=self.will, auth=self.auth, tls=self.tls,
protocol=self.protocol, transport=self.transport) protocol=self.protocol, transport=self.transport)
except Exception as e:
print(e)
...@@ -709,8 +709,11 @@ class OhmPi(object): ...@@ -709,8 +709,11 @@ class OhmPi(object):
self.exec_logger.debug(reply) self.exec_logger.debug(reply)
reply = bytes(reply, 'utf-8') reply = bytes(reply, 'utf-8')
socket.send(reply) socket.send(reply)
except zmq.Again: except zmq.ZMQError as e:
time.sleep(.1) if e.errno == zmq.EAGAIN:
pass # no message was ready (yet!)
else:
traceback.print_exc()
def measure(self, cmd_id=None): def measure(self, cmd_id=None):
"""Run the sequence in a separate thread. Can be stopped by 'OhmPi.stop()'. """Run the sequence in a separate thread. Can be stopped by 'OhmPi.stop()'.
......
#!bin/bash #!bin/bash
cd /home/pi/OhmPi
source ./ohmpy/bin/activate source "/home/pi/OhmPi/ohmpy/bin/activate"
python ohmpi.py 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