Skip to content

Commit

Permalink
Adding configurable can RX/TX pins (#9)
Browse files Browse the repository at this point in the history
* Adding configurable can RX/TX pins

* Adding optional CAN enable Pin config
  • Loading branch information
jamiejones85 authored Aug 23, 2024
1 parent 9b392c9 commit 4fa06c0
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 7 deletions.
6 changes: 6 additions & 0 deletions data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<script src="subscription.js" type="text/javascript"></script>
<script src="log.js" type="text/javascript"></script>
<script src="wifi.js" type="text/javascript"></script>
<script src="settings.js" type="text/javascript"></script>
<script src="plot.js" type="text/javascript"></script>
<script src="ui.js" type="text/javascript"></script>

Expand Down Expand Up @@ -148,6 +149,9 @@ <h3 class="underline">New CAN Mapping</h3>
<a class="tablink" onclick="ui.openPage('wifi', this, 'white')">
<!-- /icon-wifi.png -->
<img class="navimg" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAVdJREFUSInt1L9KXEEYBfDf2ogsBhTR7FpIYq2WEtIFspV2koBo8A2CIija+Ao+gJWEkKQQBZUETRM2L2AvWPkvASURFHXXYmfZRd27dy9IUnjgNPebc74z880dHvGvkYqoteEVcuhDVyAc4BA7+IbvOInbdACfcIViTF7hI/qjjLNYRSGIzrGFGbzEc6QDe8O3WWzjImgKWEHmtvlr7IdFf7CI7rhbRicWlI6piGMMlYvjVanXw+Kk6MKGym7GYQynmHR36Bm8xxp28Rdn2AtG0+i5pUlhKniO1UryFEsqZxvFS3y4p1FNDONI5XZ8CUl6lQbcgmd4i+WqECcYjdOgPKjNYFQP3fgcNNdorid4g3eif8L7MIKJBjWgFXPI41dgHvN4ksSwGoPBsNaAf+NFlEFTnQYZtCu9Nzl0BObwVem9yiaOH5BOWPs/8QM/GxHUm8EjHh43i7Jhzxg3JD0AAAAASUVORK5CYII="><p class="navbar-text">WIFI SETTINGS</p></a>
<a class="tablink" onclick="ui.openPage('settings', this, 'white')">
<!-- /icon-tool.png -->
<img class="navimg" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAUdJREFUSInl1M8uBEEQx/HPcmbj33IQVxJeQ4Q34U6WFV7FS/hzccGFOHEgvMAiER6AdeiaZIwsM6xEopJOd1dXf6vnN9XNH7RxNHGOx2gnaGHgp/Al3KPTpS3/FP4aoF0sYBo34bvE6HfhE3gI0BZqaAQ0gzcidgQrKsrVDNBewOtd4PmkrSoJzmPTQsw3voB3cAx9JRNMRX8afaew3sAh5nAbvtnSx8dTQAdjXpQoP56JcbtKgpPYtFg4dV6STK6lmJ+VhTdwHZuahbU61sNflwrgIGLXysLzn/9ZndewHbF30o2vBM+qZQybklyjGJZk2Y/YV++lrAQv6l5sd72CX+NIeugepPJdzcV2tTFcBORKeiI+81eyoRInz/sr285vwmt4xgsmew3PErQDNh9JegbPbMvHsusZHPojSVuSa0f68f/c3gAXBoY3A6QNPQAAAABJRU5ErkJggg=="><p class="navbar-text">SETTINGS</p></a>
<a class="tablink" onclick="ui.openPage('files', this, 'white')">
<!-- /icon-file-text.png -->
<img class="navimg" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAK5JREFUSIljYKAzYGdgYOhmYGB4xsDA8J8A/srAwCBGqgWdRBiMjC+TagnM5VYE1JFtCUwTseouQenrDAwMkrSwQJRUS0i1gIFUS0i1ABv+TIoFh5HEDuMQx4bhgIUI12IDtngciBcQG0RE62eiwDCiACELCIU1etyQbAHVwWgckGwB1cHQj4PnUNqaDLNgxcczfIo6GEirMrHhdnwWsEEteUqGwU+hhrMR6WPqAAAbTIrOKieWjwAAAABJRU5ErkJggg=="><p class="navbar-text">FILES</p></a>
Expand Down Expand Up @@ -526,6 +530,8 @@ <h3 class="underline">Existing CAN Mappings</h3>

<!-- WIFI SETTINGS -->
<div id="wifi" class="tabdiv main-content"></div>
<!-- SETTINGS -->
<div id="settings" class="tabdiv main-content"></div>

<!-- FILES -->
<div id="files" class="tabdiv main-content">
Expand Down
32 changes: 32 additions & 0 deletions data/settings-updated.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html>
<head>
<!--
* This file is part of the esp8266 web interface
*
* Copyright (C) 2018 Johannes Huebner <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="10;url=http://192.168.4.1/" />
<title>Modify settings</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
Settings updated. <a href="/">Return to main page</a>.
</body>
</html>

33 changes: 33 additions & 0 deletions data/settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
* This file is part of the esp8266 web interface
*
* Copyright (C) 2018 Johannes Huebner <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
-->
<div id="settingsInner">
<h2>Settings</h2>
<form id="settingsForm" enctype="multipart/form-data" action="settings" method="POST">

<p><label for="canTXPin">CAN TX Pin: </label><input id="canTXPin" name="canTXPin" type="text" value="%canTXPin%"/></p>
<p><label for="canRXPin">CAN RX Pin: </label><input id="canRXPin" name="canRXPin" type="text" value="%canRXPin%"/></p>
<p><label for="canEnablePin">CAN Enable Pin: </label><input id="canEnablePin" name="canEnablePin" type="text" value="%canEnablePin%"/> Set to 0 if not required</p>

<p><strong>Open Inverter Can board:- TX Pin: 25 RX Pin: 26</strong></p>
<p><strong>Lilygo T-Can board:- TX Pin: 27 RX Pin: 26 Can Enable Pin: 23</strong></p>
<a href="#" onclick="ui.settingsForm('settingsForm');"><button>
<img class="buttonimg" src="/icon-check-circle.png">Modify</button></a>
</form>
</div>
20 changes: 20 additions & 0 deletions data/settings.js
Original file line number Diff line number Diff line change
@@ -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();
},

}
1 change: 1 addition & 0 deletions data/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ var ui = {
ui.parameterDatabaseCheckForUpdates();
inverter.canMapping(ui.populateExistingCanMappingTable);
wifi.populateWiFiTab();
settings.populateSettingsTab();
ui.populateFileList();
ui.refreshStatusBox();
ui.getNodeId();
Expand Down
56 changes: 53 additions & 3 deletions esp32-web-interface.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <time.h>
#include "driver/uart.h"
#include "src/oi_can.h"
#include "src/config.h"

#define DBG_OUTPUT_PORT Serial
#define INVERTER_PORT UART_NUM_2
Expand Down Expand Up @@ -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);

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

Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "config.h"
#include <EEPROM.h>
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();
}
33 changes: 33 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions src/oi_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<gpio_num_t>(txPin),
.rx_io = static_cast<gpio_num_t>(rxPin),
.clkout_io = TWAI_IO_UNUSED,
.bus_off_io = TWAI_IO_UNUSED,
.tx_queue_len = 30,
Expand Down
2 changes: 1 addition & 1 deletion src/oi_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4fa06c0

Please sign in to comment.