Page 1 of 1

Unable to connect to local MQTT broker via websockets example - invalid header=0x48

Posted: Thu Jan 14, 2021 5:01 pm
by Xtensa2C
Hi all,

I just wanted to get started with MQTT for some self made home automation.
Therefore I was planning on using MQTT over websockets to connect to a local mosquitto MQTT broker.
Until now I was able to connect to my broker via the MQTT TCP example. I was also able to connect to the public mosquitto MQQT test server via the websockets example.

But if I try to connect to my local hosted server I always get the error at the bottom from the ESP.
To my understanding the connection is refused by the ESP because the server sends an unsupported fixed header byte.
But I don't know why and how to fix that.

My System: Windows 10 Ultimate 64 bit (Added incoming and outgoing firewall rule on port 9001)
ESP IDF: 4.2
Controller: ESP 32
Mosquitto broker: 2.0.5

This what I changed in the example:
  1. static void mqtt_app_start(void)
  2. {
  3.     esp_mqtt_client_config_t mqtt_cfg = {
  4.            // .uri = "ws://test.mosquitto.org:8080/mqtt",
  5.        .host ="192.168.0.12",
  6.        .port =9001,
  7.     };
This is my mosquitto broker config
  1. allow_anonymous true
  2. port 1883
  3. listener 9001
  4. protocol websockets
This is the error output from the ESP:
  1. I (2627) example_connect: Connected to Vodafone-BC4C
  2. I (2627) example_connect: IPv4 address: 192.168.0.9
  3. I (2637) example_connect: IPv6 address: fe80:0000:0000:0000:3e71:bfff:fef0:9b48
  4. I (2647) MQTTWS_EXAMPLE: Other event id:7
  5. I (2687) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
  6. E (2807) MQTT_CLIENT: mqtt_message_receive: received a message with an invalid header=0x48
  7. E (2807) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned -1
  8. I (2807) MQTT_CLIENT: Error MQTT Connected
  9. I (2817) MQTTWS_EXAMPLE: MQTT_EVENT_DISCONNECTED
  10. I (17817) MQTTWS_EXAMPLE: Other event id:7
  11. I (17837) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
  12. E (17937) MQTT_CLIENT: mqtt_message_receive: received a message with an invalid header=0x48
  13. E (17937) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned -1
  14. I (17947) MQTT_CLIENT: Error MQTT Connected
  15. I (17957) MQTTWS_EXAMPLE: MQTT_EVENT_DISCONNECTED
Any help would be gladly appreciated ;)

Re: Unable to connect to local MQTT broker via websockets example - invalid header=0x48

Posted: Thu Jan 14, 2021 5:59 pm
by chegewara
Did you try to set esp_mqtt_transport_t in esp_mqtt_client_config_t. I think its default to TCP or SSL.

Re: Unable to connect to local MQTT broker via websockets example - invalid header=0x48

Posted: Fri Jan 15, 2021 1:29 pm
by Xtensa2C
Oh wow, thank you so much I totally overlooked that part.
The documentation even says that the transport type is normally set in the URI.
Of course I have to manually set it if I am connecting to my local server ...