Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Input:

topic : STRING(255);

xTrigger : BOOL;

message : STRING;

Output:




Info

Type: FunctionBlock

Responsible: AndreAndré


To set up a MQTT publisher in codesys the following libraries are necessary to use:

  • WagoSysPlainMem
  • WagoAppCloud

Before the PLC can publish to over MQTT, the WagoAppCloud settings needs to be changed to fit the system this is done by writing the PLC's IP into the searchbar of a web browser, then cloud connectivity → connection 1. The settings are set to:

To set up a MQTT publisher in codesys the following libraries are necessary to use:

  • WagoSysPlainMem
  • WagoAppCloud

The functionblock MQTT_Publisher was then created , with the following code:

 

Code Block
languageactionscript3
titleCode Overview
linenumberstrue
FUNCTION_BLOCK MQTTPublisher
VAR_INPUT
    topic : STRING(255);
    xtrigger : BOOL;
    message : STRING;
END_VAR
VAR
    mqttPub : wagoappcloud.FbPublishMQTT_2(econnection := econnectionid.Connection1);
    dwSize : DWORD;
    aData : ARRAY[0..100] OF BYTE;
    xRetain : BOOL := FALSE;
    xError : BOOL := FALSE;
    xBusy : BOOL := FALSE;
    eQualityOfService : eQualityOfService := 1;

END_VAR

wagosysplainmem.MemCopy(pDest:=ADR(aData),pSource:=ADR(message),udiSize:=TO_WORD(len(message))); 
mqttPub(sTopic:=topic,eQualityOfService:=eQualityOfService,xRetain:=xRetain,dwSize:=TO_WORD(len(message)),aData:=aData,xTrigger:=xtrigger,xBusy=>xBusy,xError=>xError);