apply formatting guidelines to existing files

This commit is contained in:
gilex-dev 2023-11-12 22:45:47 +01:00
parent e47bc6c4ab
commit 8c9de56fab
3 changed files with 404 additions and 401 deletions

View File

@ -3,24 +3,24 @@
void reload_home() // generates current time and changes variables in home_html
{
char time_current[3][3] = {"", "", ""};
if (myTime.hour() < 10) {
sprintf(time_current[0], "0%d", myTime.hour());
} else {
sprintf(time_current[0], "%d", myTime.hour());
}
if (myTime.minute() < 10) {
sprintf(time_current[1], "0%d", myTime.minute());
} else {
sprintf(time_current[1], "%d", myTime.minute());
}
if (myTime.second() < 10) {
sprintf(time_current[2], "0%d", myTime.second());
} else {
sprintf(time_current[2], "%d", myTime.second());
}
char time_current[3][3] = {"", "", ""};
if (myTime.hour() < 10) {
sprintf(time_current[0], "0%d", myTime.hour());
} else {
sprintf(time_current[0], "%d", myTime.hour());
}
if (myTime.minute() < 10) {
sprintf(time_current[1], "0%d", myTime.minute());
} else {
sprintf(time_current[1], "%d", myTime.minute());
}
if (myTime.second() < 10) {
sprintf(time_current[2], "0%d", myTime.second());
} else {
sprintf(time_current[2], "%d", myTime.second());
}
sprintf(home_html, "<!DOCTYPE html>\
sprintf(home_html, "<!DOCTYPE html>\
<html lang='de'>\
<head>\
<meta charset='UTF-8'>\
@ -93,13 +93,13 @@ top: 2px;\
</div>\
</body>\
</html>",
time_current[0], time_current[1], time_current[2], stat[0]);
time_current[0], time_current[1], time_current[2], stat[0]);
}
void reload_settings() {
sprintf(
settings_html,
"<span> Ziel: </span><input type='number' min=0 max=50 name='temperature' value='%d' class='temperature'>°C <span>\
sprintf(
settings_html,
"<span> Ziel: </span><input type='number' min=0 max=50 name='temperature' value='%d' class='temperature'>°C <span>\
<br>Aktuell: %d°C</span>\
<br>\
<input type='submit'>\
@ -175,45 +175,45 @@ void reload_settings() {
</div>\
</body>\
</html>",
temperatureSet, temperature, time_all[0][0], time_all[1][0],
time_all[2][0], time_all[3][0], time_all[4][0], time_all[5][0],
time_all[6][0], time_all[7][0], time_all[0][1], time_all[1][1],
time_all[2][1], time_all[3][1], time_all[4][1], time_all[5][1],
time_all[6][1], time_all[7][1], time_all[0][2], time_all[1][2],
time_all[2][2], time_all[3][2], time_all[4][2], time_all[5][2],
time_all[6][2], time_all[7][2], time_all[0][3], time_all[1][3],
time_all[2][3], time_all[3][3], time_all[4][3], time_all[5][3],
time_all[6][3], time_all[7][3], time_all[0][4], time_all[1][4],
time_all[2][4], time_all[3][4], time_all[4][4], time_all[5][4],
time_all[6][4], time_all[7][4], time_all[0][5], time_all[1][5],
time_all[2][5], time_all[3][5], time_all[4][5], time_all[5][5],
time_all[6][5], time_all[7][5], time_all[0][6], time_all[1][6],
time_all[2][6], time_all[3][6], time_all[4][6], time_all[5][6],
time_all[6][6], time_all[7][6], time_all[0][7], time_all[1][7],
time_all[2][7], time_all[3][7], time_all[4][7], time_all[5][7],
time_all[6][7], time_all[7][7]);
temperatureSet, temperature, time_all[0][0], time_all[1][0],
time_all[2][0], time_all[3][0], time_all[4][0], time_all[5][0],
time_all[6][0], time_all[7][0], time_all[0][1], time_all[1][1],
time_all[2][1], time_all[3][1], time_all[4][1], time_all[5][1],
time_all[6][1], time_all[7][1], time_all[0][2], time_all[1][2],
time_all[2][2], time_all[3][2], time_all[4][2], time_all[5][2],
time_all[6][2], time_all[7][2], time_all[0][3], time_all[1][3],
time_all[2][3], time_all[3][3], time_all[4][3], time_all[5][3],
time_all[6][3], time_all[7][3], time_all[0][4], time_all[1][4],
time_all[2][4], time_all[3][4], time_all[4][4], time_all[5][4],
time_all[6][4], time_all[7][4], time_all[0][5], time_all[1][5],
time_all[2][5], time_all[3][5], time_all[4][5], time_all[5][5],
time_all[6][5], time_all[7][5], time_all[0][6], time_all[1][6],
time_all[2][6], time_all[3][6], time_all[4][6], time_all[5][6],
time_all[6][6], time_all[7][6], time_all[0][7], time_all[1][7],
time_all[2][7], time_all[3][7], time_all[4][7], time_all[5][7],
time_all[6][7], time_all[7][7]);
}
void handleToggle() {
int i =
server.arg("number")
.toInt(); // handles manual toggle and converts GET argument to int
override[i] = !override[i];
debugln(F("---------------------------"));
debug(F("[RECIVED] for: "));
debugln(i);
digitalWrite(pins[i], !digitalRead(pins[i]));
digitalWrite(2, !digitalRead(2));
if (digitalRead(pins[i]) == HIGH) {
sprintf(stat[i], "AN");
} else {
sprintf(stat[i], "AUS");
}
debug(F("Status: "));
debugln(stat[i]);
reload_home();
server.sendHeader(F("Location"), F("/"), true);
server.send(302, F("text/html"), home_html);
debugln(override[i]);
debugln(F("---------------------------"));
int i =
server.arg("number")
.toInt(); // handles manual toggle and converts GET argument to int
override[i] = !override[i];
debugln(F("---------------------------"));
debug(F("[RECIVED] for: "));
debugln(i);
digitalWrite(pins[i], !digitalRead(pins[i]));
digitalWrite(2, !digitalRead(2));
if (digitalRead(pins[i]) == HIGH) {
sprintf(stat[i], "AN");
} else {
sprintf(stat[i], "AUS");
}
debug(F("Status: "));
debugln(stat[i]);
reload_home();
server.sendHeader(F("Location"), F("/"), true);
server.send(302, F("text/html"), home_html);
debugln(override[i]);
debugln(F("---------------------------"));
}

View File

@ -14,7 +14,7 @@ const char version[] = " 0.8.1";
ESP8266WebServer server;
IPAddress staticIP(192, 168, 2, 4); // static IP address
IPAddress gateway(192, 168, 2, 1); // Router's IP address
IPAddress gateway(192, 168, 2, 1); // Router's IP address
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 2, 1);
Timezone myTime;
@ -30,12 +30,12 @@ int temperatureSet;
static unsigned long lastTempCycle = -5000;
char time_all[8][8][3] = {
{}, {}, {}, {},
{}, {}, {}, {}}; // 3D array for load_EEPROM() and handleTime()
{}, {}, {}, {},
{}, {}, {}, {}}; // 3D array for load_EEPROM() and handleTime()
const unsigned int pins[1] = {4};
char stat[1][4] = {"AUS"}; // text on button
char t_stat[1][4] = {""}; // for "status of timer changed"
char stat[1][4] = {"AUS"}; // text on button
char t_stat[1][4] = {""}; // for "status of timer changed"
bool override[1] = {false}; // for manual override
char home_html[1346];
char settings_html[987];
@ -154,188 +154,191 @@ void reload_settings();
void handleToggle();
void load_EEPROM() {
unsigned int EEPROM_Addr = 0;
for (unsigned int i0 = 0; i0 < 8; i0++) {
for (unsigned int i = 0; i < 8; i++) {
if (EEPROM.read(EEPROM_Addr) < 10) {
sprintf(time_all[i0][i], "0%d", EEPROM.read(EEPROM_Addr));
} else {
sprintf(time_all[i0][i], "%d", EEPROM.read(EEPROM_Addr));
}
EEPROM_Addr += 4;
yield();
}
}
temperatureSet = EEPROM.read(256);
unsigned int EEPROM_Addr = 0;
for (unsigned int i0 = 0; i0 < 8; i0++) {
for (unsigned int i = 0; i < 8; i++) {
if (EEPROM.read(EEPROM_Addr) < 10) {
sprintf(time_all[i0][i], "0%d", EEPROM.read(EEPROM_Addr));
} else {
sprintf(time_all[i0][i], "%d", EEPROM.read(EEPROM_Addr));
}
EEPROM_Addr += 4;
yield();
}
}
temperatureSet = EEPROM.read(256);
}
void handleHome() {
reload_home();
server.send(200, "text/html", home_html);
debugln(ESP.getFreeHeap(), DEC);
reload_home();
server.send(200, "text/html", home_html);
debugln(ESP.getFreeHeap(), DEC);
}
void handleSettings() {
reload_settings();
server.setContentLength(strlen(settings_html_pre) + strlen(settings_html));
server.send(200, "text/html", settings_html_pre);
server.sendContent(settings_html);
debugln(ESP.getFreeHeap(), DEC);
reload_settings();
server.setContentLength(strlen(settings_html_pre) + strlen(settings_html));
server.send(200, "text/html", settings_html_pre);
server.sendContent(settings_html);
debugln(ESP.getFreeHeap(), DEC);
}
void handleTime() {
if (server.arg("rule") != "") {
debugln("write main");
const char args[4][9] = {"time_o", "time_f", "time_o_1", "time_f_1"};
if (server.arg("rule") != "") {
debugln("write main");
const char args[4][9] = {"time_o", "time_f", "time_o_1", "time_f_1"};
for (unsigned int arg = 0; arg < 4; arg++) {
if (server.arg(args[arg]) != "") {
char buff_h[2][2] = {server.arg(args[arg])[0],
server.arg(args[arg])[1]};
char buff_m[2][2] = {server.arg(args[arg])[3],
server.arg(args[arg])[4]};
sprintf(time_all[arg * 2][server.arg("rule").toInt()], "%s", buff_h);
sprintf(time_all[arg * 2 + 1][server.arg("rule").toInt()], "%s",
buff_m);
for (unsigned int arg = 0; arg < 4; arg++) {
if (server.arg(args[arg]) != "") {
char buff_h[2][2] = {server.arg(args[arg])[0],
server.arg(args[arg])[1]};
char buff_m[2][2] = {server.arg(args[arg])[3],
server.arg(args[arg])[4]};
sprintf(time_all[arg * 2][server.arg("rule").toInt()], "%s",
buff_h);
sprintf(time_all[arg * 2 + 1][server.arg("rule").toInt()], "%s",
buff_m);
EEPROM.put(server.arg("rule").toInt() * 4 + arg * 64,
atoi(time_all[arg * 2][server.arg("rule").toInt()]));
EEPROM.commit();
EEPROM.put(server.arg("rule").toInt() * 4 + arg * 64 + 32,
atoi(time_all[arg * 2 + 1][server.arg("rule").toInt()]));
EEPROM.commit();
}
}
}
if (server.arg("temperature").toInt() != temperatureSet) {
debugln("write temperature");
temperatureSet = server.arg("temperature").toInt();
EEPROM.put(256, temperatureSet);
EEPROM.commit();
}
reload_settings();
server.setContentLength(strlen(settings_html_pre) + strlen(settings_html));
server.send(301, "text/html", settings_html_pre);
server.sendContent(settings_html);
EEPROM.put(server.arg("rule").toInt() * 4 + arg * 64,
atoi(time_all[arg * 2][server.arg("rule").toInt()]));
EEPROM.commit();
EEPROM.put(
server.arg("rule").toInt() * 4 + arg * 64 + 32,
atoi(time_all[arg * 2 + 1][server.arg("rule").toInt()]));
EEPROM.commit();
}
}
}
if (server.arg("temperature").toInt() != temperatureSet) {
debugln("write temperature");
temperatureSet = server.arg("temperature").toInt();
EEPROM.put(256, temperatureSet);
EEPROM.commit();
}
reload_settings();
server.setContentLength(strlen(settings_html_pre) + strlen(settings_html));
server.send(301, "text/html", settings_html_pre);
server.sendContent(settings_html);
}
void timer() {
int ri = 0;
bool isOn = false;
while (ri <= 7) {
if ((((myTime.hour() * 60 + myTime.minute() >=
atoi(time_all[0][ri]) * 60 + atoi(time_all[1][ri])) and
(myTime.hour() * 60 + myTime.minute() <
atoi(time_all[2][ri]) * 60 + atoi(time_all[3][ri]))) // first range
or ((myTime.hour() * 60 + myTime.minute() >=
atoi(time_all[4][ri]) * 60 + atoi(time_all[5][ri])) and
(myTime.hour() * 60 + myTime.minute() <
atoi(time_all[6][ri]) * 60 + atoi(time_all[7][ri]))) or
((atoi(time_all[0][ri]) == atoi(time_all[2][ri])) and
((atoi(time_all[1][ri]) == atoi(time_all[3][ri])) and
(atoi(time_all[0][ri]) + atoi(time_all[1][ri]) !=
0)))) // second range
and temperature <= temperatureSet) {
isOn = true;
}
ri++;
}
if (isOn) {
if ((override[r]) and (strcmp(t_stat[r], "off") == 0)) {
override[r] = false;
debugln(F("debug#1"));
}
sprintf(t_stat[r], "on");
if (!override[r]) {
digitalWrite(pins[r], HIGH);
digitalWrite(2, HIGH); // LED
if (strcmp(stat[r], "AUS") == 0) {
debug(r);
debugln(F(" is on"));
sprintf(stat[r], "AN");
}
}
} else {
if ((override[r]) and (strcmp(t_stat[r], "on") == 0)) {
override[r] = false;
}
sprintf(t_stat[r], "off");
if (!override[r]) {
digitalWrite(pins[r], LOW);
digitalWrite(2, LOW); // LED
if (strcmp(stat[r], "AN") == 0) {
debug(r);
debugln(F(" is off"));
sprintf(stat[r], "AUS");
}
}
}
int ri = 0;
bool isOn = false;
while (ri <= 7) {
if ((((myTime.hour() * 60 + myTime.minute() >=
atoi(time_all[0][ri]) * 60 + atoi(time_all[1][ri])) and
(myTime.hour() * 60 + myTime.minute() <
atoi(time_all[2][ri]) * 60 +
atoi(time_all[3][ri]))) // first range
or ((myTime.hour() * 60 + myTime.minute() >=
atoi(time_all[4][ri]) * 60 + atoi(time_all[5][ri])) and
(myTime.hour() * 60 + myTime.minute() <
atoi(time_all[6][ri]) * 60 + atoi(time_all[7][ri]))) or
((atoi(time_all[0][ri]) == atoi(time_all[2][ri])) and
((atoi(time_all[1][ri]) == atoi(time_all[3][ri])) and
(atoi(time_all[0][ri]) + atoi(time_all[1][ri]) !=
0)))) // second range
and temperature <= temperatureSet) {
isOn = true;
}
ri++;
}
if (isOn) {
if ((override[r]) and (strcmp(t_stat[r], "off") == 0)) {
override[r] = false;
debugln(F("debug#1"));
}
sprintf(t_stat[r], "on");
if (!override[r]) {
digitalWrite(pins[r], HIGH);
digitalWrite(2, HIGH); // LED
if (strcmp(stat[r], "AUS") == 0) {
debug(r);
debugln(F(" is on"));
sprintf(stat[r], "AN");
}
}
} else {
if ((override[r]) and (strcmp(t_stat[r], "on") == 0)) {
override[r] = false;
}
sprintf(t_stat[r], "off");
if (!override[r]) {
digitalWrite(pins[r], LOW);
digitalWrite(2, LOW); // LED
if (strcmp(stat[r], "AN") == 0) {
debug(r);
debugln(F(" is off"));
sprintf(stat[r], "AUS");
}
}
}
}
void getCurrentTemperatur() {
if (millis() - lastTempCycle > 5000) {
sensors.requestTemperatures();
temperature = int(sensors.getTempCByIndex(0)) != -127
? int(sensors.getTempCByIndex(0))
: 255; // TODO: mark as invalid
lastTempCycle = millis();
debugln(temperature);
}
if (millis() - lastTempCycle > 5000) {
sensors.requestTemperatures();
temperature = int(sensors.getTempCByIndex(0)) != -127
? int(sensors.getTempCByIndex(0))
: 255; // TODO: mark as invalid
lastTempCycle = millis();
debugln(temperature);
}
}
void setup() {
int p = 0;
while (p <= 7) {
pinMode(pins[p], OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(pins[p], LOW);
digitalWrite(2, LOW); // LED
p++;
}
Serial.begin(74880);
EEPROM.begin(260); // 4 bits for EEPROM, address 0-255, value 0-255
sensors.begin();
debug(F("\nver."));
debugln(version);
debugln(F("check for updates at "
"https://somepi.ddns.net/gitea/gilex-dev/ESP8266-IOT-timer/\n"));
load_EEPROM();
// WLAN-config
// ---------------------------------------------------------------------------------------------------------------------
WiFi.mode(WIFI_STA);
WiFi.config(staticIP, gateway, subnet, dns);
WiFi.hostname("ESP8266 IOT development");
WiFi.begin(SSID, PASSWORD);
int p = 0;
while (p <= 7) {
pinMode(pins[p], OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(pins[p], LOW);
digitalWrite(2, LOW); // LED
p++;
}
Serial.begin(74880);
EEPROM.begin(260); // 4 bits for EEPROM, address 0-255, value 0-255
sensors.begin();
debug(F("\nver."));
debugln(version);
debugln(F("check for updates at "
"https://somepi.ddns.net/gitea/gilex-dev/ESP8266-IOT-timer/\n"));
load_EEPROM();
// WLAN-config
// ---------------------------------------------------------------------------------------------------------------------
WiFi.mode(WIFI_STA);
WiFi.config(staticIP, gateway, subnet, dns);
WiFi.hostname("ESP8266 IOT development");
WiFi.begin(SSID, PASSWORD);
debugln(F("Connecting ..."));
while (WiFi.status() != WL_CONNECTED) {
delay(250);
debug('.');
}
debug(F("\nConnected to "));
debugln(WiFi.SSID());
debug(F("IP address: "));
debugln(WiFi.localIP());
//----------------------------------------------------------------------------------------------------------------------------------
myTime.setLocation("de");
waitForSync();
debugln(F("Connecting ..."));
while (WiFi.status() != WL_CONNECTED) {
delay(250);
debug('.');
}
debug(F("\nConnected to "));
debugln(WiFi.SSID());
debug(F("IP address: "));
debugln(WiFi.localIP());
//----------------------------------------------------------------------------------------------------------------------------------
myTime.setLocation("de");
waitForSync();
debug(F("Local time: "));
debugln(myTime.dateTime("H:i:s"));
setInterval();
server.on("/", HTTP_GET, handleHome);
server.on("/settings", HTTP_GET, handleSettings);
server.on("/settings", HTTP_POST, handleTime);
server.on("/toggle", HTTP_GET, handleToggle);
server.on("/version", HTTP_GET,
[] { server.send(200, "text/html", version); });
getCurrentTemperatur();
server.begin();
debugln(F("Webserver started"));
debug(F("Local time: "));
debugln(myTime.dateTime("H:i:s"));
setInterval();
server.on("/", HTTP_GET, handleHome);
server.on("/settings", HTTP_GET, handleSettings);
server.on("/settings", HTTP_POST, handleTime);
server.on("/toggle", HTTP_GET, handleToggle);
server.on("/version", HTTP_GET,
[] { server.send(200, "text/html", version); });
getCurrentTemperatur();
server.begin();
debugln(F("Webserver started"));
}
void loop() {
server.handleClient();
getCurrentTemperatur();
timer();
yield();
delay(50);
server.handleClient();
getCurrentTemperatur();
timer();
yield();
delay(50);
}