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

