esp8266_web_settings beta
Public Member Functions | Static Public Attributes | List of all members
grmcdorman::device::MqttPublisher Class Reference

This class supports generic MQTT publishing. More...

#include <grmcdorman/device/MqttPublisher.h>

Inheritance diagram for grmcdorman::device::MqttPublisher:
grmcdorman::device::Device

Public Member Functions

 MqttPublisher (const __FlashStringHelper *manufacturer, const __FlashStringHelper *model, const __FlashStringHelper *software_version, Client *client=nullptr)
 Construct a new Mqtt Device object. More...
 
void set_defaults () override
 Set defaults. More...
 
void setup () override
 Setup the device. More...
 
void loop () override
 Main loop. More...
 
DynamicJsonDocument as_json () const override
 Get the values, as a JSON document. More...
 
virtual void set_devices (const std::vector< Device * > &list) override
 Add a list of devices that will publish. More...
 
virtual String get_status () const
 Get a status report. More...
 
- Public Member Functions inherited from grmcdorman::device::Device
 Device (const __FlashStringHelper *device_name, const __FlashStringHelper *device_identifier)
 Construct a new Device object. More...
 
virtual ~Device ()
 Destroy the Device object.
 
const __FlashStringHelper * name () const
 Get the device name; used for UI names and IDs. More...
 
virtual const __FlashStringHelper * identifier () const
 Get the device identifier. More...
 
virtual bool publish (DynamicJsonDocument &json) const
 Publish the value and attributes. More...
 
const definition_list_tget_definitions () const
 Get the definitions list. More...
 
const ::grmcdorman::SettingInterface::settings_list_t & get_settings () const
 Get the settings list. More...
 
bool is_enabled () const
 Get whether this device is enabled. More...
 
void set_enabled (bool state)
 Set whether this device is enabled. More...
 
void set (const String &setting, const String &value)
 If possible, set a setting's value. More...
 
String get (const String &setting) const
 Get a setting value, as a string. More...
 
virtual bool get_is_published () const
 Get whether the device readings have been published. More...
 
void set_is_published ()
 Set the device as having published readings. More...
 
void clear_is_published ()
 Set the device as not having published readings.
 

Static Public Attributes

static constexpr uint16_t CONNECTION_TRIES = 5
 Number of times to try establishing a connection.
 
static constexpr uint32_t CONNECTION_RETRY_INTERVAL = 5
 Second between attempts to retry establishing a connection.
 
- Static Public Attributes inherited from grmcdorman::device::Device
static constexpr int D0 = 16
 D0 is GPIO16, HIGH at boot, not suitable for most usages.
 
static constexpr int D1 = 5
 D1 is GPIO5; often used as SCL.
 
static constexpr int D2 = 4
 D2 is GPIO4; often used as SDA.
 
static constexpr int D3 = 0
 D3 is GPIO0; pulled up; connected to FLASH button; not for input.
 
static constexpr int D4 = 2
 D4 is GPIO2; pulled up; HIGH at boot; on-board LED; not for input.
 
static constexpr int D5 = 14
 D5 is GPIO14; SPI (SCLK)
 
static constexpr int D6 = 12
 D6 is GPIO12; SPI (MISO)
 
static constexpr int D7 = 13
 D7 is GPIO13; SPI (MOSI)
 
static constexpr int D8 = 15
 D8 is GPIO15; pulled to GND; SPI (CS); not recommended.
 
static const ExclusiveOptionSetting::names_list_t data_line_names { FPSTR("D1"), FPSTR("D2"), FPSTR("D3"), FPSTR("D5"), FPSTR("D6"), FPSTR("D7")}
 Names for each configurable data line; see settingsMap.
 

Additional Inherited Members

- Public Types inherited from grmcdorman::device::Device
typedef std::vector< const Definition * > definition_list_t
 The type containing a list of Definition objects.
 
- Static Public Member Functions inherited from grmcdorman::device::Device
static void set_system_identifiers (const __FlashStringHelper *firmware_name_value, const String &system_identifier_value=String())
 Set the system identifier values. More...
 
static const __FlashStringHelper * get_firmware_name ()
 Get the firmware prefix. More...
 
static const String & get_system_identifier ()
 Get the system identifier. More...
 
- Protected Member Functions inherited from grmcdorman::device::Device
void initialize (definition_list_t &&definition_list, ::grmcdorman::SettingInterface::settings_list_t &&setting_list)
 Initialize the definition and setting lists. More...
 
- Static Protected Member Functions inherited from grmcdorman::device::Device
static int index_to_dataline (int index)
 Convert a data line index to a ESP data line. More...
 
static int dataline_to_index (int dataLine)
 Convert a data line to an index. More...
 
- Protected Attributes inherited from grmcdorman::device::Device
ToggleSetting enabled
 Whether this device is enabled.
 
- Static Protected Attributes inherited from grmcdorman::device::Device
static const int settingsMap [6]
 The set of data lines usable for communication. More...
 

Detailed Description

This class supports generic MQTT publishing.

SSL connections are not currently supported.

It does not support subscriptions.

If the connection is lost, an immediate attempt to connect is made on the next publish attempt.

When a connection is attempted, the device will attempt CONNECTION_TRIES at an interval of CONNECTION_RETRY_INTERVAL. If this fails, it will not attempt a connection again until the configured reconnection interval elapses.

Constructor & Destructor Documentation

◆ MqttPublisher()

grmcdorman::device::MqttPublisher::MqttPublisher ( const __FlashStringHelper *  manufacturer,
const __FlashStringHelper *  model,
const __FlashStringHelper *  software_version,
Client *  client = nullptr 
)

Construct a new Mqtt Device object.

The manufacturer, model, and software version are used when publishing auto-discovery to Home Assistant. For example, if your device is a home-built weather station, then you could supply your name as the manufacturer and the model as "Weather Station".

The software version is not restricted to standard naming conventions; one simple solution is to use the predefined DATE and TIME macros. Doing so would give a simple unique string to every build.

The communication client, if provided, should be an appropriate client to be used for network communications. If not provided, a WiFiClient is created.

Parameters
manufacturerThe IoT device manufacturer.
modelThe IoT device model.
software_versionThe firmware version.
clientThe communication client for the PubSubClient class.

Member Function Documentation

◆ as_json()

DynamicJsonDocument grmcdorman::device::MqttPublisher::as_json ( ) const
overridevirtual

Get the values, as a JSON document.

The structure is identical to the document created inside publish.

Returns
DynamicJsonDocument

Reimplemented from grmcdorman::device::Device.

◆ get_status()

String grmcdorman::device::MqttPublisher::get_status ( ) const
virtual

Get a status report.

This is also used for the device_status info message, with the exception of various disabled states.

When the MQTT publisher is disabled or nonoperational (e.g. no MQTT server configured), this returns an empty string.

Returns
String containing status report.

Reimplemented from grmcdorman::device::Device.

◆ loop()

void grmcdorman::device::MqttPublisher::loop ( )
overridevirtual

Main loop.

This is called in the main application loop. This should avoid blocking as doing so will prevent the other devices from running.

An exception is a "device" which requires user interaction, e.g. WiFi association, before the rest of the system can run.

Implements grmcdorman::device::Device.

◆ set_defaults()

void grmcdorman::device::MqttPublisher::set_defaults ( )
overridevirtual

Set defaults.

This sets the MQTT identifier to the system identifier, and the MQTT topic prefix to the firmware identifier.

Reimplemented from grmcdorman::device::Device.

◆ set_devices()

virtual void grmcdorman::device::MqttPublisher::set_devices ( const std::vector< Device * > &  list)
inlineoverridevirtual

Add a list of devices that will publish.

When the MQTT device publishes, per the timing set in its configuration, it will poll each of these devices for publish data. If any device returns data, a publish operation will be performed.

No attempt is made to segregate device data; the devices must be configured such that each has unique keys in the JSON to be published.

If no devices are added, no publishing occurs.

Parameters
listList of devices to manage.

Reimplemented from grmcdorman::device::Device.

◆ setup()

void grmcdorman::device::MqttPublisher::setup ( )
overridevirtual

Setup the device.

Call once when the system boots. This should perform device-specific setup, if the device is enabled, as well as preparing UI settings (notably info settings) for communication.

This must be called after initial values are loaded.

Implements grmcdorman::device::Device.


The documentation for this class was generated from the following files: