Last active
May 10, 2020 16:49
-
-
Save vishalroygeek/f67febeee9c1dddc310f1d6930625b83 to your computer and use it in GitHub Desktop.
A python script that helps Raspberry Pi make a beep-beep sound when there is no internet 📡
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import time | |
import urllib.request | |
#Defining variables | |
buzzer_pin = 23 | |
def setup(): | |
GPIO.setmode(GPIO.BCM) | |
#Setting the buzzer pin as output | |
GPIO.setup(buzzer_pin, GPIO.OUT) | |
#Function to check if device has internet connection | |
def isOnline(host='http://google.com'): | |
try: | |
urllib.request.urlopen(host) | |
return True | |
except: | |
return False | |
#Function to control a passive buzzer | |
def buzz(frequency, length): | |
if(frequency==0): | |
time.sleep(length) | |
return | |
period = 1.0 / frequency | |
delayValue = period / 2 | |
numCycles = int(length * frequency) | |
for i in range(numCycles): | |
GPIO.output(buzzer_pin, True) | |
time.sleep(delayValue) | |
GPIO.output(buzzer_pin, False) | |
time.sleep(delayValue) | |
#Initializing | |
setup() | |
while True: | |
if isOnline(): | |
print('Device is connected to internet') | |
time.sleep(5.0) | |
else: | |
print("Device isn't connected to internet") | |
#Lets make a beep when there is no internet ;) | |
buzz(2489, 0.2) | |
time.sleep(2.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment