2 Commits

Author SHA1 Message Date
0923c59280 add config switch 2022-12-23 14:48:47 +01:00
015309f20f looks good 2022-12-23 10:45:09 +01:00
4 changed files with 78 additions and 51 deletions

View File

@ -10,7 +10,7 @@
#define LED_RED 3 #define LED_RED 3
#define LED_GREEN 2 #define LED_GREEN 2
#define LED_BLUE 45 #define LED_BLUE 45
#define CONFIG_SWITCH 46

View File

@ -26,8 +26,8 @@ const uint32_t MAGIC = 0xaffe0001;
config_t myConfig = { config_t myConfig = {
.magic = MAGIC, .magic = MAGIC,
.appEui = { 0, 0, 0, 0, 0, 0, 0, 0 }, .appEui = { 0xa0, 0x57, 0x81, 0x00, 0x01, 0x12, 0xaa, 0xf3 },
.appKey = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .appKey = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 },
.modbus_poll_slots = { .modbus_poll_slots = {
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x01 }, { .typ = INPUT_REGISTERS, .id = 7, .address = 0x01 },
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x02 }, { .typ = INPUT_REGISTERS, .id = 7, .address = 0x02 },
@ -111,47 +111,20 @@ static void handleRoot() {
configParsingFailed = 0; configParsingFailed = 0;
buffer << "<h2>Error when parsing field " << configParsingFailed << ", error " << configParsingError << "</h2>"; buffer << "<h2>Error when parsing field " << configParsingFailed << ", error " << configParsingError << "</h2>";
} }
buffer << " <form action=\"config\" method=\"GET\">" buffer << " <form action=\"config\" method=\"POST\">"
" <table>" " <table>"
" <tr>" " <tr>"
" <td>" " <td>";
" DevEui" buffer << " <textarea name=\"config\" cols=\"50\" rows=\"40\">";
" </td><td>";
buffer << " <input type=\"text\" readonly name=\"DevEui\" id=\"DevEui\" size=\"23\" value=\""; buffer << "# Configuration of the Modbus LoRaWAN Gateway";
for (int i = 0; i < 8; i++) {
if (i != 0) { buffer << " </textarea";
buffer << ":"; buffer << " </td><td valign=\"top\">"
} " Edit the textarea beside. <br/>"
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)devEui[i]; " Make sure to keep all parameters within and follow the shown format.<br/>"
} " You can use the # char to introduce line-based comments.<br/>"
buffer << "\"> (readonly)"; " </td>"
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppEui"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppEui\" id=\"AppEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appEui[i];
}
buffer << "\">";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppKey"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppKey\" id=\"AppKey\" size=\"47\" value=\"";
for (int i = 0; i < 16; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appKey[i];
}
buffer << "\">";
buffer << " </td>"
" </tr>" " </tr>"
" <tr><td colspan=\"2\">" " <tr><td colspan=\"2\">"
" <button type=\"submit\">Save</button>" " <button type=\"submit\">Save</button>"
@ -194,17 +167,11 @@ static void parseField(char* arg, int fieldNum, int reqTokens, uint8_t *dest) {
} }
static void handleConfigSave() { static void handleConfigSave() {
char *arg1 = (char*)server.arg("AppEui").c_str(); char *configText = (char*)server.arg("config").c_str();
Serial.printf("AppEui: %s\n\r", arg1); Serial.println(configText);
parseField(arg1, 1, 8, myConfig.appEui);
if (!configParsingFailed) {
char *arg2 = (char*)server.arg("AppKey").c_str();
Serial.printf("AppKey: %s\n\r", arg2);
parseField(arg2, 2, 16, myConfig.appKey);
}
configSaved = !configParsingFailed; configSaved = true;
server.sendHeader("Location", String("/"), true); server.sendHeader("Location", String("/"), true);
server.send(302, "text/plain", ""); server.send(302, "text/plain", "");
} }

View File

@ -18,10 +18,13 @@ void setup() {
pinMode(LED_GREEN, OUTPUT); pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_GREEN, LOW); digitalWrite(LED_GREEN, LOW);
pinMode(CONFIG_SWITCH, INPUT_PULLUP);
Serial.begin(115200); Serial.begin(115200);
configLoad(); configLoad();
productionMode = (digitalRead(CONFIG_SWITCH) == HIGH);
if (productionMode) { if (productionMode) {
productionSetup(); productionSetup();
} else { } else {

View File

@ -0,0 +1,57 @@
" DevEui"
" </td><td>";
buffer << " <input type=\"text\" readonly name=\"DevEui\" id=\"DevEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)devEui[i];
}
buffer << "\"> (readonly)";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppEui"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppEui\" id=\"AppEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appEui[i];
}
buffer << "\">";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppKey"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppKey\" id=\"AppKey\" size=\"47\" value=\"";
for (int i = 0; i < 16; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appKey[i];
}
buffer << "\">";
static void handleConfigSave() {
char *arg1 = (char*)server.arg("AppEui").c_str();
Serial.printf("AppEui: %s\n\r", arg1);
parseField(arg1, 1, 8, myConfig.appEui);
if (!configParsingFailed) {
char *arg2 = (char*)server.arg("AppKey").c_str();
Serial.printf("AppKey: %s\n\r", arg2);
parseField(arg2, 2, 16, myConfig.appKey);
}
configSaved = !configParsingFailed;
server.sendHeader("Location", String("/"), true);
server.send(302, "text/plain", "");
}