title: NodeMCU esp8266 using PubSubClient.h callback function gets ignored
body: <p><strong>EDIT</strong>: The topics to publish and subscribe are correct even if they look different. with my publish, I writing on the field 4. Moreover I run also MQTT.fx in sub/pub mode with the same topics and it works perfectly.</p>
  
  <p>I want to use mqtt with my NodeMCU with esp8266. I adapted <a href="https://github.com/knolleary/pubsubclient/blob/master/examples/mqtt_esp8266/mqtt_esp8266.ino" rel="nofollow noreferrer">simple MQTT example</a> to my minimal project. In my code (attached below) I'm trying to publish and subscribe to a ThingSpeak channel. The publish function does not have any problem, it publishes every time it is called. But the callback function is simply ignored, my code never enters in it.</p>
  
  <p>To give more infor here you have My settings:
  <a href="https://i.stack.imgur.com/VzxMK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VzxMK.png" alt="settings"></a></p>
  
  <pre><code>#include &lt;ESP8266WiFi.h&gt;
  #include &lt;PubSubClient.h&gt;
  
  // Update these with values suitable for your network.
  
  const char* ssid = "*****";
  const char* password = "*****";
  const char* mqtt_server = "mqtt.thingspeak.com";
  
  WiFiClient espClient;
  PubSubClient client(espClient);
  long lastMsg = 0;
  char msg[50];
  int value = 0;
  
  void setup_wifi() {
  
    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(".");
    }
  
    randomSeed(micros());
  
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
  
  void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Ciao sono la callback1\n");
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (int i = 0; i &lt; length; i++) {
      Serial.print((char)payload[i]);
    }
    Serial.println();
  
    // Switch on the LED if an 1 was received as first character
    if ((char)payload[0] == '1') {
      Serial.print("Ciao sono la callback2\n");
      digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
      // but actually the LED is on; this is because
      // it is acive low on the ESP-01)
    } else {
      Serial.print("Ciao sono la callback3\n");
      digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
    }
  
  }
  
  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())) {
        Serial.println("connected");
        // Once connected, publish an announcement...
        client.publish("channels/517055/publish/V4ZW4Y01ZLFD5XYR", "field5=7&amp;status=MQTTPUBLISH");
        // ... and resubscribe
        client.subscribe("channels/517055/subscribe/fields/field4");
        Serial.print("Ciao sono subscribe 01\n");
      } else {
        Serial.print("failed, rc=");
        Serial.print(client.state());
        Serial.println(" try again in 5 seconds");
        // Wait 5 seconds before retrying
        delay(5000);
      }
    }
  }
  
  void setup() {
    pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
    Serial.begin(115200);
    setup_wifi();
    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);
    Serial.print("Ciao sono la callback 10\n");
    client.subscribe("channels/517055/subscribe/fields/field4");
    Serial.print("Ciao subscribe 10\n");
  }
  
  void loop() {
  
    if (!client.connected()) {
      reconnect();
    }
    client.loop();
  
    long now = millis();
    if (now - lastMsg &gt; 2000) {
      lastMsg = now;
      ++value;
      snprintf (msg, 75, "field4= %ld&amp;status=MQTTPUBLISH", value);
      Serial.print("Publish message: ");
      Serial.println(msg);
      client.publish("channels/517055/publish/V4ZW4Y01ZLFD5XYR", msg);
    }
  }
  </code></pre>
  
  <p>Moreover here you have my serial monitor output:</p>
  
  <pre><code>Connecting to ZICHICHI
  scandone
  scandone
  state: 0 -&gt; 2 (b0)
  state: 2 -&gt; 3 (0)
  state: 3 -&gt; 5 (10)
  add 0
  aid 1
  cnt 
  
  connected with ZICHICHI, channel 1
  dhcp client start...
  ............ip:192.168.1.68,mask:255.255.255.0,gw:192.168.1.254
  .
  WiFi connected
  IP address: 
  192.168.1.68
  Ciao sono la callback 10
  Ciao subscribe 10
  Attempting MQTT connection...connected
  Ciao sono subscribe 01
  Publish message: field4= 1&amp;status=MQTTPUBLISH
  Publish message: field4= 2&amp;status=MQTTPUBLISH
  pm open,type:2 0
  Publish message: field4= 3&amp;status=MQTTPUBLISH
  Publish message: field4= 4&amp;status=MQTTPUBLISH
  Publish message: field4= 5&amp;status=MQTTPUBLISH
  Publish message: field4= 6&amp;status=MQTTPUBLISH
  Publish message: field4= 7&amp;status=MQTTPUBLISH
  Publish message: field4= 8&amp;status=MQTTPUBLISH
  Publish message: field4= 9&amp;status=MQTTPUBLISH
  Publish message: field4= 10&amp;status=MQTTPUBLISH
  Publish message: field4= 11&amp;status=MQTTPUBLISH
  Publish message: field4= 12&amp;status=MQTTPUBLISH
  Publish message: field4= 13&amp;status=MQTTPUBLISH
  Publish message: field4= 14&amp;status=MQTTPUBLISH
  Publish message: field4= 15&amp;status=MQTTPUBLISH
  Publish message: field4= 16&amp;status=MQTTPUBLISH
  Publish message: field4= 17&amp;status=MQTTPUBLISH
  </code></pre>
  
  <p>I hope someone know what I made wrong.</p>