Beckhoff#Using TwinCAT TF6701 to Publish message inAWS IOT Core

What is TF6701?

In this tutorial I will explain how to use Beckhoff TF6701 to publish the message toBroker.

We can use this to connect to AWS,Google ,Azure also.


Manual

Please download the manual from the below link:

https://download.beckhoff.com/download/Document/automation/twincat3/TF6701_TC3_IoT_Communication_MQTT_EN.pdf

System Requirement

And also, you do not need any additional installation of TF6701.

Packages

This time, I will use TF6701 only.Let’s explain TF6720 in the future :)!。

Let me explain AWS IOT Core. It is a platform that comes from Amazon, we can use this platform to connect all our devices to the internet. In TwinCAT , we can use TF6701 to save our life and take the configuration to be easy.

Build the AWS IOT Core

Please reference the following post to get more information about how to build the AWS IOT Core platform.

Siemens#Using CC712 to Connect your S7-1500 to AWS IOT Core | (soup01.com)

Function Block

The FB_IotMqttClient Function block is used to connect to AWS Iot Core.

By using this Function block of tF6701, we can easily connect the Runtime variables to Local/Cloud MQTT Broker.

VAR_INPUT

sClientIdSTRING(255)Reference to the name of “Things” in AWS IOT Core.
sHostNameSTRING(255)The URL of Broker.Reference to the “EndPoint” of AWS IOT Core.
nHostPortUINTDefault=1883. If TLS is used, please change to 8883.
sTopicPrefixSTRING(255)The command that you want to insert automatically.
nKeepAliveUINTWatchdog Time(s)
sUserNameSTRING(255)Username while login to the Broker(Option)
sUserPasswordSTRING(255)Password while login to the Broker(Option)
stWillST_IotMqttWillThe message while connection is failed.
stTLSST_IotMqttTlsif TLS-secured connection is used, you need to set these parameters.
ipMessageFiFoI_IotMqttMessageFiFoFB_IotMqttMessageQueue instance

ST_IotMqttWill

You can set the Qos,Payload in this DUT.

sTopicSTRING(255)Message Topic
pPayloadPVOIDThe Memory address of the Payload
nPayloadSizeUDINTThe size of the Payload
eQoSTcIotMqttQosQos(Quality of Service)
bRetainBOOL1=take sequence

ST_IotMqttTls

The DUT of TLS security settings.

You can useCA(certificate authority) or PSK(PreSharedKey) .

sCASTRING(255)The path of certificate authority (CA)
sCertSTRING(255)The path of Client certificate
sKeyFileSTRING(255)The path of Client Private Key Path
sKeyPwdSTRING(255)The password of the Private Key
sCrlSTRING(255)The path of certificate revocation list
sCiphersSTRING(255)
sVersionSTRING(80)TLS version。‘tlsv1’・ ‘tlsv1.1’・‘tlsv1.2’・‘tlsv1.3’
bNoServerCertCheckBOOLCheck the Server certification is valid or not
sPskIdentitySTRING(255)TLS PSK PreSharedKey identity
aPskKeyARRAY[1..64] OF BYTETLS PSK Psk Key
nPskKeyLenUSINTThe size of Psk key
sAzureSasSTRING(511)Only used in Microsoft Azure IoT Hub

VAR_OUTPUT

bErrorBOOL1=FB is error
hrErrorCodePVOIDErrorCode
eConnectionStateETcIotMqttClientStateCurrent Status
bConnectedBOOLBroker connection is established or not

Method

Execute

You need to call this Method cyclically .

VAR_INPUT

bConnectBOOL1=Connect to Broker

Publish

MQTT BrokerにPublish動作するMethodです。

VAR_IN

sTopicSTRINGThe Pushlish Topic
pPayloadPVOIDThe memory address of payload
nPayloadSizeUDINTThe size of the payload
eQoSTcIotMqttQosQos
bRetainBOOL
bQueueBOOLSpare

RETRUN

PublishBOOL1=Publish completed

Subscribe

Sorry I will explain it in my next post.

Unsubscribe

Sorry I will explain it in my next post.

VAR_INPUT

sTopicSTRINGThe topic that you want to Unsubscription

Return

UnsubscribeBOOL1=Unsubscribe is completed.

ActivateExponentialBackoff

Method for backoff.Not used in this post.

DeactivateExponentialBackoff

Method for backoff.Not used in this post.


Implemention

Now I will explain how to implement it,

Insert Library

References>Add library.]

Search Tc3_IotBase and Import it.

And also import the Tc2_Utilities library.

Program

Now is the time to create the program.The flow is very simple – set all the connection parameters(certificate,Private Key,Endpoint, port and Client id), then call the Function Block to establish a connection to AWS IOT CORE. Finally publish the message.

VAR

VAR
MqttClient :FB_IotMqttClient;
bPublish :BOOL:=FALSE;
sTopicEmail :STRING(255) := ‘TwinCAT/TF6701/Devices1’;
bSend :BOOL;
sPayloadEmail :STRING(255) := ‘{“default”: “Hello from TwinCAT”,”message”: “Hello from TwinCAT”}’;
bConnect :BOOL;
bsetPars :BOOL;
icoun :INT;
MessageQueue :FB_IotMqttMessageQueue;

END_VAR

MAIN

MqttClient.nHostPortis the Endpoint URL ofAWS IOT Core.

MqttClient.nHostPort – we care usingMQTT TLS Secure,please set it to 8883.

MqttClient.sClientId – the Name of your “Things” inAWS IOT Core.

sCert and sKeyFileis are the files that you download while ”Things” is created.

IF bsetPars THEN
MqttClient.stTLS.sCA:=’c:\Cert\aws\AmazonRootCA1.pem’;
MqttClient.stTLS.sCert:=’c:\Cert\aws\ccf8d8b84f-certificate.pem.crt’;
MqttClient.stTLS.sKeyFile:=’c:\Cert\aws\ccf8d8b84f-private.pem.key’;
MqttClient.sHostName:=’a30hlb7uyo754j-ats.iot.us-east-2.amazonaws.com’;
MqttClient.nHostPort:=8883;
MqttClient.sClientId:=’mythings’;
MqttClient.ipMessageQueue:=MessageQueue;
MqttClient.sTopicPrefix:= ”;
END_IF;

MqttClient.Execute(bConnect:=bConnect);
IF MqttClient.bConnected THEN
icoun:=icoun+1;

END_IF

IF  bPublish  THEN
MqttClient.Publish(
sTopic:= sTopicEmail
,pPayload:= ADR(sPayloadEmail)
,nPayloadSize:=LEN2(ADR(sPayloadEmail))
,eQoS:= TcIotMqttQos.AtMostOnceDelivery
,bRetain:= FALSE
,bQueue:= FALSE);
bPublish:=False;
END_IF

Result

open the AWS IOT Core Console, go to Test> Subscribe.

Enter the Topic name that you published.

In my case ,the name is TwinCAT/TF6701/Devices1.

Let’s Turn on the bPublish.

You can see the Message that was published from TwinCAT!

Sample Project

https://github.com/soup01Threes/TwinCAT3/blob/main/TwinCAT%20Project1_TF6701_aws.tnzip

Finally

If Error MQTT_ERR_TLS_CA_NOFOUND is output , please check your path of certification again.

Also, before the connection is established, the status always shows とMQTT_ERR_NOMEM.

https://github.com/eclipse/paho.mqtt.python/issues/392

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

シェアする

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

フォローする