@@ -73,8 +73,10 @@ void MqttLogger::sendBuffer()
|
||||
{
|
||||
if (this->bufferCnt > 0)
|
||||
{
|
||||
bool doSerial = this->mode==MqttLoggerMode::SerialOnly || this->mode==MqttLoggerMode::MqttAndSerial;
|
||||
if (this->mode!=MqttLoggerMode::SerialOnly && this->client != NULL && this->client->connected())
|
||||
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())
|
||||
{
|
||||
this->client->publish(topic, 0, true, this->buffer, this->bufferCnt);
|
||||
} else if (this->mode == MqttLoggerMode::MqttAndSerialFallback)
|
||||
@@ -86,6 +88,11 @@ void MqttLogger::sendBuffer()
|
||||
Serial.write(this->buffer, this->bufferCnt);
|
||||
Serial.println();
|
||||
}
|
||||
if (doWebSerial)
|
||||
{
|
||||
WebSerial.write(this->buffer, this->bufferCnt);
|
||||
WebSerial.println();
|
||||
}
|
||||
this->bufferCnt=0;
|
||||
}
|
||||
this->bufferEnd=this->buffer;
|
||||
@@ -113,4 +120,12 @@ size_t MqttLogger::write(uint8_t character)
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t MqttLogger::write(const uint8_t *buffer, size_t size) {
|
||||
size_t n = 0;
|
||||
while (size--) {
|
||||
n += write(*buffer++);
|
||||
}
|
||||
return n;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
MqttLogger - offer print() interface like Serial but by publishing to a given mqtt topic.
|
||||
MqttLogger - offer print() interface like Serial but by publishing to a given mqtt topic.
|
||||
Uses Serial as a fallback when no mqtt connection is available.
|
||||
|
||||
Claus Denk
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <Arduino.h>
|
||||
#include <Print.h>
|
||||
#include <espMqttClient.h>
|
||||
#include "MycilaWebSerial.h"
|
||||
|
||||
#define MQTT_MAX_PACKET_SIZE 1024
|
||||
|
||||
@@ -20,6 +21,8 @@ enum MqttLoggerMode {
|
||||
SerialOnly = 1,
|
||||
MqttOnly = 2,
|
||||
MqttAndSerial = 3,
|
||||
MqttAndSerialAndWeb = 4,
|
||||
SerialAndWeb = 5,
|
||||
};
|
||||
|
||||
class MqttLogger : public Print
|
||||
@@ -42,10 +45,11 @@ public:
|
||||
void setTopic(const char* topic);
|
||||
void setMode(MqttLoggerMode mode);
|
||||
void setRetained(boolean retained);
|
||||
|
||||
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buffer, size_t size);
|
||||
using Print::write;
|
||||
|
||||
|
||||
uint16_t getBufferSize();
|
||||
boolean setBufferSize(uint16_t size);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user