Application sample

Creation of the PLC program

Defining a structure

Create a structure within which you define the process data to be sent. Assign attributes for the declared variables to define their representation in the app (see Attributes).

TYPE ST_ProcessData :
STRUCT
    {attribute 'iot.DisplayName' := 'Kitchen Lights'}
    bLamp1 : BOOL;

    {attribute 'iot.DisplayName' := 'Living Room Lights'}
    bLamp2 : BOOL;

    {attribute 'iot.DisplayName' := 'Outside Temperature'}
    {attribute 'iot.ReadOnly' := 'true'}
    {attribute 'iot.Unit' := 'Celsius'}
    {attribute 'iot.MinValue' := '5'}
    {attribute 'iot.MaxValue' := '30'}
    nTemp  : REAL;
    stSecondLevel : ST_Test;
END_STRUCT
END_TYPE

Configuration

In the main program, declare an instance of the function block FB_IotCommunicator. Define the outputs according to your connection data (see FB_IotCommunicator). In addition, declare the structure with the process data to be sent and an instance of the TON timer function block.

fbIoT : FB_IotCommunicator := (
    sHostName := 'YOUR_MQTT_BROKER', // MQTT Broker Adress
    nPort := 1883,                   // MQTT Port
    sMainTopic := 'plants',          // Main Topic
    sDeviceName := 'Building 12.3',  // Device Name
    sUser := 'engineer1',            // MQTT Username
    sPassword := 'abcdefg');         // MQTT Password

stData: ST_ProcessData;              // Values to send
timer : TON;                         // Timer to send data

Establishing a connection

Cyclically call the Execute method in the implementation part of the main program via the instance of the function block FB_IotCommunicator to maintain the connection to the broker and thus enable sending and receiving of data and messages (see Execute).

fbIoT.Execute(TRUE);

Sending data

Send the process data to the broker with a sample rate of 500 ms. To this end, call the instance of the timer function block with the corresponding input variables and the SendData method of the function block FB_IotCommunicator (see SendData).

timer(IN := NOT timer.Q, PT := T#500MS);

IF fbIoT.bConnected AND timer.Q THEN
    fbIoT.SendData(ADR(stData), SIZEOF(stData));
END_IF

A nested structure must be transferred here for structuring over several levels. The following structure shows a simple example of how nesting can be continued indefinitely.

TYPE ST_Test :
STRUCT
    stLevel1 : ST_Level_1;
    nCounter : INT;
END_STRUCT
END_TYPE
TYPE ST_Level_1 :
STRUCT
    nDoubleCounter: INT;
    stLevel2 : ST_Level_2;
END_STRUCT
END_TYPE

Receiving and evaluating commands

Call the function block FB_IotCommand and its methods to receive and evaluate commands (see FB_IotCommand).

IF fbIoT.fbCommand.bAvailable THEN
    IF fbIoT.fbCommand.sVarName = 'bLamp1' THEN
        fbIoT.fbCommand.GetValue(ADR(stData.bLamp1), SIZEOF(stData.bLamp1), E_IotCommunicatorDatatype.type_BOOL);
    ELSIF fbIoT.fbCommand.sVarName = 'stSecondLevel.nDoubleCounter' THEN
        fbIoT.fbCommand.GetValue(ADR(stData.stSecondLevel.stLevel1.nDoubleCounter), SIZEOF(stData.stSecondLevel.stLevel1.nDoubleCounter), E_IotCommunicatorDatatype.type_BOOL);
    END_IF
    fbIoT.fbCommand.Remove();
END_IF

Sending (push) messages

Call the SendMessage method of the function block FB_IotCommunicator to send a (push) message to the broker (see SendMessage).

fbIoT.SendMessage('This is a test alarm message!');