diff --git a/data/index.html b/data/index.html index 1456751..ea638c1 100644 --- a/data/index.html +++ b/data/index.html @@ -31,6 +31,7 @@ + @@ -148,6 +149,9 @@

New CAN Mapping

+ + + @@ -526,6 +530,8 @@

Existing CAN Mappings

+ +
diff --git a/data/settings-updated.html b/data/settings-updated.html new file mode 100644 index 0000000..52f23f0 --- /dev/null +++ b/data/settings-updated.html @@ -0,0 +1,32 @@ + + + + + + +Modify settings + + + +Settings updated. Return to main page. + + + diff --git a/data/settings.html b/data/settings.html new file mode 100644 index 0000000..c6d8cea --- /dev/null +++ b/data/settings.html @@ -0,0 +1,33 @@ + +
+

Settings

+
+ +

+

+

Set to 0 if not required

+ +

Open Inverter Can board:- TX Pin: 25 RX Pin: 26

+

Lilygo T-Can board:- TX Pin: 27 RX Pin: 26 Can Enable Pin: 23

+ +
+
\ No newline at end of file diff --git a/data/settings.js b/data/settings.js new file mode 100644 index 0000000..26339f4 --- /dev/null +++ b/data/settings.js @@ -0,0 +1,20 @@ +var settings = { + + wifiValidatePasswordLength: function(pw) + { + document.getElementById("apsubmit").disabled = pw.length < 8; + }, + + populateSettingsTab: function() + { + var settingsTab = document.getElementById("settings"); + var settingsFetchRequest = new XMLHttpRequest(); + settingsFetchRequest.onload = function() + { + settingsTab.innerHTML = this.responseText; + } + settingsFetchRequest.open("GET", "/settings"); + settingsFetchRequest.send(); + }, + +} \ No newline at end of file diff --git a/data/ui.js b/data/ui.js index 6fc046e..34a31ab 100644 --- a/data/ui.js +++ b/data/ui.js @@ -139,6 +139,7 @@ var ui = { ui.parameterDatabaseCheckForUpdates(); inverter.canMapping(ui.populateExistingCanMappingTable); wifi.populateWiFiTab(); + settings.populateSettingsTab(); ui.populateFileList(); ui.refreshStatusBox(); ui.getNodeId(); diff --git a/esp32-web-interface.ino b/esp32-web-interface.ino index b3d9472..23cb0c0 100644 --- a/esp32-web-interface.ino +++ b/esp32-web-interface.ino @@ -57,6 +57,7 @@ #include #include "driver/uart.h" #include "src/oi_can.h" +#include "src/config.h" #define DBG_OUTPUT_PORT Serial #define INVERTER_PORT UART_NUM_2 @@ -102,6 +103,7 @@ uint16_t indexSDIObuffer = 0; uint16_t blockCountSD = 0; File dataFile; int startLogAttempt = 0; +Config config; uint32_t deleteOldest(uint64_t spaceRequired); @@ -570,16 +572,56 @@ static void handleNodeId() int id = server.arg("id").toInt(); int speed = server.arg("canspeed").toInt(); OICan::BaudRate baud = speed == 0 ? OICan::Baud125k : (speed == 1 ? OICan::Baud250k : OICan::Baud500k); - OICan::Init(id, baud); + OICan::Init(id, baud, config.getCanTXPin(), config.getCanRXPin()); } else if(server.hasArg("id")) { int id = server.arg("id").toInt(); - OICan::Init(id, OICan::Baud500k); + OICan::Init(id, OICan::Baud500k, config.getCanTXPin(), config.getCanRXPin()); } server.send(200, "text/plain", String(OICan::GetNodeId()) + "," + String(OICan::GetBaudRate())); } +static void handleSettings() +{ + bool updated = true; + if(server.hasArg("canRXPin") && server.hasArg("canRXPin") && server.hasArg("canEnablePin")) + { + config.setCanRXPin(atoi(server.arg("canRXPin").c_str())); + config.setCanTXPin(atoi(server.arg("canTXPin").c_str())); + config.setCanEnablePin(atoi(server.arg("canEnablePin").c_str())); + + config.saveSettings(); + OICan::Init(OICan::GetNodeId(), OICan::GetBaudRate(), config.getCanTXPin(), config.getCanRXPin()); + + + if (config.getCanEnablePin() > 0) { + pinMode(config.getCanEnablePin(), OUTPUT); + digitalWrite(config.getCanEnablePin(), LOW); + } + } + else + { + File file = SPIFFS.open("/settings.html", "r"); + String html = file.readString(); + file.close(); + html.replace("%canRXPin%", String(config.getCanRXPin()).c_str()); + html.replace("%canTXPin%", String(config.getCanTXPin()).c_str()); + html.replace("%canEnablePin%", String(config.getCanEnablePin()).c_str()); + + server.send(200, "text/html", html); + updated = false; + } + + if (updated) + { + File file = SPIFFS.open("/settings-updated.html", "r"); + size_t sent = server.streamFile(file, getContentType("settings-updated.html")); + file.close(); + } +} + + static void handleWifi() { bool updated = true; @@ -692,7 +734,14 @@ void setup(void){ MDNS.begin(host); - OICan::Init(1, OICan::Baud500k); + config.load(); + + if (config.getCanEnablePin() > 0) { + pinMode(config.getCanEnablePin(), OUTPUT); + digitalWrite(config.getCanEnablePin(), LOW); + } + + OICan::Init(1, OICan::Baud500k, config.getCanTXPin(), config.getCanRXPin()); updater.setup(&server); @@ -726,6 +775,7 @@ void setup(void){ server.on("/baud", handleBaud); server.on("/version", [](){ server.send(200, "text/plain", "1.1.R"); }); server.on("/nodeid", handleNodeId); + server.on("/settings", handleSettings); //called when the url is not defined here //use it to load content from SPIFFS diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..017c659 --- /dev/null +++ b/src/config.cpp @@ -0,0 +1,46 @@ +#include "config.h" +#include +Config::Config() { +} + +void Config::load() { + + EEPROM.begin(sizeof(settings)); + EEPROM.get(0, settings); + if (settings.version != EEPROM_VERSION) { + //defaults + settings.version = EEPROM_VERSION; + settings.canRXPin = GPIO_NUM_26; + settings.canTXPin = GPIO_NUM_25; + settings.canEnablePin = 0; + } +} +int Config::getCanRXPin() { + return settings.canRXPin; +} + +int Config::getCanTXPin() { + return settings.canTXPin; +} + +int Config::getCanEnablePin() { + return settings.canEnablePin; +} + + +void Config::setCanEnablePin(int pin) { + settings.canEnablePin = pin; +} + +void Config::setCanTXPin(int pin) { + settings.canTXPin = pin; +} + +void Config::setCanRXPin(int pin) { + settings.canRXPin = pin; +} + +void Config::saveSettings() { + EEPROM.put(0, settings); //save all change to eeprom + EEPROM.commit(); +} diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..bbe91f2 --- /dev/null +++ b/src/config.h @@ -0,0 +1,33 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define EEPROM_VERSION 2 +typedef struct { + int version; + int canRXPin; + int canTXPin; + int canEnablePin; +} EEPROMSettings; + + +class Config +{ + public: + Config(); + void load(); + int getCanRXPin(); + void setCanRXPin(int pin); + + int getCanTXPin(); + void setCanTXPin(int pin); + + + int getCanEnablePin(); + void setCanEnablePin(int pin); + + void saveSettings(); + private: + EEPROMSettings settings; + +}; +#endif \ No newline at end of file diff --git a/src/oi_can.cpp b/src/oi_can.cpp index f2c7c0a..4ea1dcd 100644 --- a/src/oi_can.cpp +++ b/src/oi_can.cpp @@ -652,11 +652,11 @@ BaudRate GetBaudRate() { return baudRate; } -void Init(uint8_t nodeId, BaudRate baud) { +void Init(uint8_t nodeId, BaudRate baud, int txPin, int rxPin) { twai_general_config_t g_config = { .mode = TWAI_MODE_NORMAL, - .tx_io = GPIO_NUM_25, - .rx_io = GPIO_NUM_26, + .tx_io = static_cast(txPin), + .rx_io = static_cast(rxPin), .clkout_io = TWAI_IO_UNUSED, .bus_off_io = TWAI_IO_UNUSED, .tx_queue_len = 30, diff --git a/src/oi_can.h b/src/oi_can.h index fe7034c..2f894cb 100644 --- a/src/oi_can.h +++ b/src/oi_can.h @@ -25,7 +25,7 @@ namespace OICan { enum SetResult { Ok, UnknownIndex, ValueOutOfRange, CommError }; enum BaudRate { Baud125k, Baud250k, Baud500k }; -void Init(uint8_t nodeId, BaudRate baud); +void Init(uint8_t nodeId, BaudRate baud, int txPin, int rxPin); void Loop(); bool SendJson(WiFiClient c); void SendCanMapping(WiFiClient c);