From 55ad417f388491d83d58ca12db501a78867d4164 Mon Sep 17 00:00:00 2001 From: MGodfrey50 Date: Sun, 10 Feb 2019 22:39:02 -0800 Subject: [PATCH] Add files via upload --- buzzer.py | 32 ++++++++++++ checkInternet.py | 18 +++++++ distance.py.save | 130 +++++++++++++++++++++++++++++++++++++++++++++++ ultrasonic_1.py | 72 ++++++++++++++++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 buzzer.py create mode 100644 checkInternet.py create mode 100644 distance.py.save create mode 100644 ultrasonic_1.py diff --git a/buzzer.py b/buzzer.py new file mode 100644 index 0000000..9edd0f5 --- /dev/null +++ b/buzzer.py @@ -0,0 +1,32 @@ +# This program sends a signal to the buzzer (3 pronged passive) + +import RPi.GPIO as GPIO +GPIO.setmode(GPIO.BCM) + + +# Using GPIO 20 as the signal pin +# The Left and center pins are Ground and +ve respectively + +GPIO_PIN = 20 +GPIO.setup(GPIO_PIN, GPIO.OUT) + +# Set the frequency in Hz of the output pin +Freq = 500 #In Hertz +pwm = GPIO.PWM(GPIO_PIN, Freq) +pwm.start(50) + +#print ("Please press enter" ,input()) + +# Unless there's a keyboard interrupt -send the signal to the buzzer +try: + while True: + print ("----------------------------------------") + print ("Aktuelle Frequenz: %d", Freq) + Freq = input("Enter a Frequency between (50 -5000):") + Freq = float(Freq) + pwm.ChangeFrequency(Freq) + + # Stop when ^C or Esc keys are pressed +except KeyboardInterrupt: + pwm.stop() + GPIO.cleanup() diff --git a/checkInternet.py b/checkInternet.py new file mode 100644 index 0000000..3f01265 --- /dev/null +++ b/checkInternet.py @@ -0,0 +1,18 @@ +# import urllib.request +import urllib + +def connected(host='https://www.google.com'): + try: + urllib.request.urlopen(host) + return True + except: + return False + +# test +# print( 'connected' if connected() else 'no internet!' ) +if connected(): + print("Connected") +else: + print("Not working") + + diff --git a/distance.py.save b/distance.py.save new file mode 100644 index 0000000..6597cde --- /dev/null +++ b/distance.py.save @@ -0,0 +1,130 @@ +## +# 2015-05-16 - Ultrasound code - Reliable +# Maarten Pater (www.mirdesign.nl) +# +# Based on work of Keith Hekker and Matt Hawkins +# Keith: http://khekker.blogspot.nl/2013/03/raspberry-pi-and-monitoring-sump-pump.html +# Matt: http://www.raspberrypi-spy.co.uk/2012/12/ultrasonic-distance-measurement-using-python-part-1/ +# +# Buy the device here: http://www.dx.com/p/314393 +# +# Example usage at the bottom! +# +## +import RPi.GPIO as GPIO +import time +import datetime + +# Pin settings +PIN_ULTRA_SWITCH_ON = GPIO.HIGH +PIN_ULTRA_SWITCH_OFF = GPIO.LOW +PIN_ULTRA_TRIGGER =23 +PIN_ULTRA_ECHO = 24 + +# Device settings +ULTRA_SLEEP_AFTER_TRIGGER = 0.00001 +ULTRA_SLEEP_AFTER_READ_SAPLE = 0.05 + +# Bad sample settings +BAIL_OUT_THRESHOLD_WAITING_FOR_ECHO = 200 +BAIL_OUT_THRESHOLD_RECEIVING_ECHO = 2500 +BAIL_OUT_THRESHOLD_ACCEPT_FAILED_READS = 0 + +# GPIO initialisation +GPIO.setmode(GPIO.BCM) +GPIO.setup(PIN_ULTRA_TRIGGER, GPIO.OUT) +GPIO.setup(PIN_ULTRA_ECHO, GPIO.IN) +GPIO.output(PIN_ULTRA_TRIGGER, PIN_ULTRA_SWITCH_OFF) + +def GetDistance(NumberOfSamples = 10): + dDistances = 0 + dCount = 0 + distanceAvg = -1 + + # Reading samples + for x in range(0,NumberOfSamples): + distance = ReadValue() + if (distance != -1): # -1 means: No successful run + dDistances = dDistances + distance + dCount = dCount + 1 + time.sleep(ULTRA_SLEEP_AFTER_READ_SAPLE) # Give the device some rest + + if (dCount > 0): + distanceAvg = dDistances / dCount + + return distanceAvg + +def Diagnose(): + while True: + distance = ReadValue() + print ("Distance : %.1f (final)") % distance + +def ReadValue(NumberOfSamples = 10): + + dDistance = 0 + bailOutCount = 0 + + for x in range(0,NumberOfSamples): + start = time.time() + stop = start + + # Send 10us pulse to trigger + GPIO.output(PIN_ULTRA_TRIGGER, PIN_ULTRA_SWITCH_ON) + time.sleep(ULTRA_SLEEP_AFTER_TRIGGER) + GPIO.output(PIN_ULTRA_TRIGGER, PIN_ULTRA_SWITCH_OFF) + + # We are going to bail out if device takes + # to long to provide expected answer + bailedOut = False + waitCount = 0 + + # Wait until the devices is ready send + # 8 samples on 40khz + while GPIO.input(PIN_ULTRA_ECHO)==0: + waitCount = waitCount + 1 + + # Bail out if we are waiting to long for the 8 signals to end + if (waitCount > BAIL_OUT_THRESHOLD_WAITING_FOR_ECHO): + bailOutCount = bailOutCount + 1 + bailedOut = True + break + start = time.time() + stop = start + + if (bailedOut == False): + waitCount = 0 + while GPIO.input(PIN_ULTRA_ECHO)==1: + waitCount = waitCount + 1 + + # Bail out if we are waiting to long for the 8 signals to echo + if (waitCount > BAIL_OUT_THRESHOLD_RECEIVING_ECHO): + bailOutCount = bailOutCount + 1 + bailedOut = True + break + stop = time.time() + + if (bailedOut == False): + elapsed = stop-start + distance = elapsed * 34300 / 2 # Speed of sound / back and forth + + if x > 0 and distance > 0: + dDistance = dDistance + distance + + retVal = -1 + + # Only return a distance if we didn't exceed the number of failed samples + if(bailOutCount <= BAIL_OUT_THRESHOLD_ACCEPT_FAILED_READS): + divideBy = (NumberOfSamples-1-bailOutCount) + if(divideBy > 0): + retVal = dDistance / divideBy + + return retVal + +# To show unlimited reads +Diagnose() + +# To show just 1 read +print (GetDistance()) + +# Clean up +GPIO.cleanup() diff --git a/ultrasonic_1.py b/ultrasonic_1.py new file mode 100644 index 0000000..e932d0b --- /dev/null +++ b/ultrasonic_1.py @@ -0,0 +1,72 @@ +#!/usr/bin/python +#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +#|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k| +#+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +# +# ultrasonic_1.py +# Measure distance using an ultrasonic module +# +# Ultrasonic related posts: +# http://www.raspberrypi-spy.co.uk/tag/ultrasonic/ +# +# Author : Matt Hawkins +# Date : 16/10/2016 +# ----------------------- + +# Import required Python libraries +from __future__ import print_function +import time +import RPi.GPIO as GPIO + +# Use BCM GPIO references +# instead of physical pin numbers +GPIO.setmode(GPIO.BCM) + +# Define GPIO to use on Pi +GPIO_TRIGGER = 23 +GPIO_ECHO = 24 + +# Speed of sound in cm/s at temperature +temperature = 20 +speedSound = 33100 + (0.6*temperature) + +print("Ultrasonic Measurement") +print("Speed of sound is",speedSound/100,"m/s at ",temperature,"deg") + +# Set pins as output and input +GPIO.setup(GPIO_TRIGGER,GPIO.OUT) # Trigger +GPIO.setup(GPIO_ECHO,GPIO.IN) # Echo + +# Set trigger to False (Low) +GPIO.output(GPIO_TRIGGER, False) + +# Allow module to settle +time.sleep(0.5) + +# Send 10us pulse to trigger +GPIO.output(GPIO_TRIGGER, True) +# Wait 10us +time.sleep(0.00001) +GPIO.output(GPIO_TRIGGER, False) +start = time.time() + +while GPIO.input(GPIO_ECHO)==0: + start = time.time() + +while GPIO.input(GPIO_ECHO)==1: + stop = time.time() + +# Calculate pulse length +elapsed = stop-start + +# Distance pulse travelled in that time is time +# multiplied by the speed of sound (cm/s) +distance = elapsed * speedSound + +# That was the distance there and back so halve the value +distance = distance / 2 + +print("Distance : {0:5.1f}".format(distance)) + +# Reset GPIO settings +GPIO.cleanup() \ No newline at end of file