Versions Compared

Key

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

To be able to publish using MQTT in Codesys some prerequisite changes and settings needs to be done, this is a step by step guide on how to setup mosquitto to publish with over MQTT in Codesys.

First step is to change IPv4 settings on the Ethernet port. This is done by going into (Settings → Network & internet → Ethernet) and under the tab IP assigment press edit then change from automatic to manual and turn on IPv4, and change to the settings show in this image:

...

The PLC which is currently in use has the IP: 158.38.140.53 and the IP for the Ethernet port is set to the same but +/- 1 to the last digit. And change the Subnet mask to 255.255.255.0.

Next step is to change the cloud connectivity settings for the PLC, this is done by writing the PLC's IP into the search bar of a web browser. The settings to change are found under:

(Configuration → Cloud Conectivity → Connection 1 or 2) here we change the settings to match our IPv4. We start by first changing the "Cloud platform" to MQTT AnyCloud, the "Hostname" will be the same as the IPv4 and thus for this example 158.38.140.54, the port will be 1883 which is for unencrypted MQTT connections, and lastly "Data Protocol" will be changed to Native MQTT. The settings should look like this:
Image Removed

Assuming everything is running localy, the last change is to add a rule to the PC's firewall, this is done by entering (Control panel → System and Security → Windows Defender Firewall → Advanced settings → Innbound Rules → New Rule...)

The new rule should be of type Port and TCP 1883.

Publishing from Codesys

To publish from Codesys the libraries wagoAppCloud and wagoSysPlainMem are used, these are added by using the "Libary Manager" in Codesys.

FUNCTION_BLOCK MQTTPublisher
VAR_INPUT
    topic : STRING(255);
    xtrigger : BOOL;
    message : STRING := 'Hello, World';
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;

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

 


To setup mosquitto, this has to be downloaded and installed first, after the installation a few lines of code needs to be added to the configuration file mosqitto.conf:

  • listener 1883
  • allow_anonymous true

To start the broker, run the commands:
 

  • cd "C:/Program files/mosquitto"
  • mosquitto -v -c mosquitto.conf

The broker is now running and, the publisher and subscriber can be found at 24_7 MQTT Publisher and 24_7 CDP-Studio Dashboard/24_7 MQTT Subscriber.This is a simple code to publish "Hello, World!"