SRF node - add ability to set time interval

to close #338
This commit is contained in:
Dave Conway-Jones 2017-08-14 09:20:18 +01:00
parent 7950b7d39a
commit 363bb55b26
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D
5 changed files with 19 additions and 10 deletions

View File

@ -23,8 +23,10 @@ These can be any spare valid Pi GPIO pins. e.g.
7,11
You can also set the repeat frequency of measurements - default 0.5 seconds.
Outputs a `msg.payload` with a number representing the range in cm.
Produces one measure every 0.5s - but only if the distance is different from the previous reading.
Produces one measure every 0.5s (by default) - but only if the distance is different from the previous reading.
**Note:** we are using the actual physical pin numbers on connector P1 as they are easier to locate.

View File

@ -15,6 +15,7 @@ GPIO.setwarnings(False)
ECHO = 0
TRIGGER = 0
OLD = 0
SLEEP = 0.5
def Measure():
start = 0
@ -48,13 +49,14 @@ def Measure():
# Main program loop
if len(sys.argv) > 1:
pins = sys.argv[1].lower().split(',')
if len(pins) != 2:
print "Bad number of pins supplied"
if len(pins) != 3:
print "Bad parameters supplied"
print pins
sys.exit(0)
TRIGGER = int(pins[0])
ECHO = int(pins[1])
SLEEP = float(pins[2])
GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers
GPIO.setup(TRIGGER, GPIO.OUT) # Trigger
@ -72,7 +74,7 @@ if len(sys.argv) > 1:
if distance != OLD:
print(distance)
OLD = distance
time.sleep(0.5)
time.sleep(SLEEP)
except: # try to clean up on exit
print("0.0");
GPIO.cleanup(TRIGGER)
@ -81,5 +83,5 @@ if len(sys.argv) > 1:
else:
print "Bad params"
print " sudo nrsrf.py trigger_pin,echo_pin"
print " sudo nrsrf.py trigger_pin,echo_pin,rate_in_seconds"
sys.exit(0)

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-pisrf",
"version" : "0.0.5",
"version" : "0.1.0",
"description" : "A Node-RED node for a Raspberry Pi to use a SRF04 or SRF05 range finder",
"dependencies" : {
},

View File

@ -4,11 +4,14 @@
<label for="node-input-pins"><i class="fa fa-circle"></i> Pins</label>
<input type="text" id="node-input-pins" placeholder="Trigger,Echo">
</div>
<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-topic"><i class="fa fa-bars"></i> Topic</label>
<input type="text" id="node-input-topic" placeholder="optional topic">
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
@ -20,7 +23,7 @@
<script type="text/x-red" data-help-name="rpi-srf">
<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 measure every 0.5s - but only if the distance is different from the previous reading.</p>
<p>Produces one measurement every 0.5s (default) - but only if the distance is different from the previous reading.</p>
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
</script>
@ -31,6 +34,7 @@
defaults: {
name: { value:"" },
topic: { value:"SRF" },
pulse: {value:"0.5" },
pins: { value:"", required:true, validate:RED.validators.regex(/^\d+,\d+$/) }
},
inputs:0,

View File

@ -26,12 +26,13 @@ module.exports = function(RED) {
RED.nodes.createNode(this, n);
this.topic = n.topic;
this.pins = n.pins;
this.pins += ","+(n.pulse || 0.5);
var node = this;
if (node.pins !== undefined) {
node.child = spawn(gpioCommand, [node.pins]);
node.running = true;
if (RED.settings.verbose) { node.log("pin: " + node.pins + " :"); }
if (RED.settings.verbose) { node.log("parameters: " + node.pins + " :"); }
node.child.stdout.on('data', function(data) {
if (RED.settings.verbose) { node.log("out: " + data + " :"); }
@ -59,7 +60,7 @@ module.exports = function(RED) {
}
else {
node.error("Invalid GPIO pins: " + node.pin);
node.error("Invalid Parameters: " + node.pins);
}
var wfi = function(done) {