JXCT Soil Sensor 7-in-1 v3.4.9 (June 2025)
Professional IoT soil monitoring system with ESP32, Modbus RTU, MQTT, and advanced compensation algorithms
Загрузка...
Поиск...
Не найдено
Файл mqtt_client.cpp

Взаимодействие с MQTT-брокером и Home Assistant. Подробнее...

#include <Arduino.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "modbus_sensor.h"
#include "wifi_manager.h"
#include <WiFiClient.h>
#include "jxct_device_info.h"
#include "jxct_config_vars.h"
#include "jxct_format_utils.h"
#include "logger.h"
#include "debug.h"
#include "jxct_constants.h"
#include <NTPClient.h>
#include "ota_manager.h"

См. исходные тексты.

Классы

struct  HomeAssistantConfigCache
 
struct  DNSCache
 

Функции

PubSubClient mqttClient (espClient)
 
const char * getMqttLastError ()
 
void mqttCallback (char *topic, byte *payload, unsigned int length)
 
void publishHomeAssistantConfig ()
 
void invalidateHAConfigCache ()
 
IPAddress getCachedIP (const char *hostname)
 
const char * getClientId ()
 
const char * getMqttClientName ()
 
const char * getStatusTopic ()
 
const char * getCommandTopic ()
 
static const char * getOtaStatusTopic ()
 
static const char * getOtaCommandTopic ()
 
void publishAvailability (bool online)
 
void setupMQTT ()
 
bool connectMQTT ()
 
void handleMQTT ()
 
bool shouldPublishMqtt ()
 
void publishSensorData ()
 
void removeHomeAssistantConfig ()
 
void handleMqttCommand (const String &cmd)
 

Переменные

NTPClient * timeClient
 
WiFiClient espClient
 
bool mqttConnected = false
 
static char mqttLastErrorBuffer [128] = ""
 
static char clientIdBuffer [32] = ""
 
static char statusTopicBuffer [128] = ""
 
static char commandTopicBuffer [128] = ""
 
static char otaStatusTopicBuffer [128] = ""
 
static char otaCommandTopicBuffer [128] = ""
 
static struct HomeAssistantConfigCache haConfigCache = {"", "", "", "", "", "", "", false, "", ""}
 
static char pubTopicCache [7][128] = {"", "", "", "", "", "", ""}
 
static bool pubTopicCacheValid = false
 
static char cachedSensorJson [256] = ""
 
static unsigned long lastCachedSensorTime = 0
 
static bool sensorJsonCacheValid = false
 
struct DNSCache dnsCacheMqtt = {"", IPAddress(0, 0, 0, 0), 0, false}
 

Подробное описание

Взаимодействие с MQTT-брокером и Home Assistant.

Реализация подключения, публикации данных, обработки команд и интеграции с Home Assistant через discovery.

См. определение в файле mqtt_client.cpp

Функции

◆ connectMQTT()

bool connectMQTT ( )

◆ getCachedIP()

IPAddress getCachedIP ( const char * hostname)

См. определение в файле mqtt_client.cpp строка 752

Перекрестные ссылки DEBUG_PRINTF, DNS_CACHE_TTL и dnsCacheMqtt.

Используется в setupMQTT().

◆ getClientId()

const char * getClientId ( )

См. определение в файле mqtt_client.cpp строка 81

Перекрестные ссылки clientIdBuffer.

Используется в getMqttClientName().

◆ getCommandTopic()

const char * getCommandTopic ( )

См. определение в файле mqtt_client.cpp строка 117

Перекрестные ссылки commandTopicBuffer и config.

Используется в connectMQTT() и mqttCallback().

◆ getMqttClientName()

const char * getMqttClientName ( )

См. определение в файле mqtt_client.cpp строка 94

Перекрестные ссылки config и getClientId().

Используется в connectMQTT().

◆ getMqttLastError()

const char * getMqttLastError ( )

См. определение в файле mqtt_client.cpp строка 28

Перекрестные ссылки mqttLastErrorBuffer.

Используется в sendHealthJson() и sendServiceStatusJson().

◆ getOtaCommandTopic()

static const char * getOtaCommandTopic ( )
static

См. определение в файле mqtt_client.cpp строка 134

Перекрестные ссылки config и otaCommandTopicBuffer.

Используется в connectMQTT() и mqttCallback().

◆ getOtaStatusTopic()

static const char * getOtaStatusTopic ( )
static

См. определение в файле mqtt_client.cpp строка 127

Перекрестные ссылки config и otaStatusTopicBuffer.

Используется в handleMQTT().

◆ getStatusTopic()

const char * getStatusTopic ( )

См. определение в файле mqtt_client.cpp строка 107

Перекрестные ссылки config и statusTopicBuffer.

Используется в connectMQTT() и publishAvailability().

◆ handleMQTT()

void handleMQTT ( )

См. определение в файле mqtt_client.cpp строка 311

Перекрестные ссылки config, connectMQTT(), getOtaStatus(), getOtaStatusTopic(), logMQTT(), logSuccess(), logWarn() и mqttClient.

Используется в loop().

◆ handleMqttCommand()

void handleMqttCommand ( const String & cmd)

См. определение в файле mqtt_client.cpp строка 674

Перекрестные ссылки config, DEBUG_PRINT, DEBUG_PRINTLN, handleOTA(), publishAvailability(), publishHomeAssistantConfig(), publishSensorData(), removeHomeAssistantConfig(), resetConfig(), saveConfig() и triggerOtaCheck().

Используется в mqttCallback().

◆ invalidateHAConfigCache()

void invalidateHAConfigCache ( )

См. определение в файле mqtt_client.cpp строка 734

Перекрестные ссылки haConfigCache, INFO_PRINTLN и mqttLastErrorBuffer.

Используется в saveConfig().

◆ mqttCallback()

void mqttCallback ( char * topic,
byte * payload,
unsigned int length )

См. определение в файле mqtt_client.cpp строка 722

Перекрестные ссылки DEBUG_PRINTF, getCommandTopic(), getOtaCommandTopic() и handleMqttCommand().

Используется в setupMQTT().

◆ mqttClient()

PubSubClient mqttClient ( espClient )

Перекрестные ссылки espClient.

◆ publishAvailability()

void publishAvailability ( bool online)

См. определение в файле mqtt_client.cpp строка 141

Перекрестные ссылки DEBUG_PRINTF, getStatusTopic() и mqttClient.

Используется в connectMQTT() и handleMqttCommand().

◆ publishHomeAssistantConfig()

void publishHomeAssistantConfig ( )

См. определение в файле mqtt_client.cpp строка 521

Перекрестные ссылки config, DEBUG_PRINTLN, DEVICE_MANUFACTURER, DEVICE_MODEL, DEVICE_SW_VERSION, getDeviceId(), haConfigCache, INFO_PRINTLN, mqttClient, mqttLastErrorBuffer, pubTopicCache и pubTopicCacheValid.

Используется в connectMQTT() и handleMqttCommand().

◆ publishSensorData()

void publishSensorData ( )

См. определение в файле mqtt_client.cpp строка 440

Перекрестные ссылки cachedSensorJson, config, DEBUG_PRINTLN, lastCachedSensorTime, mqttClient, mqttLastErrorBuffer, sensorData, sensorJsonCacheValid, shouldPublishMqtt() и timeClient.

Используется в handleMqttCommand() и loop().

◆ removeHomeAssistantConfig()

void removeHomeAssistantConfig ( )

См. определение в файле mqtt_client.cpp строка 658

Перекрестные ссылки getDeviceId(), INFO_PRINTLN, mqttClient и mqttLastErrorBuffer.

Используется в handleMqttCommand().

◆ setupMQTT()

void setupMQTT ( )

См. определение в файле mqtt_client.cpp строка 149

Перекрестные ссылки config, DEBUG_PRINTF, DEBUG_PRINTLN, ERROR_PRINTF, ERROR_PRINTLN, getCachedIP(), INFO_PRINTLN, mqttCallback(), mqttClient и mqttLastErrorBuffer.

Используется в setup().

◆ shouldPublishMqtt()

bool shouldPublishMqtt ( )

См. определение в файле mqtt_client.cpp строка 364

Перекрестные ссылки config, DEBUG_PRINTF, DEBUG_PRINTLN и sensorData.

Используется в publishSensorData().

Переменные

◆ cachedSensorJson

char cachedSensorJson[256] = ""
static

См. определение в файле mqtt_client.cpp строка 61

Используется в publishSensorData().

◆ clientIdBuffer

char clientIdBuffer[32] = ""
static

См. определение в файле mqtt_client.cpp строка 34

Используется в getClientId().

◆ commandTopicBuffer

char commandTopicBuffer[128] = ""
static

См. определение в файле mqtt_client.cpp строка 36

Используется в getCommandTopic().

◆ dnsCacheMqtt

struct DNSCache dnsCacheMqtt = {"", IPAddress(0, 0, 0, 0), 0, false}

Используется в getCachedIP().

◆ espClient

WiFiClient espClient

См. определение в файле mqtt_client.cpp строка 22

Используется в mqttClient() и setup().

◆ haConfigCache

struct HomeAssistantConfigCache haConfigCache = {"", "", "", "", "", "", "", false, "", ""}
static

◆ lastCachedSensorTime

unsigned long lastCachedSensorTime = 0
static

См. определение в файле mqtt_client.cpp строка 62

Используется в publishSensorData().

◆ mqttConnected

bool mqttConnected = false

См. определение в файле mqtt_client.cpp строка 24

◆ mqttLastErrorBuffer

char mqttLastErrorBuffer[128] = ""
static

◆ otaCommandTopicBuffer

char otaCommandTopicBuffer[128] = ""
static

См. определение в файле mqtt_client.cpp строка 38

Используется в getOtaCommandTopic().

◆ otaStatusTopicBuffer

char otaStatusTopicBuffer[128] = ""
static

См. определение в файле mqtt_client.cpp строка 37

Используется в getOtaStatusTopic().

◆ pubTopicCache

char pubTopicCache[7][128] = {"", "", "", "", "", "", ""}
static

См. определение в файле mqtt_client.cpp строка 57

Используется в publishHomeAssistantConfig().

◆ pubTopicCacheValid

bool pubTopicCacheValid = false
static

См. определение в файле mqtt_client.cpp строка 58

Используется в publishHomeAssistantConfig().

◆ sensorJsonCacheValid

bool sensorJsonCacheValid = false
static

См. определение в файле mqtt_client.cpp строка 63

Используется в publishSensorData().

◆ statusTopicBuffer

char statusTopicBuffer[128] = ""
static

См. определение в файле mqtt_client.cpp строка 35

Используется в getStatusTopic().

◆ timeClient

NTPClient* timeClient
extern

См. определение в файле main.cpp строка 43