openshwprojects / OpenBK7231T_App

Open source firmware (Tasmota/Esphome replacement) for BK7231T, BK7231N, BL2028N, T34, XR809, W800/W801, W600/W601 and BL602
https://openbekeniot.github.io/webapp/devicesList.html
1.39k stars 241 forks source link

[Feature Request] MQTT: custom base topic #295

Open saper-2 opened 1 year ago

saper-2 commented 1 year ago

Hello,

Is possible to set custom MQTT "base topic" ?

For now all MQTT is going under topic /<client-id>/... , but I want it to go under e.g.: /the/my_custom/base/topic , so assembled topics would looks like:

/the/my_custom/base/topic/<client-id>/connected
/the/my_custom/base/topic/<client-id>/IP/get
/the/my_custom/base/topic/<client-id>/0/get
/the/my_custom/base/topic/<client-id>/1/set
/the/my_custom/base/topic/<client-id>/1/get
...

To add this functionality:

  1. create a setting (under MQTT) "Base Topic", char[31] - should be sufficient, and store this with other settings,
  2. In line https://github.com/openshwprojects/OpenBK7231T_App/blob/c8074c00825911e6ff23a53cdd8302ccc5aa0aff/src/mqtt/new_mqtt.c#L584 , in place CFG_GetMQTTClientId() first assemble the topic to string <base-topic>/<client-id>, then pass it to the function in 2nd argument, /or/ just create new function that read this base topic from configuration and return it e.g.: const char* CFG_GetMQTTBaseTopic(), and use it instead of CFG_GetMQTTClientId (and in places where topic is used)...
  3. I also noticed, there is few other places too (e.g. sprintf) where <client-id> is used as topic (seems like debug purposes?), and it is used "directly" in will_topic in routine MQTT_do_connect .

This basically related to issue #48 but more simplified.

This is probably the only thing that keeps me away from anything that is non-ESP based - I just hate to have junkyard in my MQTT server(broker ; I have everything there organized) ...

openshwprojects commented 1 year ago

Hey, we can do this, but since you already proposed a solution, maybe just create a pull request and I will integrate it?

This is a good feature.

iprak commented 1 year ago

We would want to make sure that the buffers involved are big enough to hold the additional base topic.

saper-2 commented 1 year ago

Hi,

I noticed it, I think I saw this with get topic - there is calculation of buffer done with some "hard" coded lengths. This is something that need attention too while writing changes/code.

For now, I'm not up to the task πŸ˜‰, I have other things to take care of after work. Also setting up env for building will take me a bit of time, since I never touched this bkt chips.

openshwprojects commented 1 year ago

@saper-2 EDIT: I looked at the code and adding a base topic field would be simpler Wait a sec

@saper-2 do you want cmnd/%s/ to be transformed to lights/cmnd/%s/ ?basically, prepend everything with a string?

or would you expect the cmnd/%s/ to be transformed to cmnd/lights/%s/ ?

EDIT: wouldn't just setting in that way in current build work? image

iprak commented 1 year ago

I do not think this will work with OpenBK because the status and cmd are on different root topics. I think what was being asked wa something like this where status and command are based on the pre-defined root topic. image

saper-2 commented 1 year ago

@saper-2 do you want cmnd/%s/ to be transformed to lights/cmnd/%s/ ?basically, prepend everything with a string?

Yes, exactly πŸ˜„ , so I could set a root-topic like @iprak named it.

or would you expect the cmnd/%s/ to be transformed to cmnd/lights/%s/ ?

I think it would be wiser in this case to go completely into "tasmota style" where we can define how the whole topic would we want to look like.


Here I will give a brief explanation about tasmota topics/prefixes and sub-topics πŸ˜‰

This is just for "reference" , I know implementing this would require a lot of time, so I not even try asking for this πŸ˜…

Here is how tasmota config looks like:

image

And here is a bit of explanation: %topic% is device name - in OpenBK client-id is used as "topic", %prefix% "group" of subtopics in 3 categories:

image

I know, I wasted my time but.... 😝


EDIT: wouldn't just setting in that way in current build work? image

hmmm it didn't worked last time... works but I'd call it "half-solution" image

Why half? Because client-id is a "client id" not really a way to create topic (root topic) - this can be confusing. I'd leave client-id as client-id and wouldn't try to mix it with topic (base topic/root topic). Or maybe just change currently used client-id to root-topic ,and mqtt client-id create from FullName with appended last 4 or 6 digits of MAC (hex format)

saper-2 commented 1 year ago

Hi,

I just noticed: publishing to topic: home/bkt/NousA1/1/set , value 1 with client set to home/bkt/NousA1 - I can't control relay via MQTT . So Client can't be used to set topic.

mvadu commented 10 months ago

I just installed this on a new device, and trying to move my setup I use for Tasmota to this. I see that mqtt config screen does have custom base topic support. But in Tasmota we can set this up via commands too, and I am not sure if that is possible yet.

image

Tasmote commands