Skip to main content
1-Visitor
August 13, 2019
Solved

Pushing vibration sensor Data to Thingworx with EMS

  • August 13, 2019
  • 2 replies
  • 5036 views

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()

 

 

 

 

 

 

Best answer by jwyatt

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

2 replies

jwyatt5-Regular MemberAnswer
5-Regular Member
August 13, 2019

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

ggmurp011-VisitorAuthor
1-Visitor
August 13, 2019

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.

5-Regular Member
August 13, 2019

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

 

Community Manager
August 20, 2019

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