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:

  • 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 apiKey As String
 Dim FSO As FileSystemObject
 Dim TS As TextStream
 Set FSO = New FileSystemObject
 Dim logs<SIGNAL 1>, logs<SIGNAL 2> As String
 logs<SIGNAL 1> = "LOGS\<SIGNAL 1>.txt"
 logs<SIGNAL 2> = "LOGS\<SIGNAL 2>.txt"

 apiKey = "<API KEY DATABOOM>"
 UrlClockGet = "https://api.databoom.com/v1/aut/gmtclock/plain"
 UrlToPostTo = "https://api.databoom.com/v1/signals/push"

 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 "POST", 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 "apikey", apiKey

 Dim <SIGNAL 1>, <SIGNAL 2>
 <SIGNAL 1> = GetVariableValue("<SIGNAL 1>")
 <SIGNAL 2> = GetVariableValue("<SIGNAL 2>")

 JSONString = "{""device"":""<DEVICE TOKEN>"",""date"":"" + sDate + "", ""signals"":["
 JSONString = JSONString + "{""name"":""<SIGNAL TOKEN 1>"",""value"":"+ <SIGNAL 1> +"},"
 JSONString = JSONString + "{""name"":""<SIGNAL TOKEN 2>"",""value"":"+ <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"":""<DEVICE TOKEN>"",""date"":"" + sDate + "", ""signals"":{"

  bulk = bulk + " ""<SIGNAL TOKEN 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 + " ""<SIGNAL TOKEN 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 "apikey", apiKey
   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"":"+ <SIGNAL 1> +"}" + vbCrLf
  TS.Close
  Set TS = FSO.OpenTextFile(logs<SIGNAL 2>, ForAppending, True)
  TS.Write "{""date"":""+ sDate +"",""value"":"+ <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

You need to set the following parameters in the script to make it work correctly:

  • <API KEY DATABOOM> your own api key, visible in your profile.
  • <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}.

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.