From 24ee6b5201b4b0c5bebedaa15a3effc5c211a750 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Sun, 11 Apr 2021 16:33:12 +0200 Subject: [PATCH] Fixes in WiFiClient mode --- src/TinyMqtt.cpp | 28 +++++++++++++++++----------- src/TinyMqtt.h | 16 ++++++++++++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 3b51ddc..7171f4a 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -28,12 +28,15 @@ MqttBroker::~MqttBroker() // private constructor used by broker only MqttClient::MqttClient(MqttBroker* parent, TcpClient* new_client) - : parent(parent), client(new_client) + : parent(parent) { #ifdef TCP_ASYNC + client = new_client; client->onData(onData, this); // client->onConnect() TODO // client->onDisconnect() TODO +#else + client = new WiFiClient(*new_client); #endif alive = millis()+5000; // client expires after 5s if no CONNECT msg } @@ -80,15 +83,18 @@ void MqttClient::connect(std::string broker, uint16_t port, uint16_t ka) close(); if (client) delete client; client = new TcpClient; + + debug("Trying to connect to " << broker.c_str() << ':' << port); +#ifdef TCP_ASYNC client->onData(onData, this); client->onConnect(onConnect, this); - debug("Trying to connect to " << broker.c_str() << ':' << port); + client->connect(broker.c_str(), port); +#else if (client->connect(broker.c_str(), port)) - { - #ifndef TCP_ASYNC - onConnect(this, client); - #endif - } + { + onConnect(this, client); + } +#endif } void MqttBroker::addClient(MqttClient* client) @@ -129,13 +135,13 @@ void MqttBroker::onClient(void* broker_ptr, TcpClient* client) MqttBroker* broker = static_cast(broker_ptr); broker->addClient(new MqttClient(broker, client)); - debug("New client #" << broker->clients.size()); + debug("New client"); } void MqttBroker::loop() { #ifndef TCP_ASYNC - WiFiClient client = server.available(); + WiFiClient client = server->available(); if (client) { @@ -267,7 +273,7 @@ void MqttClient::loop() } } -void MqttClient::onConnect(void *mqttclient_ptr, AsyncClient*) +void MqttClient::onConnect(void *mqttclient_ptr, TcpClient*) { MqttClient* mqtt = static_cast(mqttclient_ptr); debug("cnx: connecting"); @@ -288,7 +294,7 @@ void MqttClient::onConnect(void *mqttclient_ptr, AsyncClient*) } #ifdef TCP_ASYNC -void MqttClient::onData(void* client_ptr, AsyncClient*, void* data, size_t len) +void MqttClient::onData(void* client_ptr, TcpClient*, void* data, size_t len) { char* char_ptr = static_cast(data); MqttClient* client=static_cast(client_ptr); diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index 7cf31d3..006c01e 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -1,6 +1,14 @@ #pragma once + +// TODO Should add a AUnit with both TCP_ASYNC and not TCP_ASYNC +// #define TCP_ASYNC // Uncomment this to use ESPAsyncTCP instead of normal cnx + #ifdef ESP8266 - #include + #ifdef TCP_ASYNC + #include + #else + #include + #endif #elif defined(ESP32) #include #include // https://github.com/me-no-dev/AsyncTCP @@ -16,14 +24,14 @@ #include // #define TINY_MQTT_DEBUG +#define TINY_MQTT_DEBUG + #ifdef TINY_MQTT_DEBUG #define debug(what) { Serial << __LINE__ << ' ' << what << endl; delay(100); } #else #define debug(what) {} #endif -#define TCP_ASYNC - #ifdef TCP_ASYNC using TcpClient = AsyncClient; using TcpServer = AsyncServer; @@ -201,8 +209,8 @@ class MqttClient static long counter; private: -#ifdef TCP_ASYNC static void onConnect(void * client_ptr, TcpClient*); +#ifdef TCP_ASYNC static void onData(void* client_ptr, TcpClient*, void* data, size_t len); #endif MqttError sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos);