MQTT Broker add-on, not recieving certain messages


I’m having some trouble using the mosquitto broker add-on, and was hoping anyone had some suggestions. I’ve tried to solve it for several days without luck.

Background ;
Moved from RPi running Home Assistant + mosquitto (not add-on)
Moved to Win 10 machine running VBox Home Assistant incl Mosquitto add-on

I have Sonoff Bridge, Zigbee2mqtt and some ESP8266/ESP32 communicating through mqtt.
The ESP’s are not running ESPHome.

Everything except the ESP’s are working fine in the MQTT add-on. ESP will connect, but not publish. I also get “exceeded timeout, disconnecting”, but this seems to go away if I use mqtt_username + mqtt_password in the code.

I set up a RPi with mosquitto broker for testing the ESP’s and as long as I run mosquitto on Pi it works.

I tried port forward of port 1883, tried disabling win 10 firewall and tried different config’s in the mosquitto broker add-on without luck.

This is serial print in Arduino IDE, no publish message between connection and heartbeat.

WiFi connected
IP address: 
Attempting MQTT connection...connected
20 second heartbeat:  mqtt alive

This is config in mosquitto broker add-on. Username and password is same as in HA, and Arduino code. I also tried with anonymous false + removed user from acl, and active false

  - username: user
    password: password 
anonymous: true 
  active: true 
  folder: mosquitto
certfile: fullchain.pem
keyfile: privkey.pem
require_certificate: false

MQTT settings

Broker: core-mosquitto
Port: 1883
Username: same as add-on/arduino
Password: same as add-on/arduino

ESP8266 code :

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

// Update these with values suitable for your network.

#define LED 0 //GPIO16 is LED on my ESP8266 module

//"**CHANGE**" wifi SSID and password
const char* ssid = "SSID";
const char* password = "password";

//"**CHANGE**" MQTT server IP address
const char* mqtt_server = "192.168.HA.IP";  //mqtt server server 2.3
const char* mqtt_user = "user"; // same as in mqtt add-on config
const char* mqtt_password = "password"; // same as in mqtt add-on config

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

IPAddress server(192, 168, x, xx);
unsigned long heartbeat_millis=0;
int heartbeat_cnt=0;

//"**CHANGE**" debug output
#define MyDebug 0   //0=no debug, 1=deeebug

**CHANGE** mqtt_name[] used as part of topic name
const char mqtt_name[]="2etg";
//const char mqtt_name[]="livingroom";
//const char mqtt_name[]="basement";

const int pinHandShake = 4; //handshake pin, esp8266 GPIO4/D2

//buffer for char[] used in mqtt publishes, size at least as big as topic + msg length
char mqtt_buf[120];  // e.g "/ble/esp1/d76dcd1b3720/volt" is 30 chars long

bool conn_status;
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

const int serialBufSize = 100;      //size for at least the size of incoming JSON string
static char serialbuffer[serialBufSize];  // {"mac":c2f154bb0af9,"rssi":-55,"volt":3.05,"mag":1}
static char mytopic[120];  //MQTT topic string size, must be less than size declared.
//todo: add error checking for mqtt msg lengths

void setup_wifi() {

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {


  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  digitalWrite(LED, HIGH);  //off

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i = 0; i < length; i++) {

  char message_buff[60]; //payload string
  // create string for payload
  int i=0;
  for(i=0; i<length; i++)
    message_buff[i] = payload[i];
  message_buff[i] = '\0';  //terminate buffer string class eol
  String msgString = String(message_buff);


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client2-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      // Once connected, publish an announcement...
      client.publish("outTopic", "hello world");
      // ... and resubscribe
      //client.subscribe("50C51C570B00");   //switched ble

    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  pinMode(LED, OUTPUT);     //  BUILTIN_LED pin as an output, actually pin D0 (GPIO16)
  digitalWrite(LED, LOW);  //LED on
  Serial.begin(9600);   //ESP8266 default serial on UART0 is GPIO1 (TX) and GPIO3 (RX)
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  pinMode(pinHandShake, OUTPUT);
  client.setServer(mqtt_server, 1883);

//stealing this code from
//non-blocking serial readline routine, very nice.  Allows mqtt loop to run.
int readline(int readch, char *buffer, int len)
  static int pos = 0;
  int rpos;

  if (readch > 0) {
    switch (readch) {
      case '\n': // Ignore new-lines
      case '\r': // Return on CR
        rpos = pos;
        pos = 0;  // Reset position index ready for next time
        return rpos;
        if (pos < len-1) {
          buffer[pos++] = readch;   //first buffer[pos]=readch; then pos++;
          buffer[pos] = 0;
  // No end of line has been found, so return -1.
  return -1;

//json iterator that parses char* and publish each key-value pair as mqtt data
void localParseJson(char *mybuf)

  //JSON parsing
  //JSON object exists in this scope, routine should execute atomatically no interrupts
  //If software serial is used, and parsing is interrupted, json object still be valid?
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& object = jsonBuffer.parseObject(mybuf);
    Incoming uart:
    Iterate and publish as
    /ble/livingroom/c2f154bb0af9/rssi -55
    /ble/livingroom/c2f154bb0af9/volt 3
    /ble/livingroom/c2f154bb0af9/mag 1

  1)  count # fields in after MAC
  2)  concat "ble"/"MAC"/+iterate field
  3)  capture data to publish
  if (object.success())
    // Parsing success, valid json
    //todo:  it's too easy to forget mytopic size, not checking for size when string being assembled.
    //    add error checking and improve readability

    int topic_char_index=0;
    // mytopic[i] = '\0'; make sure null terminate mytopic
    //construct the first part of the topic name, which is just /ble/mac
    mytopic[0]='/'; topic_char_index++;
    mytopic[1]='y'; topic_char_index++;
    mytopic[2]='y'; topic_char_index++;
    mytopic[3]='y'; topic_char_index++;
    mytopic[4]='/'; topic_char_index++;  //topic_char_index is 5;

    //add gateway location name, /ble/LIVINGROOM
    int mqtt_name_len = sizeof(mqtt_name);  //returns num of chars + 1 for \0 character
    #if MyDebug
    Serial.print("topic_char_index before mqtt_name:  ");
    Serial.print("size of mqtt_name_len should be 16:  ");

    for (int i=0; i<(mqtt_name_len-1); i++)
      #if MyDebug
      mytopic[topic_char_index] = mqtt_name[i];
    mytopic[topic_char_index] = '/';  topic_char_index++;
    #if MyDebug
    Serial.print("topic_char_index after mqtt_name:  ");
    //set string pointer to JSON Value containing the key "mac"
    // so it's pointing to "mac":c2f154bb0af9
    const char* mac_name = object["mac"];
    #if MyDebug
    Serial.println("mac name is");
    int sizeofff = strlen(mac_name);    //returns exactly number of characters in mac_name.  Doesn't add 1 like sizeof

    #if MyDebug
    Serial.print("size of macname: ");

    // /ble/livingroom/c2f154bb0af9/
    //fill mac address into topic name string
    for (int i=0; i<sizeofff; i++)
      mytopic[topic_char_index]= mac_name[i];
    mytopic[topic_char_index] = '/';

    #if MyDebug
    Serial.println("mytopic after mac address add");
     iterate through sensor topics portions

    //as we iterate through the keys in json, need a marker for char[] index
    //up to the MAC address:  "/ble/livingroom/c2f154bb0af9/"  <---- here
    int topic_char_index_marker = topic_char_index;
    for (JsonObject::iterator i=object.begin(); i!=object.end(); ++i)
      topic_char_index = topic_char_index_marker; //reset index to just after MAC location
       /ble/livingroom/c2f154bb0af9/  <---- here

      #if MyDebug

      //copy string for key into the prepared topic string
      const char* key_name = i->key;  //funny, key_name has to be const to compile in ESP8266. 
      int keysize = strlen(key_name);

      #if MyDebug
      Serial.print("key size:  ");
      Serial.print(",        topic_char_index:  ");
      for (int j=0; j<keysize; j++)
        mytopic[topic_char_index] = key_name[j];  topic_char_index++;
      /* note that topic_char_index is sitting at position after rss"i"
       *  /ble/c2f154bb0af9/rssi   <----- rssi, volt, etc...

      mytopic[topic_char_index] = '\0';  //terminate string w/ null

      #if MyDebug
      Serial.print("topic_char_index of null char");
      Serial.println("printing entire mytopic:");

      //mqtt publish relies a null terminated string for topic names
    }//end iterator

  }//end if json parsed successful
    client.publish(mqtt_name, "bad json format");
    client.publish(mqtt_name, mybuf);
    Serial.println("--->bad json:");
  } //failed json parse

  digitalWrite(LED, HIGH);  //LED off
} //end routine localParseJson

// main loop
void loop() {
  if (!client.connected()) {
  //buffer for reading serial, must be writable
  if (Serial.available() > 0)
    //received serial line of json
    if (readline(, serialbuffer, serialBufSize) > 0)
      digitalWrite(pinHandShake, HIGH);
      digitalWrite(LED, LOW);  //LED on
      #if MyDebug
      Serial.print("You entered: >");
      localParseJson(serialbuffer); //call JSON iterator on this line from serial
      digitalWrite(LED, HIGH);  //LED off
      digitalWrite(pinHandShake, LOW);

  }//end seria.available

  //every X seconds, publish heartbeat
  if ((millis() - heartbeat_millis > 20000) || ((millis() - heartbeat_millis) < 0))
    heartbeat_millis = millis();
    conn_status = client.connected();
    Serial.print("20 second heartbeat:  ");
    if (conn_status == 0)
      Serial.print("mqtt dead, attempting to connect...");
      if (client.connect(mqtt_name))
        Serial.println("failed to reconnect MQTT"); 
        Serial.println("reconnected succesful");
      Serial.println("mqtt alive");
    if (conn_status == 0)
      client.publish(mqtt_name, "had to reconnect");
    client.publish(mqtt_name,itoa(heartbeat_cnt, mqtt_buf, 10));
  } //end heartbeat
} //end loop

I tried publishing to my Pi Mosquitto, then bridging that with my mosquitto add-on.
Like so ;
ESP8266 --> Pi Mosquitto --> Bridge --> Mosquitto add-on

Message is recieved on Pi Mosquitto, and brigde connection is successful, but no messages are recieved on mosquitto add-on.

Mosquitto add-on log ;

1600082697: New connection from 192.168.pi.ip on port 1883.
1600082697: New bridge connected from 192.168.pi.ip as PiMqtt.bridge-01 (p1, c0, k60).