Skip to content

Commit

Permalink
Added if-defs around code for MQTT - Updated README.MD (#107)
Browse files Browse the repository at this point in the history
* Added ifdefs around all code for MQTT stuff, because compiling fails, if some of the defines starting with MQTT_ are missing/uncommented and it uses unneeded space, if MQTT_ENABLE is not defined at all - Updated README.MD file - MQTT TLS notes added - minor fixes in the README file.

* Refactor MQTT preprocessor directives to use #ifdef instead of #if defined for consistency
  • Loading branch information
Martinius79 authored Dec 9, 2024
1 parent 2420502 commit c0d3e2e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 3 additions & 1 deletion EleksTubeHAX_pio/src/Mqtt_client_ips.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*/

#include "Mqtt_client_ips.h"
#include <WiFi.h> // for ESP32
#ifdef MQTT_ENABLED
#include "WiFi.h" // for ESP32
#include <PubSubClient.h> // Download and install this library first from: https://www.arduinolibraries.info/libraries/pub-sub-client
#include <ArduinoJson.h>
#include "TempSensor.h"
Expand Down Expand Up @@ -1043,3 +1044,4 @@ void MqttPeriodicReportBack()
MqttReportBackEverything(true);
}
}
#endif
3 changes: 3 additions & 0 deletions EleksTubeHAX_pio/src/Mqtt_client_ips.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "GLOBAL_DEFINES.h"
#include <FS.h>

#ifdef MQTT_ENABLED
extern bool MqttConnected;

// commands from server
Expand Down Expand Up @@ -68,4 +69,6 @@ void MqttLoopFrequently();
void MqttLoopInFreeTime();
void MqttReportBackEverything(bool force);

#endif // MQTT_ENABLED

#endif /* mqtt_client_H_ */
14 changes: 8 additions & 6 deletions EleksTubeHAX_pio/src/TFTs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ void TFTs::setDigit(uint8_t digit, uint8_t value, show_t show)
showNoWifiStatus();
}

#ifdef MQTT_ENABLED
if (digit == SECONDS_TENS)
if (!MqttConnected)
{
showNoMqttStatus();
}
#endif

if (digit == HOURS_ONES)
{
Expand Down Expand Up @@ -545,18 +547,18 @@ void TFTs::DrawImage(uint8_t file_index)
uint16_t TFTs::read16(fs::File &f)
{
uint16_t result;
reinterpret_cast<uint8_t*>(&result)[0] = f.read(); // LSB
reinterpret_cast<uint8_t*>(&result)[1] = f.read(); // MSB
reinterpret_cast<uint8_t *>(&result)[0] = f.read(); // LSB
reinterpret_cast<uint8_t *>(&result)[1] = f.read(); // MSB
return result;
}

uint32_t TFTs::read32(fs::File &f)
{
uint32_t result;
reinterpret_cast<uint8_t*>(&result)[0] = f.read(); // LSB
reinterpret_cast<uint8_t*>(&result)[1] = f.read();
reinterpret_cast<uint8_t*>(&result)[2] = f.read();
reinterpret_cast<uint8_t*>(&result)[3] = f.read(); // MSB
reinterpret_cast<uint8_t *>(&result)[0] = f.read(); // LSB
reinterpret_cast<uint8_t *>(&result)[1] = f.read();
reinterpret_cast<uint8_t *>(&result)[2] = f.read();
reinterpret_cast<uint8_t *>(&result)[3] = f.read(); // MSB
return result;
}

Expand Down
8 changes: 8 additions & 0 deletions EleksTubeHAX_pio/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "Menu.h"
#include "StoredConfig.h"
#include "WiFi_WPS.h"
#ifdef MQTT_ENABLED
#include "Mqtt_client_ips.h"
#endif
#include "TempSensor_inc.h"
#ifdef HARDWARE_NovelLife_SE_CLOCK // NovelLife_SE Clone XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// #include "Gestures.h"
Expand Down Expand Up @@ -123,6 +125,7 @@ void setup()
Serial.println("Done!");
tfts.setTextColor(TFT_WHITE, TFT_BLACK);

#ifdef MQTT_ENABLED
// Setup MQTT
tfts.setTextColor(TFT_YELLOW, TFT_BLACK);
tfts.print("MQTT start...");
Expand All @@ -131,6 +134,7 @@ void setup()
tfts.println("Done!");
Serial.println("MQTT start Done!");
tfts.setTextColor(TFT_WHITE, TFT_BLACK);
#endif

#ifdef GEOLOCATION_ENABLED
tfts.setTextColor(TFT_NAVY, TFT_BLACK);
Expand Down Expand Up @@ -195,6 +199,7 @@ void loop()
// Do all the maintenance work
WifiReconnect(); // if not connected attempt to reconnect

#ifdef MQTT_ENABLED
MqttLoopFrequently();

bool MqttCommandReceived =
Expand Down Expand Up @@ -433,6 +438,7 @@ void loop()
Serial.println(" Done.");
}
}
#endif

buttons.loop();

Expand Down Expand Up @@ -713,7 +719,9 @@ void loop()
time_in_loop = millis() - millis_at_top;
if (time_in_loop < 20)
{
#ifdef MQTT_ENABLED
MqttLoopInFreeTime();
#endif
PeriodicReadTemperature();
if (bTemperatureUpdated)
{
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Some clocks needs a button pressed while the powering phase (plugging the USB ca

If you just want to use this firmware without setting up the development tools and libraries and everything, navigate to folder `\pre-built-firmware\` and modify `_ESP32 write flash.cmd` to upload the version for your clock.

More info can be found in the `\pre-built-firmware\` folder in the [README.MD](pre-built-firmware\README.md).
More info can be found in the `\pre-built-firmware\` folder in the [README.MD](pre-built-firmware/README.md).
Like:

- Features enabled in the images
Expand Down Expand Up @@ -194,7 +194,7 @@ For more info ask your favorite search engine or AI Chatbot :)
### 4.6 Lost firmware file

If you lost your orginal firmware file and wants to restore the orginal firmware you normaly have a problem, because even if you get a backup file from another user, it is locked to the MAC address of the other ESP32 MAC.
Under some conditions, it should be possible to change the encoded MAC address in the firmware. This worked for EleksTube Gen1 clocks. See [Issue 8]('https://github.com/SmittyHalibut/EleksTubeHAX/issues/8').
Under some conditions, it should be possible to change the encoded MAC address in the firmware. This worked for EleksTube Gen1 clocks. See [Issue 8](https://github.com/SmittyHalibut/EleksTubeHAX/issues/8).

Note: There is no gurantee, that you are able to change the MAC address in the firmware file successfully! If the orginal manufacturer decides to encode the MAC address or hide it, the descriped method will not work!

Expand Down Expand Up @@ -467,11 +467,11 @@ Note: The `_USER_DEFINES.h` is included in the default `.gitignore` file, so tha

If you want to integrate the clock into your Home Assistant, you need to make sure, that Home Assistant and the clock uses the same MQTT broker.

Currently, you will need a local MQTT broker for this.

Normally you will already have a MQTT broker running locally, which supports the HA discovery and communication messages, like Mosquitto.
If not done already, you can set it up easily via an Add-On in HA.
See: [Home Assistant MQTT Integration]("https://www.home-assistant.io/integrations/mqtt/")
See: [Home Assistant MQTT Integration](https://www.home-assistant.io/integrations/mqtt/)

You can also use an internet-based broker which supports "Home Assistant messages" (topics) for discovery and communication, like HiveMQ.

If you set up the broker, you first need to enter the used IP, port, username and password of it into the matching `MQTT_*` defines in your `_USER_DEFINES.h` (MQTT config section). You have to define `MQTT_ENABLED` as well.

Expand All @@ -481,16 +481,14 @@ After flashing the firmware, the clock will first try connect to the WiFi and th

If the connection was successfull the clock sends the discovery messages for HA and the device should be vissible in the MQTT integration.

Note: In the moment, no known internet-based/non-local MQTT broker, which supports HA discovery and communication messages is useable. This may/will change in the future.

Note: If you change your used clock faces in the data folder, the `clockfaces.txt` in the `data` folder needs to be changed too. This is, because the shown names in HA for the clock faces under "Main light" are hard coded and read from there.

Note: `#define MQTT_CLIENT` is used as a unique device name (i.e. it should be different if you have several IPS clocks) and is the "root" part of the topic for all entities that will be interacted with via MQTT.

In the future: If you want to use an internet-based broker, you can use i.e. HiveMQ. You will need to create an account there and set it up in HA and in here.
See: [Connect HA to HiveMQ]("https://www.hivemq.com/blog/connect-home-assistant-to-hivemq-cloud/")
Note: If you want to use an internet-based broker, you can use HiveMQ. You will need to create an account there and set it up in HA and in here. 'MQTT_USE_TLS' must be defined, because HiveMQ only supports encrypted connections. The HiveMQ TLS cert is based on the root CA of Let's Encrypt, so you also need to copy the 'mqtt-ca-root.pem' file from the 'data - other graphics' folder into the 'data' folder of the PIO project and upload the data partiton (file system) with the changed app partition. Other brokers or your localy used cert for your MQTT broker may need another root CA to be set.
See: [Connect HA to HiveMQ](https://www.hivemq.com/blog/connect-home-assistant-to-hivemq-cloud/)

##### 5.6.4.1.2 Used integration
##### 5.6.4.1.2 Used integrations

Interactions between the firmware of the clock and Home Assistant is done like descriped in the MQTT integration documentations (see below).

Expand Down Expand Up @@ -536,7 +534,7 @@ The settings for the LED modes and the general clock settings can only be set fr

##### 5.6.5.2.1 Setup

If the `MQTT_HOME_ASSISTANT` define is not enabled, just `MQTT_ENABLED`, the MQTT support is very limited in the moment!
If the `MQTT_HOME_ASSISTANT` define is NOT enabled, just `MQTT_ENABLED`, the MQTT support is very limited in the moment!

You can still use MQTT to control the clock, but only via direct MQTT messages, sent from a MQTT client (like MQTT Explorer or similar or from a WebUI/API of the internet-based broker).

Expand All @@ -550,6 +548,8 @@ If you choose an internet based one, you will need to create an account, (maybe

If you choose a local one, you will need to set up the broker on your local network and do the same.

Note: If you want to use encrypted connection to your broker, you need to enable 'MQTT_USE_TLS' and copy a valid root CA cert file to the 'data' folder. See the notes under the HA section for HiveMQ above.

#### 5.6.5.3 Debugging MQTT

Further analysis or debugging problems with the MQTT code is easier to perform using a MQTT message reader tool, for example, "MQTT Explorer", a common HA Add-On for users, who prefer manual configuration.
Expand Down

0 comments on commit c0d3e2e

Please sign in to comment.