hsaturn / TinyMqtt

ESP 8266 / 32 / WROOM Small footprint Mqtt Broker and Client
GNU General Public License v3.0
198 stars 41 forks source link

Client life, local to broker, too short #62

Closed hsaturn closed 1 year ago

hsaturn commented 1 year ago

Hello

I'm starting this new issue and close others in order to solve this.

I'm running a setup for the day on the main branch (not a release). If this evening this setup still runs, then I'll close all timeout related issue and will release a new version of TinyMqtt

My setup is


The ESP Broker sketch, derived from client-with-wifi (as alranel said in previous discussion):

#include <TinyMqtt.h>   // https://github.com/hsaturn/TinyMqtt

const char *ssid     = "... my ssid ... ";
const char *password = "... my password ...";

std::string topic="sensor/temperature";

MqttBroker broker(1883);
MqttClient client(&broker);

void onPublish(const MqttClient* /* source */, const Topic& topic, const char* payload, size_t /* length */)
{ Serial << "--> client A received " << topic.c_str() << ", " << payload << endl; }

void setup()
{
  Serial.begin(115200);
  delay(500);
  Serial << "Clients with wifi " << endl;

    if (strlen(ssid)==0)
        Serial << "****** PLEASE EDIT THE EXAMPLE AND MODIFY ssid/password *************" << endl;

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {   Serial << '-'; delay(500); }

  Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;

  broker.begin();

  client.setCallback(onPublish);
  client.subscribe(topic);
}

void loop()
{
  broker.loop();  // Don't forget to add loop for every broker and clients

  client.loop();
}

The ESP Client sketch, directly derived from simple-client.ino example:

#include "TinyMqtt.h"    // https://github.com/hsaturn/TinyMqtt
#include "TinyStreaming.h" // https://github.com/hsaturn/TinyConsole

const char* BROKER = "192.168.1.33";  <--- THE IP OF THE ESP 8266 BROKER
const uint16_t BROKER_PORT = 1883;

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

static float temp=19;
static MqttClient client;

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

    Serial << "Simple clients with wifi " << endl;

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
    { delay(500); Serial << '.'; }

  Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;

    client.connect(BROKER, BROKER_PORT);
}

void loop()
{
    client.loop();  // Don't forget to call loop() for each broker and client

    // delay(1000);     please avoid usage of delay (see below how this done using next_send and millis())
    static auto next_send = millis();

    if (millis() > next_send)
    {
        if (not client.connected())
        {
            Serial << millis() << ": Not connected to broker" << endl;
      next_send += 1000;
            return;
        }
        next_send += 1000+random(900*1000);
    Serial << "next send in " << (next_send/1000) << "sec." << endl;

        auto rnd=random(100);

        if (rnd > 66) temp += 0.1;
        else if (rnd < 33) temp -= 0.1;

        Serial << "--> Publishing a new sensor/temperature value: " << temp << endl;

        client.publish("sensor/temperature", String(temp));
    }
}
hsaturn commented 1 year ago

My two esp's worked all the day. The client published a topic randomly every 15 mins without any disconnection.

17:20:09.656 -> --> client A received sensor/temperature, 18.10 17:34:32.879 -> --> client A received sensor/temperature, 18.00 17:44:16.089 -> --> client A received sensor/temperature, 18.00 17:52:38.903 -> --> client A received sensor/temperature, 18.10 17:57:17.112 -> --> client A received sensor/temperature, 18.10 18:02:39.064 -> --> client A received sensor/temperature, 18.20 18:11:14.041 -> --> client A received sensor/temperature, 18.30 18:26:05.190 -> --> client A received sensor/temperature, 18.40 18:33:17.603 -> --> client A received sensor/temperature, 18.30 18:37:01.366 -> --> client A received sensor/temperature, 18.20 18:48:42.512 -> --> client A received sensor/temperature, 18.30 18:55:35.435 -> --> client A received sensor/temperature, 18.40 19:01:59.265 -> --> client A received sensor/temperature, 18.30 19:12:45.374 -> --> client A received sensor/temperature, 18.20 19:25:11.875 -> --> client A received sensor/temperature, 18.20 19:30:00.177 -> --> client A received sensor/temperature, 18.20

So I'm closing this issue

alranel commented 1 year ago

I have been testing for 48 hours and I confirm it works for me as well without timeouts. Thank you, great job!

hsaturn commented 1 year ago

Hmmm, I've just fixed today a timeout issue in a specific case (broker connected to another broker). But this should impact every single client connection. The Mqtt Specs says that 0 is a value accepted for keep_alive (infinite). This should work in other cases.