Przeglądaj źródła

Exception handling for MQTT

master
Dirk Alders 2 lat temu
rodzic
commit
d0817bc504
1 zmienionych plików z 9 dodań i 5 usunięć
  1. 9
    5
      ambient_info.py

+ 9
- 5
ambient_info.py Wyświetl plik

@@ -1,6 +1,7 @@
1 1
 import config
2 2
 import logging
3 3
 import paho.mqtt.client as paho
4
+import socket
4 5
 import report
5 6
 import time
6 7
 
@@ -17,11 +18,14 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
17 18
 def send_data_to_mqtt(data):
18 19
     client= paho.Client("temp_sens")
19 20
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
20
-    client.connect(config.MQTT_SERVER, 1883)
21
-    for key in data:
22
-        topic = config.MQTT_TOPIC + "/" + key
23
-        logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
24
-        client.publish(topic, data[key])
21
+    try:
22
+        client.connect(config.MQTT_SERVER, 1883)
23
+        for key in data:
24
+            topic = config.MQTT_TOPIC + "/" + key
25
+            logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
26
+            client.publish(topic, data[key])
27
+    except (socket.timeout, OSError) as e:
28
+        logger.warning("Erro while sending ambient information")
25 29
 
26 30
 
27 31
 def dht_callback(**data):

Ładowanie…
Anuluj
Zapisz