#!/usr/bin/env python from gpiozero import DistanceSensor from time import sleep import urllib import pickle # Define what pins to use echo_pin = 17 trigger_pin = 4 # Define urls for IFTTT ocuppied_url = "https://maker.ifttt.com/trigger/car_in_garage/with/key/c8Z6PR-5Dful7BaL_C7r5R" vacant_url = "https://maker.ifttt.com/trigger/car_not_in_garage/with/key/c8Z6PR-5Dful7BaL_C7r5R" # Load previous results or return None distance_file = "distance.bin" try: with open(distance_file, 'rb') as file_handle: old_distance = pickle.load(file_handle) except: old_distance = None # Get sensor data sensor = DistanceSensor(echo=echo_pin, trigger=trigger_pin) distance = sensor.distance print("Distance:") print(distance) # Save current results to file with open(distance_file, 'wb') as file_handle: pickle.dump(distance, file_handle) # Check for the presence of a car # Ocuppied if sensor.distance < 1.0: # Only send an alert if the state changes if old_distance == None or old_distance == 1.0: webpage = urllib.urlopen(ocuppied_url) print("Webpage result:") print(webpage.read()) # Vacant else: # Only send an alert if the state changes if old_distance == None or old_distance < 1.0: webpage = urllib.urlopen(vacant_url) print("Webpage result:") print(webpage.read())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question