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

Pushing vibration sensor Data to Thingworx with EMS

ggmurp01
6-Contributor

Pushing vibration sensor Data to Thingworx with EMS

Hello everyone. I have completed the Raspberry Pi Quickstart.

 I want to read the data from a vibration sensor connected to the raspberry through using the EMS and display that data on Thingworx .  My Python file is running fine. When I run ./wsems and ./luaScriptResource everything connects fine. The problem is the value column doesn't update in Thingworx.

 

Here is the PiTemplate.lua file:

--PiTemplate
module ("templates.PiTemplate", thingworx.template.extend)
--hardware readings
properties.cpu_temperature = { baseType="NUMBER", pushType="ALWAYS", value=0 }
properties.cpu_freq = { baseType="NUMBER", pushType="ALWAYS", value=0 }
properties.cpu_volt = { baseType="NUMBER", pushType="ALWAYS", value=0 }
--sensor readings
properties.sensor = {baseType="STRING", pushType="ALWAYS", value="Stable"}

serviceDefinitions.GetSystemProperties(
output { baseType="BOOLEAN", description="" },
description { "updates properties" }
)

services.GetSystemProperties = function(me, headers, query, data)
log.trace("[PiTemplate]","########### in GetSystemProperties#############")

querySensors()
queryHardware()

end
function querySensors()

local f = io.popen("python /home/pi/Desktop/vibe2.py")
local s = f:read("*a")
properties.sensor.value = s
f:close()
log.debug("[PiTemplate]", string.format("sensor:", properties.sensor.value))

end


function queryHardware()
local tempCmd = io.popen("vcgencmd measure_temp")
local freqCmd = io.popen("vcgencmd measure_clock arm")
local voltCmd = io.popen("vcgencmd measure_volts core")
local s = tempCmd:read("*a")
s = string.match(s,"temp=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("temp %.1f",s))
properties.cpu_temperature.value = s
s = freqCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw freq %s",s))
s = string.match(s,"frequency%(45%)=(%d+)");
s = s/1000000
log.debug("[PiTemplate]",string.format("scaled freq %d",s))
properties.cpu_freq.value = s
s = voltCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw volts %s", s))
s = string.match(s,"volt=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("scaled volts %.1f", s))
properties.cpu_volt.value = s
end

tasks.refreshProperties = function(me)
log.trace("[PiTemplate]","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In tasks.refreshProperties~~~~~~~~~~~~~ ")
queryHardware()
querySensors()

end

 

-----------------------------------------------------------------------------------------------------------------------------------

Here is the python file :

 

import time
import RPi.GPIO as GPIO

vibe = 4

 

GPIO.setmode(GPIO.BCM)
GPIO.setup(vibe, GPIO.IN)


def callback(Vibe):
if GPIO.input(vibe):
sensor = "Vibration Detected"
print(sensor)

else:
sensor = "Vibration Detected"
print(sensor)



GPIO.add_event_detect(vibe, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(vibe, callback)

while True:
time.sleep(1)

GPIO.cleanup()

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jwyatt
10-Marble
(To:ggmurp01)

Greetings,

 

I in no way claim to be a Lua expert, but a few suggestions...

 

I put your PiTemplate.lua code into a Lua checker (never used it before, but a quick google gave me this site: https://rextester.com/l/lua_online_compiler ), and it gave the same error.

 

I then stripped out the string.format() function to a separate line instead of embedding it inside the log.debug() function, and the error moved back to the new string.format() line. That makes me think that there's just something wrong with your formatting of this string function.

 

I then did a google on string.format() and found this site: https://www.gammon.com.au/scripts/doc.php?lua=string.format 

 

It looks like the format should be more like the following: 

string.format ("To wield the %s you need to be level %i", "sword", 10)

 

In other words, it looks like your string.format("sensor:" sensor, properties.sensor.value) line may be in error. Specifically, it looks like you need "%s"-es or "%i"-es or whatever depending on your desired formatting to tell the string.format() function where to stick your "sensor" and "value" variables.

 

That's my best guess at the moment. Please let us know if this works out. 

 

Thank you,

Jason

View solution in original post

8 REPLIES 8
jwyatt
10-Marble
(To:ggmurp01)

Greetings,

 

I in no way claim to be a Lua expert, but a few suggestions...

 

I put your PiTemplate.lua code into a Lua checker (never used it before, but a quick google gave me this site: https://rextester.com/l/lua_online_compiler ), and it gave the same error.

 

I then stripped out the string.format() function to a separate line instead of embedding it inside the log.debug() function, and the error moved back to the new string.format() line. That makes me think that there's just something wrong with your formatting of this string function.

 

I then did a google on string.format() and found this site: https://www.gammon.com.au/scripts/doc.php?lua=string.format 

 

It looks like the format should be more like the following: 

string.format ("To wield the %s you need to be level %i", "sword", 10)

 

In other words, it looks like your string.format("sensor:" sensor, properties.sensor.value) line may be in error. Specifically, it looks like you need "%s"-es or "%i"-es or whatever depending on your desired formatting to tell the string.format() function where to stick your "sensor" and "value" variables.

 

That's my best guess at the moment. Please let us know if this works out. 

 

Thank you,

Jason

ggmurp01
6-Contributor
(To:jwyatt)

I added %s to the string.format part and I'm still not seeing data push through to Thingworx:

 

 

local f = io.popen("python /home/pi/Desktop/vibe2.py")
local s = f:read("*a")
properties.sensor.value = s
f:close()
log.debug("[PiTemplate]", string.format("sensor %s", s))

end

 

--------

My syntax seems to be correct but my data just doesn't seem to be where it needs to be.

jwyatt
10-Marble
(To:ggmurp01)

Although the end of the Raspberry Pi guide does include an optional step to connect the Pi to an external temperature sensor, the vibration sensor you're attempting to use now appears to be a personal extension of the project.

 

For this reason (and because we don't have the specific sensor you're trying to use), I'm afraid that we can only offer limited suggestions on resolving your issue.

 

My first suggestion would be to take the sensor out of the Lua template code. Could the sensor itself could be malfunctioning?

 

Please try hard-coding "s" to some obviously-recognizable value, and see if you get an output. If so, then we know we've resolved the issue with string.format()... and log.debug(), for that matter.

 

Assuming that works, then try working backwards. 

 

When you originally said, "My python file is running fine," what exactly does that mean? Are you seeing the print(sensor) statements in some python output? Does it respond correctly to "shaking" or whatnot from just the python script itself? 

 

Also (and you may have already corrected this since your original post), but it looks like your python script has two different conditions that are trying to print out "Vibration Detected". Shouldn't one of those branches be "Vibration *NOT* Detected"? 

 

Can we comment out most of your python code and just hardcode a value to be delivered from it? If the python is constantly providing a recognizable test-output, then we can test just the interface from the Lua Template code connecting to the Python output.

 

Etc.

 

Thank you,

Jason

 

ggmurp01
6-Contributor
(To:jwyatt)

The variable in the python code returns my printed out value when my vibration sensor is shaken. The vibration sensor I'm using is https://www.adafruit.com/product/1767.  So the problem is with probably with the Lua Template code connecting to the Python output.

 

I updated my PiTemplate code:

 

--PiTemplate
module ("templates.PiTemplate", thingworx.template.extend)
--hardware readings
properties.cpu_temperature = { baseType="NUMBER", pushType="ALWAYS", value=0 }
properties.cpu_freq = { baseType="NUMBER", pushType="ALWAYS", value=0 }
properties.cpu_volt = { baseType="NUMBER", pushType="ALWAYS", value=0 }
--sensor readings
properties.sensor = {baseType="STRING", pushType="ALWAYS", value=0}


serviceDefinitions.GetSystemProperties(
output { baseType="BOOLEAN", description="" },
description { "updates properties" }
)

services.GetSystemProperties = function(me, headers, query, data)
log.trace("[PiTemplate]","########### in GetSystemProperties#############")

querySensors()
queryHardware()

end

function querySensors()

local f = io.popen("python ./home/pi/Desktop/vibe2.py")
local s = f:read("*a")
properties.sensor.value = s
f:close()
log.debug("[PiTemplate]",string.format("sensor %s", properties.sensor.value ))

end


function queryHardware()
local tempCmd = io.popen("vcgencmd measure_temp")
local freqCmd = io.popen("vcgencmd measure_clock arm")
local voltCmd = io.popen("vcgencmd measure_volts core")
local s = tempCmd:read("*a")
s = string.match(s,"temp=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("temp %.1f",s))
properties.cpu_temperature.value = s
s = freqCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw freq %s",s))
s = string.match(s,"frequency%(45%)=(%d+)");
s = s/1000000
log.debug("[PiTemplate]",string.format("scaled freq %d",s))
properties.cpu_freq.value = s
s = voltCmd:read("*a")
log.debug("[PiTemplate]",string.format("raw volts %s", s))
s = string.match(s,"volt=(%d+\.%d+)");
log.debug("[PiTemplate]",string.format("scaled volts %.1f", s))
properties.cpu_volt.value = s
end

tasks.refreshProperties = function(me)
log.trace("[PiTemplate]","~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In tasks.refreshProperties~~~~~~~~~~~~~ ")
queryHardware()
querySensors()

end

--------------------------------------------------------------------

 

 

 

ggmurp01
6-Contributor
(To:ggmurp01)

When the  ./luaScriptReasource runs it is connecting to thingworx. I have pushed the other values through. It's just that for some reason the string variable from the python file is not being read in.

ggmurp01
6-Contributor
(To:ggmurp01)

Here is a picture of what the output looks like on thing works:

 

When you call your Python script from your Lua template, the Python script runs in a new process and the new process returns a success or failure code - it does not return what your Python script prints to the console. Here are a few different strategies for returning information from a Python script:

https://stackoverflow.com/questions/18231415/best-way-to-return-a-value-from-a-python-script

 

When a device is determining when to send data to the platform it may be easier to use to the REST API:

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

 

slangley
23-Emerald II
(To:ggmurp01)

Hi @ggmurp01.

 

If one of the previous responses have allowed you to resolve your issue, please mark the appropriate one as the Accepted Solution for the benefit of others with same issue.

 

Regards.

 

--Sharon

Top Tags