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:
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
sClientId | STRING(255) | Reference to the name of “Things” in AWS IOT Core. |
sHostName | STRING(255) | The URL of Broker.Reference to the “EndPoint” of AWS IOT Core. |
nHostPort | UINT | Default=1883. If TLS is used, please change to 8883. |
sTopicPrefix | STRING(255) | The command that you want to insert automatically. |
nKeepAlive | UINT | Watchdog Time(s) |
sUserName | STRING(255) | Username while login to the Broker(Option) |
sUserPassword | STRING(255) | Password while login to the Broker(Option) |
stWill | ST_IotMqttWill | The message while connection is failed. |
stTLS | ST_IotMqttTls | if TLS-secured connection is used, you need to set these parameters. |
ipMessageFiFo | I_IotMqttMessageFiFo | FB_IotMqttMessageQueue instance |
ST_IotMqttWill
You can set the Qos,Payload in this DUT.
sTopic | STRING(255) | Message Topic |
pPayload | PVOID | The Memory address of the Payload |
nPayloadSize | UDINT | The size of the Payload |
eQoS | TcIotMqttQos | Qos(Quality of Service) |
bRetain | BOOL | 1=take sequence |
ST_IotMqttTls
The DUT of TLS security settings.
You can useCA(certificate authority) or PSK(PreSharedKey) .
sCA | STRING(255) | The path of certificate authority (CA) |
sCert | STRING(255) | The path of Client certificate |
sKeyFile | STRING(255) | The path of Client Private Key Path |
sKeyPwd | STRING(255) | The password of the Private Key |
sCrl | STRING(255) | The path of certificate revocation list |
sCiphers | STRING(255) | |
sVersion | STRING(80) | TLS version。‘tlsv1’・ ‘tlsv1.1’・‘tlsv1.2’・‘tlsv1.3’ |
bNoServerCertCheck | BOOL | Check the Server certification is valid or not |
sPskIdentity | STRING(255) | TLS PSK PreSharedKey identity |
aPskKey | ARRAY[1..64] OF BYTE | TLS PSK Psk Key |
nPskKeyLen | USINT | The size of Psk key |
sAzureSas | STRING(511) | Only used in Microsoft Azure IoT Hub |
VAR_OUTPUT
bError | BOOL | 1=FB is error |
hrErrorCode | PVOID | ErrorCode |
eConnectionState | ETcIotMqttClientState | Current Status |
bConnected | BOOL | Broker connection is established or not |
Method
Execute
You need to call this Method cyclically .
VAR_INPUT
bConnect | BOOL | 1=Connect to Broker |
Publish
MQTT BrokerにPublish動作するMethodです。
VAR_IN
sTopic | STRING | The Pushlish Topic |
pPayload | PVOID | The memory address of payload |
nPayloadSize | UDINT | The size of the payload |
eQoS | TcIotMqttQos | Qos |
bRetain | BOOL | |
bQueue | BOOL | Spare |
RETRUN
Publish | BOOL | 1=Publish completed |
Subscribe
Sorry I will explain it in my next post.
Unsubscribe
Sorry I will explain it in my next post.
VAR_INPUT
sTopic | STRING | The topic that you want to Unsubscription |
Return
Unsubscribe | BOOL | 1=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.