diff --git a/README.md b/README.md index 4fa42589..d81ff467 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,11 @@ A 3d-printable housing can be found here: https://www.thingiverse.com/thing:4571 -##### Rolling - (2020-10-04) +##### Rolling - (2020-10-13) + +* Implementation of user and password for MQTT Authentication (see `config.ini`) + +2020-10-04 * First simple MQTT Client - to be configured in `config.ini` (see example) diff --git a/code/lib/jomjol_flowcontroll/ClassFlowMQTT.cpp b/code/lib/jomjol_flowcontroll/ClassFlowMQTT.cpp index a3f3019f..c1d66426 100644 --- a/code/lib/jomjol_flowcontroll/ClassFlowMQTT.cpp +++ b/code/lib/jomjol_flowcontroll/ClassFlowMQTT.cpp @@ -14,7 +14,9 @@ ClassFlowMQTT::ClassFlowMQTT() topic = ""; clientname = "watermeter"; OldValue = ""; - flowpostprocessing = NULL; + flowpostprocessing = NULL; + user = ""; + password = ""; } ClassFlowMQTT::ClassFlowMQTT(std::vector* lfc) @@ -24,6 +26,8 @@ ClassFlowMQTT::ClassFlowMQTT(std::vector* lfc) clientname = "watermeter"; OldValue = ""; flowpostprocessing = NULL; + user = ""; + password = ""; ListFlowControll = lfc; @@ -53,6 +57,14 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph) while (this->getNextLine(pfile, &aktparamgraph) && !this->isNewParagraph(aktparamgraph)) { zerlegt = this->ZerlegeZeile(aktparamgraph); + if ((toUpper(zerlegt[0]) == "USER") && (zerlegt.size() > 1)) + { + this->user = zerlegt[1]; + } + if ((toUpper(zerlegt[0]) == "PASSWORD") && (zerlegt.size() > 1)) + { + this->password = zerlegt[1]; + } if ((toUpper(zerlegt[0]) == "URI") && (zerlegt.size() > 1)) { this->uri = zerlegt[1]; @@ -70,7 +82,7 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph) if ((uri.length() > 0) && (topic.length() > 0)) { - MQTTInit(uri, clientname); + MQTTInit(uri, clientname, user, password); } return true; diff --git a/code/lib/jomjol_flowcontroll/ClassFlowMQTT.h b/code/lib/jomjol_flowcontroll/ClassFlowMQTT.h index efeb35df..a22173ef 100644 --- a/code/lib/jomjol_flowcontroll/ClassFlowMQTT.h +++ b/code/lib/jomjol_flowcontroll/ClassFlowMQTT.h @@ -11,7 +11,8 @@ class ClassFlowMQTT : protected: std::string uri, topic, clientname; std::string OldValue; - ClassFlowPostProcessing* flowpostprocessing; + ClassFlowPostProcessing* flowpostprocessing; + std::string user, password; public: diff --git a/code/lib/jomjol_mqtt/interface_mqtt.cpp b/code/lib/jomjol_mqtt/interface_mqtt.cpp index 2f0844e3..dfa30408 100644 --- a/code/lib/jomjol_mqtt/interface_mqtt.cpp +++ b/code/lib/jomjol_mqtt/interface_mqtt.cpp @@ -64,12 +64,18 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ mqtt_event_handler_cb((esp_mqtt_event_handle_t) event_data); } -void MQTTInit(std::string _mqttURI, std::string _clientid){ +void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password){ esp_mqtt_client_config_t mqtt_cfg = { .uri = _mqttURI.c_str(), .client_id = _clientid.c_str(), }; + if (_user.length() && _password.length()){ + mqtt_cfg.username = _user.c_str(); + mqtt_cfg.password = _password.c_str(); + printf("Connect to MQTT: %s, %s", mqtt_cfg.username, mqtt_cfg.password); + }; + client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_register_event(client, esp_mmqtt_ID, mqtt_event_handler, client); esp_mqtt_client_start(client); diff --git a/code/lib/jomjol_mqtt/interface_mqtt.h b/code/lib/jomjol_mqtt/interface_mqtt.h index 3651887e..c34c93a6 100644 --- a/code/lib/jomjol_mqtt/interface_mqtt.h +++ b/code/lib/jomjol_mqtt/interface_mqtt.h @@ -1,4 +1,4 @@ #include -void MQTTInit(std::string _mqttURI, std::string _clientid); +void MQTTInit(std::string _mqttURI, std::string _clientid, std::string _user = "", std::string _password = ""); void MQTTPublish(std::string _key, std::string _content); \ No newline at end of file diff --git a/code/src/version.cpp b/code/src/version.cpp index d6c96be1..3fb59199 100644 --- a/code/src/version.cpp +++ b/code/src/version.cpp @@ -1,4 +1,4 @@ -const char* GIT_REV="f8e8c75"; +const char* GIT_REV="04f69f0"; const char* GIT_TAG=""; const char* GIT_BRANCH="rolling"; -const char* BUILD_TIME="2020-10-04 08:06"; \ No newline at end of file +const char* BUILD_TIME="2020-10-13 20:10"; \ No newline at end of file diff --git a/code/version.cpp b/code/version.cpp index d6c96be1..3fb59199 100644 --- a/code/version.cpp +++ b/code/version.cpp @@ -1,4 +1,4 @@ -const char* GIT_REV="f8e8c75"; +const char* GIT_REV="04f69f0"; const char* GIT_TAG=""; const char* GIT_BRANCH="rolling"; -const char* BUILD_TIME="2020-10-04 08:06"; \ No newline at end of file +const char* BUILD_TIME="2020-10-13 20:10"; \ No newline at end of file diff --git a/firmware/bootloader.bin b/firmware/bootloader.bin index 4e5fd20e..eb151bca 100644 Binary files a/firmware/bootloader.bin and b/firmware/bootloader.bin differ diff --git a/firmware/firmware.bin b/firmware/firmware.bin index f59d6f5f..b1558c8b 100644 Binary files a/firmware/firmware.bin and b/firmware/firmware.bin differ diff --git a/sd-card/config/config.ini b/sd-card/config/config.ini index ef553304..17fc2e0e 100644 --- a/sd-card/config/config.ini +++ b/sd-card/config/config.ini @@ -40,8 +40,10 @@ CheckDigitIncreaseConsistency = False ;[MQTT] ;Uri = mqtt://IP-MQTT-SERVER:1883 -;Topic = /watermeter/readout +;Topic = watermeter/readout ;ClientID = wasser +;user = USERNAME +;password = PASSWORD [AutoTimer] AutoStart= True