Skip to content

Instantly share code, notes, and snippets.

@domadev812
Forked from ToeJamson/1.py
Last active November 15, 2017 21:05
Show Gist options
  • Select an option

  • Save domadev812/c130d734fa40e8a43a01258da2a80841 to your computer and use it in GitHub Desktop.

Select an option

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)
from pubnub.pnconfiguration import PNConfiguration
from pubnub.pubnub import PubNub
import RPi.GPIO as GPIO
import time
import sys
loopcount = 0
distance = pulse_duration*17150
distance = round(distance, 2)
loopcount+=1
print("Distance:",distance,"cm")
print("Measured distance")
message = {['distance', distance]}
print pubnub.publish().channel(channel).message(message)
time.sleep(1)
GPIO.cleanup()
sys.exit()
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)
subchannel = 'MotionDetector'
def callback(submessage, channel):
if submessage["motion"] == 1:
GPIO.output(TRIG,False)
print("Waiting for sensor to settle.")
time.sleep(2)
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()
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'
GPIO.setmode(GPIO.BCM)
TRIG = 20
ECHO = 26
print("Distance Measurement in Progess")
GPIO.setup(TRIG,GPIO.OUT)
GPIO.setup(ECHO,GPIO.IN)`
GPIO.output(TRIG,False)
print("Waiting for sensor to settle.")
time.sleep(2)
while True:
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
print("before pulse start") # debug statement
pulse_start = time.time()
while GPIO.input(ECHO)==0:
pulse_start = time.time()
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