PLC Client
This PLC sample shows the use of the TwinCAT Analytics Storage Provider library. The sample code shows reading and writing. For the sample to work coherently, both the use of the Analytics Logger for sending measured data to an MQTT Message Broker and the import of historical data via the Analytics Stream Helper are shown.
The basis is an appropriately set up native MQTT Message Broker and an Analytics Storage Provider service.
The PLC sample shows the following steps:
- Analytics Logger: stream of variables from a Global Variable List to a MQTT Message Broker.
- Analytics Storage Provider: starting and stopping stores and recordings, as well as reading recordings and historical data.
- Analytics Stream Helper: receiving the historical data from the Analytics Storage Provider and mapping the data into a Global Variable List for the historical data.
Analytics Storage Provider GUID Glossary
When using the Storage Provider, various GUIDs occur that are required to identify services and data. The following describes where the GUIDs come from, what purpose they serve and where they can be viewed if necessary.
The analytics data stream sent by the Analytics Logger is basically described by three parameters:
- Topic [STRING]
Where is the data sent to? - TwinCAT SystemID [GUID]
From which TwinCAT system is the data sent? - LayoutID / Symbol Info ID [GUID]
What does the data look like? The GUID is a hash of the symbol information.
The above parameters are required to identify recordings at the Storage Provider. |
Several Storage Providers can be connected to an MQTT Message Broker. Two parameters are required to identify a Storage Provider.
- MainTopic [STRING](Where are the data/services provided?)
- ProviderGuid [GUID](Unique identifier of the service)
RecorderGuid is used to recognize who has started a recording at the Storage Provider. This GUID is automatically generated at each Storage Provider Manager or client and attached to the StartRecord commands. In the PLC, this can be freely assigned at the StartRecord command.
From version 3.2.14, three additional GUIDs can be specified on the StartRecord command.
- Storage [GUID]
This GUID specifies the storage in which the data is to be saved. The GUID is generated automatically in the Analytics Storage Provider Configurator. It can be read there or in the Analytics Storage Provider Manager. If no GUID is specified, the Master Storage is used. - SubBroker/DataBroker [GUID]
This GUID specifies the data broker from which the Analytics Stream is to be received. The Analytics Storage Provider offers the option of recording from several message brokers. The GUID is generated automatically in the Analytics Storage Provider Configurator. It can be read there or in the Analytics Storage Provider Manager. If no GUID is specified, the Master Data Broker is used. - DataKey [GUID]
The DataKey can be used to find, read and monitor recordings in progress. This DataKey can be freely selected in the PLC. If no DataKey is specified, a DataKey is automatically generated by the Storage Provider.
The following screenshots contain the parameters and GUIDs described above.
Topic / TwinCAT system ID / Layout (System Info ID)
Storage Provider MainTopic / ProviderGuid / RecorderGuid
Storage GUID
Data Broker GUID
Sample code architecture
All relevant parts of the configuration and the program code are marked in the following picture:
Stream Helper
For receiving the historical data sent by the Analytics Storage Provider via MQTT.
Variable Live/Historical
The GVL is for the live data and the GVL_Hist is for the historical data.
Storage Provider Command Helper Functions
These Helper Functions generate the commands for communication with the Storage Provider Service in JSON format.
MAIN program
The Main program invokes communication to the Analytics Storage Provider. The Main Historical program implements the mapping of historical data from the Stream Helper into the GVL_Hist.
Analytics Logger
Sends the variables of the GVL to an MQTT Message Broker.
Sample Start
Notice | |
Too little router memory can lead to system crashes Increase the router memory in the real-time settings to 256 MB. It is also recommended to increase the maximum stack size of the global task configuration to 512 KB. |
Before the sample can be started, you must set the MQTT Message Broker you are using in three different places.
Analytics Stream Helper:
Analytics Logger:
MAIN program:
You must then change the sMainTopic and the sProviderGUID for the FB_ALY_StorageProvider. This can be found as described above in this document.
Now go to the MAIN program to control the sample. With the enum eCtrl you can set the action you would like to perform. The available options are:
- ReadASPDescription (also contains the storage description)
- ReadDataBroker
- StartStorage
- ShutdownStorage
- StartRecord
- StopRecord
- IsRecordingRunning
- ReadHistoricalStreams
- ReadRecords
- GetHistorical
With a rising edge at the variable bExecute the action selected in the enum is executed. If you have made more than one record, you can see this in the array aRecordInfo. With the index it is then possible to select the different records. The timespans are also displayed, you could theoretically still adjust these within the timespan. To do this, you would need to modify the logic of the sample in the helper function F_CreateAlySPGetHistCmd accordingly.
The Storage Provider Recorder GUID selected in the document above can optionally be set in the PLC in the F_CreateAlySPStartRecordCmd function. Theoretically, it can be any GUID, it is only used to identify the recorder.
Download: Sample