# MQTT with TLS and IIoT Library

**URL:** <https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448>\
**Category:** CODESYS\
**Tags:** mqtt, codesys\
**Created:** [December 1, 2025, 3:25pm UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448 "2025-12-01T15:25:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![quenorha](https://sea2.discourse-cdn.com/flex016/user_avatar/www.wago.community/quenorha/32/429_2.png) [@quenorha](https://www.wago.community/u/quenorha)\
**Post date:** [December 1, 2025, 3:25pm UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448/1 "2025-12-01T15:25:48Z")

</div>

Does anyone successfully has successfully set up a TLS MQTT communication with IIoT Library ?

I have created a PKI and a Mosquitto broker. It works fine and I can use it with several client.  
Then I want to do it with MQTT function block from CODESYS (WAGO dataagent works fine).  
I import my client certificate in .pfx file. I even tried to import the CA certificate.

As soon as I enable Peer verification in the TLSContext the connection is bad and I get this message on my mosquitto broker :  
OpenSSL Error[0]: error:0A000410:SSL routines::ssl/tls alert handshake failure  
1764601655: Client disconnected: Protocol error.

Any idea ?

Thanks

---

<div class="post-metadata">

**Author:** ![WagoIcard](https://sea2.discourse-cdn.com/flex016/user_avatar/www.wago.community/wagoicard/32/1508_2.png) [@WagoIcard](https://www.wago.community/u/WagoIcard)\
**Post date:** [December 4, 2025, 5:53am UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448/2 "2025-12-04T05:53:54Z")

</div>

Idk if this is related but for an application with a customer which use TLS connection I had to do this from time to time:

```
_sWgetCommand := 'wget https://curl.se/ca/cacert.pem -P /etc/ssl/certs';
				
WagoSysProcess.FuExecuteCommand(
	sCommand := _sWgetCommand, 
	R_sStdOut := _sWgetStdOut, 
	uiStdOutSize	:= SIZEOF(_sWgetStdOut), 
	R_sStdError := _sWgetStdError, 
	uiStdErrorSize	:= SIZEOF(_sWgetStdError), 
	tTimeout := T#5S, 
	pResult := ADR(_eWgetResult)
);

```

---

<div class="post-metadata">

**Author:** ![WagoMarcel](https://avatars.discourse-cdn.com/v4/letter/w/4bbf92/32.png) [@WagoMarcel](https://www.wago.community/u/WagoMarcel)\
**Post date:** [March 26, 2026, 2:09pm UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448/3 "2026-03-26T14:09:45Z")

</div>

Hey @quenorha,

i ran across your thread yesterday while searching for a working example myself. I managed to fix it myself today and learned a lot about codesys-specialties regarding TLS. If your question is still present, just give me some information about the steps you already tried and we will find a solution!

Best regards  
Marcel

---

<div class="post-metadata">

**Author:** ![quenorha](https://sea2.discourse-cdn.com/flex016/user_avatar/www.wago.community/quenorha/32/429_2.png) [@quenorha](https://www.wago.community/u/quenorha)\
**Post date:** [March 26, 2026, 3:59pm UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448/4 "2026-03-26T15:59:03Z")

</div>

Hello Marcel,

Thanks. I finally find it out. I was totally missing that I had to add my CA cert in the trust area.

I’ll try to send capture when I find some time.

---

<div class="post-metadata">

**Author:** ![ValentinF](https://avatars.discourse-cdn.com/v4/letter/v/e274bd/32.png) [@ValentinF](https://www.wago.community/u/ValentinF)\
**Post date:** [April 22, 2026, 5:55am UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448/5 "2026-04-22T05:55:01Z")

</div>

Hello,

I came across your post while searching for examples. Do you happen to have a working example? As soon as I add the itfTLSContext, I get a TCP\_Init error, and I can’t figure out what’s causing it.

I’ve already loaded the certificate into the Wago’s trusted store.  
The latest version of the MQTT library is also installed.  
If I enable UseTLS instead of using TLSContext, I get a connection (though apparently the certificate is not validated in that case).

Perhaps you can help me out.

Best regards,

Valentin

---

<div class="post-metadata">

**Author:** ![quenorha](https://sea2.discourse-cdn.com/flex016/user_avatar/www.wago.community/quenorha/32/429_2.png) [@quenorha](https://www.wago.community/u/quenorha)\
**Post date:** [April 22, 2026, 9:58am UTC](https://www.wago.community/t/mqtt-with-tls-and-iiot-library/2448/6 "2026-04-22T09:58:10Z")

</div>

I used the default MQTT IIoT CODESYS library example from CODESYS.  
The problem comes probably from the TLS configuration rather than from the program.

What broker do you use ?  
Could you explain which certificates you have generated and where it is deployed ?

Here is a documentation (in french, use your favorite translate tool) regarding TLS and certificates handling.

> **[Support WAGO AUTOMATION](https://supportwago.fr/index.php?projectID=e380a5c06533)**
>
> Support WAGO AUTOMATION

It includes a script to generate your PKI.

In the chapter 10.3 you’ll find information regarding certificate handling for MQTT in CODESYS Security screen.
