refactored added new LCD
This commit is contained in:
63
mqtt.h
Normal file
63
mqtt.h
Normal file
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "mqtt.h"
|
||||
#include "config.h"
|
||||
|
||||
#include <WiFi.h> // ESP32 WiFi driver
|
||||
#include <PubSubClient.h> // For MQTT
|
||||
#include <string>
|
||||
#include "RunningMedian.h"
|
||||
|
||||
|
||||
void initMQTT( void );
|
||||
void handleMQTT( void );
|
||||
bool initWifi();
|
||||
|
||||
uint32_t getDeviceID( void );
|
||||
|
||||
void publishSingle(uint32_t value, char *topic);
|
||||
|
||||
class mqttSensor
|
||||
{
|
||||
char _topic[50];
|
||||
char _topic_1h[50];
|
||||
char _topic_24h[50];
|
||||
const String _name;
|
||||
uint32_t _value;
|
||||
RunningMedian _average1h;
|
||||
RunningMedian _average24h;
|
||||
public:
|
||||
|
||||
mqttSensor(String name, String deviceID, uint8_t avgSamples): _name(name),
|
||||
_average1h(avgSamples),
|
||||
_average24h(avgSamples*24)
|
||||
{
|
||||
sprintf(_topic, "Sensors/%s/%s", deviceID.c_str(),name.c_str() );
|
||||
sprintf(_topic_1h, "Sensors/%s/%s/1h/", deviceID.c_str(),name.c_str() );
|
||||
sprintf(_topic_24h, "Sensors/%s/%s/24h/", deviceID.c_str(),name.c_str() );
|
||||
}
|
||||
|
||||
|
||||
void publish(void)
|
||||
{
|
||||
publishSingle(_value, _topic);
|
||||
publishSingle(_average1h.getMedian(),_topic_1h);
|
||||
publishSingle(_average24h.getMedian(),_topic_24h);
|
||||
}
|
||||
|
||||
virtual void set(uint32_t value)
|
||||
{
|
||||
_value = value;
|
||||
_average1h.add(_value);
|
||||
_average24h.add(_value);
|
||||
}
|
||||
|
||||
uint32_t value() {return _value;}
|
||||
|
||||
void print( void )
|
||||
{
|
||||
Serial.printf("sensor:%s = %d\n",_name.c_str(), _value);
|
||||
}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user