cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Raspberry Pi - real time connectivity

gabitudor
7-Bedrock

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)

 

1 ACCEPTED SOLUTION

Accepted Solutions

I mentioned the REST API option to be sure you are aware of the different ways to connect sensors to TW, there is an overview here:

https://developer.thingworx.com/en/resources/guides/choosing-connectivity-method

 

Here is a quick overview how the different pieces of code on an edge device using the EMS work together:

EMS

The EMS keeps an Always-On connection open between the Raspberry Pi and TW. The TW platform can use this open Always-On connection at any time to send commands to, or retrieve property values from Things that are maintained on the EMS.

 

LSR

The LSR connects with the EMS and registers the Things it knows about and then loops polling your custom code for property value changes based on the scanRate you set in config.lua.

 

Custom Code

Your custom code interacts with your hardware and responds to commands or returns values whenever it is called by the LSR.

 

If you are trying to catch a sensor reading that exists for less 500 milliseconds, It does not seem like a good idea for your application to rely on the LSR to call your code faster than 2 times per second. Your code should be running in a loop of its own and maintain the value until the next time it is called  by the LSR.

 

The big advantage of the EMS and LSR is that they allow users to make on demand push or pull of information to edge hardware whenever they desire. If your application is only pushing data to the platform, you could consider using the REST API to push data directly from your code to the TW platform.

 

As an aside, you mentioned a PIR sensor, which stands for passive infrared, but the code snippet you included seems to be trying to use an acoustic sensor to determine distance. I would be surprised if you would be able to get meaningful distance measurement using Python running on Linux due to changing system latency caused by other running processes.

View solution in original post

4 REPLIES 4

The frequency that the Lua script resource checks for property updates is controlled by the scanRate property in config.lua

https://developer.thingworx.com/en/resources/guides/thingworx-raspberry-pi-quickstart/configure-lua-script-resource-lsr

 

For infrequent device to cloud messages you could consider calling the ThingWorx REST interface directly from the device. You could create a server on the ThingWorx platform to handle sensor events:  

https://developer.thingworx.com/en/resources/guides/thingworx-rest-api-quickstart/call-custom-service

 

 

thank you for the replay! but i do not think i get something. i am creating a REST connection between Raspberry Pi and TW but dont i need to call the service that sees the movement from the sensor? 

i mean, when i get to call it? and how?

 

thank you!

I mentioned the REST API option to be sure you are aware of the different ways to connect sensors to TW, there is an overview here:

https://developer.thingworx.com/en/resources/guides/choosing-connectivity-method

 

Here is a quick overview how the different pieces of code on an edge device using the EMS work together:

EMS

The EMS keeps an Always-On connection open between the Raspberry Pi and TW. The TW platform can use this open Always-On connection at any time to send commands to, or retrieve property values from Things that are maintained on the EMS.

 

LSR

The LSR connects with the EMS and registers the Things it knows about and then loops polling your custom code for property value changes based on the scanRate you set in config.lua.

 

Custom Code

Your custom code interacts with your hardware and responds to commands or returns values whenever it is called by the LSR.

 

If you are trying to catch a sensor reading that exists for less 500 milliseconds, It does not seem like a good idea for your application to rely on the LSR to call your code faster than 2 times per second. Your code should be running in a loop of its own and maintain the value until the next time it is called  by the LSR.

 

The big advantage of the EMS and LSR is that they allow users to make on demand push or pull of information to edge hardware whenever they desire. If your application is only pushing data to the platform, you could consider using the REST API to push data directly from your code to the TW platform.

 

As an aside, you mentioned a PIR sensor, which stands for passive infrared, but the code snippet you included seems to be trying to use an acoustic sensor to determine distance. I would be surprised if you would be able to get meaningful distance measurement using Python running on Linux due to changing system latency caused by other running processes.

got it. yes, the code is calculating some distance but i am not interested in having it. the reason i put it there was to send some random values when the sensor sees movement. i will try with the REST API thanks

Top Tags