# ESP8266 MQTT Callback function not called

**URL:** <https://forum.arduino.cc/t/esp8266-mqtt-callback-function-not-called/851580>\
**Category:** Programming\
**Created:** [April 21, 2021, 7:48am UTC](https://forum.arduino.cc/t/esp8266-mqtt-callback-function-not-called/851580 "2021-04-21T07:48:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yscheol](https://avatars.discourse-cdn.com/v4/letter/y/b38774/32.png) [@yscheol](https://forum.arduino.cc/u/yscheol)\
**Post date:** [April 21, 2021, 7:48am UTC](https://forum.arduino.cc/t/esp8266-mqtt-callback-function-not-called/851580/1 "2021-04-21T07:48:26Z")

</div>

Hello

I am using esp8266 and MQTT.  
I'm going to use publish and subscribe to output sensor values and switch toggle.  
Outputting with 'publish' works well. However, 'subscribe' is ignored. Maybe the callback function is not called.

This is part of my code.

```cpp
#include <ESP8266WiFi.h>
#include <PubSubClient.h> //MQTT

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

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);

  //WiFi Server
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)  
  {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  client.setServer(mqtt_server, 1883); 
  client.setCallback(callback); //MOTT receive callback
}

void loop() {
  if (!client.connected()) //서버 연결 끊기면 다시 연결 대기
  {
    reconnect();
  }
.
.
.
        //Send to MQTT sensor value (success)
        sprintf(bpm_msg, "BMP : %d", beatAvg);
        client.publish("/bpm", bpm_msg); 
        sprintf(spo2_msg, "SPO2 : %d", SPO2f);
        client.publish("/spo2", spo2_msg);
}

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Try MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client"))
    {
      Serial.println("connected");
      client.subscribe("/mode"); //received from mode topic
    }

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

void callback(char* topic, byte* payload, unsigned int length) //call when received?
{
  Serial.print("Message arrived ["); //Not outputed!!
  Serial.print(topic);
  Serial.print("] ");
  String message;

  for (int i = 0; i < length; i++)
  {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  //MQTT received
  if (topic == '/mode') //mode토픽에서 수신
  {
    Serial.print("Status : ");
    if ((char)payload[0] == 'on')
    {
      mode_state = 1;
      Serial.println("ON");
    }

    if ((char)payload[0] == 'off')
    {
      mode_state = 0;
      Serial.println("OFF");
    }
  }
}

```

And I used MQTT in Node RED.

![image](https://europe1.discourse-cdn.com/arduino/original/4X/6/a/e/6ae9ed39206cbecca433c26ec52ffb6de08c63a6.png)

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/a/6/7a64cd1e98e46be4db400a8dcb84fed3c178a4ba.png)

![image](https://europe1.discourse-cdn.com/arduino/original/4X/7/7/9/779c3e85a86f55709aa6e4c0196b61346a2bf9d2.png)

On the dashboard, switch on/off control works well and outputs well to debug mqtt.

Can I get some help with this problem?  
Thank you.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [April 21, 2021, 7:59am UTC](https://forum.arduino.cc/t/esp8266-mqtt-callback-function-not-called/851580/2 "2021-04-21T07:59:03Z")

</div>

> [@yscheol](#):
>
> ` Serial.print("Message arrived ["); //Not outputed!!`

Often, Serial.print() will not work reliably in a call back routine, depending on how it is initiated.  
You have to find some other way of debugging that routine.

---

<div class="post-metadata">

**Author:** ![yscheol](https://avatars.discourse-cdn.com/v4/letter/y/b38774/32.png) [@yscheol](https://forum.arduino.cc/u/yscheol)\
**Post date:** [April 21, 2021, 9:09am UTC](https://forum.arduino.cc/t/esp8266-mqtt-callback-function-not-called/851580/3 "2021-04-21T09:09:14Z")

</div>

Thank you for your answer!

I replaced ESP8266 with a different board and it worked strangely well. I'll have to test it a little bit more.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 19, 2021, 9:09am UTC](https://forum.arduino.cc/t/esp8266-mqtt-callback-function-not-called/851580/4 "2021-08-19T09:09:44Z")

</div>

Closed on August 19, 2021, 9:09am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
