Skip to content

Commit 968a6ae

Browse files
committed
Adding configurable can RX/TX pins
1 parent fad7802 commit 968a6ae

File tree

10 files changed

+196
-7
lines changed

10 files changed

+196
-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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
27+
<a href="#" onclick="ui.settingsForm('settingsForm');"><button>
28+
<img class="buttonimg" src="/icon-check-circle.png">Modify</button></a>
29+
</form>
30+
</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: 39 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
@@ -100,6 +101,7 @@ uint16_t indexSDIObuffer = 0;
100101
uint16_t blockCountSD = 0;
101102
File dataFile;
102103
int startLogAttempt = 0;
104+
Config config;
103105

104106
bool createNextSDFile()
105107
{
@@ -572,16 +574,47 @@ static void handleNodeId()
572574
int id = server.arg("id").toInt();
573575
int speed = server.arg("canspeed").toInt();
574576
OICan::BaudRate baud = speed == 0 ? OICan::Baud125k : (speed == 1 ? OICan::Baud250k : OICan::Baud500k);
575-
OICan::Init(id, baud);
577+
OICan::Init(id, baud, config.getCanTXPin(), config.getCanRXPin());
576578
}
577579
else if(server.hasArg("id")) {
578580
int id = server.arg("id").toInt();
579-
OICan::Init(id, OICan::Baud500k);
581+
OICan::Init(id, OICan::Baud500k, config.getCanTXPin(), config.getCanRXPin());
580582
}
581583

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

587+
static void handleSettings()
588+
{
589+
bool updated = true;
590+
if(server.hasArg("canRXPin") && server.hasArg("canRXPin"))
591+
{
592+
config.setCanRXPin(atoi(server.arg("canRXPin").c_str()));
593+
config.setCanTXPin(atoi(server.arg("canTXPin").c_str()));
594+
config.saveSettings();
595+
OICan::Init(OICan::GetNodeId(), OICan::GetBaudRate(), config.getCanTXPin(), config.getCanRXPin());
596+
597+
}
598+
else
599+
{
600+
File file = SPIFFS.open("/settings.html", "r");
601+
String html = file.readString();
602+
file.close();
603+
html.replace("%canRXPin%", String(config.getCanRXPin()).c_str());
604+
html.replace("%canTXPin%", String(config.getCanTXPin()).c_str());
605+
server.send(200, "text/html", html);
606+
updated = false;
607+
}
608+
609+
if (updated)
610+
{
611+
File file = SPIFFS.open("/settings-updated.html", "r");
612+
size_t sent = server.streamFile(file, getContentType("settings-updated.html"));
613+
file.close();
614+
}
615+
}
616+
617+
585618
static void handleWifi()
586619
{
587620
bool updated = true;
@@ -692,7 +725,9 @@ void setup(void){
692725

693726
MDNS.begin(host);
694727

695-
OICan::Init(1, OICan::Baud500k);
728+
config.load();
729+
730+
OICan::Init(1, OICan::Baud500k, config.getCanTXPin(), config.getCanRXPin());
696731

697732
updater.setup(&server);
698733

@@ -726,6 +761,7 @@ void setup(void){
726761
server.on("/baud", handleBaud);
727762
server.on("/version", [](){ server.send(200, "text/plain", "1.1.R"); });
728763
server.on("/nodeid", handleNodeId);
764+
server.on("/settings", handleSettings);
729765

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

src/config.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}
16+
}
17+
int Config::getCanRXPin() {
18+
return settings.canRXPin;
19+
}
20+
21+
int Config::getCanTXPin() {
22+
return settings.canTXPin;
23+
}
24+
25+
void Config::setCanRXPin(int pin) {
26+
settings.canRXPin = pin;
27+
}
28+
29+
void Config::setCanTXPin(int pin) {
30+
settings.canTXPin = pin;
31+
}
32+
33+
void Config::saveSettings() {
34+
EEPROM.put(0, settings); //save all change to eeprom
35+
EEPROM.commit();
36+
}

src/config.h

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

src/oi_can.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,11 @@ BaudRate GetBaudRate() {
636636
return baudRate;
637637
}
638638

639-
void Init(uint8_t nodeId, BaudRate baud) {
639+
void Init(uint8_t nodeId, BaudRate baud, int txPin, int rxPin) {
640640
twai_general_config_t g_config = {
641641
.mode = TWAI_MODE_NORMAL,
642-
.tx_io = GPIO_NUM_25,
643-
.rx_io = GPIO_NUM_26,
642+
.tx_io = static_cast<gpio_num_t>(txPin),
643+
.rx_io = static_cast<gpio_num_t>(rxPin),
644644
.clkout_io = TWAI_IO_UNUSED,
645645
.bus_off_io = TWAI_IO_UNUSED,
646646
.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)