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

Thingworx and EMS


Thingworx and EMS

     Hi,everyboby.I have completed the Raspberry Pi Quickstart documentation.

     I'm able to use EMS to send raspberry voltage and other data uploaded to the thingworx platform.   Now I want to read the data on the sensor that placed on the raspberry through use of EMS,and then  will be the sensor data uploaded to the Thingworx platform .

Can you recommend several learning documents or examples?



Re: Thingworx and EMS

Hi Wang, reading data from the sensor won't be that complicated it would require you to use the library for that sensor which if you don't have it already you will have to search/Google it and using that library you'll gather the data and push it to ThingWorx. For e.g. i used the REST API together with Python to deliver the payload but you can do the same with EMS, below code collected and pushed the data every 5 seconds from the sensor and then to the ThingWorx.


import sys

import time

import json

import logging

import requests

import Adafruit_DHT

# enabling the logging


    import http.client as http_client

except ImportError:


    import httplib as http_client


# defining the sensor type and the pin on which the data needs to be collected

sensor = 11

pin = 4

var = 1

#Webserver URL in TW

Webhost = "http://tw713:8080/Thingworx/Things/TempAndHumThing/Properties/*"

App_Key = "<appKey>"

while var == 1:

hum, temp = Adafruit_DHT.read_retry(sensor, pin)

print('Temp={0:0.1f} *C  Hum={1:0.1f}%'.format(temp, hum))

#ThingName = "TempAndHumThing"

headers = {'Content-Type': 'application/json', 'appKey': App_Key}

payload = {'temp': temp, 'hum': hum}

response = requests.put(Webhost, headers=headers,json=payload, verify=True)


time.sleep(5) # sleep for 5 seconds

There's another great reference/guide you could use from one of the community Blog post Using the C SDK to Deliver Data to ThingWorx from a Raspberry PI

Hope this would help.

Re: Thingworx and EMS

Hi Wang,

Using the Sense HAT is not limited or specific to Thingworx, basically once you have your Pi and Sense HAT hooked and configured you could use the Sense HAT from anywhere using the existing APIs to read/write to/from it. The Thingworx tutorial on getting the basic information from the Raspberry Pi (i.e. CPU freq&temp) which you've already mentioned as having done successfully can be expanded to read Sense HAT specific sensor information. The key idea is that you can call from your Pi LUA script, i.e. a PiTemplate.lua script to read basic Pi information, other Python scripts that read sensor data from the Sense HAT and then supply thier results back to set Thingworx thing properties values. You can also send from Thingworx strings to be displayed on the Sense HAT LED matrix by defining a remote service on the Pi thing and call it with the string to be displayed. The most common operation however would be to read the Sense HAT's sensor data such as  position, humidity and pressure. I have used these resources to learn about reading the Sense HAT sensor data:

there is also a specific Astro Pi related page you can refer to:

There is of course a fully fledged API documentation page available for the Sense HAT:

What I found most useful were the examples from the getting started with the sense hat. I started from the existing LUA example.lua template and built a Pi specific one to include services and properties to read Sense HAT sensor data.

Just as an example, my PiTemplate.lua script looks like this:

module ("templates.PiTemplate", thingworx.template.extend)

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 }

properties.temperature = {baseType="NUMBER", pushType="ALWAYS", value=0}
properties.pressure = {baseType="NUMBER", pushType="ALWAYS", value=0}
properties.humidity = {baseType="NUMBER", pushType="ALWAYS", value=0}

properties.yaw = {baseType="NUMBER", pushType="ALWAYS", value=0}
properties.roll = {baseType="NUMBER", pushType="ALWAYS", value=0}
properties.pitch = {baseType="NUMBER", pushType="ALWAYS", value=0}

output { baseType="BOOLEAN", description="" },
description { "updates properties" }

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

function queryOrientation()

local f = io.popen("python /home/pi/twx/microserver/etc/custom/templates/")
local s = f:read("*a")
properties.pitch.value = s
log.debug("[PiTemplate]", string.format("Pitch: %.2f", properties.pitch.value))

local f = io.popen("python etc/custom/templates/")
local s = f:read("*a")
properties.roll.value = s
log.debug("[PiTemplate]", string.format("Roll: %.2f", properties.roll.value))

local f = io.popen("python etc/custom/templates/")
local s = f:read("*a")
properties.yaw.value = s
log.debug("[PiTemplate]", string.format("Yaw: %.2f", properties.yaw.value))


function queryEnvirons()

local f = io.popen("python etc/custom/templates/")
local s = f:read("*a")
properties.humidity.value = s
log.debug("[PiTemplate]", string.format("Humidity: %5.2f", properties.humidity.value))

local f = io.popen("python etc/custom/templates/")
local s = f:read("*a")
properties.pressure.value = s
log.debug("[PiTemplate]", string.format("Pressure: %6.1f", properties.pressure.value))

local f = io.popen("python etc/custom/templates/")
local s = f:read("*a")
properties.temperature.value = s
log.debug("[PiTemplate]", string.format("Temperature: %4.1f", properties.temperature.value))


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

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

The python scripts for doing the actual reading look like this example:

def get_pitch():

    from sense_hat import SenseHat

    sense = SenseHat()

    o = sense.get_orientation()
    pitch = round(o["pitch"],1)
    return pitch
print("{0}". format(get_pitch()))

And they all follow a similar approach, defined in the Astro Pi and the Raspberry Pi Sense Hat web pages mentioned above.

One mention I'd like to make is on how to get location information while using the Pi board. The Sense HAT has no such feature but given that the Raspberry Pi has several USB ports, inserting a USB GPS dongle is pretty straight forward while connecting and reading location information from the dongle is not a trivial task but neither is it very difficult given the amount of resources already available about connecting and reading NMEA data from a USB GPS dongle.

Hope this info helps.



Re: Thingworx and EMS


     Thank you for your helpful assistance.This is a very useful example.

     If you want to read any of the sensors attached to the raspberry pi , and then send it to the Thingworx platform via EMS and LSR. First you need to read the data on the sensor with the python script, and then set the property/services in the Template.lua.  Is that right?

     Is it possible to read the data on the sensor directly with lua ,without a python script?

Yours sincerely! 

Re: Thingworx and EMS

Wang, what sort of sensor do you have? I've not done that myself so not sure if you can read it, probably it'll just be about finding the libraries within lua that can do that for you, so that you can directly read from the sensor . Though i feel (may be just my opinion here) python is better equipped with quite a view libraries with useful functionalities. 

Re: Thingworx and EMS


    Thanks for your help!

    I want to send the data of the read sensor to the thingworx platform via EMS this connection.Can I use lua to read sensor data? Now I have speed sensors, ultrasonic sensors and so on. I want to measure the displacement of the ultrasonic sensor through the EMS upload to the platform for observation.What language is used to read the data on the sensor is better? And then in the Template folder under the Template.lua set properties and services.

Re: Thingworx and EMS

Hi Wang,

As far as I could find, the Sense Hat can be queried using the Python scripts and libraries that come already compiled and packaged for it so if for example you'd attach to the GPIO port some other sensors then you won't necessarily need to use the Python libraries, they may not even exist for your particular sensor type and make, however given that Raspberry Pi does not explicitly prevent you from reading/writing to specific pins on the GPIO using any method, bash scripting or C compiled code for example, I don't see why you should be limited to calling Python from Lua. So far however the preferred approach with Raspberry Pi is to go for the Python scripting and existing libraries.

I used this approach of calling Python scripts from Lua since the Sense Hat already had came with the building blocks I needed to interact with the sensors so it didn't make much sense (no pun intended) to reinvent all the libraries that are already written and work quite well for the Sense HAT just so I don't make an outbound call to a Python script and have to read back its results. It actually helped to separate sensor specific code and Thingworx specific code.

I've also done some similar work with Arduino, if you're familiar with it, and at least in my opinion the Raspberry Pi and its approach to interact with add-ons and other boards is somehow at a higher level, not as low level as you have for Arduino, which mainly uses embedded C libraries for all of the sensors and other things you can connect to it. The Python scripting that is preferred in Raspberry Pi can be a bit of a put-off at first but once you go past Python's quirks it's easy to get along.

One other aspect I see is that  the EMS is biased in the sense that it has to use Lua scripting, the reasons are more or less valid depending on whose point of view you look from, but at least for me, having to call a third party script from Lua and pass parameters then process results is not a major setback. I also wanted to have GPS capabilities for the Pi so I attached a USB GPS dongle to one of my Raspberry Pi USB ports. Since this USB GPS dongle doesn't have any Python scripting already built into the Raspberry Pi libraries I had to devise a way to get the location information from the dongle in some other ways. I ended up using Python as well, not for some specific limitation but since all other scripts were in Python I didn't see a reason why change the approach, so after some digging, I had a script that can be called from Lua and it will return the location as recorded by the GPS dongle. It's not a perfect combination but it's consistent and works quite well.

Coming back to the Lua and Python requirement, this I think is of concern for the current setup, i.e. using Raspberry Pi with Sense HAT, however if you're planning to go onto a much broader scope, having Lua as the center point, "standard" language in which all Thingworx platform related code is done is a good approach -  you only need to worry about how your specific sensor/thing/device can be called from the OS and pass on the results or receive input parameters. Also considering that you may have to integrate devices from different vendors using different modes to interact with them makes a "standard" language for the sensors unfeasible.

Hope this helps and if you need some extra info about connecting the GPS dongle let me know, I can share the code I used.



Re: Thingworx and EMS


     Thank you for your help.I don't have sense HAT this module and I am using Arduino's development board . Now I want to read the data on the ultrasonic sensor through EMS to the thingworx platform . You also said that EMS can only use lua scripting , I shouldn't use lua scripting to read data?  Can I read it in other languages?

Yours sincerely!

Re: Thingworx and EMS


     About Edge MicroServer(EMS)&LuaScriptResource(LUA) have more information or helpful examples for?

With your best regards!

Re: Thingworx and EMS

Wang, reading through your responses i feel you are looking for more examples on how lua can read your sensor data directly, is that correct? BTW just to put it out here so you are aware that LSR is optional with EMS, you can use EMS standalone too. I'm not aware if we have any examples on how to read the data from your specific sensor using Lua. Have you already tried searching for the available libraries for your sensor that can be easily integrated with lua?

Could you share the exact details of your sensor like the model, type etc.. which you are hooking up to Arduino board?


Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum! ______________________________