Skip to main content
14-Alexandrite
July 15, 2019
Solved

WEB API CALL On EMS (Get Data From Sensors)

  • July 15, 2019
  • 6 replies
  • 2900 views

Hello,

I'm newbie on Thingworx technilogie and after doing the guides am trying now to develop my first application.

The idea is simple:

1- Use Raspberry pi as Gateway (Done)

2-Connect EMS to IOT Thinworx (Done)

3- Configure my first Thing : Pi_Thing with auto_bind and connect it to the remote thing on the platform (Done)

4- On Mu PiTemplate, i want to develop a lua script to get json result , parse it and sent the resut to a remote property  on thingworx platform. 

Reading the LUA Doc i think i have to user tw_http request:

Some thing like: tw_http.get(host, port, path, headers, query)

For test reason im using a public api just to test if it works:

https://pokeapi.co/api/v2/pokemon/ditto/

 

Can you help with the lua script  to get the JSON result from the API on the file Pi_Template

 

I created to property:

properties.JsonTest={baseType="Json", pushType="ALWAYS", value="{}"}

when i call the api:

properties.JsonTest = tw_http.get("https://pokeapi.co", "", "api/v2/pokemon/ditto/", "", "")

 an error occur !

ERROR] 2019-06-21 08:47:03,171 luaScriptProxy::execute: [1979708496] Error executing script PiThing, Error = Error: .../EMS/microserver/etc/custom/templates/PiTemplate.lua:49: Error allocating HTTP Client

 

 

Can you help me.

 

Thank you 

 

 

 

Best answer by Constantine

Hello,

 

Just my two cents.

 

The approach with querying local sensors from a Raspberry Pi is very common, so you're on the right way. Whether you should use EMS or something else depends on many different factors, e.g. your Pi setup, your programming experience, etc.

 

EMS is a good starting point, and it is designed with exactly that kind of architecture in mind. You should be able to use 3rd-party HTTP client libraries to reach your sensors, but I'm not an expert in Lua, so cannot tell you anything specific. I encourage you to google for it and try to poll your sensors in pure Lua without using EMS first, just to make sure you get this part correctly. Then put it into EMS to connect it to ThingWorx.

 

Alternatively, if you feel more comfortable with Java or C, you can use a corresponding EDGE SDK and write all sensor polling logic in a language of your choice. That will probably take longer, because you'll need to figure out how to use EDGE SDK and handle connection to your ThingWorx instance. But at the same time it might save you time in the long-run, when you familiarize yourself with the SDK. For instance, that's the route I take when writing gateways, because I'm already quite familiar with EDGE SDK for Java. But it wasn't all that trivial to learn it in the first place, so if you need to make something work right now, then you should probably stick to EMS.

 

Finally, if you are really desperate, you can use pretty much any language you know (shell scripting, Python, etc.), ditch EDGE SDK and push your sensor data into ThingWorx via REST API. Surely this will not be as efficient as using EDGE SDK, and you will lose some benefits of AlwaysOn protocol. This approach allows hacking together quick throwaway demos, which may be useful sometimes. Probably not the right way to go, given how far you've already advanced with EMS configuration.

 

Regards,
Constantine

6 replies

14-Alexandrite
July 17, 2019

I changed my method, i try to use http handler to fetch data  from sensor

Something like :

 

properties.JsonTest = {baseType="Json", pushType="NEVER", handler="http", key="https://pokeapi.co/api/v2/pokemon/ditto/" }

Error:

[ERROR] 2019-06-22 00:59:59,16 UnencryptedClientStream::doopen: Error opening socket. Error: 111

 

is this the best approch to fetch data from sensors (Executing EMS on raspberry pi) ?

can i use i third party libraray  to make http request (API REST) ?

 

thank you for your help 

16-Pearl
July 18, 2019

Hi @AliBenBelgacem 

 

the EMS is one method of many.

 

More complex methods would involve programming your own client-application e.g. with the ThingWorx Java Edge SDK, or using Python scripts to read the data and send it via REST API.

If the JSON you want to pull / push is located on a different server you can also use the ContentLoaderFunctions Resource to invoke GetJson, PostJson or PutJson services.

 

Cheers,

Michael

14-Alexandrite
July 18, 2019

The Idea , is to fetch sensors data via REST API CALL. all sensors are connecte to Local area  network  ans are not exposed to internet.

The Thingworx platform is connected to one EMS and the EMS is responsible to fetch data from sensors and send them periodicaly to the cloud.

 

the Resources["ContentLoaderFunctions"]  can be called from luascript to hit local ip address  ?

 

Thank you