moving non-settings to mqtt stuff

This commit is contained in:
Robert van den Breemen 2021-03-06 18:36:14 +01:00
parent 74246c8b47
commit e3de480cb1
3 changed files with 24 additions and 25 deletions

View File

@ -26,16 +26,18 @@
enum states_of_MQTT stateMQTT = MQTT_STATE_INIT;
String MQTTclientId;
String MQTTPubNamespace = "";
String MQTTSubNamespace = "";
String NodeId = "";
//===========================================================================================
void startMQTT()
{
if (!settingMQTTenable) return;
stateMQTT = MQTT_STATE_INIT;
//setup for mqtt discovery
settingNodeId = getUniqueId();
settingMQTTPubNamespace = settingMQTTtopTopic + "/value/" + settingNodeId;
settingMQTTSubNamespace = settingMQTTtopTopic + "/set/" + settingNodeId;
NodeId = getUniqueId();
MQTTPubNamespace = settingMQTTtopTopic + "/value/" + NodeId;
MQTTSubNamespace = settingMQTTtopTopic + "/set/" + NodeId;
handleMQTT(); //initialize the MQTT statemachine
// handleMQTT(); //then try to connect to MQTT
// handleMQTT(); //now you should be connected to MQTT ready to send
@ -52,7 +54,7 @@ void handleMQTTcallback(char* topic, byte* payload, unsigned int length) {
char subscribeTopic[100];
// naming convention <mqtt top>/set/<node id>/<command>
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/", settingMQTTSubNamespace.c_str());
snprintf(subscribeTopic, sizeof(subscribeTopic), "%s/", MQTTSubNamespace.c_str());
strlcat(subscribeTopic, OTGW_COMMAND_TOPIC, sizeof(subscribeTopic));
//what is the incoming message?
if (stricmp(topic, subscribeTopic) == 0)
@ -105,12 +107,12 @@ void handleMQTT()
if (settingMQTTuser.length() == 0)
{
Debug(F("without a Username/Password "));
MQTTclient.connect(CSTR(MQTTclientId), CSTR(settingMQTTPubNamespace), 0, true, "offline");
MQTTclient.connect(CSTR(MQTTclientId), CSTR(MQTTPubNamespace), 0, true, "offline");
}
else
{
Debugf("Username [%s] ", CSTR(settingMQTTuser));
MQTTclient.connect(CSTR(MQTTclientId), CSTR(settingMQTTuser), CSTR(settingMQTTpasswd), CSTR(settingMQTTPubNamespace), 0, true, "offline");
MQTTclient.connect(CSTR(MQTTclientId), CSTR(settingMQTTuser), CSTR(settingMQTTpasswd), CSTR(MQTTPubNamespace), 0, true, "offline");
}
//If connection was made succesful, move on to next state...
@ -121,12 +123,12 @@ void handleMQTT()
stateMQTT = MQTT_STATE_IS_CONNECTED;
//DebugTln(F("Next State: MQTT_STATE_IS_CONNECTED"));
// birth message, sendMQTT retains by default
sendMQTT(CSTR(settingMQTTPubNamespace), "online");
sendMQTT(CSTR(MQTTPubNamespace), "online");
//First do AutoConfiguration for Homeassistant
doAutoConfigure();
//Subscribe to topics
char topic[100];
strcpy(topic, CSTR(settingMQTTSubNamespace));
strcpy(topic, CSTR(MQTTSubNamespace));
strlcat(topic, "/#", sizeof(topic));
DebugTf("Subscribe to MQTT: TopicId [%s]\r\n", topic);
if (MQTTclient.subscribe(topic)){
@ -266,7 +268,7 @@ void sendMQTTData(const char* topic, const char *json, const bool retain = false
if (!MQTTclient.connected() || !isValidIP(MQTTbrokerIP)) return;
// DebugTf("Sending data to MQTT server [%s]:[%d]\r\n", settingMQTTbroker.c_str(), settingMQTTbrokerPort);
char full_topic[100];
snprintf(full_topic, sizeof(full_topic), "%s/", CSTR(settingMQTTPubNamespace));
snprintf(full_topic, sizeof(full_topic), "%s/", CSTR(MQTTPubNamespace));
strlcat(full_topic, topic, sizeof(full_topic));
//DebugTf("Sending MQTT: TopicId [%s] Message [%s]\r\n", full_topic, json);
if (!MQTTclient.publish(full_topic, json, retain)) DebugTln("MQTT publish failed.");
@ -350,14 +352,14 @@ void resetMQTTBufferSize()
sTopic.replace("%homeassistant%", CSTR(settingMQTThaprefix));
/// node
sTopic.replace("%node_id%", CSTR(settingNodeId));
sTopic.replace("%node_id%", CSTR(NodeId));
Debugf("[%s]\r\n", CSTR(sTopic));
/// ----------------------
DebugTf("sMsg[%s]==>", CSTR(sMsg));
/// node
sMsg.replace("%node_id%", CSTR(settingNodeId));
sMsg.replace("%node_id%", CSTR(NodeId));
/// hostname
sMsg.replace("%hostname%", CSTR(settingHostname));
@ -366,10 +368,10 @@ void resetMQTTBufferSize()
sMsg.replace("%version%", CSTR(String(_VERSION)));
// pub topics prefix
sMsg.replace("%mqtt_pub_topic%", CSTR(settingMQTTPubNamespace));
sMsg.replace("%mqtt_pub_topic%", CSTR(MQTTPubNamespace));
// sub topics
sMsg.replace("%mqtt_sub_topic%", CSTR(settingMQTTSubNamespace));
sMsg.replace("%mqtt_sub_topic%", CSTR(MQTTSubNamespace));
Debugf("[%s]\r\n", CSTR(sMsg)); DebugFlush();

View File

@ -77,7 +77,6 @@ String errorupgrade = "";
//All things that are settings
String settingHostname = _HOSTNAME;
String settingNodeId = _HOSTNAME;
//MQTT settings
bool statusMQTTconnection = false;
@ -87,10 +86,8 @@ String settingMQTTbroker= "192.168.88.254";
int16_t settingMQTTbrokerPort = 1883;
String settingMQTTuser = "";
String settingMQTTpasswd = "";
String settingMQTThaprefix = HOMEASSISTANT_PREFIX;
String settingMQTThaprefix = HOME_ASSISTANT_DISCOVERY_PREFIX;
String settingMQTTtopTopic = "otgw";
String settingMQTTPubNamespace = "";
String settingMQTTSubNamespace = "";
bool settingNTPenable = true;
String settingNTPtimezone = DEFAULT_TIMEZONE;
bool settingLEDblink = true;

View File

@ -2,15 +2,15 @@
#define _VERSION_MAJOR 0
#define _VERSION_MINOR 8
#define _VERSION_PATCH 0
#define _VERSION_BUILD 762
#define _VERSION_GITHASH e19d9d1
#define _VERSION_BUILD 764
#define _VERSION_GITHASH 74246c8
//#define _VERSION_PRERELEASE beta //uncomment to define prerelease labels: alpha - beta - rc
#define _VERSION_DATE "06-03-2021"
#define _VERSION_TIME "16:41:28"
#define _VERSION_TIME "18:33:07"
#define _SEMVER_CORE "0.8.0"
#define _SEMVER_BUILD "0.8.0+762"
#define _SEMVER_GITHASH "0.8.0+e19d9d1"
#define _SEMVER_FULL "0.8.0+e19d9d1"
#define _SEMVER_BUILD "0.8.0+764"
#define _SEMVER_GITHASH "0.8.0+74246c8"
#define _SEMVER_FULL "0.8.0+74246c8"
#define _SEMVER_NOBUILD "0.8.0 (06-03-2021)"
#define _VERSION "0.8.0+e19d9d1 (06-03-2021)"
#define _VERSION "0.8.0+74246c8 (06-03-2021)"
//The version information is created automatically, more information here: https://github.com/rvdbreemen/autoinc-semver