This function allows you to publish topics on a cloud service Beebotte a cloud platform for connected objects in real time (article), the function uses the FB MQTTClient to send data with the MQTT protocol.
Function
CODESYS: Not available
LogicLab: eLLabMQTTLib
Description
MQTT (@MQTTClient_v3) The allocation address of the FB must be indicated MQTTClient which will be used by the function to manage the protocol MQTT.
Topic (@STRING) Topic name definition string pointer.
DType (VR_TYPE) Variable type to publish.
DBuffer (PVOID) Indicate address of the variable to be published.
QoS (USINT) Quality of service publication topic. Accepted values 0 and 1.
The function returns a (BOOL) FALSE if execution error, TRUE if correct execution.
Examples
How to use the examples.
This example handles subscription and publication to the service Beebotte to the canal UserTest of two variables REALValue e StringValue. By publishing a value it will be possible to display it in the dashboards, and at the same time the published value will enhance the subscribed variable.
LogicLab (Ptp208, ST_TPBeebotte)
PROGRAM ST_TPBeebotte
VAR
i : UDINT; (* Auxiliary variable *)
Publish : BOOL; (* Publish command *)
TCPClient : SysTCPClient; (* TCP client management *)
FIFO : FIFOFile_v1; (* FIFO on file *)
MQTT : MQTTClient_v3; (* MQTT client FB *)
RxSData : STRING[ 128 ]; (* Rx subscription data *)
SPData : ARRAY[0..1] OF MQTT_TS_DATA; (* MQTT topic subscribe data *)
REALValue : ARRAY[0..1] OF REAL; (* REAL value [Tx][Rx] *)
STRINGValue : ARRAY[0..1] OF STRING[ 32 ]; (* STRING value [Tx][Rx] *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_TPBeebotte"
// *****************************************************************************
// This program subscribes and publish dato to Beebotte service.
// To see the sent data please visit
// https://beebotte.com/dash/74b90ea0-47d2-11e9-8573-fb68fdbc6d81#.Yzw7UkxByCp
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// INITIALIZATION
// -------------------------------------------------------------------------
// Initialize the FBs parameters.
IF (SysFirstLoop) THEN
// Set FIFO parameters.
FIFO.FIFOFilename:=eNULL; //Path and name of FIFO file
FIFO.FIFOSize:=2048; //FIFO file size
FIFO.FIFOIDx:=eNULL; //FIFO indexes
// Set TCPClient parameters.
TCPClient.PeerAdd:=ADR('mqtt.beebotte.com'); //Peer address
TCPClient.PeerPort:=1883; //Peer port
TCPClient.LocalAdd:=ADR('0.0.0.0'); //Local address
TCPClient.LocalPort:=0; //Local port
TCPClient.FlushTm:=0; //Flush time (mS)
TCPClient.LifeTm:=90; //Life time (S)
TCPClient.RxSize:=512; //Rx buffer size
TCPClient.TxSize:=512; //Tx buffer size
// Set MQTTClient parameters.
MQTT.SpyOn:=TRUE; //Spy active
MQTT.FIFOFile:=ADR(FIFO); //FIFO on file
MQTT.Username:=ADR('token_O6vzjmPn1A3lU38w'); //Username
MQTT.Password:=eNULL; //Broker password
MQTT.ClientID:=eNULL; //Client identifier
MQTT.KeepAlive:=T#90s; //Keep alive time
MQTT.Delay:=T#2s; //Send delay time
MQTT.Timeout:=T#5s; //Execution timeout
// Topic subscribe definitions.
MQTT.TSData:=ADR(SPData); //Topic subscribe data
MQTT.TSNumber:=SIZEOF(SPData)/SIZEOF(SPData[0]); //Topic subscribe number
MQTT.TSNumber:=2; //Topic subscribe number
i:=MQTT.Subscribe(0, ADR('UsersTest/REALValue'), ADR(RxSData), SIZEOF(RxSData), 0);
i:=MQTT.Subscribe(1, ADR('UsersTest/STRINGValue'), ADR(RxSData), SIZEOF(RxSData), 0);
END_IF;
// -------------------------------------------------------------------------
// BEEBOTTE SUBSSCRIBE AND PUBLISH
// -------------------------------------------------------------------------
// FBs management.
TCPClient(Connect:=MQTT.Connect); //TCPClient management
MQTT(Enable:=TRUE, File:=TCPClient.File); //MQTTClient management
// Manage received subscriptions, is received a JSON string as below:
// {"data":123456,"ispublic":true,"ts":1560781762147}
IF (SPData[0].Status.0) THEN
i:=JSONDecoder(ADR(RxSData), ADR('data'), REAL_TYPE, ADR(REALValue[1]), 1, SIZEOF(REALValue[1]));
END_IF;
IF (SPData[1].Status.0) THEN
i:=JSONDecoder(ADR(RxSData), ADR('data'), STRING_TYPE, ADR(STRINGValue[1]), 1, 32);
END_IF;
// To publish topics set by debug the "Publish" variable.
IF NOT(Publish) THEN RETURN; END_IF;
Publish:=FALSE; //Publish command
i:=MQTTCBeebotte(ADR(MQTT), ADR('UsersTest/REALValue'), REAL_TYPE, ADR(REALValue[0]), 0);
i:=MQTTCBeebotte(ADR(MQTT), ADR('UsersTest/STRINGValue'), STRING_TYPE, ADR(STRINGValue[0]), 0);
// [End of file]