Compare commits
7 Commits
f54f387b10
...
518473b5a4
| Author | SHA1 | Date | |
|---|---|---|---|
| 518473b5a4 | |||
| 5529d26b55 | |||
| 3470681342 | |||
| 7345b125ee | |||
| 70bcffe505 | |||
| d6643a446e | |||
| 9b7765c768 |
182
index.html
182
index.html
@ -1,37 +1,68 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
<html lang="de">
|
<html lang="de">
|
||||||
<head>
|
<head>
|
||||||
<title id="title"></title>
|
<title id="title"></title>
|
||||||
<link rel="icon" type="image/svg" href="icon.svg">
|
<link rel="icon" type="image/svg" href="icon.svg">
|
||||||
<style>
|
<style>
|
||||||
.relay {
|
body {
|
||||||
display: flex;
|
font-family: sans-serif;
|
||||||
flex-direction: row;
|
margin: 0;
|
||||||
text-align: center;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
.relay > * {
|
.relay {
|
||||||
flex: 1;
|
display: flex;
|
||||||
}
|
flex-direction: row;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
.name {
|
.relay > * {
|
||||||
flex: 3;
|
flex: 1;
|
||||||
}
|
margin: 0.25em;
|
||||||
|
padding: 0.25em;
|
||||||
|
}
|
||||||
|
|
||||||
.header {
|
.name {
|
||||||
font-weight: bold;;
|
flex: 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
input, select, button {
|
.header {
|
||||||
width: 100%;
|
font-weight: bold;;
|
||||||
}
|
}
|
||||||
|
|
||||||
input[type=number], select {
|
input, select, button {
|
||||||
text-align: right;
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
#info {
|
input[type=number], select {
|
||||||
display: none;
|
text-align: right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.state {
|
||||||
|
}
|
||||||
|
|
||||||
|
.stateOn {
|
||||||
|
background-color: palegreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stateOff {
|
||||||
|
background-color: indianred;
|
||||||
|
}
|
||||||
|
|
||||||
|
.switchOn {
|
||||||
|
background-color: palegreen;
|
||||||
|
}
|
||||||
|
|
||||||
|
.switchOff {
|
||||||
|
background-color: indianred;
|
||||||
|
}
|
||||||
|
|
||||||
|
.switchCycle {
|
||||||
|
background-color: lightskyblue;
|
||||||
|
}
|
||||||
|
|
||||||
|
#info {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@ -41,12 +72,13 @@
|
|||||||
<div class="relay">
|
<div class="relay">
|
||||||
<div class="header name">Name</div>
|
<div class="header name">Name</div>
|
||||||
<div class="header state">Status</div>
|
<div class="header state">Status</div>
|
||||||
<div class="header initial">Initial</div>
|
<div class="header countdown">Countdown</div>
|
||||||
<div class="header onMillis">Auto Aus</div>
|
<div class="header onMillis">Auto Aus</div>
|
||||||
<div class="header offMillis">Auto An</div>
|
<div class="header offMillis">Auto An</div>
|
||||||
<div class="header switchOn">Ein</div>
|
<div class="header"></div>
|
||||||
<div class="header switchOff">Aus</div>
|
<div class="header"></div>
|
||||||
<div class="header switchCycle">Zyklus</div>
|
<div class="header"></div>
|
||||||
|
<div class="header initial">Initial</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -71,6 +103,68 @@
|
|||||||
|
|
||||||
let timeout;
|
let timeout;
|
||||||
|
|
||||||
|
function updateValue(tag, clazz, innerTag, value) {
|
||||||
|
const input = tag.getElementsByClassName(clazz)[0].getElementsByTagName(innerTag)[0];
|
||||||
|
if (document.activeElement !== input) {
|
||||||
|
input.value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateState(relayTag, state) {
|
||||||
|
const tag = relayTag.getElementsByClassName("state")[0];
|
||||||
|
if (state) {
|
||||||
|
tag.innerText = "Ein";
|
||||||
|
tag.classList.add("stateOn");
|
||||||
|
tag.classList.remove("stateOff");
|
||||||
|
} else {
|
||||||
|
tag.innerText = "Aus";
|
||||||
|
tag.classList.add("stateOff");
|
||||||
|
tag.classList.remove("stateOn");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const SECOND = 1000;
|
||||||
|
const MINUTE = (60 * SECOND);
|
||||||
|
const HOUR = (60 * MINUTE);
|
||||||
|
const DAY = (24 * HOUR);
|
||||||
|
|
||||||
|
function countdownString(relayData, millis) {
|
||||||
|
const rest = Math.ceil((millis - relayData.stateMillis - (Date.now() - dataAge)) / SECOND) * SECOND;
|
||||||
|
if (millis <= 0 || (relayData.onCount === 0 && !relayData.state)) {
|
||||||
|
return "-";
|
||||||
|
}
|
||||||
|
const days = rest / DAY;
|
||||||
|
const hours = rest / HOUR;
|
||||||
|
const minutes = rest / MINUTE;
|
||||||
|
const seconds = rest / SECOND;
|
||||||
|
if (days >= 1) {
|
||||||
|
return Math.floor(days) + "d " + Math.floor(hours % 24) + "h " + Math.ceil(minutes % 60) + "m " + Math.ceil(seconds % 60) + "s";
|
||||||
|
}
|
||||||
|
if (hours >= 1) {
|
||||||
|
return Math.floor(hours) + "h " + Math.floor(minutes % 60) + "m " + Math.ceil(seconds % 60) + "s";
|
||||||
|
}
|
||||||
|
if (minutes >= 1) {
|
||||||
|
return Math.floor(minutes) + "m " + Math.ceil(seconds % 60) + "s";
|
||||||
|
}
|
||||||
|
if (seconds >= 1) {
|
||||||
|
return Math.ceil(rest / SECOND) + "s";
|
||||||
|
}
|
||||||
|
return "Warte...";
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateCountdown(relayTag, relayData) {
|
||||||
|
const tag = relayTag.getElementsByClassName("countdown")[0];
|
||||||
|
if (relayData.state) {
|
||||||
|
tag.innerText = countdownString(relayData, relayData.onMillis);
|
||||||
|
} else {
|
||||||
|
tag.innerText = countdownString(relayData, relayData.offMillis);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let data;
|
||||||
|
|
||||||
|
let dataAge;
|
||||||
|
|
||||||
function request(query = "") {
|
function request(query = "") {
|
||||||
if (timeout) {
|
if (timeout) {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
@ -81,16 +175,18 @@
|
|||||||
r.open("GET", getUrl(`set?${query}`));
|
r.open("GET", getUrl(`set?${query}`));
|
||||||
r.onreadystatechange = () => {
|
r.onreadystatechange = () => {
|
||||||
if (r.readyState === 4 && r.status === 200) {
|
if (r.readyState === 4 && r.status === 200) {
|
||||||
const data = JSON.parse(r.response);
|
data = JSON.parse(r.response);
|
||||||
|
dataAge = Date.now();
|
||||||
title.innerText = data.hostname;
|
title.innerText = data.hostname;
|
||||||
for (let index = 0; index < data.relays.length; index++) {
|
for (let index = 0; index < data.relays.length; index++) {
|
||||||
const relayData = data.relays[index];
|
const relayData = data.relays[index];
|
||||||
const tag = document.getElementById("relay" + index) || create(index);
|
const relayTag = document.getElementById("relay" + index) || create(index);
|
||||||
tag.getElementsByClassName("name")[0].getElementsByTagName("input")[0].value = relayData.name;
|
updateValue(relayTag, "name", "input", relayData.name);
|
||||||
tag.getElementsByClassName("state")[0].innerText = relayData.state ? "Ein" : "Aus";
|
updateState(relayTag, relayData.state);
|
||||||
tag.getElementsByClassName("onMillis")[0].getElementsByTagName("input")[0].value = relayData.onMillis;
|
updateCountdown(relayTag, relayData);
|
||||||
tag.getElementsByClassName("offMillis")[0].getElementsByTagName("input")[0].value = relayData.offMillis;
|
updateValue(relayTag, "onMillis", "input", relayData.onMillis);
|
||||||
tag.getElementsByClassName("initial")[0].getElementsByTagName("select")[0].value = relayData.initial;
|
updateValue(relayTag, "offMillis", "input", relayData.offMillis);
|
||||||
|
updateValue(relayTag, "initial", "select", relayData.initial);
|
||||||
}
|
}
|
||||||
info.innerText = JSON.stringify(data, null, 2);
|
info.innerText = JSON.stringify(data, null, 2);
|
||||||
}
|
}
|
||||||
@ -119,7 +215,7 @@
|
|||||||
function newButton(relayIndex, relayTag, clazz, key, value, text) {
|
function newButton(relayIndex, relayTag, clazz, key, value, text) {
|
||||||
const div = newDiv(relayIndex, relayTag, clazz);
|
const div = newDiv(relayIndex, relayTag, clazz);
|
||||||
|
|
||||||
const button = document.createElement("button")
|
const button = document.createElement("div")
|
||||||
button.innerText = text;
|
button.innerText = text;
|
||||||
button.onclick = () => set(key, relayIndex, value);
|
button.onclick = () => set(key, relayIndex, value);
|
||||||
div.append(button);
|
div.append(button);
|
||||||
@ -152,16 +248,30 @@
|
|||||||
|
|
||||||
newInput(relayIndex, relayTag, "name", "text");
|
newInput(relayIndex, relayTag, "name", "text");
|
||||||
newDiv(relayIndex, relayTag, "state");
|
newDiv(relayIndex, relayTag, "state");
|
||||||
newSelect(relayIndex, relayTag, "initial", [["OFF", "Aus"], ["ON", "Ein"], ["CYCLE", "Zyklus"]]);
|
newDiv(relayIndex, relayTag, "countdown");
|
||||||
newInput(relayIndex, relayTag, "onMillis", "number");
|
newInput(relayIndex, relayTag, "onMillis", "number");
|
||||||
newInput(relayIndex, relayTag, "offMillis", "number");
|
newInput(relayIndex, relayTag, "offMillis", "number");
|
||||||
newButton(relayIndex, relayTag, "switchOn", "state", "true", "Ein");
|
newButton(relayIndex, relayTag, "switchOn", "state", "true", "Ein");
|
||||||
newButton(relayIndex, relayTag, "switchOff", "state", "false", "Aus");
|
newButton(relayIndex, relayTag, "switchOff", "state", "false", "Aus");
|
||||||
newButton(relayIndex, relayTag, "switchCycle", "onCount", -1, "Zyklus");
|
newButton(relayIndex, relayTag, "switchCycle", "onCount", -1, "Zyklus");
|
||||||
|
newSelect(relayIndex, relayTag, "initial", [["OFF", "Aus"], ["ON", "Ein"], ["CYCLE", "Zyklus"]]);
|
||||||
return relayTag;
|
return relayTag;
|
||||||
}
|
}
|
||||||
|
|
||||||
request();
|
request();
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
if (!data) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (let index = 0; index < data.relays.length; index++) {
|
||||||
|
const relayData = data.relays[index];
|
||||||
|
const relayTag = document.getElementById("relay" + index) || create(index);
|
||||||
|
updateCountdown(relayTag, relayData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setInterval(() => update(), 500);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@ -16,4 +16,4 @@ upload_speed = 921600
|
|||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
build.filesystem = littlefs
|
build.filesystem = littlefs
|
||||||
lib_deps = bblanchon/ArduinoJson @ 7.4.2
|
lib_deps = bblanchon/ArduinoJson @ 7.4.2
|
||||||
build_flags = -D STATUS_PIN=2 -D STATUS_INVERT=false
|
build_flags = -D STATUS_PIN=2 -D STATUS_INVERT=false -D CORE_DEBUG_LEVEL=0
|
||||||
@ -30,7 +30,7 @@ protected:
|
|||||||
void _write(const bool state) {
|
void _write(const bool state) {
|
||||||
if (state != get()) {
|
if (state != get()) {
|
||||||
if (logState) {
|
if (logState) {
|
||||||
Serial.printf("%s: %s\n", name.c_str(), state ? "ON" : "OFF");
|
Serial.printf("[RELAY] \"%s\" = %s\n", name.c_str(), state ? "ON" : "OFF");
|
||||||
}
|
}
|
||||||
stateMillis = millis();
|
stateMillis = millis();
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/Relay.h
24
src/Relay.h
@ -18,31 +18,39 @@ public:
|
|||||||
|
|
||||||
void setup() override {
|
void setup() override {
|
||||||
Output::setup();
|
Output::setup();
|
||||||
Output::setName(configRead(String("relayName") + index, nameFallback));
|
Output::setName(configRead(path("name"), nameFallback));
|
||||||
Output::setInitial(configRead(String("relayInitial") + index, INITIAL_OFF));
|
Output::setInitial(configRead(path("initial"), INITIAL_OFF));
|
||||||
Output::setOnMillis(configRead(String("relayOnMillis") + index, 0L));
|
Output::setOnMillis(configRead(path("onMillis"), 0L));
|
||||||
Output::setOffMillis(configRead(String("relayOffMillis") + index, 0L));
|
Output::setOffMillis(configRead(path("offMillis"), 0L));
|
||||||
_applyInitial();
|
_applyInitial();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setName(const String &value) override {
|
void setName(const String &value) override {
|
||||||
Output::setName(value);
|
Output::setName(value);
|
||||||
configWrite(String("relayName") + index, nameFallback, value);
|
configWrite(path("name"), nameFallback, value, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setInitial(const Initial value) override {
|
void setInitial(const Initial value) override {
|
||||||
Output::setInitial(value);
|
Output::setInitial(value);
|
||||||
configWrite(String("relayInitial") + index, INITIAL_OFF, value);
|
configWrite(path("initial"), INITIAL_OFF, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOnMillis(const unsigned long value) override {
|
void setOnMillis(const unsigned long value) override {
|
||||||
Output::setOnMillis(value);
|
Output::setOnMillis(value);
|
||||||
configWrite(String("relayOnMillis") + index, 0L, value);
|
configWrite(path("onMillis"), 0L, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOffMillis(const unsigned long value) override {
|
void setOffMillis(const unsigned long value) override {
|
||||||
Output::setOffMillis(value);
|
Output::setOffMillis(value);
|
||||||
configWrite(String("relayOffMillis") + index, 0L, value);
|
configWrite(path("offMillis"), 0L, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
String path(const char *name) const {
|
||||||
|
char path[64];
|
||||||
|
snprintf(path, sizeof(path), "/relay%d/%s", index, name);
|
||||||
|
return String(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
124
src/config.cpp
124
src/config.cpp
@ -1,83 +1,141 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <LittleFS.h>
|
#include <LittleFS.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
|
enum CONFIG_LOG {
|
||||||
|
CONFIG_LOG_FALLBACK,
|
||||||
|
CONFIG_LOG_READ,
|
||||||
|
CONFIG_LOG_UNCHANGED,
|
||||||
|
CONFIG_LOG_WRITE,
|
||||||
|
};
|
||||||
|
|
||||||
|
void listDir(const String &path, const String &indent) {
|
||||||
|
auto dir = LittleFS.open(path, "r");
|
||||||
|
if (!dir || !dir.isDirectory()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (true) {
|
||||||
|
File child = dir.openNextFile();
|
||||||
|
if (!child) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (child.isDirectory()) {
|
||||||
|
Serial.printf("%s[D] [ ] %s\n", indent.c_str(), child.name());
|
||||||
|
listDir(child.path(), indent + " ");
|
||||||
|
}
|
||||||
|
child.close();
|
||||||
|
}
|
||||||
|
dir.rewindDirectory();
|
||||||
|
while (true) {
|
||||||
|
File child = dir.openNextFile();
|
||||||
|
if (!child) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!child.isDirectory()) {
|
||||||
|
Serial.printf("%s[F] [%4d] %s\n", indent.c_str(), child.size(), child.name());
|
||||||
|
}
|
||||||
|
child.close();
|
||||||
|
}
|
||||||
|
dir.close();
|
||||||
|
}
|
||||||
|
|
||||||
void configSetup() {
|
void configSetup() {
|
||||||
LittleFS.begin();
|
LittleFS.begin(true);
|
||||||
|
Serial.println("Filesystem-content:");
|
||||||
|
listDir("/", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
File configOpen(const String &name, const char *mode) {
|
File configOpen(const String &path, const bool write) {
|
||||||
char path[64];
|
if (!write && !LittleFS.exists(path)) {
|
||||||
snprintf(path, sizeof(path), "/%s", name.c_str());
|
return {};
|
||||||
return LittleFS.open(path, mode);
|
}
|
||||||
|
return LittleFS.open(path, write ? "w" : "r", write);
|
||||||
}
|
}
|
||||||
|
|
||||||
long configRead(const String &name, const long fallback) {
|
void doLog(const String &path, const String &value, const bool isPassword, const CONFIG_LOG type, const bool enable) {
|
||||||
if (auto file = configOpen(name, "r")) {
|
if (!enable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Serial.printf(
|
||||||
|
"[CONFIG] %-20s = %-30s [%s]\n",
|
||||||
|
path.c_str(),
|
||||||
|
isPassword ? "*" : value.c_str(),
|
||||||
|
type == CONFIG_LOG_FALLBACK ? "fallback" : type == CONFIG_LOG_READ ? "READ" : type == CONFIG_LOG_UNCHANGED ? "UNCHANGED" : type == CONFIG_LOG_WRITE ? "WRITE" : ""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
long configRead(const String &path, const long fallback, const bool log) {
|
||||||
|
if (auto file = configOpen(path, false)) {
|
||||||
const auto content = file.readString();
|
const auto content = file.readString();
|
||||||
file.close();
|
file.close();
|
||||||
return content.toInt();
|
const auto value = content.toInt();
|
||||||
|
doLog(path, String(value), false, CONFIG_LOG_READ, log);
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
doLog(path, String(fallback), false, CONFIG_LOG_FALLBACK, log);
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const long fallback, const long value) {
|
bool configWrite(const String &path, const long fallback, const long value) {
|
||||||
if (configRead(name, fallback) == value) {
|
if (configRead(path, fallback, false) == value) {
|
||||||
|
doLog(path, String(value), false, CONFIG_LOG_UNCHANGED, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Serial.printf("[CONFIG] \"%s\" = \"%ld\"\n", name.c_str(), value);
|
if (auto file = configOpen(path, true)) {
|
||||||
if (auto file = configOpen(name, "w")) {
|
|
||||||
const auto content = String(value);
|
const auto content = String(value);
|
||||||
file.write(reinterpret_cast<const uint8_t *>(content.c_str()), content.length());
|
file.write(reinterpret_cast<const uint8_t *>(content.c_str()), content.length());
|
||||||
file.close();
|
file.close();
|
||||||
|
doLog(path, String(value), false, CONFIG_LOG_WRITE, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configRead(const String &name, const bool fallback) {
|
bool configRead(const String &path, const bool fallback) {
|
||||||
return configRead(name, fallback ? 1L : 0L) > 0;
|
return configRead(path, fallback ? 1L : 0L) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const bool fallback, const bool value) {
|
bool configWrite(const String &path, const bool fallback, const bool value) {
|
||||||
return configWrite(name, fallback ? 1L : 0L, value ? 1L : 0L);
|
return configWrite(path, fallback ? 1L : 0L, value ? 1L : 0L);
|
||||||
}
|
}
|
||||||
|
|
||||||
String configRead(const String &name, const char *fallback) {
|
String configRead(const String &path, const char *fallback) {
|
||||||
return configRead(name, String(fallback));
|
return configRead(path, String(fallback));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const char *fallback, const char *value) {
|
bool configWrite(const String &path, const char *fallback, const char *value) {
|
||||||
return configWrite(name, String(fallback), String(value));
|
return configWrite(path, String(fallback), String(value), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
String configRead(const String &name, const String &fallback) {
|
String configRead(const String &path, const String &fallback, const bool log, const bool isPassword) {
|
||||||
if (auto file = configOpen(name, "r")) {
|
if (auto file = configOpen(path, false)) {
|
||||||
const auto content = file.readString();
|
const auto value = file.readString();
|
||||||
file.close();
|
file.close();
|
||||||
return content;
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_READ, log);
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
doLog(path, fallback.c_str(), isPassword, CONFIG_LOG_FALLBACK, log);
|
||||||
return fallback;
|
return fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const String &fallback, const String &value) {
|
bool configWrite(const String &path, const String &fallback, const String &value, const bool isPassword) {
|
||||||
if (configRead(name, fallback) == value) {
|
if (configRead(path, fallback, false) == value) {
|
||||||
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_UNCHANGED, true);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Serial.printf("[CONFIG] \"%s\" = \"%s\"\n", name.c_str(), value.c_str());
|
if (auto file = configOpen(path, true)) {
|
||||||
if (auto file = configOpen(name, "w")) {
|
|
||||||
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
file.write(reinterpret_cast<const uint8_t *>(value.c_str()), value.length());
|
||||||
file.close();
|
file.close();
|
||||||
|
doLog(path, value.c_str(), isPassword, CONFIG_LOG_WRITE, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Initial configRead(const String &name, const Initial &fallback) {
|
Initial configRead(const String &path, const Initial &fallback) {
|
||||||
return stringToInitial(configRead(name, initialToString(fallback)));
|
return stringToInitial(configRead(path, initialToString(fallback)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool configWrite(const String &name, const Initial &fallback, const Initial &value) {
|
bool configWrite(const String &path, const Initial &fallback, const Initial &value) {
|
||||||
return configWrite(name, initialToString(fallback), initialToString(value));
|
return configWrite(path, initialToString(fallback), initialToString(value));
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/config.h
20
src/config.h
@ -7,24 +7,24 @@
|
|||||||
|
|
||||||
void configSetup();
|
void configSetup();
|
||||||
|
|
||||||
long configRead(const String &name, long fallback);
|
long configRead(const String &path, long fallback, bool log = true);
|
||||||
|
|
||||||
bool configWrite(const String &name, long fallback, long value);
|
bool configWrite(const String &path, long fallback, long value);
|
||||||
|
|
||||||
bool configRead(const String &name, bool fallback);
|
bool configRead(const String &path, bool fallback);
|
||||||
|
|
||||||
bool configWrite(const String &name, bool fallback, bool value);
|
bool configWrite(const String &path, bool fallback, bool value);
|
||||||
|
|
||||||
String configRead(const String &name, const char *fallback);
|
String configRead(const String &path, const char *fallback);
|
||||||
|
|
||||||
bool configWrite(const String &name, const char *fallback, const char *value);
|
bool configWrite(const String &path, const char *fallback, const char *value);
|
||||||
|
|
||||||
String configRead(const String &name, const String &fallback);
|
String configRead(const String &path, const String &fallback, bool log = true, bool isPassword = false);
|
||||||
|
|
||||||
bool configWrite(const String &name, const String &fallback, const String &value);
|
bool configWrite(const String &path, const String &fallback, const String &value, bool isPassword);
|
||||||
|
|
||||||
Initial configRead(const String &name, const Initial &fallback);
|
Initial configRead(const String &path, const Initial &fallback);
|
||||||
|
|
||||||
bool configWrite(const String &name, const Initial &fallback, const Initial &value);
|
bool configWrite(const String &path, const Initial &fallback, const Initial &value);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -29,10 +29,8 @@ void httpRelay(const int index, Output &relay) {
|
|||||||
if (server.hasArg(stateKey)) {
|
if (server.hasArg(stateKey)) {
|
||||||
const auto state = server.arg(stateKey);
|
const auto state = server.arg(stateKey);
|
||||||
if (state == "true") {
|
if (state == "true") {
|
||||||
Serial.printf("[HTTP] %s = %s\n", stateKey.c_str(), state.c_str());
|
|
||||||
relay.set(true);
|
relay.set(true);
|
||||||
} else if (state == "false") {
|
} else if (state == "false") {
|
||||||
Serial.printf("[HTTP] %s = %s\n", stateKey.c_str(), state.c_str());
|
|
||||||
relay.set(false);
|
relay.set(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,13 +39,10 @@ void httpRelay(const int index, Output &relay) {
|
|||||||
if (server.hasArg(initialKey)) {
|
if (server.hasArg(initialKey)) {
|
||||||
const auto initial = server.arg(initialKey);
|
const auto initial = server.arg(initialKey);
|
||||||
if (initial == "OFF") {
|
if (initial == "OFF") {
|
||||||
Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str());
|
|
||||||
relay.setInitial(INITIAL_OFF);
|
relay.setInitial(INITIAL_OFF);
|
||||||
} else if (initial == "ON") {
|
} else if (initial == "ON") {
|
||||||
Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str());
|
|
||||||
relay.setInitial(INITIAL_ON);
|
relay.setInitial(INITIAL_ON);
|
||||||
} else if (initial == "CYCLE") {
|
} else if (initial == "CYCLE") {
|
||||||
Serial.printf("[HTTP] %s = %s\n", initialKey.c_str(), initial.c_str());
|
|
||||||
relay.setInitial(INITIAL_CYCLE);
|
relay.setInitial(INITIAL_CYCLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -55,21 +50,18 @@ void httpRelay(const int index, Output &relay) {
|
|||||||
const auto onCountKey = String("onCount") + index;
|
const auto onCountKey = String("onCount") + index;
|
||||||
if (server.hasArg(onCountKey)) {
|
if (server.hasArg(onCountKey)) {
|
||||||
const auto value = server.arg(onCountKey).toInt();
|
const auto value = server.arg(onCountKey).toInt();
|
||||||
Serial.printf("[HTTP] %s = %ld\n", onCountKey.c_str(), value);
|
|
||||||
relay.setOnCount(value);
|
relay.setOnCount(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto onMillisKey = String("onMillis") + index;
|
const auto onMillisKey = String("onMillis") + index;
|
||||||
if (server.hasArg(onMillisKey)) {
|
if (server.hasArg(onMillisKey)) {
|
||||||
const auto value = server.arg(onMillisKey).toInt();
|
const auto value = server.arg(onMillisKey).toInt();
|
||||||
Serial.printf("[HTTP] %s = %ld\n", onMillisKey.c_str(), value);
|
|
||||||
relay.setOnMillis(value);
|
relay.setOnMillis(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto offMillisKey = String("offMillis") + index;
|
const auto offMillisKey = String("offMillis") + index;
|
||||||
if (server.hasArg(offMillisKey)) {
|
if (server.hasArg(offMillisKey)) {
|
||||||
const auto value = server.arg(offMillisKey).toInt();
|
const auto value = server.arg(offMillisKey).toInt();
|
||||||
Serial.printf("[HTTP] %s = %ld\n", offMillisKey.c_str(), value);
|
|
||||||
relay.setOffMillis(value);
|
relay.setOffMillis(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,13 +8,13 @@
|
|||||||
#define STATUS_INVERT true
|
#define STATUS_INVERT true
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Relay relay0(1, "RELAY #0", 12, false, true);
|
Relay relay0(0, "RELAY #0", 12, false, true);
|
||||||
|
|
||||||
Relay relay1(2, "RELAY #1", 5, false, true);
|
Relay relay1(1, "RELAY #1", 5, false, true);
|
||||||
|
|
||||||
Relay relay2(3, "RELAY #2", 4, false, true);
|
Relay relay2(2, "RELAY #2", 4, false, true);
|
||||||
|
|
||||||
Relay relay3(4, "RELAY #3", 15, false, true);
|
Relay relay3(3, "RELAY #3", 15, false, true);
|
||||||
|
|
||||||
Output status("Status", STATUS_PIN, STATUS_INVERT, false);
|
Output status("Status", STATUS_PIN, STATUS_INVERT, false);
|
||||||
|
|
||||||
|
|||||||
19
src/wifi.cpp
19
src/wifi.cpp
@ -3,9 +3,9 @@
|
|||||||
#include "http.h"
|
#include "http.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
#define CONFIG_HOSTNAME "hostname"
|
#define CONFIG_HOSTNAME "/wifi/hostname"
|
||||||
#define CONFIG_WIFI_SSID "wifiSSID"
|
#define CONFIG_WIFI_SSID "/wifi/ssid"
|
||||||
#define CONFIG_WIFI_PASSWORD "wifiPassword"
|
#define CONFIG_WIFI_PASSWORD "/wifi/password"
|
||||||
|
|
||||||
#define DEFAULT_HOSTNAME "PatrixSonoff4ChPro"
|
#define DEFAULT_HOSTNAME "PatrixSonoff4ChPro"
|
||||||
#define DEFAULT_WIFI_SSID "HappyNet"
|
#define DEFAULT_WIFI_SSID "HappyNet"
|
||||||
@ -28,7 +28,7 @@ void wifiConnect() {
|
|||||||
|
|
||||||
const auto hostname = configRead(CONFIG_HOSTNAME, DEFAULT_HOSTNAME);
|
const auto hostname = configRead(CONFIG_HOSTNAME, DEFAULT_HOSTNAME);
|
||||||
const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID);
|
const auto wifiSSID = configRead(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID);
|
||||||
const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD);
|
const auto wifiPass = configRead(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, true, true);
|
||||||
|
|
||||||
WiFi.hostname(hostname);
|
WiFi.hostname(hostname);
|
||||||
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
|
WiFi.begin(wifiSSID.c_str(), wifiPass.c_str());
|
||||||
@ -66,22 +66,21 @@ void wifiLoop() {
|
|||||||
void wifiChangeHostname(const char *hostname) {
|
void wifiChangeHostname(const char *hostname) {
|
||||||
if (configWrite(CONFIG_HOSTNAME, DEFAULT_HOSTNAME, hostname)) {
|
if (configWrite(CONFIG_HOSTNAME, DEFAULT_HOSTNAME, hostname)) {
|
||||||
WiFi.setHostname(hostname);
|
WiFi.setHostname(hostname);
|
||||||
Serial.printf("[WiFi] hostname: %s\n", hostname);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiChangeSSID(const char *ssid) {
|
void wifiChangeSSID(const char *ssid) {
|
||||||
if (configWrite(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID, ssid)) {
|
configWrite(CONFIG_WIFI_SSID, DEFAULT_WIFI_SSID, ssid);
|
||||||
Serial.printf("[WiFi] SSID: %s\n", ssid);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiChangePassword(const char *password) {
|
void wifiChangePassword(const char *password) {
|
||||||
configWrite(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, password);
|
configWrite(CONFIG_WIFI_PASSWORD, DEFAULT_WIFI_PASSWORD, password, true);
|
||||||
Serial.printf("[WiFi] password: ***");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiSetup() {
|
void wifiSetup() {
|
||||||
|
#ifdef ESP32
|
||||||
|
esp_log_level_set("wifi", ESP_LOG_NONE);
|
||||||
|
#endif
|
||||||
ArduinoOTA.onStart([] {
|
ArduinoOTA.onStart([] {
|
||||||
Serial.println("[OTA] Begin!");
|
Serial.println("[OTA] Begin!");
|
||||||
status.set(true);
|
status.set(true);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user