Ewon TechForum
Providing technical solutions to technical requests


eWON and ThingWorx
simon Offline
eWON Support
#1
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.

[Image: attachment.php?aid=6]

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


Attached Files
.png   2016-05-23_11-12-50.png (Size: 90,05 KB / Downloads: 603)
Reply

rmcrabb Offline
Junior Member
#2
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.
Reply

simon Offline
eWON Support
#3
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:
//################ CONFIGURATION #################
appKey$="0e13ccdc-f6a9-4678-925e-83f060d5872e"
ThingworxIP_URL$="57.33.6.55"
ThinkName$ = "MyThing"
Changepushtime% = 2 //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:
Counter=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 i% = 0 THEN
           IF GroupA$ = "1" And GROUPA%= 1 THEN json$ = json$ + '"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
           IF GroupB$ = "1" And GROUPB%= 1 THEN json$ = json$ + '"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
           IF GroupC$ = "1" And GROUPC%= 1 THEN json$ = json$ + '"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
           IF GroupD$ = "1" And GROUPD%= 1 THEN json$ = json$ + '"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
       ELSE
           IF GroupA$ = "1" And GROUPA%= 1 THEN json$ = json$ + ',"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
           IF GroupB$ = "1" And GROUPB%= 1 THEN json$ = json$ + ',"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
           IF GroupC$ = "1" And GROUPC%= 1 THEN json$ = json$ + ',"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
           IF GroupD$ = "1" And GROUPD%= 1 THEN json$ = json$ + ',"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"': counter% = counter% +1
       ENDIF
  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$ + '"' + tagName$+ '":"'+STR$ GETIO tagName$ + '"'
  ELSE
          json$ = json$ + ',"' + tagName$+ '":"'+STR$ GETIO 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$
ENDFN


Simon
Reply

rmcrabb Offline
Junior Member
#4
Simon,
THANK YOU!!! Works perfectly! The only request would be to add the {Thing Name} to the customization.
Reply

simon Offline
eWON Support
#5
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
Reply

rmcrabb Offline
Junior Member
#6
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.
Reply

simon Offline
eWON Support
#7
Very interesting!!

Thanks for this feedback!

Simon
Reply

rmcrabb Offline
Junior Member
#8
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?
Reply

simon Offline
eWON Support
#9
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/defaul...riptor.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
Reply

rmcrabb Offline
Junior Member
#10
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" }
Reply

simon Offline
eWON Support
#11
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
Reply

TomHu Offline
Ewon Distributor
#12
Hi Simon,

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

Cheers,
Tom
Reply

simon Offline
eWON Support
#13
Hi Tom,

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

Simon
Reply

ltitel Offline
Junior Member
#14
(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
Reply



Possibly Related Threads…
8 hours ago
Last Post: simon
  Creating Totaliser Within eWON Started by Josh
3 Replies - 1.076 Views
14-02-2020, 06:06 PM
Last Post: simon
24-01-2020, 09:45 PM
Last Post: tedsch
07-01-2020, 03:34 PM
Last Post: simon
22-10-2019, 10:42 AM
Last Post: simon
03-09-2019, 02:08 PM
Last Post: simon
  MQTT eWon Flexy 205 Started by RFreist
3 Replies - 1.484 Views
18-07-2019, 12:30 PM
Last Post: simon
20-05-2019, 03:36 PM
Last Post: simon
15-05-2019, 09:49 PM
Last Post: Bernd
26-04-2019, 10:32 AM
Last Post: simon



Users browsing this thread:
1 Guest(s)



Theme © Ewon 2019 - Forum software by © MyBB - Cookie policy