Versions Compared

Key

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

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:

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);

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. The settings are set to:

Image Added