From 245109940c5826eb9c7a94bbe668a52b8398a905 Mon Sep 17 00:00:00 2001 From: JsBergbau <37013344+JsBergbau@users.noreply.github.com> Date: Thu, 8 Jul 2021 09:32:55 +0200 Subject: [PATCH] PiSrf: Added decimal places option + stop bugfix + more keywords (#816) * Added decimal places option + bugfix + keywords Added option to output more decimal places. When no decimal places are configured output stays the same for full backward compatibility. Fixed bug that on restart flows error "Error stopping node: Close timed out" occured. Also removed sudo, because no need to run code with root rights. On systems where you need password for sudo it wouldn't run then. Added Keywords for "HC-SR04" and "SR04", because this module is fully compatible with this node. * Added decimal places option + bugfix + keywords Added option to output more decimal places. When no decimal places are configured output stays the same for full backward compatibility. Fixed bug that on restart flows error "Error stopping node: Close timed out" occured. Also removed sudo, because no need to run code with root rights. On systems where you need password for sudo it wouldn't run then. Added Keywords for "HC-SR04" and "SR04", because this module is fully compatible with this node. * Added decimal places option + bugfix + keywords Added option to output more decimal places. When no decimal places are configured output stays the same for full backward compatibility. Fixed bug that on restart flows error "Error stopping node: Close timed out" occured. Also removed sudo, because no need to run code with root rights. On systems where you need password for sudo it wouldn't run then. Added Keywords for "HC-SR04" and "SR04", because this module is fully compatible with this node. --- hardware/PiSrf/nrsrf | 2 +- hardware/PiSrf/nrsrf.py | 17 +++++++++++------ hardware/PiSrf/package.json | 4 ++-- hardware/PiSrf/pisrf.html | 8 +++++++- hardware/PiSrf/pisrf.js | 1 + 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/hardware/PiSrf/nrsrf b/hardware/PiSrf/nrsrf index 6c8fcbc1..960fdfbb 100755 --- a/hardware/PiSrf/nrsrf +++ b/hardware/PiSrf/nrsrf @@ -4,4 +4,4 @@ python_cmd='python3' command -v python3 > /dev/null || python_cmd=`python` BASEDIR=$(dirname $0) -sudo $python_cmd -u $BASEDIR/nrsrf.py $@ +exec $python_cmd -u $BASEDIR/nrsrf.py $@ \ No newline at end of file diff --git a/hardware/PiSrf/nrsrf.py b/hardware/PiSrf/nrsrf.py index fbe7138c..f9999122 100755 --- a/hardware/PiSrf/nrsrf.py +++ b/hardware/PiSrf/nrsrf.py @@ -11,8 +11,11 @@ import os, select import signal def signal_handler(sig, frame): - sys.exit(0) + #sys.exit(0) #Program won't stop with it + os._exit(0) signal.signal(signal.SIGINT, signal_handler) +signal.signal(signal.SIGTERM, signal_handler) + # Turn off warnings if you run it a second time... GPIO.setwarnings(False) @@ -52,7 +55,7 @@ def Measure(): # Main program loop if len(sys.argv) > 1: pins = sys.argv[1].lower().split(',') - if len(pins) != 3: + if not 3 <= len(pins) <=4 : print("Bad parameters supplied") print(pins) sys.exit(0) @@ -60,6 +63,7 @@ if len(sys.argv) > 1: TRIGGER = int(pins[0]) ECHO = int(pins[1]) SLEEP = float(pins[2]) + precision = int(pins[3]) if len(pins) >= 4 else 0 GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers GPIO.setup(TRIGGER, GPIO.OUT) # Trigger @@ -74,15 +78,16 @@ if len(sys.argv) > 1: while True: try: - distance = int( Measure() + 0.5 ) + distance = round( Measure(),precision) + distance = int(distance) if precision == 0 else distance if distance != OLD and distance > 2 and distance < 400: print(distance) OLD = distance time.sleep(SLEEP) - except: # try to clean up on exit - print("0.0") + except Exception as e: # try to clean up on exit + print("0.0") else: print("Bad params") - print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds") + print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds, [precision_digits]") sys.exit(0) diff --git a/hardware/PiSrf/package.json b/hardware/PiSrf/package.json index 4a03d4a1..15f615c6 100644 --- a/hardware/PiSrf/package.json +++ b/hardware/PiSrf/package.json @@ -1,6 +1,6 @@ { "name" : "node-red-node-pisrf", - "version" : "0.1.6", + "version" : "0.1.7", "description" : "A Node-RED node for a Raspberry Pi to use a SRF04 or SRF05 range finder", "dependencies" : { }, @@ -9,7 +9,7 @@ "url":"https://github.com/node-red/node-red-nodes/tree/master/hardware/PiSrf" }, "license": "Apache-2.0", - "keywords": [ "node-red", "SRF04", "SRF05", "ultrasonic", "range" ], + "keywords": [ "node-red", "SRF04", "SRF05", "ultrasonic", "range", "HC-SR04", "SR04" ], "node-red" : { "nodes" : { "rpi-srf": "pisrf.js" diff --git a/hardware/PiSrf/pisrf.html b/hardware/PiSrf/pisrf.html index 5dfe2f6b..b0a45eca 100644 --- a/hardware/PiSrf/pisrf.html +++ b/hardware/PiSrf/pisrf.html @@ -7,6 +7,10 @@
+
+
+ +
@@ -24,6 +28,7 @@

Raspberry Pi input from an SRF04 or SRF05 ultrasonic range finder.

Outputs a msg.payload with a number representing the range in cm.

Produces one measurement every 0.5s (default) - but only if the distance is different from the previous reading.

+

You can specify resolution up to one decimal place.

Note: we are using the actual physical pin numbers on connector P1 as they are easier to locate.

@@ -35,7 +40,8 @@ name: { value:"" }, topic: { value:"SRF" }, pulse: {value:"0.5" }, - pins: { value:"", required:true, validate:RED.validators.regex(/^\d+,\d+$/) } + pins: { value:"", required:true, validate:RED.validators.regex(/^\d+,\d+$/) }, + precision: {value:"0", validate:RED.validators.regex(/^0|1|^$/)} }, inputs:0, outputs:1, diff --git a/hardware/PiSrf/pisrf.js b/hardware/PiSrf/pisrf.js index aed2c45c..a996977e 100644 --- a/hardware/PiSrf/pisrf.js +++ b/hardware/PiSrf/pisrf.js @@ -33,6 +33,7 @@ module.exports = function(RED) { this.topic = n.topic; this.pins = n.pins; this.pins += ","+(n.pulse || 0.5); + this.pins += ","+(n.precision || 0); var node = this; if (allOK === true) {