Using the M2Web API to sync data between remote Flexys

M2Web API & REQUESTHTTPX

Using the REQUESTHTTPX BASIC command and the M2Web API, it’s possible to exchange tag values between two devices. This option is especially flexible as it does not require a public IP or really any IP addressing and uses the already established Talk2M connections. In this guide, we’ll share two BASIC scripts to accomplish this. The first sends the value of a single tag to the remote Flexy, while the second scans through the tag list to send the values of any Flexys with the description send. Both of these scripts are available to be used as is or to modify to suit your needs.

Prerequisites

  • You must have a Talk2M Developer ID
  • Your devices must be registered and online in a Talk2M account
  • Each tag you want to sync must have a tag with the same name on the other device
  • For the advanced script, each tag you want to sync must have the tag description set to send
  • You must be using an Ewon Flexy on a firmware revision >= 11

Use Cases

  • Useful when you do not have public IP addressing or a direct connection
  • Useful for creating a more versatile application

API Limitations

With the M2Web API, users are limited to:

  • 10,000 calls per day on Free+ accounts
  • 30,000 calls per day on Pro accounts

Each REQUESTHTTPX command to m2web.talk2m.com constitutes a call. The code in this tutorial sends tag data every 10 seconds, which (assuming the code is only running on one device) will be slightly less than the free daily API limit at 8640 calls per day. Because a call can update the value for many tags in a single HTTP request, it doesn’t matter how many tags are updated with each call.

Step 0: Obtaining Developer ID

Talk2M developer IDs are free. You can apply for one here: Registration | Ewon Developers

Step 1: Creating Shareable Tags

For this example, we’re going to create a tag called Potentiometer. If you’re using the simple code, you can omit the description, but for the advanced code we’ll add the description send. With that code, if a tag has the description send then it will share data to the other Flexy that has a tag matching the same name.

Step 2: BASIC IDE Code

This section of code a timer go off every 10 seconds and then push the values to the other flexy in the update remote section. It also sets the developer ID and API token needed for authentication when using the API. The “devicename” is what you called the device that you’re writing to in eCatcher. Note that if it has any special characters in the name, such as a space, you must represent this character using percent encoding. For example, if your device is named Flexy 205, set the variable devicename to “Flexy%20205”.

The two screenshots in this section show the configuration of variables for the simple and advanced scripts respectively. The only real difference between the two is that the simple script requires you to specify the tag being synced in the variable tagName.

If you have concerns about storing your Talk2M credentials here, you can create a new user solely for making these M2Web API calls. This user does not need admin rights.

Simple
img2 - Account Variables Simple

Advanced
img3 - Account Variables Advanced

Step 3: Begin Syncing Data

If you’ve configured your tags on both devices, entered your account and device details correctly, and finally saved and run the code, the device running the code will begin sending the values for tags with the description send every 10 seconds.

Appendix

Script - Simple

TSET 1, 10
ONTIMER 1, "GOTO UpdateRemote"

//Account info
account$ = "AcntName"
username$ = "UserName"
password$ = "Password"
developerid$ = "xxxxx-xxxxx-xxxxx"

//ewon specific info
devicename$ = "DeviceName"
deviceusername$ = "adm"
devicepassword$ = "adm"
method$ = "GET"

tagName$ = "YourTag"

UpdateRemote:
  tagValue$ = STR$ GETIO tagName$
  
  url$ = "https://m2web.talk2m.com/t2mapi/get/"+devicename$+"/rcgi.bin/UpdateTagForm?TagName1=" + tagName$ + "&TagValue1=" + tagValue$

  url$ = url$ + "&t2maccount="+account$+"&t2musername="+username$+"&t2mpassword="+password$
  url$ = url$ +"&t2mdeveloperid="+developerid$+"&t2mdeviceusername="+deviceusername$+"&t2mdevicepassword=" + devicepassword$
  REQUESTHTTPX url$, method$, "", ""
  END

Script - Advanced

TSET 1, 10
ONTIMER 1, "GOTO UpdateRemote"
ONSTATUS "@response()"

//Account info
account$ = "AcntName"
username$ = "UserName"
password$ = "Password"
developerid$ = "xxxxx-xxxxx-xxxxx"

//info for remote flexy
devicename$ = "DeviceName"
deviceusername$ = "adm"
devicepassword$ = "adm"

url$ = "https://m2web.talk2m.com/t2mapi/get/"+devicename$+"/rcgi.bin/UpdateTagForm?"
accountinfo$ = "&t2maccount="+account$+"&t2musername="+username$+"&t2mpassword="+password$
accountinfo$ = accountinfo$+"&t2mdeveloperid="+developerid$+"&t2mdeviceusername="+deviceusername$+"&t2mdevicepassword="+devicepassword$

UpdateRemote:
TSET 1, 0
method$ = "POST"
//get number of tags and loop through each one
SETSYS SYS, "LOAD"
C% = GETSYS PRG,"NBTAGS"
x%=1
n%=0
FOR i% = 0 TO (C% - 1)
  SETSYS TAG,"LOAD", -i%
  tagname$ = GETSYS TAG,"Name"
  desc$ = GETSYS TAG, "Description"
  If (desc$ = "send") THEN
    a$ = GETSYS TAG,"TagValue"
    IF (x% = 1) THEN
      payload$ = "&TagName" + STR$(x%) + "=" +tagname$ + "&TagValue" + STR$(x%) + "=" + a$
      n%=1
    ELSE
      //for each loop grab the tag at with that ID and send to remote site
      payload$ = payload$ + "&TagName" + STR$(x%) + "=" + tagname$ + "&TagValue" + STR$(x%) + "=" + a$
    ENDIF
    x%=x%+1
  ENDIF
NEXT i%

IF (n% = 1) THEN
  payload$ = payload$ + accountinfo$
  REQUESTHTTPX url$, method$, "", payload$
  actionID% = GETSYS PRG, "ACTIONID"
  //ONSTATUS "@response()"
  PRINT "Data sent"
ENDIF
TSET 1, 10
END

//Un-comment ONSTATUS above to trigger response function
FUNCTION response()
  eventId% = GETSYS PRG, "EVTINFO"
  IF (eventId% = actionID%) THEN
    SETSYS PRG, "ACTIONID", eventId%
    stat% = GETSYS PRG, "ACTIONSTAT"
    IF (stat% = 0) THEN
       b$ = RESPONSEHTTPX "STATUSCODE"
        IF (b$ <> "200")THEN
          LOGEVENT  "HTTP RESPONSE: " + b$ ,77
          //can add an event here on error
        ENDIF
    ELSE
      LOGEVENT "Error (ERROR = "+Str$(stat%) + ")" , 78
      //LOGEVENT url$,79 
      //LOGEVENT payload$,80
    ENDIF
  ENDIF
ENDFN