Implement MQTT discovery for Home Assistant

This commit is contained in:
Kai Gerken 2022-07-02 20:24:55 +02:00
parent b1f09d482f
commit bbc725f316
6 changed files with 105 additions and 11 deletions

View file

@ -29,6 +29,7 @@ class mqtt {
DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup"));
mAddressSet = true;
mClient->setServer(broker, port);
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
mPort = port;
snprintf(mUser, MQTT_USER_LEN, "%s", user);
@ -38,14 +39,17 @@ class mqtt {
void sendMsg(const char *topic, const char *msg) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
if(mAddressSet) {
char top[64];
snprintf(top, 64, "%s/%s", mTopic, topic);
char top[64];
snprintf(top, 64, "%s/%s", mTopic, topic);
sendMsg2(top, msg);
}
void sendMsg2(const char *topic, const char *msg) {
if(mAddressSet) {
if(!mClient->connected())
reconnect();
if(mClient->connected())
mClient->publish(top, msg);
mClient->publish(topic, msg);
}
}