add will message on <device>/status topic, so status is always available even when offline.
add wifi/bssid topic to track which AP is connected fix reconnect breaking out of loop
This commit is contained in:
parent
67f1d03919
commit
adf6b38bfb
2 changed files with 33 additions and 17 deletions
|
@ -5,18 +5,39 @@ TaskHandle_t mqttTaskHandle = NULL;
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
PubSubClient client(wifiClient);
|
PubSubClient client(wifiClient);
|
||||||
|
|
||||||
|
// avoid circular deps, just forward declare externs used here.
|
||||||
|
bool hasLightLevel();
|
||||||
|
float getLightLevel();
|
||||||
|
String getMyHostname();
|
||||||
|
|
||||||
void onMqttCallback(char* topic, byte* payload, unsigned int length)
|
void onMqttCallback(char* topic, byte* payload, unsigned int length)
|
||||||
{
|
{
|
||||||
Serial.println("MQTT message arrived");
|
Serial.println("MQTT message arrived");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const String getDeviceTopic()
|
||||||
|
{
|
||||||
|
const String hostname = getMyHostname();
|
||||||
|
const String rootTopic = preferences.getString("mqttRootTopic", DEFAULT_MQTT_ROOTTOPIC);
|
||||||
|
String fullTopic = rootTopic;
|
||||||
|
if (!rootTopic.endsWith("/") && rootTopic != "")
|
||||||
|
{
|
||||||
|
fullTopic += "/";
|
||||||
|
}
|
||||||
|
fullTopic += hostname + "/";
|
||||||
|
return String(fullTopic);
|
||||||
|
}
|
||||||
|
|
||||||
boolean connectMqtt()
|
boolean connectMqtt()
|
||||||
{
|
{
|
||||||
boolean result = client.connect("btclockClient");
|
const String willTopic = getDeviceTopic() + "status";
|
||||||
|
boolean result = client.connect("btclockClient", willTopic.c_str(), 0, true, "offline");
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
Serial.println("[MQTT] could not connect");
|
Serial.println("[MQTT] could not connect");
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
publish("status", "online", true);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,11 +64,6 @@ void setupMqtt()
|
||||||
connectMqtt();
|
connectMqtt();
|
||||||
}
|
}
|
||||||
|
|
||||||
// avoid circular deps, just forward declare externs used here.
|
|
||||||
bool hasLightLevel();
|
|
||||||
float getLightLevel();
|
|
||||||
String getMyHostname();
|
|
||||||
|
|
||||||
void mqttTask(void *pvParameters)
|
void mqttTask(void *pvParameters)
|
||||||
{
|
{
|
||||||
int t=0;
|
int t=0;
|
||||||
|
@ -61,7 +77,7 @@ void mqttTask(void *pvParameters)
|
||||||
{
|
{
|
||||||
// reconnect
|
// reconnect
|
||||||
if (!connectMqtt())
|
if (!connectMqtt())
|
||||||
return;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasLightLevel())
|
if (hasLightLevel())
|
||||||
|
@ -74,6 +90,7 @@ void mqttTask(void *pvParameters)
|
||||||
int8_t rssi = WiFi.RSSI();
|
int8_t rssi = WiFi.RSSI();
|
||||||
std::string rssi_s = std::to_string(static_cast<int>(rssi));
|
std::string rssi_s = std::to_string(static_cast<int>(rssi));
|
||||||
publish("wifi/rssi", rssi_s.c_str());
|
publish("wifi/rssi", rssi_s.c_str());
|
||||||
|
publish("wifi/bssid", WiFi.BSSIDstr().c_str());
|
||||||
}
|
}
|
||||||
std::string heap_free_s = std::to_string(static_cast<int>(ESP.getFreeHeap()));
|
std::string heap_free_s = std::to_string(static_cast<int>(ESP.getFreeHeap()));
|
||||||
publish("mem/heap_free", heap_free_s.c_str());
|
publish("mem/heap_free", heap_free_s.c_str());
|
||||||
|
@ -89,6 +106,11 @@ void setupMqttTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
void publish(const char *topic, const char *value)
|
void publish(const char *topic, const char *value)
|
||||||
|
{
|
||||||
|
publish(topic, value, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void publish(const char *topic, const char *value, boolean retain)
|
||||||
{
|
{
|
||||||
if (!client.connected())
|
if (!client.connected())
|
||||||
{
|
{
|
||||||
|
@ -96,16 +118,9 @@ void publish(const char *topic, const char *value)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const String hostname = getMyHostname();
|
const String fullTopic = getDeviceTopic() + topic;
|
||||||
const String rootTopic = preferences.getString("mqttRootTopic", DEFAULT_MQTT_ROOTTOPIC);
|
|
||||||
String fullTopic = rootTopic;
|
|
||||||
if (!rootTopic.endsWith("/") && rootTopic != "")
|
|
||||||
{
|
|
||||||
fullTopic += "/";
|
|
||||||
}
|
|
||||||
fullTopic += hostname + "/" + topic;
|
|
||||||
|
|
||||||
if (!client.publish(fullTopic.c_str(), value))
|
if (!client.publish(fullTopic.c_str(), value, retain))
|
||||||
{
|
{
|
||||||
Serial.println("[MQTT] could not write");
|
Serial.println("[MQTT] could not write");
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,4 +6,5 @@
|
||||||
|
|
||||||
void setupMqtt();
|
void setupMqtt();
|
||||||
void setupMqttTask();
|
void setupMqttTask();
|
||||||
void publish(const char *key, const char *value);
|
void publish(const char *topic, const char *value);
|
||||||
|
void publish(const char *topic, const char *value, boolean retain);
|
||||||
|
|
Loading…
Reference in a new issue