Skip to content

Commit

Permalink
Add support for MQTT birth message (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
sidoh authored Mar 24, 2019
1 parent a31f9ed commit 45c7c28
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dist/index.html.gz.h

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions lib/MQTT/MqttClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ArduinoJson.h>
#include <WiFiClient.h>
#include <MiLightRadioConfig.h>
#include <AboutStringHelper.h>

MqttClient::MqttClient(Settings& settings, MiLightClient*& milightClient)
: milightClient(milightClient),
Expand Down Expand Up @@ -80,6 +81,13 @@ bool MqttClient::connect() {
}
}

void MqttClient::sendBirthMessage() {
if (settings.mqttBirthTopic.length() > 0) {
String aboutStr = AboutStringHelper::generateAboutString(true);
mqttClient->publish(settings.mqttBirthTopic.c_str(), aboutStr.c_str());
}
}

void MqttClient::reconnect() {
if (lastConnectAttempt > 0 && (millis() - lastConnectAttempt) < MQTT_CONNECTION_ATTEMPT_FREQUENCY) {
return;
Expand All @@ -88,6 +96,7 @@ void MqttClient::reconnect() {
if (! mqttClient->connected()) {
if (connect()) {
subscribe();
sendBirthMessage();

#ifdef MQTT_DEBUG
Serial.println(F("MqttClient - Successfully connected to MQTT server"));
Expand Down
1 change: 1 addition & 0 deletions lib/MQTT/MqttClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MqttClient {
char* domain;
unsigned long lastConnectAttempt;

void sendBirthMessage();
bool connect();
void subscribe();
void publishCallback(char* topic, byte* payload, int length);
Expand Down
25 changes: 25 additions & 0 deletions lib/Settings/AboutStringHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <AboutStringHelper.h>
#include <ArduinoJson.h>
#include <Settings.h>
#include <ESP8266WiFi.h>

String AboutStringHelper::generateAboutString(bool abbreviated) {
DynamicJsonBuffer buffer;
JsonObject& response = buffer.createObject();

response["firmware"] = QUOTE(FIRMWARE_NAME);
response["version"] = QUOTE(MILIGHT_HUB_VERSION);
response["ip_address"] = WiFi.localIP().toString();
response["reset_reason"] = ESP.getResetReason();

if (! abbreviated) {
response["variant"] = QUOTE(FIRMWARE_VARIANT);
response["free_heap"] = ESP.getFreeHeap();
response["arduino_version"] = ESP.getCoreVersion();
}

String body;
response.printTo(body);

return body;
}
11 changes: 11 additions & 0 deletions lib/Settings/AboutStringHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <Arduino.h>

#ifndef _ABOUT_STRING_HELPER_H
#define _ABOUT_STRING_HELPER_H

class AboutStringHelper {
public:
static String generateAboutString(bool abbreviated = false);
};

#endif
2 changes: 2 additions & 0 deletions lib/Settings/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void Settings::patch(JsonObject& parsedSettings) {
this->setIfPresent(parsedSettings, "mqtt_state_topic_pattern", mqttStateTopicPattern);
this->setIfPresent(parsedSettings, "mqtt_lwt_topic", mqttLwtTopic);
this->setIfPresent(parsedSettings, "mqtt_lwt_message", mqttLwtMessage);
this->setIfPresent(parsedSettings, "mqtt_birth_topic", mqttBirthTopic);
this->setIfPresent(parsedSettings, "discovery_port", discoveryPort);
this->setIfPresent(parsedSettings, "listen_repeats", listenRepeats);
this->setIfPresent(parsedSettings, "state_flush_interval", stateFlushInterval);
Expand Down Expand Up @@ -211,6 +212,7 @@ void Settings::serialize(Stream& stream, const bool prettyPrint) {
root["mqtt_state_topic_pattern"] = this->mqttStateTopicPattern;
root["mqtt_lwt_topic"] = this->mqttLwtTopic;
root["mqtt_lwt_message"] = this->mqttLwtMessage;
root["mqtt_birth_topic"] = this->mqttBirthTopic;
root["discovery_port"] = this->discoveryPort;
root["listen_repeats"] = this->listenRepeats;
root["state_flush_interval"] = this->stateFlushInterval;
Expand Down
5 changes: 5 additions & 0 deletions lib/Settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#define XQUOTE(x) #x
#define QUOTE(x) XQUOTE(x)

#ifndef FIRMWARE_NAME
#define FIRMWARE_NAME unknown
#endif

#ifndef FIRMWARE_VARIANT
#define FIRMWARE_VARIANT unknown
#endif
Expand Down Expand Up @@ -162,6 +166,7 @@ class Settings {
String mqttStateTopicPattern;
String mqttLwtTopic;
String mqttLwtMessage;
String mqttBirthTopic;
GroupStateField *groupStateFields;
size_t numGroupStateFields;
uint16_t discoveryPort;
Expand Down
21 changes: 11 additions & 10 deletions lib/WebServer/MiLightHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <MiLightRadioConfig.h>
#include <string.h>
#include <TokenIterator.h>
#include <AboutStringHelper.h>
#include <index.html.gz.h>

void MiLightHttpServer::begin() {
Expand Down Expand Up @@ -114,19 +115,19 @@ void MiLightHttpServer::onSettingsSaved(SettingsSavedHandler handler) {
}

void MiLightHttpServer::handleAbout() {
DynamicJsonBuffer buffer;
JsonObject& response = buffer.createObject();
// DynamicJsonBuffer buffer;
// JsonObject& response = buffer.createObject();

response["version"] = QUOTE(MILIGHT_HUB_VERSION);
response["variant"] = QUOTE(FIRMWARE_VARIANT);
response["free_heap"] = ESP.getFreeHeap();
response["arduino_version"] = ESP.getCoreVersion();
response["reset_reason"] = ESP.getResetReason();
// response["version"] = QUOTE(MILIGHT_HUB_VERSION);
// response["variant"] = QUOTE(FIRMWARE_VARIANT);
// response["free_heap"] = ESP.getFreeHeap();
// response["arduino_version"] = ESP.getCoreVersion();
// response["reset_reason"] = ESP.getResetReason();

String body;
response.printTo(body);
// String body;
// response.printTo(body);

server.send(200, APPLICATION_JSON, body);
server.send(200, APPLICATION_JSON, AboutStringHelper::generateAboutString());
}

void MiLightHttpServer::handleGetRadioConfigs() {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ lib_deps_external =
CircularBuffer@~1.2.0
extra_scripts =
pre:.build_web.py
build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 -DHTTP_UPLOAD_BUFLEN=128 -Idist -Ilib/DataStructures
build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 -DHTTP_UPLOAD_BUFLEN=128 -D FIRMWARE_NAME=milight-hub -Idist -Ilib/DataStructures
# -D DEBUG_PRINTF
# -D MQTT_DEBUG
# -D MILIGHT_UDP_DEBUG
Expand Down
6 changes: 6 additions & 0 deletions web/src/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ var UI_FIELDS = [ {
help: "LWT Message - sent when client disconnects uncleanly",
type: "string",
tab: "tab-mqtt"
}, {
tag: "mqtt_birth_topic",
friendly: "MQTT Birth Topic",
help: "Birth Topic - JSON blob with system details will be sent to this topic upon connection",
type: "string",
tab: "tab-mqtt"
}, {
tag: "radio_interface_type",
friendly: "Radio interface type",
Expand Down

0 comments on commit 45c7c28

Please sign in to comment.