mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
219 stars 25 forks source link

SoftAP mode and Broker #19

Closed ghost closed 9 months ago

ghost commented 9 months ago

Hi,

I need the broker independent from a router so I´d like to have the AP(-mode) and the broker on one device. I did not also set it to client mode, also I do not send from it.

I now have a setup with this ESP32-AP broker, an ESP connecting, and / or a phone connecting. All are on my table, should be no range problem. On both clients I have created loops to send one message all one or five seconds. Even with only one device connected to the AP and sending short MQTT messages (f.e. topic "phone/out" message "Broker test") all five seconds, I have delays. Starting from up to ~500ms all the time up to 30+ seconds no message is received by the broker, and then multiple messages are printed at once, happens all few minutes.

To check the incoming messages I am using the callback provided with the basic example. All I do in the loop is (have copied it together from multiple classes)

if (mTimerTest.IntervalReached()) // one dot all 200ms
    Serial.print(".");

mMqtt.loop(); 

have tried it with and without a short delay with no difference. (Timer has no now task and is only a lightweight millis()-check)

Setup reads ~

Serial.begin(115200);

WiFi.softAP(ssidName_, password_);  

delay(5000);

 mMqtt.subscribe("#", [](const char * topic, const char * payload) {
        Serial.printf("Received message in topic '%s': %s\n", topic, payload);
    });

    mMqtt.begin();

I have debug defined, Wifi shows me the connection and besides no callback is triggered indicating anything is wrong with it.

Is this normal / intended / known, or has someone an idea what could be the reason for the delays?

mlesniew commented 9 months ago

No, this is not normal, delays should be minimal and barely noticeable with this setup.

Could you check if this happens if you use this on the broker esp device:

#include <PicoMQTT.h>

PicoMQTT::Server mqtt;

void setup() {
    // Setup serial
    Serial.begin(115200);

    // Connect to WiFi
    WiFi.softAP("Foobar");

    mqtt.subscribe("#", [](const char * topic, const char * payload) {
        Serial.printf("Received message in topic '%s': %s\n", topic, payload);
    });

    mqtt.begin();
}

void loop() {
    mqtt.loop();
}
ghost commented 9 months ago

Thank you for your feedback. I have tested now some time with both codes. Sometimes I have delays, sometimes it runs quite fine. By setting the Wlan channel the problem went away or increased, so It seems it was indeed connection related.

Thanks again, also for this very useful library! :)