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

94 lines
2.5 KiB
Python
Raw Normal View History

#!/usr/bin/python
# Python driver for SRF04 and SRO05 Ultrasonic sensors
# Modified to use PINS not BCM : Dave Conway-Jones
#import
import RPi.GPIO as GPIO
import time
import sys
import os, select
import signal
def signal_handler(sig, frame):
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.
2021-07-08 09:32:55 +02:00
#sys.exit(0) #Program won't stop with it
os._exit(0)
signal.signal(signal.SIGINT, signal_handler)
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.
2021-07-08 09:32:55 +02:00
signal.signal(signal.SIGTERM, signal_handler)
# Turn off warnings if you run it a second time...
GPIO.setwarnings(False)
ECHO = 0
TRIGGER = 0
OLD = 0
SLEEP = 0.5
MAX_DIST = 400
def Measure():
realstart = time.time()
GPIO.output(TRIGGER, True)
time.sleep(0.00001)
GPIO.output(TRIGGER, False)
start = time.time()
stop = time.time()
while GPIO.input(ECHO)==0:
start = time.time()
Dif = time.time() - realstart
if Dif > 0.2:
# print("Ultrasonic Sensor Timed out, Restart.")
time.sleep(0.4)
return 400
while GPIO.input(ECHO)==1:
stop = time.time()
Dif = time.time() - realstart
if Dif > 0.4:
print("Ultrasonic Sensor Timed out, Restarting.")
time.sleep(0.2)
return 400
elapsed = stop - start
distance = (elapsed * 36000)/2
return distance
# Main program loop
if len(sys.argv) > 1:
pins = sys.argv[1].lower().split(',')
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.
2021-07-08 09:32:55 +02:00
if not 3 <= len(pins) <=4 :
2018-07-07 20:24:35 +02:00
print("Bad parameters supplied")
print(pins)
sys.exit(0)
TRIGGER = int(pins[0])
ECHO = int(pins[1])
SLEEP = float(pins[2])
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.
2021-07-08 09:32:55 +02:00
precision = int(pins[3]) if len(pins) >= 4 else 0
GPIO.setmode(GPIO.BOARD) # Use GPIO BOARD numbers
GPIO.setup(TRIGGER, GPIO.OUT) # Trigger
GPIO.output(TRIGGER, False)
GPIO.setup(ECHO, GPIO.OUT) # Echo
GPIO.output(ECHO, False)
GPIO.setup(ECHO,GPIO.IN)
# Flush stdin so we start clean
while len(select.select([sys.stdin.fileno()], [], [], 0.0)[0])>0:
os.read(sys.stdin.fileno(), 4096)
while True:
try:
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.
2021-07-08 09:32:55 +02:00
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)
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.
2021-07-08 09:32:55 +02:00
except Exception as e: # try to clean up on exit
print("0.0")
else:
2018-07-07 20:24:35 +02:00
print("Bad params")
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.
2021-07-08 09:32:55 +02:00
print(" nrsrf.py trigger_pin, echo_pin, rate_in_seconds, [precision_digits]")
sys.exit(0)