Switch to desktop version  
eWON and ThingWorx - Printable Version

+- Ewon Technical Forum (https://techforum.ewon.biz)
+-- Forum: Development (https://techforum.ewon.biz/forum-50.html)
+--- Forum: BASIC Script (https://techforum.ewon.biz/forum-52.html)
+--- Thread: eWON and ThingWorx (/thread-78.html)



eWON and ThingWorx - simon - 23-05-2016

Hi Guys,

I just managed to get my eWON push its live data (Tag values) to a ThingWorx server. (http://www.thingworx.com)

Actually it is very easy to do with the new REQUESTHTTPX function (Implemented as of the firmware 11.1)

Find here below my example.
I have created a function that pushes a single Tag value to ThingWorx.  Data must be pushed through  simple JSON files.
Every time a Tag changes, the eWON then pushes the value to the Thingworx server.

Code:
Rem --- eWON start section: Init Section
ewon_init_section:
Rem --- eWON user (start)

ONCHANGE "Qty_Color_B", "@UpdateThingWorxProperty('0e13ccdc-f6a9-4678-925e-83f060d5872e','Qty_Color_B')"
ONCHANGE "Qty_Color_C", "@UpdateThingWorxProperty('0e13ccdc-f6a9-4678-925e-83f060d5872e','Qty_Color_C')"
ONCHANGE "Qty_Color_D", "@UpdateThingWorxProperty('0e13ccdc-f6a9-4678-925e-83f060d5872e','Qty_Color_D')"
ONCHANGE "Temperature", "@UpdateThingWorxProperty('0e13ccdc-f6a9-4678-925e-83f060d5872e','Temperature')"

Rem --- eWON user (end)
End
Rem --- eWON end section: Init Section
Rem --- eWON start section: ThingWorx
Rem --- eWON user (start)

FUNCTION UpdateThingWorxProperty($appKey$,$Tagname$)
$url$ = "http://{ThingWorxServerIPorURL}/Thingworx/Things/{MyThing}/Properties/*"
$method$ = "PUT"
$Tagvalue = GETIO $Tagname$
$Tagvalue_String$ = STR$ $Tagvalue
$DataToSend$ = '{"' + $Tagname$ + '":' + $Tagvalue_String$ + '}'
$header$ = "Content-Type=application/json&Accept=application/json&appKey=" + $appKey$
REQUESTHTTPX $url$, $method$, $header$, $DataToSend$
PRINT "Value of " + $Tagname$ + " is sent to ThingWorx"
ENDFN
Rem --- eWON user (end)
End
Rem --- eWON end section: ThingWorx

  • {ThingWorxServerIPorURL} must be replaced by the IP or URL of the ThingWorx server
  • {MyThing} is the name if the "Thing" you create for the eWON in ThingWorx.
In ThingWorx, a Thing is a "device" (here the eWON) and a Thing Property is a Tag.



By the way, multiple Tags can be pushed using such a JSON : {"ThingProperty1":TagValue1, "ThingProperty2":TagValue2,...}


RE: eWON and ThingWorx - rmcrabb - 11-01-2019

This is a great example and its working for me. But I do have a couple questions. Is there an example to show how to dynanically build the json to send only the tag values that changed in one instance. 

My application has multiple tags and I only want to send the changed values to thingworx in one json.


RE: eWON and ThingWorx - simon - 14-01-2019

Hi,



Good to see it is helpful!



To respond to your request, I have updated the script by adding some codes to push the Tags only when they change.

Actually, it uses two timers. One to push the values that have changed and one to push all values.

In the script header, you can customize
  • The Thingworx key
  • The Thingworx IP or Domain Name
  • The two timer intervals
  • The Tag groups to push

(Copy the script in the init section)

Code:
Rem --- eWON start section: Cyclic Section
eWON_cyclic_section:
Rem --- eWON user (start)
Rem --- eWON user (end)
End
Rem --- eWON end section: Cyclic Section
Rem --- eWON start section: Init Section
eWON_init_section:
Rem --- eWON user (start)
//################ CONFIGURATION #################
appKey$="6a2c195f-d0ca-4b43-97be-c2df3520dc21"
ThingworxIP_URL$="52.2.36.2:8090"
ThinkName$ = "Type TehThingName"
Changepushtime% = 1 //Timer to push only Tags that has changed
Fullpushtime% = 20// Timer to push all values
//Select the Tag Group to publish -> 0 or 1
//Tag must be created and at least set in one of the groups.
GROUPA% = 1
GROUPB% = 1
GROUPC% = 1
GROUPD% = 1
//################ ENDCONFIGURATION #################
//Record the Tag ONCHANGE events into an array.
//Allows to post only values that have changed
NB%= GETSYS PRG,"NBTAGS"
DIM a(NB%,2)
FOR i% = 0 TO NB%-1
k%=i%+1
SETSYS Tag, "load",-i%
a(k%,1)=-i%
a(k%,2) = 0
GroupA$= GETSYS TAG,"IVGROUPA"
GroupB$= GETSYS TAG,"IVGROUPB"
GroupC$= GETSYS TAG,"IVGROUPC"
GroupD$= GETSYS TAG,"IVGROUPD"

IF GroupA$ = "1" And GROUPA%= 1 THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
IF GroupB$ = "1" And GROUPB%= 1 THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
IF GroupC$ = "1" And GROUPC%= 1 THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
IF GroupD$ = "1" And GROUPD%= 1 THEN Onchange -i%, "a("+ STR$ k%+",2)= 1"
NEXT i%

ONTIMER 1,"goto PublishAllValues"
ONTIMER 2, "goto PublishChangedValues"
TSET 1,Fullpushtime%
TSET 2,Changepushtime%
End

PublishAllValues:
counterSelectedTag% = 0
TagIsSelected% = 0
json$ =         '{'
  FOR i% = 0 TO NB% -1
      SETSYS Tag, "load",-i%
      tagName$= GETSYS TAG,"Name"
     
      GroupA$= GETSYS TAG,"IVGROUPA"
      GroupB$= GETSYS TAG,"IVGROUPB"
      GroupC$= GETSYS TAG,"IVGROUPC"
      GroupD$= GETSYS TAG,"IVGROUPD"
     
      IF GroupA$ = "1" And GROUPA%= 1 THEN TagIsSelected% = 1
      IF GroupB$ = "1" And GROUPB%= 1 THEN TagIsSelected% = 1
      IF GroupC$ = "1" And GROUPC%= 1 THEN TagIsSelected% = 1
      IF GroupD$ = "1" And GROUPD%= 1 THEN TagIsSelected% = 1
     
       IF TagIsSelected% = 1 THEN
         IF counterSelectedTag% = 0 THEN
           json$ = json$ + @ComputeJson$(tagName$)
         ELSE
           json$ = json$ + ',' + @ComputeJson$(tagName$)
         ENDIF
         counterSelectedTag% = counterSelectedTag% + 1
       ENDIF
       TagIsSelected% = 0
       
  NEXT i%   
json$ = json$ +   '}'
@UpdateThingWorxData(json$)
End
//Publish just the changed tags
PublishChangedValues:
counter% = 0
//Compute JSON
json$ = '{'
FOR r% = 1 TO NB%
IF a( r%,2) = 1 THEN
  a(r%,2) = 0
  negIndex% = a(r%,1)
  SETSYS Tag, "LOAD", negIndex%
  tagName$= GETSYS Tag, "name"
     IF counter% = 0 THEN
        json$ = json$ + @ComputeJson$(tagName$)
     ELSE
        json$ = json$ + ',' + @ComputeJson$(tagName$)
     ENDIF
  counter% = counter% +1
ENDIF
NEXT r%
json$ = json$ +    '}'
IF counter% > 0 THEN
  @UpdateThingWorxData(json$)
ELSE
  PRINT "[PUBLISH ONCHANGE TIMER] No Tag changes detected! -> Don't publish"
ENDIF
END
Function UpdateThingWorxData($json$)
$url$ = "http://"+ThingworxIP_URL$+"/Thingworx/Things/"+ThinkName$+"/Properties/*"
$method$ = "PUT"
$header$ = "Content-Type=application/json&Accept=application/json&appKey=" + appKey$
REQUESTHTTPX $url$, $method$, $header$, $json$
PRINT "[PUBLISH] " + $json$
ENDFN

FUNCTION ComputeJson$($tagname$)
    SETSYS TAG, "LOAD", $tagname$
    $TagFormat$ = GETSYS TAG, "Type"
    IF $TagFormat$ = "6" THEN //String
        $ComputeJson$ = '"' + $tagname$ + '":"' + GETIO $tagname$ + '"'
    ELSE //ANALOG
        $ComputeJson$ = '"' + $tagname$ + '":"' + STR$ GETIO $tagname$ + '"'
    ENDIF
ENDFN


Rem --- eWON user (end)
End
Rem --- eWON end section: Init Section


Simon


RE: eWON and ThingWorx - rmcrabb - 15-01-2019

Simon,
THANK YOU!!! Works perfectly! The only request would be to add the {Thing Name} to the customization.


RE: eWON and ThingWorx - simon - 16-01-2019

Good point !!
I have modified the script above.
Thanks for your feedback!

By the way, could you explain what is your application ? How you use Flexy in combination to PTC Thingworx ?

Simon


RE: eWON and ThingWorx - rmcrabb - 21-01-2019

My application is a piece of equipment the will be displayed at a trade show. I'm utilizing Thingworx to display live data via mashups. I also have a Vuforia augmented reality experience that will be displaying live data.


RE: eWON and ThingWorx - simon - 22-01-2019

Very interesting!!

Thanks for this feedback!

Simon


RE: eWON and ThingWorx - rmcrabb - 28-07-2019

Now I'm looking for a way to push the tag history to Thingworx. Anyway to compile all the tag history to a single .csv and push to a Thingworx repository?


RE: eWON and ThingWorx - simon - 29-07-2019

Hi,

Yes, it should be possible (even if I don't know how to do that in Thingworx).
To get the Ewon history in a csv file, you should use the Export Block Descriptor. It is a syntax that allows you to extract any kind of data from the Ewon.
See https://websupport.ewon.biz/sites/default/files/rg-0009-00-export_block_descriptor.pdf
Here an helper on https://ewonsupport.biz/ebd/ and https://ewonsupport.biz/post/index.php a test website to try a post data from a Flexy to a webserver (just need to copy the script to try)


Simon


RE: eWON and ThingWorx - rmcrabb - 29-07-2019

I could use some help with a script to get the contents of the .CSV file into the body content. Below is the documentation Thingworx

The following bullets outline requirements when using [b]SaveText[/b] to upload a text file (the content below is required for the request in some format, whether software such as Postman is being used or a custom Java application) :
  • [b]URL[/b]: localhost/Thingworx/Things/name_of_thing/Services/SaveText

  • Header, Content-Type: [b]Content-Type: Application-json[/b]

  • Make sure the [b]AppKey [/b]or[b] Basic Authentication[/b] is being used

  • Use a [b]POST[/b] request as the [b]method[/b]

  • The raw body content: { "path" : "name_of_file.txt", "content" : "I am the content of the file" }



RE: eWON and ThingWorx - simon - 05-08-2019

Hi,

Then it should be possible to push a data file using the function REQUESTHTTPX.
Here is how you have to use it :
REQUESTHTTPX "username:password@http://1.2.3.4/Thingworx/Things/name_of_thing/Services/SaveText","POST","Content-Type=Application-json",'{ "path" : "name_of_file.txt", "content" : "I am the content of the file" }'

Simon


RE: eWON and ThingWorx - TomHu - 20-11-2019

Hi Simon,

Is there a way to send data from Thingworx to the Flexy? Or just use M2Web API?

Cheers,
Tom


RE: eWON and ThingWorx - simon - 20-11-2019

Hi Tom,

I did not investigate that but the M2Web API could be a good solution.

Simon


RE: eWON and ThingWorx - ltitel - 17-01-2020

(16-01-2019, 11:55 AM)simon Wrote: Good point !!
I have modified the script above.
Thanks for your feedback!

By the way, could you explain what is your application ? How you use Flexy in combination to PTC Thingworx ?

Simon

This code worked for me as well.  Now that you can read string tags, the string tag seems to create an error.  Is there a basic function to check if a tag is a string?  Then I believe I know how to modify your logic to work with string tags. 


Thanks

Luke