Skip to content

Commit

Permalink
SSD1306toMQTT / MQTTtoSSD1306
Browse files Browse the repository at this point in the history
Additional "onstate" and "brightness" setting and regular display state reporting

Docs - separate Displays section
  • Loading branch information
DigiH committed Mar 1, 2023
1 parent 1076f4a commit a07a848
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 81 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = {
'use/sensors',
'use/actuators',
'use/boards',
'use/displays',
'use/gateway'
]
},
Expand Down
62 changes: 0 additions & 62 deletions docs/use/boards.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,65 +84,3 @@ OpenMQTTGateway support a low power mode for ESP32, this mode can be set by MQTT

The low power mode can be changed also with a push to button B when the board is processing (top button on M5stickC, M5stickC Plus and middle button of M5stack).
If you are already in low power mode 1 or 2 with M5Stack you can wake up the board by pressing the red button.

## SSD1306 Display boards ( Heltec SX127X 433Mhz boards and LILYGO® LoRa32 V2.1_1.6.1 433 Mhz )

Several options are available for the display of information on the SSD1306 Display. These options include display of the OMG logo and setup messages, redirecting of the log output to the display, and display of various module messages on the display. These options are exclusive to each other, and when a different option is enabled, the current option is disabled.

### Setting the log output

The display of serial log messages to the display can be enabled via compiler directive `-DLOG_TO_LCD=true` or via MQTT commands.

For example if you want to set the serial log to LCD

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"log-lcd":true}'`

you can also revert it back to the serial monitor:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"log-lcd":false}'`

The log level of the messages displayed is Errors and Warnings, and this can only be changed via the compiler directive `-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE`.

### Displaying Module json messages ( default )

The display of messages from various modules is also supported. Currently supported modules include `ZgatewayRTL_433` and `ZsensorBME280`.

This can be enabled with the compiler directive `-DJSON_TO_LCD=true`.

You can also change it by MQTT. For example if you want to display module json messages:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"json-lcd":true}'`

And to disable the display of module json messages:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"display-json":false}'`

### Units for display, Metric or Imperial

By default the display uses metric units, and this can be changed either by compiler directive or mqtt command.

The compiler directive is `-DDISPLAY_METRIC=true`

The mqtt command to change the units is:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"display-metric":false}'`

Please note that it may take several seconds/display updates for the units to change. This is due to the queueing of messages for display.

### Flip Display 180 degrees

This allows you to rotate the display 180 degrees. Can be set at compile time or during use, defaults to true.

The compiler directive is `-DDISPLAY_FLIP=false`

The mqtt command to change display orientation is:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"display-flip":false}'`

Please note that it may take several seconds/display updates for the display to change. This is due to the queueing of messages for display.

### Display OpenMQTTGateway Logo when Idle

When this option is enabled the OLED display will show the OpenMQTTGateway Logo when it is idle. Defaults to false

The compiler directive is `-DDISPLAY_IDLE_LOGO=true`
86 changes: 86 additions & 0 deletions docs/use/displays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Displays

## SSD1306 Display (Heltec SX127X and LILYGO® LoRa32 boards)
Several options are available for the display of information on the SSD1306 display. Some options are exclusive to each other, and when a different option is enabled, the current option is disabled.

The current SSD1306 display states are being published to the `SSD1306toMQTT` topic, e.g.

`{"onstate":true,"brightness":50,"display-metric":true,"display-flip":true,"idlelogo":true,"log-lcd":false,"json-lcd":true}`

### Display ON/OFF
To turn the SSD1306 display on or off.

This can be enabled with the compiler directive `-DDISPLAY_STATE=true`.

MQTT Display OFF command:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m {"onstate":false}`

MQTT Display ON command:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m {"onstate":true}`

### Brightness
The display brightness can be set between 0-100%.

It is recommended to set a value lower than 100 to extend the life of the OLED display. The default setting is 50.

This can be set with the compiler directive `-DDISPLAY_BRIGHTNESS=50`.

or with the runtime command

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m {"brightness":50}`

### Metric or Imperial property units
To have applicable device properties displayed in Imperial units, e.g. °F for temperature.

This can be set with the compiler directive `-DDISPLAY_METRIC=false`.

or with the runtime command

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m {"display-metric":false}`

### Rotating the display by 180 degrees

This can be set with the compiler directive `-DDISPLAY_FLIP=false`.

or with the runtime command

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m {"display-flip":false}`

### Display idle Logo
To display the OpenMQTTGateway logo during device display idle time. This reduces the likelihood of burn-in.

This can be set with the compiler directive `-DDISPLAY_IDLE_LOGO=true`.

or at runtime with

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m {"idlelogo":true}`

### Setting the log output

The display of serial log messages to the display can be enabled via compiler directive `-DLOG_TO_LCD=true` or via MQTT commands.

For example if you want to set the serial log to LCD

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"log-lcd":true}'`

you can also revert it back to the serial monitor:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"log-lcd":false}'`

The log level of the messages displayed is Errors and Warnings, and this can only be changed via the compiler directive `-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE`.

### Displaying Module json messages (default)

The display of messages from various modules is also supported. Currently supported modules include `ZgatewayRTL_433`, `ZgatewayBT` and `ZsensorBME280`.

This can be enabled with the compiler directive `-DJSON_TO_LCD=true`.

You can also change it by MQTT. For example if you want to display module json messages:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"json-lcd":true}'`

And to disable the display of module json messages:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306 -m '{"display-json":false}'`
70 changes: 53 additions & 17 deletions main/ZdisplaySSD1306.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ boolean logToLCDDisplay = LOG_TO_LCD;
boolean jsonDisplay = JSON_TO_LCD;
boolean displayMetric = DISPLAY_METRIC;
boolean displayFlip = DISPLAY_FLIP;
boolean displayState = DISPLAY_STATE;
boolean idlelogo = DISPLAY_IDLE_LOGO;
uint8_t displayBrightness = round(DISPLAY_BRIGHTNESS * 2.55);

/*
Expand All @@ -60,7 +62,7 @@ module setup, for use in Arduino setup
*/
void setupSSD1306() {
Log.trace(F("Setup SSD1306 Display" CR));
Log.trace(F("ZdisplaySSD1306 command topic: %s" CR), subjectMQTTtoSSD1306set);
Log.trace(F("ZdisplaySSD1306 command topic: %s" CR), subjectMQTTtoSSD1306);
Log.trace(F("ZdisplaySSD1306 log-lcd: %T" CR), logToLCDDisplay);
Log.trace(F("ZdisplaySSD1306 json-lcd: %T" CR), jsonDisplay);
Log.trace(F("ZdisplaySSD1306 DISPLAY_PAGE_INTERVAL: %d" CR), DISPLAY_PAGE_INTERVAL);
Expand Down Expand Up @@ -106,13 +108,11 @@ void loopSSD1306() {
/*
Display logo if it has been more than DISPLAY_PAGE_INTERVAL
*/
# if DISPLAY_IDLE_LOGO
if (uptime() > nextDisplayPage + 1 && !logoDisplayed) {
if (uptime() > nextDisplayPage + 1 && !logoDisplayed && idlelogo) {
Oled.fillScreen(BLACK);
Oled.drawLogo(rand() % 13 - 5, rand() % 32 - 13);
logoDisplayed = true;
}
# endif
}

/*
Expand All @@ -122,10 +122,24 @@ Handler for mqtt commands sent to the module
*/
void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object decoding
bool success = false;
if (cmpToMainTopic(topicOri, subjectMQTTtoSSD1306set)) {
if (cmpToMainTopic(topicOri, subjectMQTTtoSSD1306)) {
Log.trace(F("MQTTtoSSD1306 json set" CR));
// Log display set between SSD1306 lcd (true) and serial monitor (false)
if (SSD1306data.containsKey("log-lcd")) {
if (SSD1306data.containsKey("onstate")) {
if (displayState != SSD1306data["onstate"]) {
displayState = SSD1306data["onstate"];
Oled.begin();
}
displayState = SSD1306data["onstate"];
Log.notice(F("Set display state: %T" CR), logToLCDDisplay);
success = true;
} else if (SSD1306data.containsKey("brightness")) {
displayBrightness = SSD1306data["brightness"];
displayBrightness = round(displayBrightness * 2.55);
Oled.display->setBrightness(displayBrightness);
Log.notice(F("Set brightness: %d" CR), displayBrightness);
success = true;
} else if (SSD1306data.containsKey("log-lcd")) {
logToLCDDisplay = SSD1306data["log-lcd"];
Log.notice(F("Set lcd log: %T" CR), logToLCDDisplay);
logToLCD(logToLCDDisplay);
Expand All @@ -145,6 +159,9 @@ void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object dec
displayMetric = SSD1306data["display-metric"];
Log.notice(F("Set display-metric: %T" CR), displayMetric);
success = true;
} else if (SSD1306data.containsKey("idlelogo")) {
idlelogo = SSD1306data["idlelogo"];
success = true;
} else if (SSD1306data.containsKey("display-flip")) {
displayFlip = SSD1306data["display-flip"];
Log.notice(F("Set display-flip: %T" CR), displayFlip);
Expand All @@ -156,9 +173,9 @@ void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object dec
success = true;
}
if (success) {
pub(subjectSSD1306toMQTTset, SSD1306data);
stateSSD1306Display();
} else {
pub(subjectSSD1306toMQTTset, "{\"Status\": \"Error\"}"); // Fail feedback
pub(subjectSSD1306toMQTT, "{\"Status\": \"Error\"}"); // Fail feedback
Log.error(F("[ SSD1306 ] MQTTtoSSD1306 Fail json" CR), SSD1306data);
}
}
Expand All @@ -183,6 +200,7 @@ void ssd1306PubPrint(const char* topicori, JsonObject& data) {
free(topic);

Oled.display->normalDisplay();
// Oled.display->normalDisplay();

switch (hash(message->title)) {
case hash("SYStoMQTT"): {
Expand Down Expand Up @@ -488,17 +506,21 @@ void OledSerial::begin() {
xSemaphoreGive(semaphoreOLEDOperation);

display->init();
if (displayFlip) {
display->flipScreenVertically();
if (displayState) {
if (displayFlip) {
display->flipScreenVertically();
} else {
display->resetOrientation();
}
display->setFont(ArialMT_Plain_10);
display->setBrightness(displayBrightness);
drawLogo(0, 0);
display->invertDisplay();
display->setLogBuffer(OLED_TEXT_ROWS, OLED_TEXT_BUFFER);
delay(1000);
} else {
display->resetOrientation();
display->displayOff();
}
display->setFont(ArialMT_Plain_10);
display->setBrightness(displayBrightness);
drawLogo(0, 0);
display->invertDisplay();
display->setLogBuffer(OLED_TEXT_ROWS, OLED_TEXT_BUFFER);
delay(1000);
}

/*
Expand Down Expand Up @@ -624,4 +646,18 @@ void OledSerial::drawLogo(int xshift, int yshift) {
}
}

void stateSSD1306Display() {
//Publish display state
StaticJsonDocument<JSON_MSG_BUFFER> jsonBuffer;
JsonObject DISPLAYdata = jsonBuffer.to<JsonObject>();
DISPLAYdata["onstate"] = (bool)displayState;
DISPLAYdata["brightness"] = (int)round(displayBrightness / 2.55);
DISPLAYdata["display-metric"] = (bool)displayMetric;
DISPLAYdata["display-flip"] = (bool)displayFlip;
DISPLAYdata["idlelogo"] = (bool)idlelogo;
DISPLAYdata["log-lcd"] = (bool)logToLCDDisplay;
DISPLAYdata["json-lcd"] = (bool)jsonDisplay;
pub(subjectSSD1306toMQTT, DISPLAYdata);
}

#endif
8 changes: 6 additions & 2 deletions main/config_SSD1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
# define DISPLAY_FLIP true // Flip display orientation
#endif

#ifndef DISPLAY_STATE
# define DISPLAY_STATE true // set to false if you don't want to use the display
#endif

#ifndef DISPLAY_BRIGHTNESS
# define DISPLAY_BRIGHTNESS 50 // 0-100; 50 % brightness as default
#endif
Expand All @@ -95,8 +99,8 @@

/*-------------------DEFINE MQTT TOPIC FOR CONFIG----------------------*/

#define subjectMQTTtoSSD1306set "/commands/MQTTtoSSD1306"
#define subjectSSD1306toMQTTset "/SSD1306toMQTT"
#define subjectMQTTtoSSD1306 "/commands/MQTTtoSSD1306"
#define subjectSSD1306toMQTT "/SSD1306toMQTT"

/*-------------------EXTERNAL FUNCTIONS----------------------*/

Expand Down
3 changes: 3 additions & 0 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,9 @@ void loop() {
# endif
# ifdef ZactuatorONOFF
stateONOFFMeasures();
# endif
# ifdef ZdisplaySSD1306
stateSSD1306Display();
# endif
}
#endif
Expand Down

0 comments on commit a07a848

Please sign in to comment.