Skip to content

Commit 4fa06c0

Browse files
authored
Adding configurable can RX/TX pins (#9)
* Adding configurable can RX/TX pins * Adding optional CAN enable Pin config
1 parent 9b392c9 commit 4fa06c0

File tree

10 files changed

+228
-7
lines changed

10 files changed

+228
-7
lines changed

data/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<script src="subscription.js" type="text/javascript"></script>
3232
<script src="log.js" type="text/javascript"></script>
3333
<script src="wifi.js" type="text/javascript"></script>
34+
<script src="settings.js" type="text/javascript"></script>
3435
<script src="plot.js" type="text/javascript"></script>
3536
<script src="ui.js" type="text/javascript"></script>
3637

@@ -148,6 +149,9 @@ <h3 class="underline">New CAN Mapping</h3>
148149
<a class="tablink" onclick="ui.openPage('wifi', this, 'white')">
149150
<!-- /icon-wifi.png -->
150151
<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>
152+
<a class="tablink" onclick="ui.openPage('settings', this, 'white')">
153+
<!-- /icon-tool.png -->
154+
<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>
151155
<a class="tablink" onclick="ui.openPage('files', this, 'white')">
152156
<!-- /icon-file-text.png -->
153157
<img class="navimg" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAK5JREFUSIljYKAzYGdgYOhmYGB4xsDA8J8A/srAwCBGqgWdRBiMjC+TagnM5VYE1JFtCUwTseouQenrDAwMkrSwQJRUS0i1gIFUS0i1ABv+TIoFh5HEDuMQx4bhgIUI12IDtngciBcQG0RE62eiwDCiACELCIU1etyQbAHVwWgckGwB1cHQj4PnUNqaDLNgxcczfIo6GEirMrHhdnwWsEEteUqGwU+hhrMR6WPqAAAbTIrOKieWjwAAAABJRU5ErkJggg=="><p class="navbar-text">FILES</p></a>
@@ -526,6 +530,8 @@ <h3 class="underline">Existing CAN Mappings</h3>
526530

527531
<!-- WIFI SETTINGS -->
528532
<div id="wifi" class="tabdiv main-content"></div>
533+
<!-- SETTINGS -->
534+
<div id="settings" class="tabdiv main-content"></div>
529535

530536
<!-- FILES -->
531537
<div id="files" class="tabdiv main-content">

data/settings-updated.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<!--
5+
* This file is part of the esp8266 web interface
6+
*
7+
* Copyright (C) 2018 Johannes Huebner <[email protected]>
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
-->
23+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
24+
<meta http-equiv="refresh" content="10;url=http://192.168.4.1/" />
25+
<title>Modify settings</title>
26+
<link href="style.css" rel="stylesheet" type="text/css" />
27+
</head>
28+
<body>
29+
Settings updated. <a href="/">Return to main page</a>.
30+
</body>
31+
</html>
32+

data/settings.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!--
2+
* This file is part of the esp8266 web interface
3+
*
4+
* Copyright (C) 2018 Johannes Huebner <[email protected]>
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
-->
20+
<div id="settingsInner">
21+
<h2>Settings</h2>
22+
<form id="settingsForm" enctype="multipart/form-data" action="settings" method="POST">
23+
24+
<p><label for="canTXPin">CAN TX Pin: </label><input id="canTXPin" name="canTXPin" type="text" value="%canTXPin%"/></p>
25+
<p><label for="canRXPin">CAN RX Pin: </label><input id="canRXPin" name="canRXPin" type="text" value="%canRXPin%"/></p>
26+
<p><label for="canEnablePin">CAN Enable Pin: </label><input id="canEnablePin" name="canEnablePin" type="text" value="%canEnablePin%"/> Set to 0 if not required</p>
27+
28+
<p><strong>Open Inverter Can board:- TX Pin: 25 RX Pin: 26</strong></p>
29+
<p><strong>Lilygo T-Can board:- TX Pin: 27 RX Pin: 26 Can Enable Pin: 23</strong></p>
30+
<a href="#" onclick="ui.settingsForm('settingsForm');"><button>
31+
<img class="buttonimg" src="/icon-check-circle.png">Modify</button></a>
32+
</form>
33+
</div>

data/settings.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var settings = {
2+
3+
wifiValidatePasswordLength: function(pw)
4+
{
5+
document.getElementById("apsubmit").disabled = pw.length < 8;
6+
},
7+
8+
populateSettingsTab: function()
9+
{
10+
var settingsTab = document.getElementById("settings");
11+
var settingsFetchRequest = new XMLHttpRequest();
12+
settingsFetchRequest.onload = function()
13+
{
14+
settingsTab.innerHTML = this.responseText;
15+
}
16+
settingsFetchRequest.open("GET", "/settings");
17+
settingsFetchRequest.send();
18+
},
19+
20+
}

data/ui.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ var ui = {
139139
ui.parameterDatabaseCheckForUpdates();
140140
inverter.canMapping(ui.populateExistingCanMappingTable);
141141
wifi.populateWiFiTab();
142+
settings.populateSettingsTab();
142143
ui.populateFileList();
143144
ui.refreshStatusBox();
144145
ui.getNodeId();

esp32-web-interface.ino

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <time.h>
5858
#include "driver/uart.h"
5959
#include "src/oi_can.h"
60+
#include "src/config.h"
6061

6162
#define DBG_OUTPUT_PORT Serial
6263
#define INVERTER_PORT UART_NUM_2
@@ -102,6 +103,7 @@ uint16_t indexSDIObuffer = 0;
102103
uint16_t blockCountSD = 0;
103104
File dataFile;
104105
int startLogAttempt = 0;
106+
Config config;
105107

106108
uint32_t deleteOldest(uint64_t spaceRequired);
107109

@@ -570,16 +572,56 @@ static void handleNodeId()
570572
int id = server.arg("id").toInt();
571573
int speed = server.arg("canspeed").toInt();
572574
OICan::BaudRate baud = speed == 0 ? OICan::Baud125k : (speed == 1 ? OICan::Baud250k : OICan::Baud500k);
573-
OICan::Init(id, baud);
575+
OICan::Init(id, baud, config.getCanTXPin(), config.getCanRXPin());
574576
}
575577
else if(server.hasArg("id")) {
576578
int id = server.arg("id").toInt();
577-
OICan::Init(id, OICan::Baud500k);
579+
OICan::Init(id, OICan::Baud500k, config.getCanTXPin(), config.getCanRXPin());
578580
}
579581

580582
server.send(200, "text/plain", String(OICan::GetNodeId()) + "," + String(OICan::GetBaudRate()));
581583
}
582584

585+
static void handleSettings()
586+
{
587+
bool updated = true;
588+
if(server.hasArg("canRXPin") && server.hasArg("canRXPin") && server.hasArg("canEnablePin"))
589+
{
590+
config.setCanRXPin(atoi(server.arg("canRXPin").c_str()));
591+
config.setCanTXPin(atoi(server.arg("canTXPin").c_str()));
592+
config.setCanEnablePin(atoi(server.arg("canEnablePin").c_str()));
593+
594+
config.saveSettings();
595+
OICan::Init(OICan::GetNodeId(), OICan::GetBaudRate(), config.getCanTXPin(), config.getCanRXPin());
596+
597+
598+
if (config.getCanEnablePin() > 0) {
599+
pinMode(config.getCanEnablePin(), OUTPUT);
600+
digitalWrite(config.getCanEnablePin(), LOW);
601+
}
602+
}
603+
else
604+
{
605+
File file = SPIFFS.open("/settings.html", "r");
606+
String html = file.readString();
607+
file.close();
608+
html.replace("%canRXPin%", String(config.getCanRXPin()).c_str());
609+
html.replace("%canTXPin%", String(config.getCanTXPin()).c_str());
610+
html.replace("%canEnablePin%", String(config.getCanEnablePin()).c_str());
611+
612+
server.send(200, "text/html", html);
613+
updated = false;
614+
}
615+
616+
if (updated)
617+
{
618+
File file = SPIFFS.open("/settings-updated.html", "r");
619+
size_t sent = server.streamFile(file, getContentType("settings-updated.html"));
620+
file.close();
621+
}
622+
}
623+
624+
583625
static void handleWifi()
584626
{
585627
bool updated = true;
@@ -692,7 +734,14 @@ void setup(void){
692734

693735
MDNS.begin(host);
694736

695-
OICan::Init(1, OICan::Baud500k);
737+
config.load();
738+
739+
if (config.getCanEnablePin() > 0) {
740+
pinMode(config.getCanEnablePin(), OUTPUT);
741+
digitalWrite(config.getCanEnablePin(), LOW);
742+
}
743+
744+
OICan::Init(1, OICan::Baud500k, config.getCanTXPin(), config.getCanRXPin());
696745

697746
updater.setup(&server);
698747

@@ -726,6 +775,7 @@ void setup(void){
726775
server.on("/baud", handleBaud);
727776
server.on("/version", [](){ server.send(200, "text/plain", "1.1.R"); });
728777
server.on("/nodeid", handleNodeId);
778+
server.on("/settings", handleSettings);
729779

730780
//called when the url is not defined here
731781
//use it to load content from SPIFFS

src/config.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include "config.h"
2+
#include <EEPROM.h>
3+
Config::Config() {
4+
}
5+
6+
void Config::load() {
7+
8+
EEPROM.begin(sizeof(settings));
9+
EEPROM.get(0, settings);
10+
if (settings.version != EEPROM_VERSION) {
11+
//defaults
12+
settings.version = EEPROM_VERSION;
13+
settings.canRXPin = GPIO_NUM_26;
14+
settings.canTXPin = GPIO_NUM_25;
15+
settings.canEnablePin = 0;
16+
}
17+
}
18+
int Config::getCanRXPin() {
19+
return settings.canRXPin;
20+
}
21+
22+
int Config::getCanTXPin() {
23+
return settings.canTXPin;
24+
}
25+
26+
int Config::getCanEnablePin() {
27+
return settings.canEnablePin;
28+
}
29+
30+
31+
void Config::setCanEnablePin(int pin) {
32+
settings.canEnablePin = pin;
33+
}
34+
35+
void Config::setCanTXPin(int pin) {
36+
settings.canTXPin = pin;
37+
}
38+
39+
void Config::setCanRXPin(int pin) {
40+
settings.canRXPin = pin;
41+
}
42+
43+
void Config::saveSettings() {
44+
EEPROM.put(0, settings); //save all change to eeprom
45+
EEPROM.commit();
46+
}

src/config.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef CONFIG_H
2+
#define CONFIG_H
3+
4+
#define EEPROM_VERSION 2
5+
typedef struct {
6+
int version;
7+
int canRXPin;
8+
int canTXPin;
9+
int canEnablePin;
10+
} EEPROMSettings;
11+
12+
13+
class Config
14+
{
15+
public:
16+
Config();
17+
void load();
18+
int getCanRXPin();
19+
void setCanRXPin(int pin);
20+
21+
int getCanTXPin();
22+
void setCanTXPin(int pin);
23+
24+
25+
int getCanEnablePin();
26+
void setCanEnablePin(int pin);
27+
28+
void saveSettings();
29+
private:
30+
EEPROMSettings settings;
31+
32+
};
33+
#endif

src/oi_can.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,11 @@ BaudRate GetBaudRate() {
652652
return baudRate;
653653
}
654654

655-
void Init(uint8_t nodeId, BaudRate baud) {
655+
void Init(uint8_t nodeId, BaudRate baud, int txPin, int rxPin) {
656656
twai_general_config_t g_config = {
657657
.mode = TWAI_MODE_NORMAL,
658-
.tx_io = GPIO_NUM_25,
659-
.rx_io = GPIO_NUM_26,
658+
.tx_io = static_cast<gpio_num_t>(txPin),
659+
.rx_io = static_cast<gpio_num_t>(rxPin),
660660
.clkout_io = TWAI_IO_UNUSED,
661661
.bus_off_io = TWAI_IO_UNUSED,
662662
.tx_queue_len = 30,

src/oi_can.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace OICan {
2525
enum SetResult { Ok, UnknownIndex, ValueOutOfRange, CommError };
2626
enum BaudRate { Baud125k, Baud250k, Baud500k };
2727

28-
void Init(uint8_t nodeId, BaudRate baud);
28+
void Init(uint8_t nodeId, BaudRate baud, int txPin, int rxPin);
2929
void Loop();
3030
bool SendJson(WiFiClient c);
3131
void SendCanMapping(WiFiClient c);

0 commit comments

Comments
 (0)