Omron#Let’s use NX1 MQTT Communications Library

This article I will use Omron’s Sysmac Library for MQTT communication, a combination of PLCNEXT and Podman’s Container technology on the Broker side, but a regular Raspberry Pi will also work.

Let’s start!

Reference Link

Build a Broker

PLCNEXT#Auto start the podman MQTT Broker Container
MQTT#Broker Setup
Wago#Let’s use the pfc-kbus-api

MQTT Program With Other CPU

Project#RoboDK x TwinCAT x MQTT x Opencv Color Dectector
M5Stack#LAN W5500 with MQTT
Beckhoff#Using TwinCAT TF6701 to Subscribe Topics
Beckhoff#Play with EK9160 Iot Coupler
Beckhoff#Using TwinCAT TF6701 to Publish message inAWS IOT Core
Siemens#Using CC712 to Connect your S7-1500 to AWS IOT Core

MQTT Library?

The MQTT Communications Library is a library that allows OMRON NX CPU to use the software features for Pub/Sub type message exchange with MQTT Broker.

This library supports version 3.1.1 of the MQTT protocol.

Using the MQTT Communications Library, such functionality can be implemented:

  • Connect with MQTT Broker
  • As a Subscriber, receive information from Publishers in the network.
  • As a Publisher, sends information to a Subscriber in the network.
  • Communicate with MQTT Broker and check response time 

Install

Download the MQTT Communication library from Omron HP and start the EXE.

https://www.fa.omron.co.jp/product/tool/sysmac-library/

Choose language >OK to continue.

Proceed with Next>.

Agree to the license and proceed with Next>.

Proceed with Next>.

Install!

Just a second..

Done!

Method

Next, we will introduce the Function Block and DUT used in this article.

DUT

sConnectionSettings

NameData TypeDescription
IpAdrSTRING[201]Broker’s IP Address
PortNoUINTBroker’s Port Number
TLSUseBOOL1=TLS is used
TLSessionNameSTRING[17]TLS session name
UserNameSTRING[256]Login’s Username
PasswordSTRING[256]Login’s Password
WillCfgsWillCfgWill parameters
CleanSessionBOOL1=Session is cleared.

sWillCfg

NameData TypeDescription
WillTopicSTRING[512]Will Topic’s name
WillMsgSTRING[256] Will Message
WillRetainBOOL1=Retain the Message
WillQoSBYTEQoS Leve(0,1,2)
WillFlagBOOL1=Will function is enabled

MQTTClient

This Function Block creates a connection between the NX CPU and the MQTT Broker, and after the MQTTClient is successfully executed, the INOUT parameter, ClientReference, is set to MQTTPubAryByte, MQTTSubAryByte, MQTTSubString, MQTTPing. MQTTSubString, and MQTTPing Function Block.

Input Variables

Variable Data typeDescription
Enable BOOL True=start to connect
ClientID STRING[256]Identifier for the MQTT Broker to identify
ConnectionSettings OmronLib\MQTT_Comm\sConnectionSettingsParameters to connect to the MQTT Broker
KeepAlive UINT KeepAlive time setting 0-65535
Timeout UINT Timeout setting(0-20s)
DiscardMsgTime UINTSpecifies the maximum time to wait for a received message to be discarded.

Output Variables

Variable Data typeDescription
Connected BOOLTrue=Connecting with Broker
Busy BOOLTrue=Function Block is running
ErrorBOOLTrue=Function Block error occurred
ErrorID WORD0=Function Block terminated normally, others have Function Block errors.
ErrorIDEx DWORD Error Code Extensions
DiscardMsgNum UDINT This is the number of discarded message receptions
SessionPresentBOOLTrue=Session is present.

VAR_IN_OUT

ClientReferencesClientReferenceData to be shared among MQTT FBs.

MQTTPubAryByte

This Function Block sends a PUBLISH message of the message specified in the BYTE array to the MQTT broker.

VAR_IN

NameTypeDescription
Execute BOOL Function Block will execute at startup
MsgSize UINT Size of Publish message in parameter PubMsg (Byte)
PubSettings sPubFlagsRetain and QoS settings when publishing
Topic STRING[512]Topic name to publish
TimeOutUINT Timeout Settings

VAR_OUT

NameTypeDescription
Done BOOL True=Connecting with Broker
Busy BOOLTrue=Function Block is running
ErrorBOOLTrue=Function Block error occurred
ErrorID WORD0=Function Block terminated normally, others have Function Block errors.
ErrorIDEx DWORD Error Code Extensions

VAR_IN_OUT

NameTypeDescription
ClientReferencesClientReferenceData to be shared among MQTT FBs.
PubMsgARRAY[*] OF BYTEMessage to be sent to the Broker
MsgSize UINT Size of Publish message in parameter PubMsg (Byte)
PacketIDUINTIf a number other than 0 is specified at the start of execution, the message is sent as a retransmission message for the specified packet ID.
MsgTypeUSINTThe parameter is accessed if the QoS level is 1 or 2 and the PacketID is not 0. 0=PUBLISH, 1=PUBREL

MQTTPubString 

This Function Block sends a PUBLISH message of the message specified by the string to the MQTT broker.

VAR_IN

NameTypeDescription
Execute BOOL Function Block will execute at startup
PubSettings sPubFlagsRetain and QoS settings when publishing
Topic STRING[512]Topic name to publish
TimeOutUINT Timeout Settings

VAR_OUT

NameTypeDescription
Done BOOL True=Connecting with Broker
Busy BOOLTrue=Function Block is running
ErrorBOOLTrue=Function Block error occurred
ErrorID WORD0=Function Block terminated normally, others have Function Block errors.
ErrorIDEx DWORD Error Code Extensions

VAR_IN_OUT

NameTypeDescription
ClientReferencesClientReferenceData to be shared among MQTT FBs.
PubMsgARRAY[*] OF BYTEMessage to be sent to the Broker
MsgSize UINT Size of Publish message in parameter PubMsg (Byte)
PacketIDUINTIf a number other than 0 is specified at the start of execution, the message is sent as a retransmission message for the specified packet ID.
MsgTypeUSINTThe parameter is accessed if the QoS level is 1 or 2 and the PacketID is not 0. 0=PUBLISH, 1=PUBREL

MQTTSubAryByte

This Function Block starts a subscription to the Broker with the input parameter Enable=True and the receiving the message as a byte array.

VAR_IN

NameTypeDescription
Enable BOOL 1=Star to Subscribe
Topic STRING[512]The Subscribe’s topic
SubQoSBYTE Qos Setting
TimeOutUINT Timeout Settings

VAR_OUT

NameTypeDescription
Subscribed BOOL 1=Topic is Subscribed
StatusSINTScbscribe status.0:Execution is stopped1:Requesting Subscribe2:Subscribe is in progress3:Stopping Subscribe4:Waiting for Broker to reconnect
ReceivedBOOL1=Message received
MsgSizeUINTData size received (Byte)
RcvTopicSTRING[512]received Topic
ErrorBOOLTrue=Function Block error occurred
ErrorID WORD0=Function Block terminated normally, others have Function Block errors.
ErrorIDEx DWORD Error Code Extensions

VAR_IN_OUT

NameTypeDescription
ClientReferencesClientReferenceData to be shared among MQTT FBs.
RcvMsgARRAY[*] OF BYTEReceived data is stored in

MQTTSubString 

The Function Block here initiates a Subscribe to the Broker with the input parameter Enable=True and receiving the message as a string.

VAR_IN

NameTypeDescription
Enable BOOL 1=Star to Subscribe
Topic STRING[512]The Subscribe’s topic
SubQoSBYTE Qos Setting
TimeOutUINT Timeout Settings

VAR_OUT

NameTypeDescription
Subscribed BOOL 1=Topic is Subscribed
StatusSINTScbscribe status.0:Execution is stopped1:Requesting Subscribe2:Subscribe is in progress3:Stopping Subscribe4:Waiting for Broker to reconnect
ReceivedBOOL1=Message received
RcvTopicSTRING[512]Data size received (Byte)
ErrorBOOLreceived Topic
ErrorID WORDTrue=Function Block error occurred
ErrorIDEx DWORD 0=Function Block terminated normally, others have Function Block errors.

VAR_IN_OUT

NameTypeDescription
ClientReferencesClientReferenceData to be shared among MQTT FBs.
RcvMsgARRAY[*] OF BYTEReceived data is stored in

MQTTPing

This Function Block can send ping messages to the MQTT Broker and measure the response time.

VAR_IN

NameTypeDescription
Enable BOOL 1=Start to ping
TimeOutUINT Timeout setting

VAR_OUT

NameTypeDescription
Done BOOL 1=FB execution success
BusyBOOL1=FB is busy
ElapseTimeUINTTime between sending a PINGREQ packet and receiving a PINGRESP packet
ErrorBOOLTrue=Function Block error occurred
ErrorID WORD0=Function Block terminated normally, others have Function Block errors.
ErrorIDEx DWORD Error Code Extensions

VAR_IN_OUT

NameTypeDescription
ClientReferencesClientReferenceData to be shared among MQTT FBs.

Implementation

Import the library

Start Sysmac Studio and import the library into your project.

Go to Project>Library>Show References.

Click the + button.

Select the library you have just downloaded and open it.

It is OK to ignore.

MQTT Library has been imported.

Configuration

Open Controller Setup>Built-in EtherNet/IP Settings and set the IP of the port connected to the MQTT Broker from the TCP/IP Setting tab.

Program

DUT

DUT_MQTT_Basic

DUT_MQTT_Client

DUT_MQTT_Sub

DUT_MQTT_Ping

DUT_MQTT_Pub

VAR

Variables NameTypeDescritpion
bDummyBOOL
bAlwaysONBOOL
pubFlagsOmronLib\MQTT_Comm\sPubFlagsConfiguration to Publish Message to Broker
rcvMsgARRAY [0..999] OF BYTEDestination of the Byte array received in the Broker
RcvTopicSTRING[512]Topic name received in the Broker
SubMessageSTRING[1986]String data received in the Broker is stored at
MyStringSTRING[255]
pubMsgARRAY[0..999] OF BYTEByte array data to be published to the Broker
StringToAry_DataConvertedUINTをData Conversion Buffer
MQTTPubStringOmronLib\MQTT_Comm\MQTTPubStringFB Instance of Publish string data to Broker
PubMessageSTRING[1986]Destination of string data to be published to the Broker
MQTTClientOmronLib\MQTT_Comm\MQTTClientFB Instance to connect with Broker
bMQTTConnectBOOL1=Connect with MQTT Broker
ClientDUT_MQTT_ClientFB Parameter Variables
MQTTSubAryByteOmronLib\MQTT_Comm\MQTTSubAryByteBrokerにMessageをSubscribe FB Instance
SubAryByteDUT_MQTT_SubFB Parameter Variables
MQTTPingOmronLib\MQTT_Comm\MQTTPingFB Instance to ping the Broker
MQTTSubStringOmronLib\MQTT_Comm\MQTTSubStringBrokerにMessageをSubscribe FB Instance
PingDUT_MQTT_PingFB Parameter Variables
SubStringDUT_MQTT_SubFB Parameter Variables
PubAryByteDUT_MQTT_PubFB Parameter Variables
MQTTPubAryByteOmronLib\MQTT_Comm\MQTTPubAryByteDestination of Byte array data to be published to the Broker
pubStringDUT_MQTT_PubFB Parameter Variables
Result_SubAryByteDINT
Result_SubStringDINT

Rung0

Summarize Rung Comment.

Rung1

Set parameters to connect to the MQTT Broker.

Rung2

Set TLS parameters to connect to the MQTT Broker.

Rung3

Configure the WillCfg settings.

Rung4

Enable the CleanSession option.

Rung5

Configure the Qos and Retain settings when the Omron NX CPU publishes a message.

Rung6

bMQTTConnect is set to True when the Ethernet Port of the OMRON NX CPU is normal and the MQTT Function is not running or has not generated any errors. The bMQTTConnect is a parameter of the MQTTClient Function.

Rung7

Summarize the Rung Comment with a Dummy circuit.

Rung8

Call the MQTTClient Function Block to connect to the MQTT Broker.

Rung9

If the MQTT Client connects to the Broker OK and no error occurs, it will automatically subscribe the Topic to the Broker.

Rung10

Subscribe Topic PLCNEXT/Node/Data/Node1 from Broker by calling MQTTSubAryByte Function Block.。

Rung11

When a new Message is received, the data is converted from a Byte array to a string using the AryToString function, and then the string is reconverted to DINT.

Rung12

Check the communication status between NX CPU and Broker using MQTTPing Function Block.

Rung13

Subscribe Topic PLCNEXT/Node/Data/Node1 from Broker by calling MQTTSubString Function Block.

Rung14

When a new Message is received, the data is converted from string to DINT using the AryToString function.。

Rung15

Writes the Message value to be published when the NX CPU is connected to the Broker.

Rung16

Call MQTTPubAryByte Function Block to Publish Message to Broker.

Rung17

Reset the trigger device once the Message is published.

Rung18

Writes the Message value to be published when the NX CPU is connected to the Broker.

Rung19

Call MQTTPubString Function Block to Publish Message to Broker.

Rung20

Reset the trigger device once the Message is published.

Result

Done!Client.Connected is now Ture and the NX CPU has connected to the Broker.

Subscribe

Omron NX CPU subscribed “PLCNEXT/Node/Data/Node1” to the MQTT Broker.

Now, let’s try to Publish the data on the PLCNEXT side.

SubAryByte.MsgSize=5, i.e. 5Bytes of data received.

You can then see the Topic received from RevTopic.

Data 12345 received.

You can check the Topic received from RevTopic in the MQTTSubString Function Block.

Likewise, data 12345 was received.

Publish

Now we will trigger the Execute Flag of MQTTPubAryBytePublish and send data from the Omron NX CPU to the Broker in PLCNEXT.。

Done!PLC NEXT Broker received 7891.

Next, MQTTPubString’s Execute is triggered and the string data is sent to the PLC NEXT Broker.

Done!PLC NEXT Broker received 8922 numbers.

Footer_Basic

Please Support some devices for my blog

Amazon Gift List

Find ME

Twitter:@3threes2
Email:soup01threes*gmail.com (* to @)
YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

シェアする

  • このエントリーをはてなブックマークに追加

フォローする