ソースを参照

handle unstable results

Torsten Simon 3 年 前
コミット
b4b7009585
1 ファイル変更34 行追加20 行削除
  1. 34 20
      data-writer.py

+ 34 - 20
data-writer.py

@@ -15,6 +15,11 @@ import logging
 from rpi_rf import RFDevice
 VOLTAGE = 0
 AMPERE = 1
+
+# min samples in a row stable to be accepted as new value
+STABLE_COUNT = 3
+# max diff which is considered stable between new values
+STABLE_DIFF_THRESHOLD = .1
 rfdevice = None
 
 # pylint: disable=unused-argument
@@ -30,10 +35,12 @@ parser.add_argument('-g', dest='gpio', type=int, default=27,
 args = parser.parse_args()
 
 signal.signal(signal.SIGINT, exithandler)
-rfdevice = RFDevice(args.gpio)
+rfdevice = RFDevice(args.gpio, rx_tolerance=80)
 rfdevice.enable_rx()
 timestamp = None
 logging.info("Listening for codes on GPIO " + str(args.gpio))
+previous_values = {}
+counts = {}
 while True:
     if rfdevice.rx_code_timestamp != timestamp:
         timestamp = rfdevice.rx_code_timestamp
@@ -42,24 +49,31 @@ while True:
         value = (code >> 8) / 1000.
         U = 0
         I = 0
-        if codeType == VOLTAGE or True:
-            logging.info("Voltage")
-            U = value
-            client.write_points([
-                {
-                    "measurement": "solar",
-                    "tags": {
-                        "type": "U"
-                    },
-                    "fields": {
-                        "value": U
+        if value:
+            if codeType in previous_values and abs(previous_values[codeType]-value) <= STABLE_DIFF_THRESHOLD:
+                counts[codeType] += 1
+            else:
+                counts[codeType] = 0
+            previous_values[codeType] = value
+        logging.info("Data Received: " + str(code) + "/" + str(codeType) + " / " + str(value))
+        if codeType in counts and counts[codeType] >= STABLE_COUNT:
+            if codeType == VOLTAGE or True:
+                U = value
+                logging.info("Stable Voltage:" +str(U))
+                client.write_points([
+                    {
+                        "measurement": "solar",
+                        "tags": {
+                            "type": "U"
+                        },
+                        "fields": {
+                            "value": U
+                        }
                     }
-                }
-            ], time_precision='ms')
-        if codeType == AMPERE:
-            logging.info("Ampere")
-            I = value
-        
-        logging.info(str(code) + "/" + str(codeType) + ", U=" + str(U))
-    time.sleep(0.01)
+                ], time_precision='ms')
+            if codeType == AMPERE:
+                logging.info("Ampere")
+                I = value
+            
+    time.sleep(0.05)
 rfdevice.cleanup()