add mqtt module #2

Open
accumulator wants to merge 1 commit from accumulator/btclock_v3:mqtt_dev into main
First-time contributor

Add support for publishing device stats to MQTT. See also webui pull request.

Using this data in HomeAssistant requires some configuration (Note: there is an option to autoconfigure the settings below in HomeAssistant by publishing to a certain /config topic, but that is not done in this PR as I don't want to make the implementation specific for a consumer)

This is the snippet I use

---
  sensor:
    - name: "Light Sensor"
      state_class: "measurement"
      unique_id: "btclock_light_sensor"
      state_topic: "home/btclock-655690/sensors/lux"
      availability_topic: "home/btclock-655690/status"
      device:
        identifiers: "btclock-655690"
        name: "BTClock"
    - name: "Wifi signal strength"
      state_class: "measurement"
      unique_id: "btclock_wifi_strength"
      state_topic: "home/btclock-655690/wifi/rssi"
      availability_topic: "home/btclock-655690/status"
      device:
        identifiers: "btclock-655690"
        name: "BTClock"
    - name: "Wifi BSSID"
      unique_id: "btclock_wifi_bssid"
      state_topic: "home/btclock-655690/wifi/bssid"
      availability_topic: "home/btclock-655690/status"
      device:
        identifiers: "btclock-655690"
        name: "BTClock"
    - name: "Mem free"
      state_class: "measurement"
      unique_id: "btclock_mem_heap_free"
      state_topic: "home/btclock-655690/mem/heap_free"
      availability_topic: "home/btclock-655690/status"
      device:
        identifiers: "btclock-655690"
        name: "BTClock"
Add support for publishing device stats to MQTT. See also `webui` pull request. Using this data in HomeAssistant requires some configuration (Note: there is an option to autoconfigure the settings below in HomeAssistant by publishing to a certain `/config` topic, but that is not done in this PR as I don't want to make the implementation specific for a consumer) This is the snippet I use ```yaml --- sensor: - name: "Light Sensor" state_class: "measurement" unique_id: "btclock_light_sensor" state_topic: "home/btclock-655690/sensors/lux" availability_topic: "home/btclock-655690/status" device: identifiers: "btclock-655690" name: "BTClock" - name: "Wifi signal strength" state_class: "measurement" unique_id: "btclock_wifi_strength" state_topic: "home/btclock-655690/wifi/rssi" availability_topic: "home/btclock-655690/status" device: identifiers: "btclock-655690" name: "BTClock" - name: "Wifi BSSID" unique_id: "btclock_wifi_bssid" state_topic: "home/btclock-655690/wifi/bssid" availability_topic: "home/btclock-655690/status" device: identifiers: "btclock-655690" name: "BTClock" - name: "Mem free" state_class: "measurement" unique_id: "btclock_mem_heap_free" state_topic: "home/btclock-655690/mem/heap_free" availability_topic: "home/btclock-655690/status" device: identifiers: "btclock-655690" name: "BTClock" ```
accumulator added 1 commit 2024-12-02 14:05:07 +00:00
accumulator added 1 commit 2024-12-04 10:34:39 +00:00
accumulator force-pushed mqtt_dev from a2217bf52b to adf6b38bfb 2024-12-04 14:44:40 +00:00 Compare
Owner

Thanks for your PR! Very happy to see that you implemented it in the right locations and that you also updated the WebUI for it, good job!

I looked at it and I was wondering whether you also considered elims/PsychicMqttClient, since the library you now use hasn't been updated for 4 years and doesn't have SSL support which I think is desirable.

There are multiple supported targets, the Rev.A board doesn't have frontlight and no light sensor. When I tried to compile for the Rev.A target there were some errors because hasLightLevel() is not defined. You can add a guard to handle this:

#ifdef HAS_FRONTLIGHT
...
#endif

I think it's also good to just add a MQTT_HOST default to the defaults file, just for consistency.

Thanks for your PR! Very happy to see that you implemented it in the right locations and that you also updated the WebUI for it, good job! I looked at it and I was wondering whether you also considered [elims/PsychicMqttClient](https://github.com/theelims/PsychicMqttClient), since the library you now use hasn't been updated for 4 years and doesn't have SSL support which I think is desirable. There are multiple supported targets, the Rev.A board doesn't have frontlight and no light sensor. When I tried to compile for the Rev.A target there were some errors because hasLightLevel() is not defined. You can add a guard to handle this: ```cpp #ifdef HAS_FRONTLIGHT ... #endif ``` I think it's also good to just add a MQTT_HOST default to the defaults file, just for consistency.
Author
First-time contributor

Thanks for your PR! Very happy to see that you implemented it in the right locations and that you also updated the WebUI for it, good job!

I looked at it and I was wondering whether you also considered elims/PsychicMqttClient, since the library you now use hasn't been updated for 4 years and doesn't have SSL support which I think is desirable.

I took the MQTT client that is also used in the meshtastic project.

Not sure how SSL is handled in the ESP32 world, but I got the impression that there's a generic 'socket like wrapper' for secure connections in the form of WifiClientSecure (vs WifiClient for non-ssl)?

There are multiple supported targets, the Rev.A board doesn't have frontlight and no light sensor. When I tried to compile for the Rev.A target there were some errors because hasLightLevel() is not defined. You can add a guard to handle this:

#ifdef HAS_FRONTLIGHT
...
#endif

Ah yes.. I disabled building the other targets to speed up my testing :) will add.

I think it's also good to just add a MQTT_HOST default to the defaults file, just for consistency.

Will add.

> Thanks for your PR! Very happy to see that you implemented it in the right locations and that you also updated the WebUI for it, good job! > > I looked at it and I was wondering whether you also considered [elims/PsychicMqttClient](https://github.com/theelims/PsychicMqttClient), since the library you now use hasn't been updated for 4 years and doesn't have SSL support which I think is desirable. > I took the MQTT client that is also used in the meshtastic project. Not sure how SSL is handled in the ESP32 world, but I got the impression that there's a generic 'socket like wrapper' for secure connections in the form of `WifiClientSecure` (vs `WifiClient` for non-ssl)? > There are multiple supported targets, the Rev.A board doesn't have frontlight and no light sensor. When I tried to compile for the Rev.A target there were some errors because hasLightLevel() is not defined. You can add a guard to handle this: > > ```cpp > #ifdef HAS_FRONTLIGHT > ... > #endif > ``` Ah yes.. I disabled building the other targets to speed up my testing :) will add. > I think it's also good to just add a MQTT_HOST default to the defaults file, just for consistency. Will add.
accumulator added 1 commit 2024-12-06 10:10:03 +00:00
Author
First-time contributor

Instead of adding a SSL switch and user, password, port number fields in the config, I'd like to use a URL field (e.g. mqtt://user:pass@host:port or mqtts://user:pass@host:port). Do you know of a lib that can parse the URL and return its components?

Instead of adding a SSL switch and user, password, port number fields in the config, I'd like to use a URL field (e.g. `mqtt://user:pass@host:port` or `mqtts://user:pass@host:port`). Do you know of a lib that can parse the URL and return its components?
Owner

Do you know of a lib that can parse the URL and return its components?

You could use mqttString.startsWith("mqtts://") to check for SSL, and if so you could set the SSL certificate bundle similar like I do here:

client.setCACertBundle(rootca_crt_bundle_start);

> Do you know of a lib that can parse the URL and return its components? You could use `mqttString.startsWith("mqtts://")` to check for SSL, and if so you could set the SSL certificate bundle similar like I do here: https://git.btclock.dev/btclock/btclock_v3/src/commit/d6604d28d66a4e8458a8f412fff5a13c92c2599a/src/lib/ota.cpp#L112
Author
First-time contributor

Do you know of a lib that can parse the URL and return its components?

You could use mqttString.startsWith("mqtts://") to check for SSL, and if so you could set the SSL certificate bundle similar like I do here:

client.setCACertBundle(rootca_crt_bundle_start);

No, the point is to allow user, password, hostname, port number, ssl in a single config parameter, so I need to extract all these fields. simply testing with .startsWith is insufficient for anything beyond whether to use ssl.

In the meantime I hacked together a simple generic url parser that does just this, so stay tuned.

> > Do you know of a lib that can parse the URL and return its components? > > You could use `mqttString.startsWith("mqtts://")` to check for SSL, and if so you could set the SSL certificate bundle similar like I do here: > https://git.btclock.dev/btclock/btclock_v3/src/commit/d6604d28d66a4e8458a8f412fff5a13c92c2599a/src/lib/ota.cpp#L112 No, the point is to allow user, password, hostname, port number, ssl in a single config parameter, so I need to extract all these fields. simply testing with `.startsWith` is insufficient for anything beyond whether to use ssl. In the meantime I hacked together a simple generic url parser that does just this, so stay tuned.
accumulator force-pushed mqtt_dev from ee81edbcd2 to fa705e45e8 2024-12-20 11:00:50 +00:00 Compare
Author
First-time contributor

updated using elims/PsychicMqttClient, but this has compile problems, due to ESP_IDF_VERSION_MAJOR not being supported correctly.

Leaving this PR for now, as this is taking way too much time.

updated using `elims/PsychicMqttClient`, but this has compile problems, due to `ESP_IDF_VERSION_MAJOR` not being supported correctly. Leaving this PR for now, as this is taking way too much time.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u mqtt_dev:accumulator-mqtt_dev
git checkout accumulator-mqtt_dev

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git checkout main
git merge --no-ff accumulator-mqtt_dev
git checkout accumulator-mqtt_dev
git rebase main
git checkout main
git merge --ff-only accumulator-mqtt_dev
git checkout accumulator-mqtt_dev
git rebase main
git checkout main
git merge --no-ff accumulator-mqtt_dev
git checkout main
git merge --squash accumulator-mqtt_dev
git checkout main
git merge --ff-only accumulator-mqtt_dev
git checkout main
git merge accumulator-mqtt_dev
git push origin main
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: btclock/btclock_v3#2
No description provided.