knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.84k stars 1.47k forks source link

client.publish modify the sending data? #404

Open z4nD4R opened 6 years ago

z4nD4R commented 6 years ago

Hello everyone,

I'm using pubsubclient and works without issue, except one which I'm not sure if coupled with the lib itself. On my ESP32 I'm running simple code to publish humidity and temperature to MQTT broker. After some time the weird values are sending to MQTT. The interesting thing is, when I comment the code about publishing it works withou issue - no weird values are displayed.

My code:

void loop()
{
  LoopCounter++;
  client.loop();
  getDHT();
  if (LoopCounter > 10)
  {
    if (!client.connected())
    {
      connect_mqtt();
    }
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("Publishing");
    sprintf(hum,"analog_actuator=%4g",localHum);
    Serial.print(hum);Serial.print(", "); Serial.println(localHum);
    client.publish("v1/some/topic/data/151",hum); // if I commet this line no weird sensor data anymore
    sprintf(temp,"analog_actuator=%4g",localTemp);
    delay(100);
    Serial.print(temp);Serial.print(", "); Serial.println(localTemp);
    client.publish("v1/some/topic/data/150",temp); // if I commet this line no weird sensor data anymore
    LoopCounter = 0;
    digitalWrite(LED_BUILTIN, LOW);
  }
  delay(2000);
}

I'd appreciate any help.

Y

slavino commented 6 years ago

first of all - I wouldn't use delay(x) in loop();

Checkout internal timers for ESP platforms. https://github.com/marvinroger/async-mqtt-client/blob/master/examples/FullyFeatured-ESP32/FullyFeatured-ESP32.ino

z4nD4R commented 6 years ago

@slavino , not sure if coupled with the issue but could be. What kind of timer would you recomend to use?

Y