ESP8266-IOT-timer/src/main.cpp

352 lines
9.0 KiB
C++
Raw Normal View History

const char SSID[] = "";
const char PASSWORD[] = "";
2023-11-13 20:41:30 +01:00
const char version[] = " 0.8.2";
#include "globalvars.h"
#include <Arduino.h>
#include <DallasTemperature.h>
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <ezTime.h>
#include <stdint.h>
ESP8266WebServer server;
IPAddress staticIP(192, 168, 2, 4); // static 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;
2023-11-13 20:41:30 +01:00
const u_int r = 0; // D3 on Amica
const u_int oneWireBus = 0;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
2023-11-13 20:41:30 +01:00
int temperature, temperatureSet, minTemperatureSet;
static unsigned long lastTempCycle = -5000;
char time_all[8][8][3] = {
{}, {}, {}, {},
2023-11-13 20:41:30 +01:00
{}, {}, {}, {}}; // 3D array for load_EEPROM() and handleTime()
const unsigned int pins[1] = {4}; // D2 on Amica
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];
const char settings_html_pre[] PROGMEM = {"<!DOCTYPE html>\
<html lang='de'>\
<head>\
<meta charset='UTF-8'>\
<meta http-equiv='X-UA-Compatible' content='IE=edge'>\
<meta name='viewport' content='width=device-width, initial-scale=1.0'>\
<title>Einstellungen</title>\
<style>\
* {\
font-family: Arial, Helvetica, sans-serif;\
font-size: 26px;\
user-select: none;\
text-decoration: none;\
}\
a {\
color: black;\
}\
.wrapper_main {\
margin: 0 auto;\
max-width: fit-content;\
max-width: -moz-fit-content;\
text-align: center;\
background-color: rgb(214, 214, 214);\
padding: 1px 8px 8px 8px;\
border-radius: 4px;\
}\
#rule, [type='time'], .temperature {\
height: 36px;\
border: none;\
padding: 0 4px;\
margin: 4px 0;\
background-color: white;\
width: fit-content;\
width: -moz-fit-content;\
border-radius: 4px;\
}\
.temperature {\
width: 2.5em;\
}\
.animated_a, [type='submit']{\
background-color: rgb(135, 252, 119);\
border: none;\
border-radius: 4px;\
box-shadow: rgb(25, 167, 49) 0 4px;\
position: relative;\
margin: 8px 0;\
padding: 2px 8px;\
}\
[type='submit'] {\
position: relative;\
top: -2px;\
display: inline-block;\
}\
.animated_a {\
text-decoration: none;\
display: inline-block;\
margin: 8px 0;\
padding-top: 4px;\
height: 28px;\
}\
.wrapper_table {\
overflow: auto;\
}\
table {\
margin: 8px auto;\
}\
table, th, td {\
border: 1px solid black;\
border-collapse: collapse;\
user-select: text;\
padding: 0 8px;\
}\
.animated_a, [type='submit']{\
cursor: pointer;\
}\
.animated_a:active {\
box-shadow: rgb(25, 167, 49) 0 2px;\
top: 2px;\
}\
[type='submit']:active {\
box-shadow: rgb(25, 167, 49) 0 2px;\
top: 0px;\
}\
*:focus {\
outline: none;\
}\
span:empty {content: 'nicht verbunden';}\
</style>\
</head>\
<body>\
<div class='wrapper_main'>\
<form action='settings' method='POST'>\
<select name='rule' id='rule'>\
<option value='' selected disabled>Spalte Nr.</option>\
<option value='0'>1</option>\
<option value='1'>2</option>\
<option value='2'>3</option>\
<option value='3'>4</option>\
<option value='4'>5</option>\
<option value='5'>6</option>\
<option value='6'>7</option>\
<option value='7'>8</option>\
</select>\
<br>\
<input type='time' name='time_o'>\
- <input type='time' name='time_f'>\
<br>\
<input type='time' name='time_o_1'>\
- <input type='time' name='time_f_1'>\
<br>"};
void reload_home();
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);
2023-11-13 20:41:30 +01:00
minTemperatureSet = EEPROM.read(260);
}
void handleHome() {
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);
}
void handleTime() {
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);
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();
}
2023-11-13 20:41:30 +01:00
if (server.arg("min_temperature").toInt() != minTemperatureSet) {
debugln("write minTemperature");
minTemperatureSet = server.arg("min_temperature").toInt();
EEPROM.put(260, minTemperatureSet);
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) {
2023-11-13 20:41:30 +01:00
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) or
temperature <= minTemperatureSet) {
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);
}
}
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);
2023-11-13 20:41:30 +01:00
EEPROM.begin(264); // 4 bits for EEPROM, address 0-263, 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();
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);
}