Problem with MQTT (Socket error) using Pubsub library

Hi All,
I have a have a powermonitor that suddenly stopped working after I added a feature. I have not been able to figure out what happened but what ever I do I keep getting a socket error, anyone that can help?
The output I get from CloudMQTT is the following:
2019-08-18 14:51:49: New client connected from 77.213.84.230 as Hest1 (c1, k15, u'uecaymxu').
2019-08-18 14:51:49: No will message specified.
2019-08-18 14:51:49: Sending CONNACK to Hest1 (0, 0)
2019-08-18 14:51:49: Received SUBSCRIBE from Hest1
2019-08-18 14:51:49: Entrance/sensor1 (QoS 0)
2019-08-18 14:51:49: Sending SUBACK to Hest1
2019-08-18 14:51:49: Socket error on client Hest1, disconnecting.

The code is too long so I have attached it instead and below is a snipped from the publishing part

  publishData(watt, kwh, ledflag);

}
void publishData(float p_watt, float p_kwh, float p_ledflag) {

  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  // INFO: the data must be converted into a string; a problem occurs when using floats...
  root["watt"] = (String)p_watt;
  root["kwh"] = (String)p_kwh;
  root["Charger_ready"] = (String)p_ledflag;
  root.prettyPrintTo(Serial);

  char data[200];
  root.printTo(data, root.measureLength() + 1);
  client.publish(MQTT_SENSOR_TOPIC, data, true);

  yield();

}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  Serial.print("Message arrived [");
  Serial.print(p_topic);
  Serial.print("] ");
  char message[p_length + 1];
  for (int i = 0; i < p_length; i++) {
    message[i] = (char)p_payload[i];
  }
  message[p_length] = '\0';
  Serial.println(message);

  if (!processJson(message)) {
    return;
  }


}



void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("INFO: Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
      Serial.println("INFO: connected");
      client.subscribe(MQTT_SENSOR_TOPIC);
    } else {
      Serial.print("ERROR: failed, rc=");
      Serial.print(client.state());
      Serial.println("DEBUG: try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
/********************************** START PROCESS JSON*****************************************/
bool processJson(char* message) {
  StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;

  JsonObject& root = jsonBuffer.parseObject(message);

  if (!root.success()) {
    Serial.println("parseObject() failed");
    return false;
  }
  if (root.containsKey("kwh")) {
    kwh_from_HA = root["kwh"];
    LEDstate_from_HA = root["Charger_ready"];
    callback_after_restart = 1;
  }
  return true;
}

current-mqtt-for-forum.ino (9.6 KB)

I have a have a powermonitor that suddenly stopped working after I added a feature.

And why don't you tell us what feature that was?

    ThingSpeak.begin(wifiClient);

You must not use the same client for PubSub library and ThingSpeak.

    Serial.println("INFO: Closing the MQTT connection");
    client.disconnect();

Why do you actively discconect here?

Is that error reproducible? Do you always get exactly the same log output?

Dear Pylon and everybody else, beside smaller issues with the sketch, the source of the issue was that the loop did not contain a delay. So if the fix was
delay(500);
:slight_smile:
/Mogwaiz