Raspberry Pi - real time connectivity
Hi, I have used the Connect to Raspberry Pi Guide and now i have a working connectivity between TW and RPI. I have also attached a PIR motion detector sensor to the RPI.
The thing is i would like to get data real time from the Raspberry pi, right now i can only make it refreshing the Lua script once every 0.5 seconds. But in order to get movement in real time i would need something different.
this is the code i used in python. i was not able to make a loop there as i need a return statement to send it to Lua so Lua can send it to TW
#Libraries
import RPi.GPIO as GPIO
import time
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BOARD)
#set GPIO Pins
GPIO_ECHO = 8
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance():
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
dist = distance()
if dist is not None:
print("movement detected %.1f " % dist)
else:
print("failed to get the data")
sys.exit(1)

