MQTT not receiving messages from ESP-01s

Hi all, any help with this would be much appreciated!

I’m working on a project using the ESP-01s module and would like to publish some sensor readings from the ESP to Home assistant, Using the SubPubClient library on the ESP and the example code provided i have established a connection to the broker which i can confirm from Mosquitto broker logs where the ESP comes up as connected but the values published by ESP are not arriving to Home Assitant, I can see that by subscribing to ‘#’ in Integrations > MQTT broker.

The strangest thing is i was struggling to get it working where at one point it started working and day later stopped working again without any changes made, please see below my ESP code where Im essentially trying to publish the incremented value as a test.

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

// Update these with values suitable for your network.

const char* ssid = "xx";
const char* password = "xx";
const char* mqtt_server = "xx";

#define temp_topic "outTopic/temp1"
#define in_topic "inTopic/setTemp1"

WiFiClient espClient;
PubSubClient client(espClient);
#define MSG_BUFFER_SIZE	(50)
char msg[MSG_BUFFER_SIZE];
int val = 0;

long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
float diff = 0.5;
float set_temp = 23.0;

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: ");

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

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      // Once connected, publish an announcement...
      client.publish(temp_topic, "Connected");
      // ... and resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  client.setServer(mqtt_server, 1883);

 void loop() {
  if (!client.connected()) {
  long now = millis();
  if (now - lastMsg > 2000){
        temp = val;
        client.publish(temp_topic, String(temp).c_str(), true);
        lastMsg = now;

You could do this whole project in about 30 seconds with ESPHome.

I’m aware of this, I choose not to use ESpHome as the actual sensors are based on an Arduino nano 33 BLE sense and the data is sent to the ESP over serial and as I’m not familiar with ESPHome I choose to stick with this method. This should work and it did work but due to reasons unknown to me the messages are not being received.

Help with this would be much appreciated.

Do mean the values are not being published to the broker?

If so, you may get a quicker response to your issue on an Arduino forum.

Yes, I can see the ESP connects to the broker from both the esp serial monitor and in mosquitto broker Logs the IP address of the ESP connects, but no messages are being received by the broker when I publish using the ESP.

How are you verifying that?

using only HA to subscribe to the topics or are you using an MQTT sniffer program?

I recommend the latter since it will eliminate HA being the problem since it connects directly to the broker itself bypassing HA.

I use MQTTFx and/or MQTT Explorer for those tasks.

I’m checking that by subscribing to # in Integrations > MQTT broker. This should show all messages received by the broker.

i will test this using a MQTT sniffer program now.

Not if the home assistant to broker connection is the issue.

1 Like

Thank for the tip, I used MQTT Explorer which didnt work and found out the issue was in Home Assistant - mqtt connection. the issue was in the config file of mqtt broker and now operates as inteneded!

Thanks for the help!

1 Like