From 122ab8896049890f65f3dc475bff4754408742f6 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 11 Apr 2021 23:26:49 +0200 Subject: [PATCH] Rewrite client-with-wifi.ino --- .../client-with-wifi/client-with-wifi.ino | 77 +++++++++++-------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/examples/client-with-wifi/client-with-wifi.ino b/examples/client-with-wifi/client-with-wifi.ino index d8c5ba7..1f6cce6 100644 --- a/examples/client-with-wifi/client-with-wifi.ino +++ b/examples/client-with-wifi/client-with-wifi.ino @@ -1,22 +1,31 @@ #include // https://github.com/hsaturn/TinyMqtt /** - * Local broker that accept connections and two local clients - * + * + * +-----------------------------+ + * | ESP | + * | +--------+ | 1883 <--- External client/s + * | +-------->| broker | | 1883 <--- External client/s + * | | +--------+ | + * | | ^ | + * | | | | + * | v v | + * | +----------+ +----------+ | + * | | internal | | internal | | + * | | client | | client | | + * | +----------+ +----------+ | + * | | + * +-----------------------------+ + * * pros - Reduces internal latency (when publish is received by the same ESP) * - Reduces wifi traffic * - No need to have an external broker - * - can still report to a 'main' broker (TODO see documentation that have to be written) - * - accepts external clients + * - can still report to a 'main' broker (TODO see documentation that have to be written) + * - accepts external clients * * cons - Takes more memory - * - a bit hard to understand - * - * This sounds crazy: a mqtt mqtt that do not need a broker ! - * The use case arise when one ESP wants to publish topics and subscribe to them at the same time. - * Without broker, the ESP won't react to its own topics. - * - * TinyMqtt mqtt allows this use case to work. + * - a bit hard to understand + * */ #include @@ -37,8 +46,8 @@ void onPublishB(const MqttClient* source, const Topic& topic, const char* payloa void setup() { Serial.begin(115200); - delay(500); - Serial << "Clients with wifi " << endl; + delay(500); + Serial << "Clients with wifi " << endl; WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); @@ -47,8 +56,8 @@ void setup() Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl; - broker.begin(); - + broker.begin(); + mqtt_a.setCallback(onPublishA); mqtt_a.subscribe(topic); @@ -58,30 +67,30 @@ void setup() void loop() { - broker.loop(); + broker.loop(); // Don't forget to add loop for every broker and clients mqtt_a.loop(); mqtt_b.loop(); // ============= client A publish ================ - static const int intervalA = 5000; // publishes every 5s - static uint32_t timerA = millis() + intervalA; - - if (millis() > timerA) - { - Serial << "A is publishing " << topic.c_str() << endl; - timerA += intervalA; - mqtt_a.publish(topic); - } + static const int intervalA = 5000; // publishes every 5s + static uint32_t timerA = millis() + intervalA; + + if (millis() > timerA) + { + Serial << "A is publishing " << topic.c_str() << endl; + timerA += intervalA; + mqtt_a.publish(topic); + } // ============= client B publish ================ - static const int intervalB = 7000; // will send topic each 7s - static uint32_t timerB = millis() + intervalB; - - if (millis() > timerB) - { - Serial << "B is publishing " << topic.c_str() << endl; - timerB += intervalB; - mqtt_b.publish(topic, std::string(String(15+millis()%10).c_str())); - } + static const int intervalB = 7000; // will send topic each 7s + static uint32_t timerB = millis() + intervalB; + + if (millis() > timerB) + { + Serial << "B is publishing " << topic.c_str() << endl; + timerB += intervalB; + mqtt_b.publish(topic, std::string(String(15+millis()%10).c_str())); + } }