Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MGodfrey50 authored Feb 11, 2019
1 parent e7e6e9e commit 55ad417
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 0 deletions.
32 changes: 32 additions & 0 deletions buzzer.py
Original file line number Diff line number Diff line change
@@ -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()
18 changes: 18 additions & 0 deletions checkInternet.py
Original file line number Diff line number Diff line change
@@ -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")


130 changes: 130 additions & 0 deletions distance.py.save
Original file line number Diff line number Diff line change
@@ -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()
72 changes: 72 additions & 0 deletions ultrasonic_1.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 55ad417

Please sign in to comment.