Announce functionality added
This commit is contained in:
@@ -13,6 +13,8 @@ class MQTTHelper {
|
|||||||
WiFiClient _espClient;
|
WiFiClient _espClient;
|
||||||
PubSubClient _mqttClient;
|
PubSubClient _mqttClient;
|
||||||
String _will_topic;
|
String _will_topic;
|
||||||
|
String _command_topic;
|
||||||
|
String _announce_topic;
|
||||||
|
|
||||||
void reconnect(int delayMillis = 5000);
|
void reconnect(int delayMillis = 5000);
|
||||||
bool connect();
|
bool connect();
|
||||||
@@ -24,6 +26,8 @@ class MQTTHelper {
|
|||||||
deviceMac.replace(":","");
|
deviceMac.replace(":","");
|
||||||
_identifier = "etxean-"+deviceMac.substring(6);
|
_identifier = "etxean-"+deviceMac.substring(6);
|
||||||
_will_topic = getTopic("online");
|
_will_topic = getTopic("online");
|
||||||
|
_command_topic = _group + "/command";
|
||||||
|
_announce_topic = _group+"/announce";
|
||||||
_mqttClient.setCallback([this] (char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); });
|
_mqttClient.setCallback([this] (char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,7 +38,7 @@ class MQTTHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Configure(IPAddress server, uint16 port) {
|
void Configure(IPAddress server, uint16 port) {
|
||||||
Serial.println("Configuring MQTT connection to: "+server.toString()+":"+String(port));
|
Serial.println("MQTT: configuring connection to "+server.toString()+":"+String(port));
|
||||||
_mqttClient.setServer(server, port);
|
_mqttClient.setServer(server, port);
|
||||||
if (_mqttClient.connected()) {
|
if (_mqttClient.connected()) {
|
||||||
connect();
|
connect();
|
||||||
@@ -42,40 +46,48 @@ class MQTTHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
static unsigned long lastWill;
|
static unsigned long lastWillTopicSent;
|
||||||
if (!_mqttClient.connected()) {
|
if (!_mqttClient.connected()) {
|
||||||
reconnect();
|
reconnect();
|
||||||
}
|
}
|
||||||
_mqttClient.loop();
|
_mqttClient.loop();
|
||||||
|
|
||||||
auto now = millis();
|
auto now = millis();
|
||||||
if ((now - lastWill) > 1000)
|
if ((now - lastWillTopicSent) > 1000)
|
||||||
{
|
{
|
||||||
lastWill = now;
|
lastWillTopicSent = now;
|
||||||
_mqttClient.publish(_will_topic.c_str(), "true");
|
_mqttClient.publish(_will_topic.c_str(), "true");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
void publish(String sensor, T payload, bool retained = false) {
|
void publish(String node, T payload, bool retained = false, bool debug = true) {
|
||||||
_mqttClient.publish(getTopic(sensor).c_str(), String(payload).c_str(), retained);
|
auto topic = getTopic(node);
|
||||||
|
auto payload_s = String(payload);
|
||||||
|
if (debug)
|
||||||
|
Serial.println("MQTT: publish: "+topic+" = "+payload_s);
|
||||||
|
_mqttClient.publish(topic.c_str(), payload_s.c_str(), retained);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void MQTTHelper::callback(char* topic, byte* payload, unsigned int length) {
|
void MQTTHelper::callback(char* topic_chars, byte* payload_bytes, unsigned int length) {
|
||||||
Serial.print("Message arrived:");
|
String topic(topic_chars);
|
||||||
Serial.print(topic);
|
payload_bytes[length] = '\0';
|
||||||
Serial.print(" = ");
|
String payload = (char*)payload_bytes;
|
||||||
for (unsigned int i=0; i<length; i++) {
|
Serial.print("MQTT: callback on: ");
|
||||||
Serial.print((char)payload[i]);
|
Serial.println(topic+" = "+payload);
|
||||||
|
String commandTopic = getTopic("command");
|
||||||
|
|
||||||
|
if (topic == _command_topic && payload == "announce") {
|
||||||
|
Serial.println("Announcing");
|
||||||
|
announce();
|
||||||
}
|
}
|
||||||
Serial.println();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTTHelper::reconnect(int delayMillis) {
|
void MQTTHelper::reconnect(int delayMillis) {
|
||||||
// Loop until we're reconnected
|
// Loop until we're reconnected
|
||||||
while (!_mqttClient.connected()) {
|
while (!_mqttClient.connected()) {
|
||||||
Serial.print("Attempting MQTT connection...");
|
Serial.print("MQTT: attempting connection...");
|
||||||
if (connect()) {
|
if (connect()) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
} else {
|
} else {
|
||||||
@@ -91,19 +103,26 @@ void MQTTHelper::reconnect(int delayMillis) {
|
|||||||
|
|
||||||
void MQTTHelper::announce() {
|
void MQTTHelper::announce() {
|
||||||
DynamicJsonDocument doc(1024);
|
DynamicJsonDocument doc(1024);
|
||||||
doc["id"] = _identifier;
|
|
||||||
doc["ip"] = WiFi.localIP().toString();
|
doc["ip"] = WiFi.localIP().toString();
|
||||||
doc["mac"] = WiFi.macAddress();
|
doc["mac"] = WiFi.macAddress();
|
||||||
String payload;
|
String payloadSettings;
|
||||||
serializeJson(doc,payload);
|
serializeJson(doc,payloadSettings);
|
||||||
publish("settings",payload);
|
// publish to settings topic
|
||||||
|
publish("settings", payloadSettings);
|
||||||
|
|
||||||
|
doc["id"] = _identifier;
|
||||||
|
String payloadAnnounce;
|
||||||
|
serializeJson(doc,payloadAnnounce);
|
||||||
|
// publish to genneral announce topic
|
||||||
|
Serial.println("MQTT: "+_announce_topic+" = "+payloadAnnounce);
|
||||||
|
_mqttClient.publish(_announce_topic.c_str(),payloadAnnounce.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MQTTHelper::connect() {
|
bool MQTTHelper::connect() {
|
||||||
// If you do not want to use a username and password, change next line to
|
// If you do not want to use a username and password, change next line to
|
||||||
// if (_mqttClient.connect(_identifier.c_str()) {
|
// if (_mqttClient.connect(_identifier.c_str()) {
|
||||||
if (_mqttClient.connect(_identifier.c_str(), _user, _password, _will_topic.c_str(), 2, false, "false")) {
|
if (_mqttClient.connect(_identifier.c_str(), _user, _password, _will_topic.c_str(), 2, false, "false")) {
|
||||||
_mqttClient.subscribe((getTopic("settings")+"/get").c_str());
|
_mqttClient.subscribe((_command_topic).c_str());
|
||||||
announce();
|
announce();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
10
src/main.cpp
10
src/main.cpp
@@ -114,14 +114,10 @@ void loop() {
|
|||||||
// Serial.println(" mm");
|
// Serial.println(" mm");
|
||||||
|
|
||||||
if (checkBounds(distance, 0, maxDistance)) {
|
if (checkBounds(distance, 0, maxDistance)) {
|
||||||
String node = "distance";
|
mqtt.publish("distance", distance);
|
||||||
Serial.println(mqtt.getTopic(node)+" = "+String(distance));
|
|
||||||
mqtt.publish(node, distance);
|
|
||||||
|
|
||||||
bool signal = signalChange(distance, oldDistance);
|
bool signal = signalChange(distance, oldDistance);
|
||||||
node = "signal";
|
mqtt.publish("signal", signal);
|
||||||
Serial.println(mqtt.getTopic(node)+" = "+String(signal));
|
|
||||||
mqtt.publish(node, signal);
|
|
||||||
if (signal)
|
if (signal)
|
||||||
signal_led();
|
signal_led();
|
||||||
}
|
}
|
||||||
@@ -166,13 +162,11 @@ void readConfig() {
|
|||||||
DynamicJsonDocument doc(1024);
|
DynamicJsonDocument doc(1024);
|
||||||
DeserializationError error = deserializeJson(doc,configFile);
|
DeserializationError error = deserializeJson(doc,configFile);
|
||||||
serializeJsonPretty(doc,Serial);
|
serializeJsonPretty(doc,Serial);
|
||||||
Serial.println();
|
|
||||||
if (error) {
|
if (error) {
|
||||||
Serial.print("failed to load config.json: ");
|
Serial.print("failed to load config.json: ");
|
||||||
Serial.println(error.c_str());
|
Serial.println(error.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("\nparsed config.json");
|
Serial.println("\nparsed config.json");
|
||||||
IPAddress addr;
|
IPAddress addr;
|
||||||
if (addr.fromString(doc["mqtt_server"].as<char*>())) {
|
if (addr.fromString(doc["mqtt_server"].as<char*>())) {
|
||||||
|
|||||||
Reference in New Issue
Block a user