1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

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.
This commit is contained in:
JsBergbau 2021-07-08 09:32:55 +02:00 committed by GitHub
parent 4859a8bcc0
commit 245109940c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 10 deletions

View File

@ -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 $@

View File

@ -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
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)

View File

@ -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"

View File

@ -7,6 +7,10 @@
<div class="form-row">
<label for="node-input-pulse"><i class="fa fa-clock-o"></i> Repeat (S)</label>
<input type="text" id="node-input-pulse" placeholder="time between readings">
</div>
<div class="form-row">
<label for="node-input-precision"><i class="fa fa-balance-scale"></i>Decimal Places</label>
<input type="text" id="node-input-precision" placeholder="decimal places 0 or 1">
</div>
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-bars"></i> Topic</label>
@ -24,6 +28,7 @@
<p>Raspberry Pi input from an SRF04 or SRF05 ultrasonic range finder.</p>
<p>Outputs a <code>msg.payload</code> with a number representing the range in cm.</p>
<p>Produces one measurement every 0.5s (default) - but only if the distance is different from the previous reading.</p>
<p>You can specify resolution up to one decimal place.</p>
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
</script>
@ -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,

View File

@ -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) {