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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

WEB API CALL On EMS (Get Data From Sensors)

AliBenBelgacem
14-Alexandrite

WEB API CALL On EMS (Get Data From Sensors)

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 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

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

View solution in original post

6 REPLIES 6

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 

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

AliBenBelgacem
14-Alexandrite
(To:mneumann)

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 

Yeah, that scenario looks like you will need a Gateway.

I'd recommend to just test it out if the ContentLoaderFunctions are passed on via the EMS. Otherwise you'll probably need some additional logic / Thing on the EMS that passes the request forward to the LSR entities and collects the data to build a response.

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

@AliBenBelgacem In the docs it says that the headers parameter should be a Lua table. What if you try something like {} instead of "" there?

 

--------------------------------------------------------------------------------
-- Execute a HTTP GET on the given host, port, and path.
-- 
-- @param host The host to send the request to.
-- @param port The port to send the request to.
-- @param path The URL path of the request.
-- @param headers A Lua table of HTTP headers to include in the request.
-- @param query A Lua table containing the URL query parameters for the request.
-- 
-- @return The following values:
--         <ul>
--          <li>The response data</li>
--          <li>HTTP response code</li>
--          <li>Lua table of HTTP response headers</li>
--         </ul>
--
function get(host, port, path, headers, query)

 

Update: I tried specifying correct hostname and port, and it seems to work better. However, it looks like tw_http doesn't support https, or at least I couldn't figure out how to make it work. This is my  best appempt so far:

tw_http.get("pokeapi.co", 443, "api/v2/pokemon/ditto/", {}, "")

...but it tries to connect to port 443 without SSL, i.e. as if it was plain HTTP, and the server returns an HTTP 400 Bad Request. If your sensors listen on HTTP rather than HTTPS, then this approach should work. PTC Tech Support will probably provide more help on that.

 

/ Constantine

Top Tags