mlesniew / PicoMQTT

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

picomqtt in bridge mode? #26

Closed Mit4el closed 8 months ago

Mit4el commented 9 months ago

is it possible to configure picomqtt in bridge mode? to send it to the mosquito. if possible, an example

mlesniew commented 8 months ago

Sure, that can be done with PicoMQTT.

A very simple implementation could look more or less like this:

#include <PicoMQTT.h>

PicoMQTT::Client client("broker.hivemq.com");
PicoMQTT::Server server;

void setup() {
    server.subscribe("#", [](const char * topic, const char * message) {
        client.publish(topic, message);
     });

    client.subscribe("#", [](const char * topic, const char * message) {
        server.publish(topic, message);
    });
}

void loop() {
    client.loop();
    server.loop();
}

This should forward all messages published on the upstream broker to local clients and all messages published by local clients to the upstream broker.

luotope commented 8 months ago

Hi and thanks for the great software. I need exactly this solution where I'm having esp32 as a 'mid' broker in a separate system and which delivers messages also to my main broker and UI. The clients are esp8266 modules.

My problem now is that these esp8266 modules need to know the ip of esp32 broker in order to connect to it. I would rather like to find out the ip via MDNS query, but all your examples show, however, that the server object address is given while creating the client object (PicoMQTT::Client client("broker.hivemq.com").

How can I set the broker ip in setup? PubSubClient for example is having a member setServer (mqtt.setServer(mqtt_serverIP, port)).

Thanks Petri

mlesniew commented 8 months ago

You can create a matt client and simply provide an IP as string:

PicoMQTT::Client client("192.168.1.99");

mDNS addresses like "esp8266.local" should work too.

luotope commented 8 months ago

I still don't understand how I make client global if I declare it in setup. I can find the ip only after MDNS query in setup.

So this works fine: PicoMQTT::Client client("192.168.1.133");
void setup() { Serial.begin(115200); .....

But this (simplified) do not, as client will not be global void setup() { Serial.begin(115200); MDNS.queryService("esp32", "tcp"); PicoMQTT::Client client(MDNS.hostname(0)); // host name or ip address
.....

Thanks for your help

mlesniew commented 8 months ago

PicoMQTT::Client must be global.

You can still set the broker address, port and credentials in the setup function by setting the corresponding public instance variables of the client object.

luotope commented 8 months ago

Ok. I found those variables and the solution is now working! Thanks.

EDITED: When publishing ESP32's own gpio status, I need to publish it twice: using client.publish to send to my main broker and using server.publish for message to be visible in local network.

Now it works in my quite complicated environment as expected.

Thanks again.

mlesniew commented 8 months ago

Great it works for you now!