Browse Source

- revert last changes
- change structure of config
- add option to send retained MQTT messages for each device
- add option to send only if value changed for each device
- add option to send last change timestamp on a 2nd topic for each device

FloKra 9 months ago
parent
commit
f31cda65af
3 changed files with 77 additions and 47 deletions
  1. 7 0
      CHANGELOG.txt
  2. 44 29
      ioext/config.py
  3. 26 18
      ioext/ioext.py

+ 7 - 0
CHANGELOG.txt

@@ -1,3 +1,10 @@
+03.07.2023
+	- revert last changes
+	- change structure of config
+	- add option to send retained MQTT messages for each device
+	- add option to send only if value changed for each device
+	- add option to send last change timestamp on a 2nd topic for each device
+	
 03.07.2023
 	- added config option 'filterUnchanged' so that filtering on state change can be disabled now (pin state is sent by the MCU every minute by default - now this will be published every time on MQTT if filterUnchanged = False)
 	

+ 44 - 29
ioext/config.py

@@ -1,29 +1,44 @@
-# timeout in s to end script when nothing is received (will be restarted by systemd then)
-# must be higher than getStatusInterval 
-quitOnNoReceiveTimeout = 75
-filterUnchanged = False # if True, MQTT updates are only sent if the pin state has changed
-
-# status file - on ramdrive (/tmp on RasPi by default)
-# is "touched" every time a serial message comes in so that working communication can be monitored easily
-#statusFile = '/tmp/ioext_running'
-#statusFile = '/run/user/1000/ioext_running'
-statusFile = ''
-
-# serial port config
-serialPort = '/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0'
-serialBaud = 57600
-serialTimeout = 1 # should be 1 as above interval/timeout relies on it
-
-# MQTT config
-mqtt_server = "mqtt.lan"
-mqtt_port = 1883
-mqtt_user = ""
-mqtt_password = ""
-
-mqtt_base_topic = "T5/HomeSvrIOExt"
-
-mqtt_topic_tuerkontakt = "T5/Wohnungstuer/Tuerkontakt"
-mqtt_topic_pir1 = "T5/VZ/PIR1"
-mqtt_topic_pir2 = "T5/VZ/PIR2"
-mqtt_topic_out_temp = "T5/Abstr/Sensors/temp"
-mqtt_topic_out_hum = "T5/Abstr/Sensors/hum"
+# timeout in s to end script when nothing is received (will be restarted by systemd then)
+# must be higher than getStatusInterval 
+quitOnNoReceiveTimeout = 75
+
+# status file - on ramdrive (/tmp on RasPi by default)
+# is "touched" every time a serial message comes in so that working communication can be monitored easily
+#statusFile = '/tmp/ioext_running'
+#statusFile = '/run/user/1000/ioext_running'
+statusFile = ''
+
+# serial port config
+serialPort = '/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0'
+serialBaud = 57600
+serialTimeout = 1 # should be 1 as above interval/timeout relies on it
+
+# Input Pins / MQTT Topics/Settings
+P2_MQTT_Topic = "T5/Wohnungstuer/Tuerkontakt"
+P2_MQTT_SendRetained = True
+P2_MQTT_SendLastUpdate = True
+P2_MQTT_SendOnlyIfValueChanged = True
+
+P3_MQTT_Topic = "T5/VZ/PIR1"
+P3_MQTT_SendRetained = False
+P3_MQTT_SendLastUpdate = False
+P3_MQTT_SendOnlyIfValueChanged = True
+
+P4_MQTT_Topic = "T5/VZ/PIR2"
+P4_MQTT_SendRetained = False
+P4_MQTT_SendLastUpdate = False
+P4_MQTT_SendOnlyIfValueChanged = True
+
+DHT22_MQTT_Topic_Temp = "T5/Abstr/Sensors/temp"
+DHT22_MQTT_Topic_Hum = "T5/Abstr/Sensors/hum"
+DHT22_MQTT_SendRetained = False
+DHT22_MQTT_SendLastUpdate = False
+
+# MQTT config
+mqtt_server = "mqtt.lan"
+mqtt_port = 1883
+mqtt_user = "xxxxx"
+mqtt_password = "xxxxx"
+
+mqtt_base_topic = "T5/HomeSvrIOExt"
+

+ 26 - 18
ioext/ioext.py

@@ -6,6 +6,7 @@ import os
 import sys
 import paho.mqtt.client as mqtt
 import json
+from datetime import datetime
 
 # --- CONFIGURATION ---
 import config
@@ -20,9 +21,9 @@ verbose = False
 debug = False
 quiet = True
 
-lastState_tk = None
-lastState_pir1 = None
-lastState_pir2 = None
+lastState_P2 = None
+lastState_P3 = None
+lastState_P4 = None
 # --- END GLOBAL VARS ---
 
 
@@ -121,7 +122,7 @@ try:
                 print ('RX: ' + repr(serLine))    #Echo the serial buffer bytes up to the CRLF back to screen
             mqttc.publish(config.mqtt_base_topic + "/RX", str(serLine), qos=0, retain=False)
             
-            # Tuerkontakt
+            # digital input P2
             if serLine.startswith('P2='):
                 newState = None
                 if serLine == "P2=L":
@@ -129,11 +130,13 @@ try:
                 elif serLine == "P2=H":
                     newState = "ON"
                     
-                if newState is not None and (lastState_tk != newState or not filterUnchanged):
-                    lastState_tk = newState
-                    mqttc.publish(config.mqtt_topic_tuerkontakt, newState, qos=0, retain=False)
+                if newState is not None and (lastState_P2 != newState or not config.P2_MQTT_SendOnlyIfValueChanged):
+                    lastState_P2 = newState
+                    mqttc.publish(config.P2_MQTT_Topic, newState, qos=0, retain=config.P2_MQTT_SendRetained)
+                    if config.P2_MQTT_SendLastUpdate:
+                        mqttc.publish(config.P2_MQTT_Topic + "_lastUpdate", datetime.now().isoformat(), qos=0, retain=config.P2_MQTT_SendRetained)
                     
-            # PIR #1
+            # digital input P3
             if serLine.startswith('P3='):
                 newState = None
                 if serLine == "P3=L":
@@ -141,11 +144,13 @@ try:
                 elif serLine == "P3=H":
                     newState = "ON"
                     
-                if newState is not None and (lastState_pir1 != newState or not filterUnchanged):
-                    lastState_pir1 = newState
-                    mqttc.publish(config.mqtt_topic_pir1, newState, qos=0, retain=False)
+                if newState is not None and (lastState_P3 != newState or not config.P3_MQTT_SendOnlyIfValueChanged):
+                    lastState_P3 = newState
+                    mqttc.publish(config.P3_MQTT_Topic, newState, qos=0, retain=config.P3_MQTT_SendRetained)
+                    if config.P3_MQTT_SendLastUpdate:
+                        mqttc.publish(config.P3_MQTT_Topic + "_lastUpdate", datetime.now().isoformat(), qos=0, retain=config.P3_MQTT_SendRetained)
             
-            # PIR #2
+            # digital input P4
             if serLine.startswith('P4='):
                 newState = None
                 if serLine == "P4=L":
@@ -153,9 +158,11 @@ try:
                 elif serLine == "P4=H":
                     newState = "ON"
                 
-                if newState is not None and (lastState_pir2 != newState or not filterUnchanged):
-                    lastState_pir2 = newState
-                    mqttc.publish(config.mqtt_topic_pir2, newState, qos=0, retain=False)
+                if newState is not None and (lastState_P4 != newState or not config.P4_MQTT_SendOnlyIfValueChanged):
+                    lastState_P4 = newState
+                    mqttc.publish(config.P4_MQTT_Topic, newState, qos=0, retain=config.P4_MQTT_SendRetained)
+                    if config.P4_MQTT_SendLastUpdate:
+                        mqttc.publish(config.P4_MQTT_Topic + "_lastUpdate", datetime.now().isoformat(), qos=0, retain=config.P4_MQTT_SendRetained)
                    
             # DHT TH sensor
             # {"T":26.60,"H":36}
@@ -164,10 +171,11 @@ try:
                 t = round(float(th["T"]), 1)
                 h = int(th["H"])
                 if t >= -20 and t <= 50:
-                    mqttc.publish(config.mqtt_topic_out_temp, str(t), qos=0, retain=False)
+                    mqttc.publish(config.DHT22_MQTT_Topic_Temp, str(t), qos=0, retain=config.DHT22_MQTT_SendRetained)
                 if h >= 0 and h <= 100:
-                    mqttc.publish(config.mqtt_topic_out_hum, str(h), qos=0, retain=False)
-                
+                    mqttc.publish(config.DHT22_MQTT_Topic_Hum, str(h), qos=0, retain=config.DHT22_MQTT_SendRetained)
+                if config.DHT22_MQTT_SendLastUpdate:
+                        mqttc.publish(config.DHT22_MQTT_Topic_Temp + "_lastUpdate", datetime.now().isoformat(), qos=0, retain=config.DHT22_MQTT_SendRetained)
                 
         # nothing came in this time...
         else: