Merge pull request #10 from gskjold/issue-6

Make the configuration page available all the time
This commit is contained in:
Gunnar Skjold 2020-01-24 14:47:29 +01:00 committed by GitHub
commit 4bfd9dee9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 413 additions and 56 deletions

File diff suppressed because one or more lines are too long

View File

@ -27,6 +27,7 @@
class HanConfigAp {
public:
void setup(int accessPointButtonPin, Stream* debugger);
void enableWeb();
bool loop();
bool hasConfig();
configuration config;
@ -46,6 +47,7 @@ private:
// Web server
static void handleRoot();
static void handleStyle();
static void handleSave();
#if defined(ESP8266)
static ESP8266WebServer server;

View File

@ -0,0 +1,85 @@
const char CONFIG_HTML[] PROGMEM = R"=="==(
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="/style.css">
<title>AMS2MQTT - configuration</title>
</head>
<body>
<form method='post' action='/save'>
<div class="wrapper">
<div class="inner-wrapper">
<div>
<h2>WiFi</h2>
</div>
<div>
<input type='text' name='ssid' value="${config.ssid}" placeholder="SSID">
</div>
<div>
<input type='password' name='ssidPassword' value="${config.ssidPassword}" placeholder="Password">
</div>
</div>
<div class="inner-wrapper">
<div>
<h2>Meter Type</h2>
</div>
<div class="select-style">
<select name="meterType">
<option value="0" ${config.meterType0} disabled class="disabled-option"> SELECT TYPE </option>
<option value="1" ${config.meterType1}>Kaifa</option>
<option value="2" ${config.meterType2}>Aidon</option>
<option value="3" ${config.meterType3}>Kamstrup</option>
</select>
</div>
</div>
<div class="inner-wrapper">
<div>
<h2>MQTT</h2>
</div>
<div>
<label>Server & port:</label>
<input type='text' name='mqtt' value="${config.mqtt}" placeholder="server">
<input type='number' name='mqttPort' value="${config.mqttPort}" placeholder="port">
</div>
<div>
<label>Client ID:</label>
<input type='text' name='mqttClientID' value="${config.mqttClientID}" placeholder="client id">
</div>
<div>
<label>Publish topic: </label>
<input type='text' name='mqttPublishTopic' value="${config.mqttPublishTopic}" placeholder="topic">
</div>
<div>
<label>Username:</label>
<input type='text' name='mqttUser' value="${config.mqttUser}" placeholder="Blank for insecure">
</div>
<div>
<label>Password:</label>
<input type='password' name='mqttPass' value="${config.mqttPass}" placeholder="Blank for insecure">
</div>
<div>
<input class="submit-button" type='submit' value='save'>
</div>
</div>
<div class="inner-wrapper">
<div>
<h2>Webserver</h2>
</div>
<div>
<label>Username:</label>
<input type='text' name='authUser' value="${config.authUser}" placeholder="Blank for insecure">
</div>
<div>
<label>Password:</label>
<input type='password' name='authPass' value="${config.authPass}" placeholder="Blank for insecure">
</div>
</div>
</div>
</form>
<body>
</html>
)=="==";

View File

@ -38,6 +38,13 @@ bool configuration::save()
else
address += saveBool(address, false);
address += saveBool(address, isAuth());
if (isAuth()) {
address += saveString(address, authUser);
address += saveString(address, authPass);
}
bool success = EEPROM.commit();
EEPROM.end();
@ -50,8 +57,22 @@ bool configuration::load()
int address = EEPROM_CONFIG_ADDRESS;
bool success = false;
ssid = (char*)String("").c_str();
ssidPassword = (char*)String("").c_str();
meterType = (byte)0;
mqtt = (char*)String("").c_str();
mqttClientID = (char*)String("").c_str();
mqttPublishTopic = (char*)String("").c_str();
mqttSubscribeTopic = (char*)String("").c_str();
mqttUser = 0;
mqttPass = 0;
mqttPort = 1883;
authUser = 0;
authPass = 0;
EEPROM.begin(EEPROM_SIZE);
if (EEPROM.read(address) == EEPROM_CHECK_SUM)
int cs = EEPROM.read(address);
if (cs >= 71)
{
address++;
@ -80,18 +101,18 @@ bool configuration::load()
success = true;
}
else
{
ssid = (char*)String("").c_str();
ssidPassword = (char*)String("").c_str();
meterType = (byte)0;
mqtt = (char*)String("").c_str();
mqttClientID = (char*)String("").c_str();
mqttPublishTopic = (char*)String("").c_str();
mqttSubscribeTopic = (char*)String("").c_str();
mqttUser = 0;
mqttPass = 0;
mqttPort = 1883;
if(cs >= 72) {
bool auth = false;
address += readBool(address, &auth);
if (auth) {
address += readString(address, &authUser);
address += readString(address, &authPass);
} else {
authUser = 0;
authPass = 0;
}
success = true;
}
EEPROM.end();
return success;
@ -102,6 +123,10 @@ bool configuration::isSecure()
return (mqttUser != 0) && (String(mqttUser).length() > 0);
}
bool configuration::isAuth() {
return (authUser != 0) && (String(authUser).length() > 0);
}
int configuration::readInt(int address, int *value)
{
int lower = EEPROM.read(address);
@ -147,20 +172,6 @@ int configuration::saveByte(int address, byte value)
}
void configuration::print(Stream* debugger)
{
/*
char* ssid;
char* ssidPassword;
byte meterType;
char* mqtt;
int mqttPort;
char* mqttClientID;
char* mqttPublishTopic;
char* mqttSubscribeTopic;
bool secure;
char* mqttUser;
char* mqttPass;
*/
debugger->println("Configuration:");
debugger->println("-----------------------------------------------");
debugger->printf("ssid: %s\r\n", this->ssid);
@ -178,6 +189,13 @@ void configuration::print(Stream* debugger)
debugger->printf("mqttUser: %s\r\n", this->mqttUser);
debugger->printf("mqttPass: %s\r\n", this->mqttPass);
}
if (this->isAuth()) {
debugger->printf("WEB AUTH:\r\n");
debugger->printf("authUser: %s\r\n", this->authUser);
debugger->printf("authPass: %s\r\n", this->authPass);
}
debugger->println("-----------------------------------------------");
}

View File

@ -25,8 +25,12 @@ public:
char* mqttPass;
byte meterType;
char* authUser;
char* authPass;
bool hasConfig();
bool isSecure();
bool isAuth();
bool save();
bool load();
@ -35,7 +39,7 @@ protected:
private:
const int EEPROM_SIZE = 512;
const byte EEPROM_CHECK_SUM = 71; // Used to check if config is stored. Change if structure changes
const byte EEPROM_CHECK_SUM = 72; // Used to check if config is stored. Change if structure changes
const int EEPROM_CONFIG_ADDRESS = 0;
int saveString(int pAddress, char* pString);

View File

@ -0,0 +1,135 @@
const char STYLE_CSS[] PROGMEM = R"=="==(
body,div,input {
font-family: "Roboto", Arial, Lucida Grande;
}
.wrapper {
width: 500px;
position: absolute;
padding: 30px;
background-color: #FFF;
border-radius: 1px;
color: #333;
border-color: rgba(0, 0, 0, 0.03);
box-shadow: 0 2px 2px rgba(0, 0, 0, .24), 0 0 2px rgba(0, 0, 0, .12);
margin-left: 20px;
margin-top: 20px;
}
div {
padding-bottom: 5px;
}
label {
font-family: "Roboto", "Helvetica Neue", sans-serif;
font-size: 14px;
line-height: 16px;
width: 100px;
display: inline-block;
}
input {
font-family: "Roboto", "Helvetica Neue", sans-serif;
font-size: 14px;
line-height: 16px;
bottom: 30px;
border: none;
border-bottom: 1px solid #d4d4d4;
padding: 10px;
background: transparent;
transition: all .25s ease;
}
input[type=number] {
width: 70px;
margin-left: 5px;
}
input:focus {
outline: none;
border-bottom: 1px solid #3f51b5;
}
h2 {
text-align: left;
font-size: 20px;
font-weight: bold;
letter-spacing: 3px;
line-height: 28px;
}
.submit-button {
position: absolute;
text-align: right;
border-radius: 20px;
border-bottom-right-radius: 0;
border-top-right-radius: 0;
background-color: #3f51b5;
color: #FFF;
padding: 12px 25px;
display: inline-block;
font-size: 12px;
font-weight: bold;
letter-spacing: 2px;
right: 0px;
bottom: 10px;
cursor: pointer;
transition: all .25s ease;
box-shadow: 0 2px 2px rgba(0, 0, 0, .24), 0 0 2px rgba(0, 0, 0, .12);
width: 100px;
}
.select-style {
border-top: 10px solid white;
border-bottom: 1px solid #d4d4d4;
color: #ffffff;
cursor: pointer;
display: block;
font-family: Roboto, "Helvetica Neue", sans-serif;
font-size: 14px;
font-weight: 400;
height: 16px;
line-height: 14px;
min-width: 200px;
padding-bottom: 7px;
padding-left: 0px;
padding-right: 0px;
position: relative;
text-align: left;
width: 80%;
-webkit-box-direction: normal;
overflow: hidden;
background: #ffffff url("data:image/png;base64,R0lGODlhDwAUAIABAAAAAP///yH5BAEAAAEALAAAAAAPABQAAAIXjI+py+0Po5wH2HsXzmw//lHiSJZmUAAAOw==") no-repeat 98% 50%;
}
.disabled-option {
color: #d4d4d4;
}
.select-style select {
padding: 5px 8px;
width: 100%;
border: none;
box-shadow: none;
background: transparent;
background-image: none;
-webkit-appearance: none;
}
.select-style select:focus {
outline: none;
border: none;
}
@media only screen and (max-width: 1000px) {
.wrapper {
width: 80%;
}
}
@media only screen and (max-width: 300px) {
.wrapper {
width: 75%;
}
}
@media only screen and (max-width: 600px) {
.wrapper {
width: 80%;
margin-left: 0px;
margin-top: 0px;
}
.submit-button {
bottom: 0px;
width: 70px;
}
input {
width: 100%;
}
}
)=="==";

View File

@ -3,7 +3,7 @@ extra_configs = platformio-user.ini
[common]
framework = arduino
lib_deps = HanConfigAp@1.0.0, HanReader@1.0.0, HanToJson@1.0.0, ArduinoJson@^6.0.0, MQTT@^2.4.0, DallasTemperature@^3.8.0
lib_deps = HanConfigAp@1.0.0, HanReader@1.0.0, HanToJson@1.0.0, ArduinoJson@^6.0.0, MQTT@^2.4.0, DallasTemperature@^3.8.0, Base64@0.0.1
[env:esp12e]
platform = espressif8266

View File

@ -59,10 +59,11 @@ HardwareSerial* debugger = NULL;
HanReader hanReader;
// the setup function runs once when you press reset or power the board
void setup()
{
// Uncomment to debug over the same port as used for HAN communication
//debugger = &Serial;
void setup() {
#if DEBUG_MODE
debugger = &Serial;
#endif
if (debugger) {
// Setup serial port for debugging
@ -100,6 +101,8 @@ void setup()
// Compensate for the known Kaifa bug
hanReader.compensateFor09HeaderBug = (ap.config.meterType == 1);
}
ap.enableWeb();
}
// the loop function runs over and over again until power down or reset