ESP-MQTT
This commit is contained in:
@@ -7,7 +7,7 @@ MqttLogger::MqttLogger(MqttLoggerMode mode)
|
||||
this->setBufferSize(MQTT_MAX_PACKET_SIZE);
|
||||
}
|
||||
|
||||
MqttLogger::MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode)
|
||||
MqttLogger::MqttLogger(esp_mqtt_client_handle_t client, const char* topic, MqttLoggerMode mode)
|
||||
{
|
||||
this->setClient(client);
|
||||
this->setTopic(topic);
|
||||
@@ -19,9 +19,9 @@ MqttLogger::~MqttLogger()
|
||||
{
|
||||
}
|
||||
|
||||
void MqttLogger::setClient(MqttClient& client)
|
||||
void MqttLogger::setClient(esp_mqtt_client_handle_t client)
|
||||
{
|
||||
this->client = &client;
|
||||
this->client = client;
|
||||
}
|
||||
|
||||
void MqttLogger::setTopic(const char* topic)
|
||||
@@ -69,21 +69,22 @@ boolean MqttLogger::setBufferSize(uint16_t size)
|
||||
}
|
||||
|
||||
// send & reset current buffer
|
||||
void MqttLogger::sendBuffer()
|
||||
void MqttLogger::sendBuffer()
|
||||
{
|
||||
if (this->bufferCnt > 0)
|
||||
{
|
||||
bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial || this->mode==MqttLoggerMode::MqttAndSerialAndWeb || this->mode==MqttLoggerMode::SerialAndWeb;
|
||||
bool doWebSerial = this->mode==MqttLoggerMode::MqttAndSerialAndWeb || this->mode==MqttLoggerMode::SerialAndWeb;
|
||||
|
||||
if (this->mode!=MqttLoggerMode::SerialOnly && this->mode!=MqttLoggerMode::SerialAndWeb && this->client != NULL && this->client->connected())
|
||||
|
||||
if (this->mode!=MqttLoggerMode::SerialOnly && this->mode!=MqttLoggerMode::SerialAndWeb && this->client != NULL)
|
||||
{
|
||||
this->client->publish(topic, 0, true, this->buffer, this->bufferCnt);
|
||||
} else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
|
||||
esp_mqtt_client_publish(this->client, topic, (const char*)this->buffer, this->bufferCnt, 0, 1);
|
||||
}
|
||||
else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
|
||||
{
|
||||
doSerial = true;
|
||||
}
|
||||
if (doSerial)
|
||||
if (doSerial)
|
||||
{
|
||||
Serial.write(this->buffer, this->bufferCnt);
|
||||
Serial.println();
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Print.h>
|
||||
#include <espMqttClient.h>
|
||||
#include <mqtt_client.h>
|
||||
//#include "MycilaWebSerial.h"
|
||||
|
||||
#define MQTT_MAX_PACKET_SIZE 1024
|
||||
@@ -32,16 +32,16 @@ private:
|
||||
uint8_t* buffer;
|
||||
uint8_t* bufferEnd;
|
||||
uint16_t bufferCnt = 0, bufferSize = 0;
|
||||
MqttClient* client;
|
||||
esp_mqtt_client_handle_t client;
|
||||
MqttLoggerMode mode;
|
||||
void sendBuffer();
|
||||
|
||||
public:
|
||||
MqttLogger(MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
||||
MqttLogger(MqttClient& client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
||||
MqttLogger(esp_mqtt_client_handle_t client, const char* topic, MqttLoggerMode mode=MqttLoggerMode::MqttAndSerialFallback);
|
||||
~MqttLogger();
|
||||
|
||||
void setClient(MqttClient& client);
|
||||
void setClient(esp_mqtt_client_handle_t client);
|
||||
void setTopic(const char* topic);
|
||||
void setMode(MqttLoggerMode mode);
|
||||
void setRetained(boolean retained);
|
||||
|
||||
Reference in New Issue
Block a user