-
-
Save ToeJamson/efe1def508e3baa0d144 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 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, 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
| publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' | |
| subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'PASTE SUBKEY HERE' | |
| secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' | |
| cipher_key = len(sys.argv) > 4 and sys.argv[4] or '' | |
| ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False | |
| pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) |
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) |
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
| Publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo' | |
| subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo' | |
| secret_key = len(sys.argv) > 3 and sys.argv[3] or 'demo' | |
| cipher_key = len(sys.argv) > 4 and sys.argv[4] or '' | |
| ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False | |
| pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on) | |
| 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