Skip to content

Commit 2451099

Browse files
authored
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.
1 parent 4859a8b commit 2451099

5 files changed

Lines changed: 22 additions & 10 deletions

File tree

hardware/PiSrf/nrsrf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ python_cmd='python3'
44
command -v python3 > /dev/null || python_cmd=`python`
55

66
BASEDIR=$(dirname $0)
7-
sudo $python_cmd -u $BASEDIR/nrsrf.py $@
7+
exec $python_cmd -u $BASEDIR/nrsrf.py $@

hardware/PiSrf/nrsrf.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
import signal
1212

1313
def signal_handler(sig, frame):
14-
sys.exit(0)
14+
#sys.exit(0) #Program won't stop with it
15+
os._exit(0)
1516
signal.signal(signal.SIGINT, signal_handler)
17+
signal.signal(signal.SIGTERM, signal_handler)
18+
1619

1720
# Turn off warnings if you run it a second time...
1821
GPIO.setwarnings(False)
@@ -52,14 +55,15 @@ def Measure():
5255
# Main program loop
5356
if len(sys.argv) > 1:
5457
pins = sys.argv[1].lower().split(',')
55-
if len(pins) != 3:
58+
if not 3 <= len(pins) <=4 :
5659
print("Bad parameters supplied")
5760
print(pins)
5861
sys.exit(0)
5962

6063
TRIGGER = int(pins[0])
6164
ECHO = int(pins[1])
6265
SLEEP = float(pins[2])
66+
precision = int(pins[3]) if len(pins) >= 4 else 0
6367

6468
GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers
6569
GPIO.setup(TRIGGER, GPIO.OUT) # Trigger
@@ -74,15 +78,16 @@ def Measure():
7478

7579
while True:
7680
try:
77-
distance = int( Measure() + 0.5 )
81+
distance = round( Measure(),precision)
82+
distance = int(distance) if precision == 0 else distance
7883
if distance != OLD and distance > 2 and distance < 400:
7984
print(distance)
8085
OLD = distance
8186
time.sleep(SLEEP)
82-
except: # try to clean up on exit
83-
print("0.0")
87+
except Exception as e: # try to clean up on exit
88+
print("0.0")
8489

8590
else:
8691
print("Bad params")
87-
print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds")
92+
print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds, [precision_digits]")
8893
sys.exit(0)

hardware/PiSrf/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "node-red-node-pisrf",
3-
"version" : "0.1.6",
3+
"version" : "0.1.7",
44
"description" : "A Node-RED node for a Raspberry Pi to use a SRF04 or SRF05 range finder",
55
"dependencies" : {
66
},
@@ -9,7 +9,7 @@
99
"url":"https://github.com/node-red/node-red-nodes/tree/master/hardware/PiSrf"
1010
},
1111
"license": "Apache-2.0",
12-
"keywords": [ "node-red", "SRF04", "SRF05", "ultrasonic", "range" ],
12+
"keywords": [ "node-red", "SRF04", "SRF05", "ultrasonic", "range", "HC-SR04", "SR04" ],
1313
"node-red" : {
1414
"nodes" : {
1515
"rpi-srf": "pisrf.js"

hardware/PiSrf/pisrf.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
<div class="form-row">
88
<label for="node-input-pulse"><i class="fa fa-clock-o"></i> Repeat (S)</label>
99
<input type="text" id="node-input-pulse" placeholder="time between readings">
10+
</div>
11+
<div class="form-row">
12+
<label for="node-input-precision"><i class="fa fa-balance-scale"></i>Decimal Places</label>
13+
<input type="text" id="node-input-precision" placeholder="decimal places 0 or 1">
1014
</div>
1115
<div class="form-row">
1216
<label for="node-input-topic"><i class="fa fa-bars"></i> Topic</label>
@@ -24,6 +28,7 @@
2428
<p>Raspberry Pi input from an SRF04 or SRF05 ultrasonic range finder.</p>
2529
<p>Outputs a <code>msg.payload</code> with a number representing the range in cm.</p>
2630
<p>Produces one measurement every 0.5s (default) - but only if the distance is different from the previous reading.</p>
31+
<p>You can specify resolution up to one decimal place.</p>
2732
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
2833
</script>
2934

@@ -35,7 +40,8 @@
3540
name: { value:"" },
3641
topic: { value:"SRF" },
3742
pulse: {value:"0.5" },
38-
pins: { value:"", required:true, validate:RED.validators.regex(/^\d+,\d+$/) }
43+
pins: { value:"", required:true, validate:RED.validators.regex(/^\d+,\d+$/) },
44+
precision: {value:"0", validate:RED.validators.regex(/^0|1|^$/)}
3945
},
4046
inputs:0,
4147
outputs:1,

hardware/PiSrf/pisrf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = function(RED) {
3333
this.topic = n.topic;
3434
this.pins = n.pins;
3535
this.pins += ","+(n.pulse || 0.5);
36+
this.pins += ","+(n.precision || 0);
3637
var node = this;
3738

3839
if (allOK === true) {

0 commit comments

Comments
 (0)