MQTT connection failing on Arduino ESP8266 to MQTT on HA OS

I read through the MQTT documentation on HA, and the MQTT topics on this forum but could not find help to my problem.

I have a sketch that I have used for several years that sends temperature and humidity from an ESP8266 with DHT22 sensor to a Raspberry Pi running mosquitto which feeds the data to a simple dashboard display on a 3" LCD screen.

I want to re-implement this weather station using HA. I installed HA OS using the official RPi3-32 bit image, and used the Add-On Store to install the Mosquitto broker. I accepted the default configuration. I viewed the configuration by Configuration > Integrations > MQTT > Configure, and clicked RE-CONFIGURE. I see that the Broker is core-mosquitto, Port is 1883, Username is homeassistant, and Password is (a very long string). When I click SUBMIT, it shows me the options which I have not changed. In that configuration window, I can listen to #, and when I publish a packet, it appears. Therefore, I assume MQTT is running.

My sketch that works on my ESP8266 and RPi is:

> // Get ESP8266 going with Arduino IDE
> // from https://gist.github.com/balloob/1176b6d87c2816bd07919ce6e29a19e9
> // - https://github.com/esp8266/Arduino#installing-with-boards-manager
> // Required libraries (sketch -> include library -> manage libraries)
> // - PubSubClient by Nick ‘O Leary
> // - DHT sensor library by Adafruit
> 
> #include <ESP8266WiFi.h>
> #include "PubSubClient.h"
> #include <DHT.h>
> 
> #define wifi_ssid "ATT906"
> #define wifi_password "(my password)"
> 
> #define mqtt_server "192.168.1.88"  //this is the weather RPi
> #define mqtt_user ""
> #define mqtt_password ""
> 
> #define humidity_topic "sensor/humidity1"
> #define temperature_topic "sensor/temperature1"
> 
> #define DHTTYPE DHT22
> #define DHTPIN  13 // D7
> #define LEDPIN 5 // D1
> 
> WiFiClient espClient;
> PubSubClient client(espClient);
> DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
> 
> void setup() {
>   pinMode (LEDPIN, OUTPUT);
>   Serial.begin(115200);
>   dht.begin();
>   setup_wifi();
>   client.setServer(mqtt_server, 1883);
> }
> 
> void setup_wifi() {
>   delay(10);
>   // We start by connecting to a WiFi network
>   Serial.println();
>   Serial.print("Connecting to ");
>   Serial.println(wifi_ssid);
> 
>   WiFi.begin(wifi_ssid, wifi_password);
> 
>   while (WiFi.status() != WL_CONNECTED) {
>     delay(500);
>     Serial.print(".");
>   }
> 
>   Serial.println("");
>   Serial.println("WiFi connected");
>   Serial.println("IP address: ");
>   Serial.println(WiFi.localIP());
> }
> 
> void reconnect() {
>   // Loop until we're reconnected
>   while (!client.connected()) {
>     Serial.print("Attempting MQTT connection...");
>     // Attempt to connect
>     // If you do not want to use a username and password, change next line to
>     if (client.connect("ESP8266Client")) {
>     // if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
>       Serial.println("connected");
>     } else {
>       Serial.print("failed, rc=");
>       Serial.print(client.state());
>       Serial.println(" try again in 5 seconds");
>       // Wait 5 seconds before retrying
>       delay(5000);
>     }
>   }
> }
> 
> bool checkBound(float newValue, float prevValue, float maxDiff) {
>   return !isnan(newValue) &&
>          (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
> }
> 
> long lastMsg = 0;
> long fiveMinutes = 0;
> long ledToggle = 0;
> float temp = 0.0;
> float ftemp = 0.0;
> float hum = 0.0;
> float diff = 0.2;
> 
> void loop() {
> 
>   long now = millis();
>   if (now - lastMsg > 500) {
>     lastMsg = now;
>     if (!client.connected()) {
>       reconnect();
>     }
>     client.loop();
>     if (ledToggle == 0) {
>       digitalWrite(LEDPIN, HIGH);
>       ledToggle = 1;
>     } else {
>       digitalWrite(LEDPIN, LOW);
>       ledToggle = 0;
>     }
> 
>     float newTemp = dht.readTemperature();
>     float newHum = dht.readHumidity();
>     //Serial.print(temperature_topic);
>     //Serial.print(":");
>     //ftemp = newTemp * 1.8 + 32;
>     //Serial.print(ftemp);
>     //Serial.println(" F");
> 
>     if (checkBound(newTemp, temp, diff)) {
>       temp = newTemp;
>       Serial.print("New ");
>       Serial.print(temperature_topic);
>       Serial.print(":");
>       Serial.println(String(temp).c_str());
>       client.publish(temperature_topic, String(temp).c_str(), true);
>       ftemp = temp * 1.8 + 32;
>       Serial.print("in F:");
>       Serial.println(ftemp);
>     }
> 
>     // send temp every 5 minutes
>     if (now - fiveMinutes > 300000) {
>       fiveMinutes = now;
>       client.publish(temperature_topic, String(temp).c_str(), true);
>     }
> 
>     if (checkBound(newHum, hum, diff)) {
>       hum = newHum;
>       Serial.print("New ");
>       Serial.print(humidity_topic);
>       Serial.print(":");
>       Serial.println(String(hum).c_str());
>       client.publish(humidity_topic, String(hum).c_str(), true);
>     }
>   }
> }

I modified this code as follows:

> #define mqtt_server "192.168.8.43"  //this is the HA OS IP address
> #define mqtt_user "homeassistant"
> #define mqtt_password "(long string from configuration)"

The rest of the sketch is unchanged.

When I upload and run this sketch, the Serial Monitor shows:

WiFi connected
IP address:
192.168.8.48
Attempting MQTT connection…failed, rc=5 try again in 5 seconds

and this last line repeats endlessly. I assume there must be more I must include in my sketch to configure it to work with MQTT in HA but I cannot see what is needed. Can anyone help point me to what needs to be changed?

So lets start at the begining

your MQTT broker is setup and running a

dont have a user name “homeassistant” remeber reading somewhere in here that username does not work

start it go to the log TAB

have a read of check for no errors

dwonload MQTT Explorer that comes your friend

and buy drilling down you can see what happening

change the your sketch with the new user and password and it should work