JBWOPR
Loading...
Searching...
No Matches
JBWoprMqttDevice Class Reference

W.O.P.R. WiFi and MQTT device class. More...

#include <jbwoprmqtt.h>

Inheritance diagram for JBWoprMqttDevice:
JBWoprWiFiDevice JBWoprDevice JBWoprHADevice

Public Member Functions

 JBWoprMqttDevice ()
 JBWoprMqttDevice constructor.
 
bool begin (JBWoprBoardVariant variant) override
 Initialize JBWoprMqttDevice.
 
bool begin (JBWoprBoardVariant variant, JBWoprBoardPins pins) override
 Initialize JBWoprMqttDevice.
 
void loop () override
 JBWoprMqttDevice loop.
 
void setLogLevel (LogLevel level) override
 Set log level.
 
LogLevel getLogLevel () override
 Get log level.
 
bool mqttPublishMessage (const std::string &topic, JsonDocument &jsonDoc, bool retain=false)
 MQTT publish message.
 
bool mqttPublishMessage (const std::string &topic, const std::string &value, bool retain=false)
 MQTT publish message.
 
bool mqttPublishMessage (const char *topic, const char *value, bool retain=false)
 MQTT publish message.
 
void effectsStartCurrentEffect () override
 Effects start current effect.
 
virtual void effectsStopCurrentEffect ()
 Stop current effect.
 
void effectsStartEffect (JBWoprEffectBase *effect) override
 Effects start effect.
 
void effectsStartEffect (const std::string &effect) override
 Effects start effect.
 
void effectsStartEffect (const char *effect) override
 Effects start effect.
 
void displaySetState (bool state) override
 Display set state.
 
void displayClear () override
 Display clear.
 
void displaySetBrightness (uint8_t val) override
 Display set brightness.
 
void displayShowText (const char *text, JBTextAlignment alignment) override
 Display show text.
 
void displayShowText (const std::string &text, JBTextAlignment alignment) override
 Display show text.
 
void displayShowText (const String &text, JBTextAlignment alignment) override
 Display show text.
 
void displayShowText (const char *text) override
 Display show text, left aligned.
 
void displayShowText (const std::string &text) override
 Display show text, left aligned.
 
void displayShowText (const String &text) override
 Display show text, left aligned.
 
void displayScrollText (const char *text, uint16_t delay_ms) override
 Display scrolling text.
 
void displayScrollText (const std::string &text, uint16_t delay_ms) override
 Display scrolling text.
 
void displayScrollText (const String &text, uint16_t delay_ms) override
 Display scrolling text.
 
virtual void displayScrollText (const char *text)
 Set display scroll text with a delay of 100 ms.
 
virtual void displayScrollText (const std::string &text)
 Set display scroll text with a delay of 100 ms.
 
virtual void displayScrollText (const String &text)
 Set display scroll text with a delay of 100 ms.
 
void defconLedsSetState (bool state) override
 DEFCON LEDs set state.
 
void defconLedsSetDefconLevel (JBDefconLevel level) override
 DEFCON LEDs set DEFCON level.
 
void defconLedsSetColor (uint32_t color) override
 DEFCON LEDs set color.
 
void defconLedsSetBrightness (uint8_t brightness) override
 DEFCON LEDs set brightness.
 
void defconLedSetColor (JBDefconLevel level, uint32_t color)
 Set color of a specified DEFCON LED.
 
- Public Member Functions inherited from JBWoprWiFiDevice
 JBWoprWiFiDevice ()
 JBWoprWiFiDevice constructor.
 
bool begin (JBWoprBoardVariant variant) override
 Initialize JBWoprWiFiDevice.
 
bool begin (JBWoprBoardVariant variant, JBWoprBoardPins pins) override
 Initialize JBWoprWiFiDevice.
 
void loop () override
 JBWoprWiFiDevice loop.
 
void setLogLevel (LogLevel level) override
 Set log level.
 
LogLevel getLogLevel () override
 Get log level.
 
bool startWiFi ()
 Start WiFi.
 
void webPortalStart ()
 Start web portal.
 
void webPortalStop ()
 Stop web portal.
 
- Public Member Functions inherited from JBWoprDevice
 JBWoprDevice ()
 JBWoprDevice constructor.
 
JBWoprBoardVariant getBoardVariant ()
 Get W.O.P.R board variant.
 
JBWoprConfigurationgetConfiguration ()
 Get current JBWoprDevice configuration.
 
void effectsRegisterDefaultEffect (JBWoprEffectBase *effect)
 Register default effect.
 
bool effectsDefaultEffectIsRunning ()
 Check if default effect is running.
 
void effectsRegisterEffect (JBWoprEffectBase *effect)
 Register effect.
 
std::vector< JBWoprEffectBase * > effectsGetRegisteredEffects ()
 Get a list of registered effects.
 
JBWoprEffectBaseeffectsGetCurrentEffect ()
 Get current effect.
 
bool effectsCurrentEffectIsRunning ()
 Check if current effect is running.
 
std::array< Adafruit_AlphaNum4, 3 > getDisplay ()
 Get display.
 
virtual void displayShow ()
 Force display to show current data.
 
virtual void displaySetChar (uint8_t index, char chr)
 Set individual display character.
 
Adafruit_NeoPixel * getDefconLeds ()
 Get DEFCON LEDs.
 
virtual void defconLedsClear ()
 Clear all DEFCON LEDs.
 
virtual void defconLedSetDefconStateColor (JBDefconLevel level, uint32_t color)
 Set color for a DEFCON level.
 
void audioPlayTone (const uint16_t freq)
 Play audio tone.
 
void audioPlayNote (note_t note, uint8_t octave)
 Play audio tone.
 
void audioClear ()
 Stop audio playback.
 
OneButton * getButtonFrontLeft ()
 Get front left button OneButton instance.
 
OneButton * getButtonFrontRight ()
 Get front right button OneButton instance.
 
OneButton * getButtonBackTop ()
 Get back top button OneButton instance.
 
OneButton * getButtonBackBottom ()
 Get back bottom button OneButton instance.
 
virtual void buttonFrontLeftSetClickCallback (std::function< void()> func)
 Set front left button click callback.
 
virtual void buttonFrontLeftSetDoubleClickCallback (std::function< void()> func)
 Set front left button double click callback.
 
virtual void buttonFrontRightSetClickCallback (std::function< void()> func)
 Set front right button click callback.
 
virtual void buttonFrontRightSetDoubleClickCallback (std::function< void()> func)
 Set front right button double click callback.
 
virtual void buttonBackTopSetClickCallback (std::function< void()> func)
 Set back top button click callback.
 
virtual void buttonBackTopSetDoubleClickCallback (std::function< void()> func)
 Set back top button double click callback.
 
virtual void buttonBackBottomSetClickCallback (std::function< void()> func)
 Set back top button long press callback.
 
virtual void buttonBackBottomSetDoubleClickCallback (std::function< void()> func)
 Set back bottom button double click callback.
 

Protected Member Functions

void _setConfigFromJsonDocument (const JsonDocument &jsonDoc)
 Set JBWoprMqttDevice specific config values from JSON document.
 
void _setJsonDocumentFromConfig (JsonDocument &jsonDoc)
 Set JSON document values with JBWoprMqttDevice specific config values.
 
void _dumpConfig () override
 Dump configuration to logger.
 
void _setupWiFiManager () override
 Setup WiFiManager.
 
void _saveParamsCallback () override
 Set WiFiManager parameter callback.
 
bool _mqttStart ()
 Start MQTT.
 
void _mqttStop ()
 Stop MQTT.
 
bool _mqttReconnect ()
 Reconnect MQTT.
 
virtual bool _onMqttConnect ()
 Called when MQTT client get connected.
 
void _mqttCallback (const char *topic, const byte *payload, unsigned int length)
 MQTT callback.
 
virtual void _handleCommand (const std::string &entity, const std::string &subEntity, const std::string &command, const std::string &payload)
 Handle MQTT command message.
 
virtual void _handleDeviceCommand (const std::string &subEntity, const std::string &command, const std::string &payload)
 Handle MQTT device command message.
 
virtual void _handleConfigCommand (const std::string &subEntity, const std::string &command, const std::string &payload)
 Handle MQTT config command message.
 
virtual void _handleEffectCommand (const std::string &subEntity, const std::string &command, const std::string &payload)
 Handle MQTT effect command message.
 
virtual void _handleDisplayCommand (const std::string &subEntity, const std::string &command, const std::string &payload)
 Handle MQTT display command message.
 
virtual void _handleDefconCommand (const std::string &subEntity, const std::string &command, const std::string &payload)
 Handle MQTT defcon command message.
 
virtual std::string _getTopic (const char *entityId, const char *subEntityId)
 Get publish topic for specified entity.
 
virtual std::string _getTopic (const std::string &entityId, const std::string &subEntityId)
 Get publish topic for specified entity.
 
virtual std::string _getSubscriptionTopic ()
 Get subscription topic.
 
virtual std::string _getAvailabilityTopic ()
 Get availability topic.
 
void _buttonFrontLeftClick () override
 Button front left click callback.
 
void _buttonFrontLeftDoubleClick () override
 Button front left double click callback.
 
void _buttonFrontRightClick () override
 Button front right click callback.
 
void _buttonFrontRightDoubleClick () override
 Button front right double click callback.
 
void _buttonBackTopClick () override
 Button back top click callback.
 
void _buttonBackTopDoubleClick () override
 Button back top double click callback.
 
void _buttonBackBottomClick () override
 Button back bottom click callback.
 
void _buttonBackBottomDoubleClick () override
 Button back bottom double click callback.
 
- Protected Member Functions inherited from JBWoprWiFiDevice
void _loadConfiguration ()
 Load configuration from file.
 
void _saveConfiguration ()
 Save configuration to file.
 
std::string _getDeviceName ()
 Get device name.
 
virtual WiFiManager * _getWiFiManager ()
 Get WiFi manager.
 
void _apCallback (WiFiManager *wifiManager)
 AP callback.
 
void _webServerCallback ()
 Web server callback.
 
- Protected Member Functions inherited from JBWoprDevice
JBDefconLevel _getDefconLevel (std::string value)
 Get DEFCON level from string value.
 
std::string _getDefconLevelString (JBDefconLevel level)
 Get DEFCON level string from DEFCON level.
 
uint32_t _getDefconLedsPixel (JBDefconLevel level)
 Get DEFCON LED strip pixel from DEFCON level.
 

Protected Attributes

JBWoprMqttConfig _mqttConfig
 MQTT configuration.
 
const char * JSON_KEY_MQTT_USE_MQTT = "useMqtt"
 Use MQTT key name.
 
const char * JSON_KEY_MQTT_SERVER_NAME = "mqttServerName"
 MQTT server name key name.
 
const char * JSON_KEY_MQTT_SERVER_PORT = "mqttServerPort"
 MQTT server port key name.
 
const char * JSON_KEY_MQTT_USER_NAME = "mqttUserName"
 MQTT user name key name.
 
const char * JSON_KEY_MQTT_PASSWORD = "mqttPassword"
 MQTT password key name.
 
const char * JSON_KEY_CONF_MQTT_PREFIX = "mqttPrefix"
 MQTT prefix key name.
 
WiFiClient _wifiClient
 WiFi client.
 
const char * HTML_MQTT_TITLE = "<h2>MQTT settings</h2>"
 MQTT title.
 
char _mqttServerPortValue [6]
 MQTT server port value.
 
WiFiManagerParameter * _mqttTitleParam
 MQTT title WiFiManager parameter.
 
WiFiManagerParameter * _useMqttParam
 Use MQTT WiFiManager parameter.
 
WiFiManagerParameter * _mqttServerNameParam
 MQTT server name WiFiManager parameter.
 
WiFiManagerParameter * _mqttServerPortParam
 MQTT server port WiFiManager parameter.
 
WiFiManagerParameter * _mqttUserNameParam
 MQTT user name WiFiManager parameter.
 
WiFiManagerParameter * _mqttPasswordParam
 MQTT password WiFiManager parameter.
 
WiFiManagerParameter * _mqttPrefixParam
 MQTT prefix WiFiManager parameter.
 
WiFiManagerParameter * _break2Param
 Break WiFiManagerparameter.
 
PubSubClient * _mqttClient
 MQTT client.
 
bool _mqttActive = false
 MQTT active flag, set tp true after initialization.
 
const char * ENTITY_NAME_DEVICE = "device"
 Device entity name.
 
const char * ENTITY_NAME_CONFIG = "config"
 Config entity name.
 
const char * ENTITY_NAME_EFFECT = "effect"
 Effect entity name.
 
const char * ENTITY_NAME_DISPLAY = "display"
 Display text entity name.
 
const char * ENTITY_NAME_DEFCON = "defcon"
 DEFCON LED entity name.
 
const char * ENTITY_NAME_BUTTON_FRONT_LEFT = "button_front_left"
 Button front left entity name.
 
const char * ENTITY_NAME_BUTTON_FRONT_RIGHT = "button_front_right"
 Button front right entity name.
 
const char * ENTITY_NAME_BUTTON_BACK_TOP = "button_back_top"
 Button back top entity name.
 
const char * ENTITY_NAME_BUTTON_BACK_BOTTOM = "button_back_bottom"
 Button back bottom entity name.
 
const char * SUBENTITY_NAME_STATE = "state"
 State subentity name.
 
const char * SUBENTITY_NAME_BRIGHTNESS = "brightness"
 Brightness subentity name.
 
const char * SUBENTITY_NAME_COLOR = "color"
 Color subentity name.
 
const char * SUBENTITY_NAME_TEXT = "text"
 Text subentity name.
 
const char * SUBENTITY_NAME_SCROLLTEXT = "scrolltext"
 Scroll text subentity name.
 
const char * SUBENTITY_NAME_EVENT = "event"
 Event subentity name.
 
const char * SUBENTITY_NAME_LEVEL = "level"
 Level subentity name.
 
const char * SUBENTITY_NAME_NAME = "name"
 Effect subentity name.
 
const char * SUBENTITY_NAME_EFFECTS_TIMEOUT = "effects_timeout"
 Effects timeout key name.
 
const char * SUBENTITY_NAME_TIME_FORMAT = "time_format"
 Time format key name.
 
const char * SUBENTITY_NAME_DATE_FORMAT = "date_format"
 Date Format key name.
 
const char * SUBENTITY_NAME_DEFCON_BRIGHTNESS = "defcon_brightness"
 DEFCON LEDs brightness key name.
 
const char * SUBENTITY_NAME_DISPLAY_BRIGHTNESS = "display_brightness"
 Display brightness key name.
 
const char * SUBENTITY_NAME_WIFI_HOST_NAME = "host_name"
 Host name key name.
 
const char * SUBENTITY_NAME_WIFI_USE_WEB_PORTAL = "use_web_portal"
 Use portal key name.
 
const char * STATE_ON = "ON"
 State ON.
 
const char * STATE_OFF = "OFF"
 State OFF.
 
const char * EVENT_CLICK = "click"
 Click event.
 
const char * EVENT_DOUBLE_CLICK = "double_click"
 Double click event.
 
const char * COMMAND_SET = "set"
 Set command.
 
- Protected Attributes inherited from JBWoprWiFiDevice
JBWoprWiFiConfiguration _wifiConfig
 WiFi configuration.
 
bool _shouldSaveConfig = false
 Flag to save configuration.
 
const char * CONFIG_FILE_NAME = "/config.json"
 Configuration file name.
 
WiFiManager * _wifiManager
 WiFi manager.
 
std::string _apName = ""
 AP name.
 
const char * WEB_PORTAL_PASSWORD = "wopr1234"
 AP portal password.
 
const char * JSON_KEY_EFFECTS_TIMEOUT = "effectsTimeout"
 Effects timeout key name.
 
const char * JSON_KEY_TIME_FORMAT = "timeFormat"
 Time format key name.
 
const char * JSON_KEY_DATE_FORMAT = "dateFormat"
 Date Format key name.
 
const char * JSON_KEY_DEFCON_BRIGHTNESS = "defconBrightness"
 DEFCON LEDs brightness key name.
 
const char * JSON_KEY_DISPLAY_BRIGHTNESS = "displayBrightness"
 Display brightness key name.
 
const char * JSON_KEY_WIFI_HOST_NAME = "hostName"
 Host name key name.
 
const char * JSON_KEY_WIFI_NTP_SERVER = "ntpServer"
 NTP Server key name.
 
const char * JSON_KEY_WIFI_TIME_OFFSET = "timeOffsetString"
 NTP Server key name.
 
const char * JSON_KEY_WIFI_USE_WEB_PORTAL = "useWebPortal"
 Use portal key name.
 
const char * HTML_WOPR_TITLE = "<h2>W.O.P.R. settings</h2>"
 Settings title.
 
const char * HTML_NETWORK_TITLE = "<h2>Network settings</h2>"
 Network title.
 
const char * HTML_CHECKBOX_TRUE = "type=\"checkbox\" checked"
 HTML checkbox true.
 
const char * HTML_CHECKBOX_FALSE = "type=\"checkbox\""
 HTML checkbox false.
 
WiFiManagerParameter * _woprTitleParam
 W.O.P.R. title parameter.
 
WiFiManagerParameter * _networkTitleParam
 Network title parameter.
 
WiFiManagerParameter * _breakParam
 Break parameter.
 
WiFiManagerParameter * _effectsTimeoutParam
 Effects timeout parameter.
 
WiFiManagerParameter * _timeFormatParam
 Time format parameter.
 
WiFiManagerParameter * _dateFormatParam
 Date format parameter.
 
WiFiManagerParameter * _displayBrightnessParam
 Display brightness parameter.
 
WiFiManagerParameter * _defconLedsBrightnessParam
 DEFCON LEDs brightness parameter.
 
WiFiManagerParameter * _hostNameParam
 Host name parameter.
 
WiFiManagerParameter * _ntpServerNameParam
 NTP Server address.
 
WiFiManagerParameter * _timeOffsetParam
 Time offset.
 
WiFiManagerParameter * _useWebPortalParam
 Use web portal parameter.
 
char _effectsTimeoutValue [3]
 Effects timeout value, set in WiFiManager callback.
 
char _defconLedsBrightnessValue [3]
 DEFCON LEDs brightness value, set in WiFiManager callback.
 
char _displayBrightnessValue [3]
 Display brightness value, set in WiFiManager callback.
 
- Protected Attributes inherited from JBWoprDevice
JBWoprBoardVariant _woprVariant
 W.O.P.R. board version.
 
JBWoprConfiguration _config
 JBWoprDevice configuration.
 
JBWoprBoardPins _pins
 W.O.P.R. board pin assignements.
 
JBWoprEffectBase_defaultEffect = nullptr
 Default effect.
 
std::vector< JBWoprEffectBase * > _effects
 Effects.
 
JBWoprEffectBase_currentEffect = nullptr
 Current effect.
 
uint32_t _effectsCounter = 0
 Effects counter.
 
std::array< Adafruit_AlphaNum4, 3 > _display
 Display.
 
bool _displayState = true
 Display state.
 
uint32_t _displayBrightness = 100
 Display brightness.
 
Adafruit_NeoPixel _defconLeds = Adafruit_NeoPixel(5, 1, NEO_GRB + NEO_KHZ800)
 DEFCON LEDs.
 
bool _defconState = true
 DEFCON state.
 
JBDefconLevel _defconLevel = JBDefconLevel::DEFCON_NONE
 DEFCON level.
 
uint32_t _defconColors [5]
 DEFCON colors.
 
uint32_t _defconBrightness = 100
 DEFCON brightness.
 
uint32_t _defconLedsColor = 0
 DEFCON LED's color.
 
uint32_t _defconPixels [5] { 0,0,0,0,0}
 DEFCON buffered pixel colors.
 
uint32_t _defconBrigthtness = 100
 DEFCON brightness.
 
OneButton * _buttonFrontLeft
 Front left button.
 
OneButton * _buttonFrontRight
 Front right button.
 
OneButton * _buttonBackTop
 Back to.
 
OneButton * _buttonBackBottom
 Back bottom button.
 
std::function< void()> _buttonFrontLeftClickCallback
 Front left button click callback.
 
std::function< void()> _buttonFrontLeftDoubleClickCallback
 Front left button double click callback.
 
std::function< void()> _buttonFrontRightClickCallback
 Front right button click callback.
 
std::function< void()> _buttonFrontRightDoubleClickCallback
 Front right button double click callback.
 
std::function< void()> _buttonBackTopClickCallback
 Back top button click callback.
 
std::function< void()> _buttonBackTopDoubleClickCallback
 Back top button double click callback.
 
std::function< void()> _buttonBackBottomClickCallback
 Back bottom button click callback.
 
std::function< void()> _buttonBackBottomDoubleClickCallback
 Back bottom button double click callback.
 
bool _useDAC = false
 Use DAC.
 
int _audioFreq = 2000
 Audio frequency.
 
int _audioChannel = 0
 Audio channel.
 
int _audioResolution = 8
 Audio resolution.
 

Additional Inherited Members

- Public Attributes inherited from JBWoprDevice
const char * DEFCON_STRINGS [6] { "DEFCON 1", "DEFCON 2", "DEFCON 3", "DEFCON 4", "DEFCON 5", "None" }
 
- Static Protected Member Functions inherited from JBWoprDevice
static void _staticButtonFrontLeftClickCallback (void *data)
 Front left button internal click callback.
 
static void _staticButtonFrontLeftDoubleClickCallback (void *data)
 Front left button internal double click callback.
 
static void _staticButtonFrontRightClickCallback (void *data)
 Front right button internal click callback.
 
static void _staticButtonFrontRightDoubleClickCallback (void *data)
 Front right button internal double click callback.
 
static void _staticButtonBackTopClickCallback (void *data)
 Back top button internal click callback.
 
static void _staticButtonBackTopDoubleClickCallback (void *data)
 Back top button internal double click callback.
 
static void _staticButtonBackBottomClickCallback (void *data)
 Back bottom button internal click callback.
 
static void _staticButtonBackBottomDoubleClickCallback (void *data)
 Back bottom button internal double click callback.
 

Detailed Description

W.O.P.R. WiFi and MQTT device class.

This class extends the JBWoprWiFiDevice class with MQTT support.

Member Function Documentation

◆ _dumpConfig()

void JBWoprMqttDevice::_dumpConfig ( )
overrideprotectedvirtual

Dump configuration to logger.

Called when configuration is to be written to the logger.

Reimplemented from JBWoprWiFiDevice.

◆ _setConfigFromJsonDocument()

void JBWoprMqttDevice::_setConfigFromJsonDocument ( const JsonDocument &  jsonDoc)
protectedvirtual

Set JBWoprMqttDevice specific config values from JSON document.

Parameters
jsonDocJSON document

Reimplemented from JBWoprWiFiDevice.

◆ _setJsonDocumentFromConfig()

void JBWoprMqttDevice::_setJsonDocumentFromConfig ( JsonDocument &  jsonDoc)
protectedvirtual

Set JSON document values with JBWoprMqttDevice specific config values.

Parameters
jsonDocJSON document

Reimplemented from JBWoprWiFiDevice.

◆ begin() [1/2]

bool JBWoprMqttDevice::begin ( JBWoprBoardVariant  variant)
overridevirtual

Initialize JBWoprMqttDevice.

Use this method if you are using a tinyXxx board, it will assign the ESP32 pins automatically.

Parameters
variantBoard version
Returns
True if successful

Reimplemented from JBWoprDevice.

◆ begin() [2/2]

bool JBWoprMqttDevice::begin ( JBWoprBoardVariant  variant,
JBWoprBoardPins  pins 
)
overridevirtual

Initialize JBWoprMqttDevice.

Use this method if you are not using a tinyXxx board

Parameters
variantBoard version
pinsBoard pins
Returns
True if successful

Reimplemented from JBWoprDevice.

◆ getLogLevel()

LogLevel JBWoprMqttDevice::getLogLevel ( )
overridevirtual

Get log level.

Returns
Log level

Reimplemented from JBWoprDevice.

◆ loop()

void JBWoprMqttDevice::loop ( )
overridevirtual

JBWoprMqttDevice loop.

This method should be called from the main loop() method.

Reimplemented from JBWoprDevice.

◆ setLogLevel()

void JBWoprMqttDevice::setLogLevel ( LogLevel  level)
overridevirtual

Set log level.

This method will set the log level for the logger.

Parameters
levelLog level

Reimplemented from JBWoprDevice.


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