Compare commits
4 Commits
07b8a3aacf
...
ecdce86bb1
Author | SHA1 | Date |
---|---|---|
gilex-dev | ecdce86bb1 | |
gilex-dev | 8f37203b79 | |
gilex-dev | 88baeee58e | |
gilex-dev | 64ef26117f |
|
@ -195,9 +195,8 @@ void handleToggle() {
|
||||||
}
|
}
|
||||||
debug(F("Status: "));
|
debug(F("Status: "));
|
||||||
debugln(stat[i]);
|
debugln(stat[i]);
|
||||||
reload_home();
|
|
||||||
server.sendHeader(F("Location"), F("/"), true);
|
server.sendHeader(F("Location"), F("/"), true);
|
||||||
server.send(302, F("text/html"), home_html);
|
server.send(302);
|
||||||
debugln(override[i]);
|
debugln(override[i]);
|
||||||
debugln(F("---------------------------"));
|
debugln(F("---------------------------"));
|
||||||
}
|
}
|
||||||
|
|
143
src/main.cpp
143
src/main.cpp
|
@ -27,6 +27,7 @@ DallasTemperature sensors(&oneWire);
|
||||||
|
|
||||||
int temperature, temperatureSet, minTemperatureSet;
|
int temperature, temperatureSet, minTemperatureSet;
|
||||||
static unsigned long lastTempCycle = -5000;
|
static unsigned long lastTempCycle = -5000;
|
||||||
|
static unsigned long lastTimeCycle = -5000;
|
||||||
|
|
||||||
char time_all[8][8][3] = {
|
char time_all[8][8][3] = {
|
||||||
{}, {}, {}, {},
|
{}, {}, {}, {},
|
||||||
|
@ -151,6 +152,8 @@ span:empty {content: 'nicht verbunden';}\
|
||||||
void reload_home();
|
void reload_home();
|
||||||
void reload_settings();
|
void reload_settings();
|
||||||
void handleToggle();
|
void handleToggle();
|
||||||
|
void handleReset();
|
||||||
|
void handleRestart();
|
||||||
|
|
||||||
void load_EEPROM() {
|
void load_EEPROM() {
|
||||||
unsigned int EEPROM_Addr = 0;
|
unsigned int EEPROM_Addr = 0;
|
||||||
|
@ -179,7 +182,29 @@ void handleSettings() {
|
||||||
debugln(ESP.getFreeHeap(), DEC);
|
debugln(ESP.getFreeHeap(), DEC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void handleReset() {
|
||||||
|
for (int i = 0; i < 264; i++) {
|
||||||
|
EEPROM.put(i, 0);
|
||||||
|
EEPROM.commit();
|
||||||
|
}
|
||||||
|
server.send(200, "text/html",
|
||||||
|
"<html><head><meta http-equiv=\"refresh\" "
|
||||||
|
"content=\"2;url=/restart\"/></head><body><p>EEPROM has been "
|
||||||
|
"reset.</p></body></html>");
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleRestart() {
|
||||||
|
server.send(200, "text/html",
|
||||||
|
"<html><head><meta http-equiv=\"refresh\" "
|
||||||
|
"content=\"10;url=/\"/></head><body><p>Restarting...</p></"
|
||||||
|
"body></html>");
|
||||||
|
server.close();
|
||||||
|
delay(1000);
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
void handleTime() {
|
void handleTime() {
|
||||||
|
bool changed = false;
|
||||||
if (server.arg("rule") != "") {
|
if (server.arg("rule") != "") {
|
||||||
debugln("write main");
|
debugln("write main");
|
||||||
const char args[4][9] = {"time_o", "time_f", "time_o_1", "time_f_1"};
|
const char args[4][9] = {"time_o", "time_f", "time_o_1", "time_f_1"};
|
||||||
|
@ -202,6 +227,7 @@ void handleTime() {
|
||||||
server.arg("rule").toInt() * 4 + arg * 64 + 32,
|
server.arg("rule").toInt() * 4 + arg * 64 + 32,
|
||||||
atoi(time_all[arg * 2 + 1][server.arg("rule").toInt()]));
|
atoi(time_all[arg * 2 + 1][server.arg("rule").toInt()]));
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
|
changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -210,20 +236,28 @@ void handleTime() {
|
||||||
temperatureSet = server.arg("temperature").toInt();
|
temperatureSet = server.arg("temperature").toInt();
|
||||||
EEPROM.put(256, temperatureSet);
|
EEPROM.put(256, temperatureSet);
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
|
changed = true;
|
||||||
}
|
}
|
||||||
if (server.arg("min_temperature").toInt() != minTemperatureSet) {
|
if (server.arg("min_temperature").toInt() != minTemperatureSet) {
|
||||||
debugln("write minTemperature");
|
debugln("write minTemperature");
|
||||||
minTemperatureSet = server.arg("min_temperature").toInt();
|
minTemperatureSet = server.arg("min_temperature").toInt();
|
||||||
EEPROM.put(260, minTemperatureSet);
|
EEPROM.put(260, minTemperatureSet);
|
||||||
EEPROM.commit();
|
EEPROM.commit();
|
||||||
|
changed = true;
|
||||||
}
|
}
|
||||||
|
if (changed) {
|
||||||
|
lastTimeCycle = -5000; // trigger timer()
|
||||||
reload_settings();
|
reload_settings();
|
||||||
server.setContentLength(strlen(settings_html_pre) + strlen(settings_html));
|
server.sendHeader(F("Location"), F("/settings"), true);
|
||||||
server.send(301, "text/html", settings_html_pre);
|
server.send(302);
|
||||||
server.sendContent(settings_html);
|
} else {
|
||||||
|
server.send(204); // nothing changed (except maybe current temperature),
|
||||||
|
// so no page update needed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer() {
|
void timer() {
|
||||||
|
if (millis() - lastTimeCycle > 5000) {
|
||||||
int ri = 0;
|
int ri = 0;
|
||||||
bool isOn = false;
|
bool isOn = false;
|
||||||
while (ri <= 7) {
|
while (ri <= 7) {
|
||||||
|
@ -254,7 +288,7 @@ void timer() {
|
||||||
sprintf(t_stat[r], "on");
|
sprintf(t_stat[r], "on");
|
||||||
if (!override[r]) {
|
if (!override[r]) {
|
||||||
digitalWrite(pins[r], HIGH);
|
digitalWrite(pins[r], HIGH);
|
||||||
digitalWrite(2, HIGH); // LED
|
analogWrite(LED_BUILTIN_AUX, 100); // LED
|
||||||
if (strcmp(stat[r], "AUS") == 0) {
|
if (strcmp(stat[r], "AUS") == 0) {
|
||||||
debug(r);
|
debug(r);
|
||||||
debugln(F(" is on"));
|
debugln(F(" is on"));
|
||||||
|
@ -268,7 +302,7 @@ void timer() {
|
||||||
sprintf(t_stat[r], "off");
|
sprintf(t_stat[r], "off");
|
||||||
if (!override[r]) {
|
if (!override[r]) {
|
||||||
digitalWrite(pins[r], LOW);
|
digitalWrite(pins[r], LOW);
|
||||||
digitalWrite(2, LOW); // LED
|
analogWrite(LED_BUILTIN_AUX, 0); // LED
|
||||||
if (strcmp(stat[r], "AN") == 0) {
|
if (strcmp(stat[r], "AN") == 0) {
|
||||||
debug(r);
|
debug(r);
|
||||||
debugln(F(" is off"));
|
debugln(F(" is off"));
|
||||||
|
@ -276,24 +310,69 @@ void timer() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
lastTimeCycle = millis();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint ledBrightness = 0;
|
||||||
|
int ledBrightnessStep = 20;
|
||||||
|
uint lastLedCycle;
|
||||||
|
void pulseLed() {
|
||||||
|
if (millis() - lastLedCycle > 100) {
|
||||||
|
analogWrite(LED_BUILTIN_AUX, ledBrightness);
|
||||||
|
ledBrightness += ledBrightnessStep;
|
||||||
|
if (ledBrightness <= 0 || ledBrightness >= 255) {
|
||||||
|
ledBrightnessStep = -ledBrightnessStep;
|
||||||
|
}
|
||||||
|
lastLedCycle = millis();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void emergencyStop() {
|
||||||
|
for (uint pin = 0; pin < sizeof(pins) / sizeof(pins[0]); pin++) {
|
||||||
|
debug("Emergency stop, pulling down pin: ");
|
||||||
|
debugln(pin);
|
||||||
|
digitalWrite(pins[pin], LOW);
|
||||||
|
}
|
||||||
|
server.close();
|
||||||
|
char apSSID[32];
|
||||||
|
sprintf(apSSID, "ESP-%06X has an issue", ESP.getChipId());
|
||||||
|
WiFi.softAP(apSSID, "", 1, false, 0);
|
||||||
|
unsigned long lastTry = millis();
|
||||||
|
while (millis() - lastTry < (1000 * 60)) {
|
||||||
|
analogWrite(LED_BUILTIN, 255);
|
||||||
|
analogWrite(LED_BUILTIN_AUX, 0);
|
||||||
|
delay(125);
|
||||||
|
analogWrite(LED_BUILTIN, 0);
|
||||||
|
analogWrite(LED_BUILTIN_AUX, 255);
|
||||||
|
delay(125);
|
||||||
|
analogWrite(LED_BUILTIN, 255);
|
||||||
|
analogWrite(LED_BUILTIN_AUX, 255);
|
||||||
|
delay(250);
|
||||||
|
}
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
void getCurrentTemperature() {
|
||||||
|
if (millis() - lastTempCycle > 5000) {
|
||||||
|
sensors.requestTemperatures();
|
||||||
|
temperature = int(sensors.getTempCByIndex(0));
|
||||||
|
lastTempCycle = millis();
|
||||||
|
if (temperature == -127) {
|
||||||
|
emergencyStop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
int p = 0;
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
while (p <= 7) {
|
pinMode(LED_BUILTIN_AUX, OUTPUT);
|
||||||
|
analogWrite(LED_BUILTIN, 255);
|
||||||
|
analogWrite(LED_BUILTIN_AUX, 255);
|
||||||
|
uint p = 0;
|
||||||
|
while (p <= sizeof(pins) / sizeof(pins[0])) {
|
||||||
pinMode(pins[p], OUTPUT);
|
pinMode(pins[p], OUTPUT);
|
||||||
pinMode(2, OUTPUT);
|
|
||||||
digitalWrite(pins[p], LOW);
|
digitalWrite(pins[p], LOW);
|
||||||
digitalWrite(2, LOW); // LED
|
|
||||||
p++;
|
p++;
|
||||||
}
|
}
|
||||||
Serial.begin(74880);
|
Serial.begin(74880);
|
||||||
|
@ -308,15 +387,17 @@ void setup() {
|
||||||
|
|
||||||
// WiFI-config
|
// WiFI-config
|
||||||
// WiFi.mode(WIFI_STA); // comment to force use of staticIP
|
// WiFi.mode(WIFI_STA); // comment to force use of staticIP
|
||||||
WiFi.hostname("ESP8266 IOT development");
|
// WiFi.config(staticIP, gateway, subnet, dns);
|
||||||
WiFi.config(staticIP, gateway, subnet, dns);
|
WiFi.hostname("esp8266-iot-dev");
|
||||||
WiFi.begin(SSID, PASSWORD);
|
WiFi.begin(SSID, PASSWORD);
|
||||||
|
|
||||||
uint32_t notConnectedCounter = 0;
|
uint32_t notConnectedCounter = 0;
|
||||||
debug(F("Connecting "));
|
debug(F("Connecting to "));
|
||||||
|
debugln(WiFi.SSID());
|
||||||
while (WiFi.status() != WL_CONNECTED) {
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
delay(250);
|
delay(250);
|
||||||
debug(F("."));
|
debug(F("."));
|
||||||
|
analogWrite(LED_BUILTIN_AUX, 255 * (notConnectedCounter % 2)); // LED
|
||||||
notConnectedCounter++;
|
notConnectedCounter++;
|
||||||
if (notConnectedCounter >
|
if (notConnectedCounter >
|
||||||
(1000 / 250) * 15) { // Reset board if not connected after 15s
|
(1000 / 250) * 15) { // Reset board if not connected after 15s
|
||||||
|
@ -324,6 +405,7 @@ void setup() {
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
analogWrite(LED_BUILTIN_AUX, 0);
|
||||||
debug(F("\nConnected to "));
|
debug(F("\nConnected to "));
|
||||||
debugln(WiFi.SSID());
|
debugln(WiFi.SSID());
|
||||||
debug(F("IP address: "));
|
debug(F("IP address: "));
|
||||||
|
@ -333,34 +415,41 @@ void setup() {
|
||||||
// Time-config
|
// Time-config
|
||||||
myTime.setLocation("de");
|
myTime.setLocation("de");
|
||||||
// setServer("192.168.2.1"); // uncomment to set custom NTP server
|
// setServer("192.168.2.1"); // uncomment to set custom NTP server
|
||||||
if (!waitForSync(5000)) {
|
if (!waitForSync(TIMEZONED_TIMEOUT / 1000)) {
|
||||||
|
// Reset instead of using fallback timezone
|
||||||
debugln("\nResetting due to NTP not syncing");
|
debugln("\nResetting due to NTP not syncing");
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
debug(F("Local time: "));
|
debug(F("Local time: "));
|
||||||
debugln(myTime.dateTime("H:i:s"));
|
debugln(myTime.dateTime("H:i:s"));
|
||||||
setInterval();
|
setInterval(60 * 60 * 11); // + NTP_STALE_AFTER=3602 = sync every ~ 12 h
|
||||||
|
|
||||||
// Server-config
|
// Server-config
|
||||||
server.on("/", HTTP_GET, handleHome);
|
server.on("/", HTTP_GET, handleHome);
|
||||||
server.on("/settings", HTTP_GET, handleSettings);
|
server.on("/settings", HTTP_GET, handleSettings);
|
||||||
server.on("/settings", HTTP_POST, handleTime);
|
server.on("/settings", HTTP_POST, handleTime);
|
||||||
server.on("/toggle", HTTP_GET, handleToggle);
|
server.on("/toggle", HTTP_GET, handleToggle);
|
||||||
|
server.on("/reset", HTTP_GET, handleReset);
|
||||||
|
server.on("/restart", HTTP_GET, handleRestart);
|
||||||
server.on("/version", HTTP_GET, [] {
|
server.on("/version", HTTP_GET, [] {
|
||||||
server.send(200, "text/html",
|
char softwareVersion[32];
|
||||||
BUILD_VERSION + BUILD_DEBUG ? "-debug" : "");
|
sprintf(softwareVersion, "%s%s", BUILD_VERSION,
|
||||||
|
BUILD_DEBUG ? "-debug" : "");
|
||||||
|
server.send(200, "text/html", softwareVersion);
|
||||||
});
|
});
|
||||||
|
|
||||||
getCurrentTemperatur();
|
getCurrentTemperature();
|
||||||
server.begin();
|
server.begin();
|
||||||
debugln(F("Webserver started"));
|
debugln(F("Webserver started"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
getCurrentTemperatur();
|
getCurrentTemperature();
|
||||||
timer();
|
timer();
|
||||||
yield();
|
yield();
|
||||||
delay(50);
|
delay(50);
|
||||||
|
events();
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
|
pulseLed();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue