Skip to content

Commit

Permalink
Merge pull request #7 from enavarro222/config
Browse files Browse the repository at this point in the history
Add MQTT port and OTA path & port to config
  • Loading branch information
marvinroger committed Jan 26, 2016
2 parents 8a90eb2 + 709149e commit f57d75e
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 33 additions & 2 deletions src/Homie/Boot/BootConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ String BootConfig::_generateNetworksJson() {
case ENC_TYPE_AUTO:
json_network["encryption"] = "auto";
break;
}
}

networks.add(json_network);
}
Expand All @@ -113,7 +113,7 @@ void BootConfig::_onConfigRequest() {
return;
}

StaticJsonBuffer<JSON_OBJECT_SIZE(4)> parseJsonBuffer; // Max four elements in object
StaticJsonBuffer<JSON_OBJECT_SIZE(7)> 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");
Expand Down Expand Up @@ -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<uint16_t>()) {
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<const char*>()) {
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<uint16_t>()) {
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<uint16_t>();
}
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<uint16_t>();
}

if (strcmp(req_name, "") == 0) {
Logger.logln("✖ name is empty");
Expand Down Expand Up @@ -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");

Expand Down
4 changes: 3 additions & 1 deletion src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -139,6 +139,8 @@ void BootNormal::setup() {
this->_resetDebouncer.interval(5000UL);

this->_shared_interface->setupFunction();

Config.log();
}

void BootNormal::loop() {
Expand Down
2 changes: 1 addition & 1 deletion src/Homie/Boot/BootOta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
26 changes: 26 additions & 0 deletions src/Homie/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}
Expand All @@ -57,4 +63,24 @@ 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(" * 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;
5 changes: 5 additions & 0 deletions src/Homie/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Arduino.h>
#include <EEPROM.h>
#include "Constants.hpp"
#include "Logger.hpp"

namespace HomieInternals {
class ConfigClass {
Expand All @@ -13,11 +14,15 @@ 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();
void save();
void setCustomEepromSize(int count);
void log(); // print the current config to log output

private:
ConfigStruct _config_struct;
Expand Down
9 changes: 7 additions & 2 deletions src/Homie/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f57d75e

Please sign in to comment.