-
-
Save domadev812/c130d734fa40e8a43a01258da2a80841 to your computer and use it in GitHub Desktop.
Pi-o-T: Prototyping IoT w/ Data Streams and Raspberry Pi (E. Grossman)
This file contains hidden or 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
| from pubnub.pnconfiguration import PNConfiguration | |
| from pubnub.pubnub import PubNub | |
| import RPi.GPIO as GPIO | |
| import time | |
| import sys | |
| loopcount = 0 |
This file contains hidden or 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
| distance = pulse_duration*17150 |
This file contains hidden or 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
| distance = round(distance, 2) | |
| loopcount+=1 |
This file contains hidden or 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
| print("Distance:",distance,"cm") | |
| print("Measured distance") | |
| message = {['distance', distance]} | |
| print pubnub.publish().channel(channel).message(message) | |
| time.sleep(1) |
This file contains hidden or 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
| GPIO.cleanup() | |
| sys.exit() |
This file contains hidden or 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
| pnconfig = PNConfiguration() | |
| pnconfig.subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'PASTE SUBKEY HERE' | |
| pnconfig.publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' | |
| pubnub = PubNub(pnconfig) |
This file contains hidden or 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
| subchannel = 'MotionDetector' |
This file contains hidden or 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
| def callback(submessage, channel): |
This file contains hidden or 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
| if submessage["motion"] == 1: |
This file contains hidden or 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
| GPIO.output(TRIG,False) | |
| print("Waiting for sensor to settle.") | |
| time.sleep(2) |
This file contains hidden or 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
| print("Now subscribing.") | |
| ##Actually subscribe to the channel to receive the messages:## | |
| pubnub.subscribe(subchannel, callback=callback, error=callback, connect=connect, reconnect=reconnect, disconnect=disconnect) | |
| pubnub.subscribe().channels(subchannel).execute() |
This file contains hidden or 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
| pnconfig = PNConfiguration() | |
| pnconfig.subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' | |
| pnconfig.publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' | |
| pubnub = PubNub(pnconfig) | |
| channel = 'Rangefinder' |
This file contains hidden or 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
| GPIO.setmode(GPIO.BCM) |
This file contains hidden or 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
| TRIG = 20 | |
| ECHO = 26 |
This file contains hidden or 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
| print("Distance Measurement in Progess") | |
| GPIO.setup(TRIG,GPIO.OUT) | |
| GPIO.setup(ECHO,GPIO.IN)` |
This file contains hidden or 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
| GPIO.output(TRIG,False) | |
| print("Waiting for sensor to settle.") | |
| time.sleep(2) |
This file contains hidden or 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
| while True: | |
| GPIO.output(TRIG, True) | |
| time.sleep(0.00001) | |
| GPIO.output(TRIG, False) |
This file contains hidden or 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
| print("before pulse start") # debug statement | |
| pulse_start = time.time() | |
| while GPIO.input(ECHO)==0: | |
| pulse_start = time.time() |
This file contains hidden or 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
| while GPIO.input(ECHO)==1: | |
| pulse_end = time.time() | |
| print("after pulse") # debug statement | |
| pulse_duration = pulse_end - pulse_start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment