From 1d9b457fc0bc76a4996bf436ae68a1a94ce4f054 Mon Sep 17 00:00:00 2001 From: Emmanuel N Date: Sun, 17 Jan 2016 22:41:11 +0100 Subject: [PATCH 1/4] Add mth to log current config --- src/Homie/Config.cpp | 16 ++++++++++++++++ src/Homie/Config.hpp | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index 352eff20..a5d6043c 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -57,4 +57,20 @@ void ConfigClass::setCustomEepromSize(int count) { this->_custom_eeprom_size = count; } +void ConfigClass::log() { + Logger.logln("Conifg: "); + Logger.log(" * configured: "); + Logger.logln(String(this->configured)); + Logger.log(" * boot_mode: "); + Logger.logln(String(this->boot_mode)); + Logger.log(" * hostname: "); + Logger.logln(this->hostname); + Logger.log(" * wifi_ssid: "); + Logger.logln(this->wifi_ssid); + Logger.log(" * wifi_password: "); + Logger.logln(this->wifi_password); + Logger.log(" * homie_host: "); + Logger.logln(this->homie_host); +} + ConfigClass HomieInternals::Config; diff --git a/src/Homie/Config.hpp b/src/Homie/Config.hpp index 0ae74a64..ebe8bbb5 100644 --- a/src/Homie/Config.hpp +++ b/src/Homie/Config.hpp @@ -3,6 +3,7 @@ #include #include #include "Constants.hpp" +#include "Logger.hpp" namespace HomieInternals { class ConfigClass { @@ -18,6 +19,7 @@ namespace HomieInternals { bool load(); void save(); void setCustomEepromSize(int count); + void log(); // print the current config to log output private: ConfigStruct _config_struct; From 5fd55dc20d65e4e3efcda5eeb8eb2c43094780ad Mon Sep 17 00:00:00 2001 From: Emmanuel N Date: Sun, 17 Jan 2016 23:20:04 +0100 Subject: [PATCH 2/4] Add port, ota_path & ota_port in config --- src/Homie/Boot/BootConfig.cpp | 35 +++++++++++++++++++++++++++++++++-- src/Homie/Boot/BootNormal.cpp | 4 +++- src/Homie/Boot/BootOta.cpp | 2 +- src/Homie/Config.cpp | 12 ++++++++++++ src/Homie/Config.hpp | 3 +++ src/Homie/Constants.hpp | 9 +++++++-- 6 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/Homie/Boot/BootConfig.cpp b/src/Homie/Boot/BootConfig.cpp index 6e6a6555..1dfd62b4 100644 --- a/src/Homie/Boot/BootConfig.cpp +++ b/src/Homie/Boot/BootConfig.cpp @@ -88,7 +88,7 @@ String BootConfig::_generateNetworksJson() { case ENC_TYPE_AUTO: json_network["encryption"] = "auto"; break; - } + } networks.add(json_network); } @@ -113,7 +113,7 @@ void BootConfig::_onConfigRequest() { return; } - StaticJsonBuffer parseJsonBuffer; // Max four elements in object + StaticJsonBuffer parseJsonBuffer; // Max seven elements in object JsonObject& parsed_json = parseJsonBuffer.parseObject((char*)this->_http.arg("plain").c_str()); if (!parsed_json.success()) { Logger.logln("✖ Invalid or too big JSON"); @@ -141,11 +141,38 @@ void BootConfig::_onConfigRequest() { this->_http.send(400, FPSTR(PROGMEM_CONFIG_APPLICATION_JSON), FPSTR(PROGMEM_CONFIG_JSON_FAILURE)); return; } + if (parsed_json.containsKey("homie_port") && !parsed_json["homie_port"].is()) { + Logger.logln("✖ homie_port is not an unsigned integer"); + this->_http.send(400, FPSTR(PROGMEM_CONFIG_APPLICATION_JSON), FPSTR(PROGMEM_CONFIG_JSON_FAILURE)); + return; + } + if (parsed_json.containsKey("homie_ota_path") && !parsed_json["homie_ota_path"].is()) { + Logger.logln("✖ homie_ota_path is not a string"); + this->_http.send(400, FPSTR(PROGMEM_CONFIG_APPLICATION_JSON), FPSTR(PROGMEM_CONFIG_JSON_FAILURE)); + return; + } + if (parsed_json.containsKey("homie_ota_port") && !parsed_json["homie_ota_port"].is()) { + Logger.logln("✖ homie_ota_port is not an unsigned integer"); + this->_http.send(400, FPSTR(PROGMEM_CONFIG_APPLICATION_JSON), FPSTR(PROGMEM_CONFIG_JSON_FAILURE)); + return; + } const char* req_name = parsed_json["name"]; const char* req_wifi_ssid = parsed_json["wifi_ssid"]; const char* req_wifi_password = parsed_json["wifi_password"]; const char* req_homie_host = parsed_json["homie_host"]; + uint16_t req_homie_port = DEFAULT_HOMIE_PORT; + if (parsed_json.containsKey("homie_port")) { + req_homie_port = parsed_json["homie_port"].as(); + } + const char* req_homie_ota_path = DEFAULT_HOMIE_OTA_PATH; + if (parsed_json.containsKey("homie_ota_path")) { + req_homie_ota_path = parsed_json["homie_ota_path"]; + } + uint16_t req_homie_ota_port = DEFAULT_HOMIE_OTA_PORT; + if (parsed_json.containsKey("homie_ota_port")) { + req_homie_ota_port = parsed_json["homie_ota_port"].as(); + } if (strcmp(req_name, "") == 0) { Logger.logln("✖ name is empty"); @@ -182,9 +209,13 @@ void BootConfig::_onConfigRequest() { Config.wifi_ssid = req_wifi_ssid; Config.wifi_password = req_wifi_password; Config.homie_host = req_homie_host; + Config.homie_port = req_homie_port; + Config.homie_ota_path = req_homie_ota_path; + Config.homie_ota_port = req_homie_ota_port; Config.boot_mode = BOOT_NORMAL; Config.configured = true; Config.save(); + Config.log(); Logger.logln("✔ Configured"); diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index 71affe3f..f44c900b 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -25,7 +25,7 @@ void BootNormal::_wifiConnect() { } void BootNormal::_mqttConnect() { - this->_shared_interface->mqtt->setServer(Config.homie_host, HOMIE_PORT); + this->_shared_interface->mqtt->setServer(Config.homie_host, Config.homie_port); this->_shared_interface->mqtt->setCallback(std::bind(&BootNormal::_mqttCallback, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); String topic = this->_mqtt_base_topic; topic += "/$online"; @@ -139,6 +139,8 @@ void BootNormal::setup() { this->_resetDebouncer.interval(5000UL); this->_shared_interface->setupFunction(); + + Config.log(); } void BootNormal::loop() { diff --git a/src/Homie/Boot/BootOta.cpp b/src/Homie/Boot/BootOta.cpp index 3e3bc0d5..674a12d2 100644 --- a/src/Homie/Boot/BootOta.cpp +++ b/src/Homie/Boot/BootOta.cpp @@ -41,7 +41,7 @@ void BootOta::setup() { String dataToPass = Config.hostname; dataToPass += '='; dataToPass += this->_shared_interface->version; - t_httpUpdate_return ret = ESPhttpUpdate.update(Config.homie_host, OTA_PORT, "/ota", dataToPass, false, "", false); + t_httpUpdate_return ret = ESPhttpUpdate.update(Config.homie_host, Config.homie_ota_port, Config.homie_ota_path, dataToPass, false, "", false); switch(ret) { case HTTP_UPDATE_FAILED: Logger.logln("✖ Update failed"); diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index a5d6043c..f83cb285 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -36,6 +36,9 @@ bool ConfigClass::load() { this->wifi_ssid = this->_config_struct.wifi_ssid; this->wifi_password = this->_config_struct.wifi_password; this->homie_host = this->_config_struct.homie_host; + this->homie_port = this->_config_struct.homie_port; + this->homie_ota_path = this->_config_struct.homie_ota_path; + this->homie_ota_port = this->_config_struct.homie_ota_port; return true; } @@ -49,6 +52,9 @@ void ConfigClass::save() { strcpy(this->_config_struct.wifi_ssid, this->wifi_ssid); strcpy(this->_config_struct.wifi_password, this->wifi_password); strcpy(this->_config_struct.homie_host, this->homie_host); + this->_config_struct.homie_port = this->homie_port; + strcpy(this->_config_struct.homie_ota_path, this->homie_ota_path); + this->_config_struct.homie_ota_port = this->homie_ota_port; EEPROM.put(EEPROM_OFFSET, this->_config_struct); EEPROM.commit(); } @@ -71,6 +77,12 @@ void ConfigClass::log() { Logger.logln(this->wifi_password); Logger.log(" * homie_host: "); Logger.logln(this->homie_host); + Logger.log(" * homie_port: "); + Logger.logln(String(this->homie_port)); + Logger.log(" * homie_ota_path: "); + Logger.logln(this->homie_ota_path); + Logger.log(" * homie_ota_port: "); + Logger.logln(String(this->homie_ota_port)); } ConfigClass HomieInternals::Config; diff --git a/src/Homie/Config.hpp b/src/Homie/Config.hpp index ebe8bbb5..b123d743 100644 --- a/src/Homie/Config.hpp +++ b/src/Homie/Config.hpp @@ -14,6 +14,9 @@ namespace HomieInternals { const char* wifi_ssid; const char* wifi_password; const char* homie_host; + uint16_t homie_port; + const char* homie_ota_path; + uint16_t homie_ota_port; ConfigClass(); bool load(); diff --git a/src/Homie/Constants.hpp b/src/Homie/Constants.hpp index f6a46e62..abd77903 100644 --- a/src/Homie/Constants.hpp +++ b/src/Homie/Constants.hpp @@ -6,8 +6,9 @@ namespace HomieInternals { const int BAUD_RATE = 115200; - const int HOMIE_PORT = 35589; - const int OTA_PORT = 35590; + const uint16_t DEFAULT_HOMIE_PORT = 35589; + const uint16_t DEFAULT_HOMIE_OTA_PORT = 35590; + const char DEFAULT_HOMIE_OTA_PATH[] = "/ota"; const float PIN_RESET = D3; @@ -19,6 +20,7 @@ namespace HomieInternals { const int EEPROM_LENGTH_WIFI_SSID = 32 + 1; const int EEPROM_LENGTH_WIFI_PASSWORD = 63 + 1; const int EEPROM_LENGTH_HOMIE_HOST = 63 + 1; + const int EEPROM_LENGTH_HOMIE_OTA_PATH = 63 + 1; enum BootMode : byte { BOOT_NORMAL = 1, @@ -57,6 +59,9 @@ namespace HomieInternals { char wifi_ssid[EEPROM_LENGTH_WIFI_SSID]; char wifi_password[EEPROM_LENGTH_WIFI_PASSWORD]; char homie_host[EEPROM_LENGTH_HOMIE_HOST]; + uint16_t homie_port; + char homie_ota_path[EEPROM_LENGTH_HOMIE_OTA_PATH]; + uint16_t homie_ota_port; }; const int EEPROM_CONFIG_SIZE = sizeof(ConfigStruct); From ec58c1adcd7a5c2984806442111912396cc53751 Mon Sep 17 00:00:00 2001 From: Emmanuel N Date: Sun, 17 Jan 2016 23:54:42 +0100 Subject: [PATCH 3/4] Update documentation --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aa9884a7..898b0ecb 100644 --- a/README.md +++ b/README.md @@ -221,11 +221,14 @@ Save the config to the device. "name": "kitchen-light", "wifi_ssid": "Network_1", "wifi_password": "I'm a Wi-Fi password!", - "homie_host": "192.168.1.10" + "homie_host": "192.168.1.10", + "homie_port": 35589, + "homie_ota_path": "/ota", + "homie_ota_port": 35590 } ``` -`wifi_password` can be left blank if open network. +`wifi_password` can be left blank if open network. `homie_port`, `homie_ota_path` and `homie_ota_port` are optional (default values are the ones indicaded in this json). ##### Response From 709149ebbbb93a58b7a6a87d081a964508ed7a95 Mon Sep 17 00:00:00 2001 From: Emmanuel N Date: Tue, 26 Jan 2016 23:35:03 +0100 Subject: [PATCH 4/4] Remove wifi password from config logging mth --- src/Homie/Config.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index f83cb285..98cd2e08 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -73,8 +73,6 @@ void ConfigClass::log() { Logger.logln(this->hostname); Logger.log(" * wifi_ssid: "); Logger.logln(this->wifi_ssid); - Logger.log(" * wifi_password: "); - Logger.logln(this->wifi_password); Logger.log(" * homie_host: "); Logger.logln(this->homie_host); Logger.log(" * homie_port: ");