ESP8266 and MQTT disaster averted…

Fair warning, this is going to be a super geeky IOT post. Just an hour ago I figured out a rather frustrating glitch in the code I was using to connect 2 ESP8266 chips to a local MQTT Broker. For some reason the code worked perfectly on both devices until I plugged them both in. Then they would constantly disconnect form the server.

I am using the PubSubClient library for handling MQTT interaction with my local Mosquito server running on a Raspberry Pi model B. I recently got a stable interaction going with some NeoPixels when I decided it was time to connect a few of them up for unified and individual control.

Think of this as like Phillips Hue, but with full animations, locally controlled, and at a fraction of the prices.

Anyway, the moment I got more than one turned on, both start continuously looking network connection. Packets get lost and the lights go form near bullet proof to buggy as hell. Unplug one and the other returns to near stable operation.

I checked to see that both devices were pulling unique IP addresses and just to be safe I even changed the topics that they were subscribed to. I hunted for people having the same issue, but nothing. At last I ran across two posts that mentioned an issue with matching Client ID’s clashing on the same MQTT servers on remote APIs.

I hunt through the exsample code to see what is going on and sure enough. They are using the same Client ID every time on Reconnect. I changed it to a unique one.

BAM!!!

All fixed. Now I need to add a random number to the end of the Client ID so that I never have to worry about this ever again.