# FreeRTOS MQTT fails to connect to MQTT Mosquitto(v1.6.7) broker on Windows

**URL:** <https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093>\
**Category:** Kernel\
**Created:** [October 4, 2019, 6:43pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093 "2019-10-04T18:43:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 6:43pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/1 "2019-10-04T18:43:56Z")

</div>

**[wonderfuljames](https://sourceforge.net/u/wonderfuljames/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#c9f3):

I am running a FreeRTOS MQTT client on Microchip PIC32MZ eval board and able to connect to AWS MQTT server over wired Ethernet.  
But When I try to connect to a MQTT Mosquitto broker running on my Windows, I get a connection error as follows:

C:\Program Files\mosquitto\>mosquitto -v  
1570213770: mosquitto version 1.6.7 starting  
1570213770: Using default config.  
1570213770: Opening ipv6 listen socket on port 1883.  
1570213770: Opening ipv4 listen socket on port 1883.  
1570213804: New connection from 192.168.9.169 on port 1883.  
1570213804: Client disconnected due to protocol error.

The fact that the broker is getting a connection request from my PIC32MZ eval board, the IP and port # are correctly set up.  
Does anyone have experience with trying to connect FreeRTOS MQTT client to a broker other than default AWS MQTT broker?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [October 4, 2019, 6:53pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/2 "2019-10-04T18:53:34Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#c9f3/d0d2):

Yes. Is this the demo you tried? I used it with the public mosquitto broker and with mosquitto running locally: [https://www.freertos.org/mqtt/preconfiguredexamples.html](https://www.freertos.org/mqtt/preconfiguredexamples.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 8:40pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/3 "2019-10-04T20:40:11Z")

</div>

**[wonderfuljames](https://sourceforge.net/u/wonderfuljames/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#a14d):

Thanks Richard for getting back.  
Yes the Windows version of FreeRTOS MQTT client works fine with my local mosquitto.  
But FreeRTOS MQTT client running on my PIC32MZ fails to connect to the same local mosquitto.  
As I mentioned, FreeRTOS MQTT client running on my PIC32MZ works fine with AWS MQTT broker.  
In theory, the following is all I have to change?  
//static const char clientcredentialMQTT\_BROKER\_ENDPOINT[] = “[atmaz5u2t2hej-ats.iot.us-east-2.amazonaws.com](http://atmaz5u2t2hej-ats.iot.us-east-2.amazonaws.com)”;  
static const char clientcredentialMQTT\_BROKER\_ENDPOINT[] = “192.168.9.70”;

// #define clientcredentialMQTT\_BROKER\_PORT 8883  
#define clientcredentialMQTT\_BROKER\_PORT 1883

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 8:43pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/4 "2019-10-04T20:43:07Z")

</div>

**[wonderfuljames](https://sourceforge.net/u/wonderfuljames/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#11d1):

I have a bit more specific information as to the failure:

33 5802 [iot\_thread] ERROR: Handshake failed with error code -128  
34 5802 [iot\_thread] [ERROR][NET][5802] Failed to establish new connection.  
35 5802 [IP-task] FreeRTOS\_closesocket[42094 to c0a80946ip:1883]: buffers 60 socks 0  
36 5803 [iot\_thread] [ERROR][MQTT][5803] Failed to establish new MQTT connection, error NETWORK ERROR.

“Handshake failed”…

Any ideas?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 9:07pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/5 "2019-10-04T21:07:53Z")

</div>

**[wanggord](https://sourceforge.net/u/wanggord/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#4f9b):

Hi James,

From those logs, it looks like the demo application is still trying to perform a TLS handshake. If your broker is an unsecured Mosquitto server, this will cause the connection to fail.

To successfully connect to Mosquitto, you will need to change 2 lines of code in your demo. Assuming you are running [this](https://github.com/aws/amazon-freertos/blob/master/demos/mqtt/iot_demo_mqtt.c) file as your demo:

Disable TLS by not passing any credentials.

```auto
networkInfo.u.setup.pNetworkCredentialInfo = NULL; // Line 411

```

Disable AWS IoT MQTT mode.

```auto
connectInfo.awsIotMqttMode = false; // Line 419

```

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 9:52pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/6 "2019-10-04T21:52:16Z")

</div>

**[wonderfuljames](https://sourceforge.net/u/wonderfuljames/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#6699):

Dear Gordon,  
That fixed my issue.  
Thanks for the info about TLS handshake in FreeRTOS.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 9:52pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/7 "2019-10-04T21:52:22Z")

</div>

**[wonderfuljames](https://sourceforge.net/u/wonderfuljames/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#55a7):

Dear Gordon,  
That fixed my issue.  
Thanks for the info about TLS handshake in FreeRTOS.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 4, 2019, 10:03pm UTC](https://forums.freertos.org/t/freertos-mqtt-fails-to-connect-to-mqtt-mosquitto-v1-6-7-broker-on-windows/8093/8 "2019-10-04T22:03:29Z")

</div>

**[wonderfuljames](https://sourceforge.net/u/wonderfuljames/)** wrote on [Friday, October 04, 2019](https://sourceforge.net/p/freertos/discussion/382005/thread/d6ffba3724/#c9f3/d0d2/f60c):

Thanks for pointing me to this link. Quite helpful
