Movicon

 

It is possible to transmit signals to Databoom using platforms to supervise and industrial control making use of HMI/Scada software technology, like Movicon.

Data forwarding is implemented through the writing of a Visual Basic script.

References

Before proceeding with writing code, you need to add some references to your script (Edit menu -> References...):

  • Microsoft WinHTTP Services (5.1)
  • Microsoft Scripting Runtime (1.0)

Script VB

The following script shows a possible approach to the forwarding of Movicon variables to Databoom.

Sub Main()
 Dim http As WinHttp.WinHttpRequest
 Dim UrlToPostTo As String
 Dim UrlClockGet As String
 Dim sDate As String
 Dim lngTimeout As Long
 Dim JSONString As String
 Dim canConn As Boolean
 Dim oAuthToken As String
 Dim FSO As FileSystemObject
 Dim TS As TextStream 
 Set FSO = New FileSystemObject 
 Dim logsDirectory As String
 logsDirectory = "LOGS"
 Dim logs<SIGNAL 1>, logs<SIGNAL 2> As String
 logs<SIGNAL 1> = "LOGS\<SIGNAL 1>.txt"
 logs<SIGNAL 2> = "LOGS\<SIGNAL 2>.txt"

 oAuthToken = "Bearer <OAUTH TOKEN DATABOOM>"
 UrlClockGet = "https://api.databoom.com/v1/auth/gmtclock/plain"
 UrlToPostTo = "https://api.databoom.com/v1/signals/push"

 If Not FSO.FolderExists(logsDirectory) Then
  FSO.CreateFolder(logsDirectory)
 End If
 Set TS = FSO.OpenTextFile(logs<SIGNAL 1>, ForAppending, True)
 TS.Close
 Set TS = FSO.OpenTextFile(logs<SIGNAL 2>, ForAppending, True)
 TS.Close

 lngTimeout = 30000
 canConn = True

 Set http = New WinHttp.WinHttpRequest
 http.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout

 http.Open "GET", UrlClockGet, False
 http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
 On Error GoTo HandleDateRequest
  http.Send
 sDate = http.ResponseText

 http.Open "POST", UrlToPostTo, False
 http.SetRequestHeader "Content-Type", "application/json"
 http.SetRequestHeader "Authorization", oAuthToken

 Dim <SIGNAL 1>, <SIGNAL 2>
 If GetVariableValue("<SIGNAL 1>") Then
  <SIGNAL 1> = GetVariableValue("<SIGNAL 1>")
 Else
  <SIGNAL 1> = -999999
 End If
 
If GetVariableValue("<SIGNAL 2>") Then
  <SIGNAL 1> = GetVariableValue("<SIGNAL 2>")
 Else
  <SIGNAL 2> = -999999
 End If

 JSONString = "{""device"":""<TOKEN DEVICE>"",""date"":" + sDate + ", ""signals"":["
 JSONString = JSONString + "{""name"":""<TOKEN SIGNAL 1>"",""value"":"+ Replace(<SIGNAL 1>,",",".") +"},"
 JSONString = JSONString + "{""name"":""<TOKEN SIGNAL 2>"",""value"":"+ Replace(<SIGNAL 2>,",",".") +"}"
 JSONString = JSONString + "]}"
 On Error GoTo HandleFailedPost
  http.Send "" + JSONString

 If canConn = True Then
  Dim bulk As String
  Dim emptyBulk As Boolean
  emptyBulk = True
  bulk = "{""device"":""<TOKEN DEVICE>"",""date"":" + sDate + ", ""signals"":{"

  bulk = bulk + " ""<TOKEN SIGNAL 1>"" : {""description"":""<SIGNAL 1>"", ""history"":["
  addHistory logs<SIGNAL 1>, bulk
  If StrComp(Right(bulk, 1),",") = 0 Then

   bulk = Left(bulk, Len(bulk) - 1) + "]},"
   emptyBulk = False
  Else
   bulk = bulk + "]},"
  End If

  bulk = bulk + " ""<TOKEN SIGNAL 2>"" : {""description"":""<SIGNAL 2>"", ""history"":["
  addHistory logs<SIGNAL 2>, bulk
  If StrComp(Right(bulk, 1),",") = 0 Then
   bulk = Left(bulk, Len(bulk) - 1) + "]}}}"
   emptyBulk = False
  Else
   bulk = bulk + "]}}}"
  End If

  If emptyBulk = False Then
   http.Open "POST", UrlToPostTo, False
   http.SetRequestHeader "Content-Type", "application/json"
   http.SetRequestHeader "Authorization", oAuthToken
   On Error GoTo HandleFailedConn
    http.Send "" + bulk

   If canConn = True Then
    cleanHistory logs<SIGNAL 1>
    cleanHistory logs<SIGNAL 2>
   End If
  End If
 End If

 Set http = Nothing
 Set TS = Nothing
 Set FSO = Nothing

 Exit Sub

 HandleFailedConn:
  canConn = False
  Resume Next

 HandleDateRequest:
  canConn = False
  sDate = Format(Now, "YYYY-MM-DDTHH:NN:SS")
  Resume Next

 HandleFailedPost:
  canConn = False
  Set TS = FSO.OpenTextFile(logs<SIGNAL 1>, ForAppending, True)
  TS.Write "{""date"":"""+ sDate +""",""value"":"+ Replace(<SIGNAL 1>,",",".") +"}" + vbCrLf
  TS.Close
  Set TS = FSO.OpenTextFile(logs<SIGNAL 2>, ForAppending, True)
  TS.Write "{""date"":"""+ sDate +""",""value"":"+ Replace(<SIGNAL 2>,",",".") +"}" + vbCrLf
  TS.Close
  JSONString = ""
  Resume Next
End Sub

Sub addHistory(ByVal logFile, ByRef bulk)
 Dim FSO As FileSystemObject
 Dim TS As TextStream
 Set FSO = New FileSystemObject

 Set TS = FSO.OpenTextFile(logFile, ForReading)
 Do Until TS.AtEndOfStream
  bulk = bulk + TS.ReadLine + ","
 Loop
 TS.Close
End Sub

Sub cleanHistory(ByVal logFile)
 Dim FSO As FileSystemObject
 Dim TS As TextStream
 Set FSO = New FileSystemObject
 Set TS = FSO.OpenTextFile(logFile, ForWriting, True)
 TS.Write ""
 TS.Close
End Sub

Pay particular attention to the following points:

  • logsDirectory is the path of the directory where log files are stored, if it doesn't exist, it is created, be sure the path is available.
  • If variables can't be read, a value is assigned to distinguish the error -999999 (can be any other value, according to user's needs).
  • Replace(<SIGNAL 1>,",",".") allows to forward values with decimals where comma separator is used.
  • <OAUTH TOKEN DATABOOM> is a token generated in Databoom,  in Settings -> Credentials.
  • <SIGNAL 1> and <SIGNAL 2> are the Movicon variables you want to forward to Databoom. The same values are used to name the logs file too. The example has two variables, you can add more by following the same procedure.
  • <DEVICE TOKEN> is the device token in Databoom.
  • <SIGNAL TOKEN 1> and <SIGNAL TOKEN 2> are the signals token in Databoom. If the signals have already been created on Databoom, <SIGNAL TOKEN #> should be replaced with the corresponding token. On the contrary the token inserted in the script will be used for the creation of the signal on the platform.

The script forwards the data at regular intervals, every time it is called by the scheduler. In case the communication is not possible, the recorded data is stored locally in log files, and will be forwarded as soon as the connection is restored in a single request (bulk).

N.B. sDate is returned as a string from API, already quoted. Assure the quotation marks in JSON string being used correctly. Format to forward should be {"date":"2018-03-26T09:35:46","value":4}, not {"date":""2018-03-26T09:35:46"","value":8}. In particular we distinguish two cases, variables assignation and file writing:

  • JSONString = "{""device"":"""",""date"":" + sDate + ", ""signals"":["
  • bulk = "{""device"":""<TOKEN DEVICE>"",""date"":" + sDate + ", ""signals"":{"
  • TS.Write "{""date"":"""+ sDate +""",""value"":"+ <SIGNAL 1> +"}" + vbCrLf
  • TS.Write "{""date"":"""+ sDate +""",""value"":"+ <SIGNAL 2> +"}" + vbCrLf

Scheduler

To allow the permanent execution of the script, it is required to create a new scheduler. From the "Project Explorer" window, go in the "List Schedulers Objects" of your project and select the "New Schedule" on the right click of mouse.
In the properties window of the scheduler finally select "every minute" in type and the script in commands.

Movicon New Scheduler Movicon Scheduler Properties

Configuration completed!

Once the procedure has been completed, Databoom starts to record data sent from Movicon. To examine your data and have a correct representation, follow the instructions in Edit/validation of a signal.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.