Fixes in WiFiClient mode
This commit is contained in:
@@ -28,12 +28,15 @@ MqttBroker::~MqttBroker()
|
|||||||
|
|
||||||
// private constructor used by broker only
|
// private constructor used by broker only
|
||||||
MqttClient::MqttClient(MqttBroker* parent, TcpClient* new_client)
|
MqttClient::MqttClient(MqttBroker* parent, TcpClient* new_client)
|
||||||
: parent(parent), client(new_client)
|
: parent(parent)
|
||||||
{
|
{
|
||||||
#ifdef TCP_ASYNC
|
#ifdef TCP_ASYNC
|
||||||
|
client = new_client;
|
||||||
client->onData(onData, this);
|
client->onData(onData, this);
|
||||||
// client->onConnect() TODO
|
// client->onConnect() TODO
|
||||||
// client->onDisconnect() TODO
|
// client->onDisconnect() TODO
|
||||||
|
#else
|
||||||
|
client = new WiFiClient(*new_client);
|
||||||
#endif
|
#endif
|
||||||
alive = millis()+5000; // client expires after 5s if no CONNECT msg
|
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();
|
close();
|
||||||
if (client) delete client;
|
if (client) delete client;
|
||||||
client = new TcpClient;
|
client = new TcpClient;
|
||||||
|
|
||||||
|
debug("Trying to connect to " << broker.c_str() << ':' << port);
|
||||||
|
#ifdef TCP_ASYNC
|
||||||
client->onData(onData, this);
|
client->onData(onData, this);
|
||||||
client->onConnect(onConnect, 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))
|
if (client->connect(broker.c_str(), port))
|
||||||
{
|
{
|
||||||
#ifndef TCP_ASYNC
|
|
||||||
onConnect(this, client);
|
onConnect(this, client);
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttBroker::addClient(MqttClient* client)
|
void MqttBroker::addClient(MqttClient* client)
|
||||||
@@ -129,13 +135,13 @@ void MqttBroker::onClient(void* broker_ptr, TcpClient* client)
|
|||||||
MqttBroker* broker = static_cast<MqttBroker*>(broker_ptr);
|
MqttBroker* broker = static_cast<MqttBroker*>(broker_ptr);
|
||||||
|
|
||||||
broker->addClient(new MqttClient(broker, client));
|
broker->addClient(new MqttClient(broker, client));
|
||||||
debug("New client #" << broker->clients.size());
|
debug("New client");
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttBroker::loop()
|
void MqttBroker::loop()
|
||||||
{
|
{
|
||||||
#ifndef TCP_ASYNC
|
#ifndef TCP_ASYNC
|
||||||
WiFiClient client = server.available();
|
WiFiClient client = server->available();
|
||||||
|
|
||||||
if (client)
|
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*>(mqttclient_ptr);
|
MqttClient* mqtt = static_cast<MqttClient*>(mqttclient_ptr);
|
||||||
debug("cnx: connecting");
|
debug("cnx: connecting");
|
||||||
@@ -288,7 +294,7 @@ void MqttClient::onConnect(void *mqttclient_ptr, AsyncClient*)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef TCP_ASYNC
|
#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<char*>(data);
|
char* char_ptr = static_cast<char*>(data);
|
||||||
MqttClient* client=static_cast<MqttClient*>(client_ptr);
|
MqttClient* client=static_cast<MqttClient*>(client_ptr);
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
#pragma once
|
#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
|
#ifdef ESP8266
|
||||||
|
#ifdef TCP_ASYNC
|
||||||
#include <ESPAsyncTCP.h>
|
#include <ESPAsyncTCP.h>
|
||||||
|
#else
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#endif
|
||||||
#elif defined(ESP32)
|
#elif defined(ESP32)
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
|
#include <AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
|
||||||
@@ -16,14 +24,14 @@
|
|||||||
#include <MqttStreaming.h>
|
#include <MqttStreaming.h>
|
||||||
|
|
||||||
// #define TINY_MQTT_DEBUG
|
// #define TINY_MQTT_DEBUG
|
||||||
|
#define TINY_MQTT_DEBUG
|
||||||
|
|
||||||
#ifdef TINY_MQTT_DEBUG
|
#ifdef TINY_MQTT_DEBUG
|
||||||
#define debug(what) { Serial << __LINE__ << ' ' << what << endl; delay(100); }
|
#define debug(what) { Serial << __LINE__ << ' ' << what << endl; delay(100); }
|
||||||
#else
|
#else
|
||||||
#define debug(what) {}
|
#define debug(what) {}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define TCP_ASYNC
|
|
||||||
|
|
||||||
#ifdef TCP_ASYNC
|
#ifdef TCP_ASYNC
|
||||||
using TcpClient = AsyncClient;
|
using TcpClient = AsyncClient;
|
||||||
using TcpServer = AsyncServer;
|
using TcpServer = AsyncServer;
|
||||||
@@ -201,8 +209,8 @@ class MqttClient
|
|||||||
static long counter;
|
static long counter;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#ifdef TCP_ASYNC
|
|
||||||
static void onConnect(void * client_ptr, TcpClient*);
|
static void onConnect(void * client_ptr, TcpClient*);
|
||||||
|
#ifdef TCP_ASYNC
|
||||||
static void onData(void* client_ptr, TcpClient*, void* data, size_t len);
|
static void onData(void* client_ptr, TcpClient*, void* data, size_t len);
|
||||||
#endif
|
#endif
|
||||||
MqttError sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos);
|
MqttError sendTopic(const Topic& topic, MqttMessage::Type type, uint8_t qos);
|
||||||
|
|||||||
Reference in New Issue
Block a user