SH*T an Error! Fixing socket error on ESP, Mosquitto

Fig 1. Home Assistant setup

After, the earlier installation Mosquitto installation on the Raspberry PI (35 euro computer). I recently had an error on the Mosquitto add-on. My setup currently is being depicted in figure 1.

As you can see (coming soon) I made a simple custom RGB led lamp inspired by the Philips hue (more on this soon). However, I came across a Socket error showing up on the log window of Mosquitto. Showing something like this:


 Fig 2. Error (AWHHH shit, No Why?)

Currently, there is not much documentation on this error. So, follow the steps in order to make your setup work again. The clue is in on long your client stays connected to the MQTT server. So this can depend on the library you use. I programmed the RGB lamp using the Arduino IDE with the PubSub library: Link


In order to fix this we need to dive into the code of the library. You can find this in the directory shown in figure 2. (This can be different on your machine or depending on your operating system)

Fig 3.


In the libraries directory, you can find the PubSub library (shown in figure 3).

Fig 4.


In the PubSubClient navigate to the src there you can find the following:

Fig 5.


This library is written in C++. Open the PubSubClient.h  


There you can find probably something like shown in figure 5.

Fig 6. Pubsub header file (where all the magic happens)


We need to change the MQTT_KEEPALIVE. Personally, I changed this to 600. But you can change this up-to 65535 since this the value can hold an 8-bit variable.


Safe and re-upload your sketch.


If you have any questions feel free to leave a comment.


Leave a Reply

Your email address will not be published. Required fields are marked *