Change LED to PWM, Change timer to run in interval, Optimize HTTP headers, Correctly sync time, Fix invalid hostname, Fix version URL displays incorrect version
This commit is contained in:
parent
88baeee58e
commit
8f37203b79
@ -195,9 +195,8 @@ void handleToggle() {
|
||||
}
|
||||
debug(F("Status: "));
|
||||
debugln(stat[i]);
|
||||
reload_home();
|
||||
server.sendHeader(F("Location"), F("/"), true);
|
||||
server.send(302, F("text/html"), home_html);
|
||||
server.send(302);
|
||||
debugln(override[i]);
|
||||
debugln(F("---------------------------"));
|
||||
}
|
||||
|
176
src/main.cpp
176
src/main.cpp
@ -27,6 +27,7 @@ DallasTemperature sensors(&oneWire);
|
||||
|
||||
int temperature, temperatureSet, minTemperatureSet;
|
||||
static unsigned long lastTempCycle = -5000;
|
||||
static unsigned long lastTimeCycle = -5000;
|
||||
|
||||
char time_all[8][8][3] = {
|
||||
{}, {}, {}, {},
|
||||
@ -203,6 +204,7 @@ void handleRestart() {
|
||||
}
|
||||
|
||||
void handleTime() {
|
||||
bool changed = false;
|
||||
if (server.arg("rule") != "") {
|
||||
debugln("write main");
|
||||
const char args[4][9] = {"time_o", "time_f", "time_o_1", "time_f_1"};
|
||||
@ -225,6 +227,7 @@ void handleTime() {
|
||||
server.arg("rule").toInt() * 4 + arg * 64 + 32,
|
||||
atoi(time_all[arg * 2 + 1][server.arg("rule").toInt()]));
|
||||
EEPROM.commit();
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -233,91 +236,125 @@ void handleTime() {
|
||||
temperatureSet = server.arg("temperature").toInt();
|
||||
EEPROM.put(256, temperatureSet);
|
||||
EEPROM.commit();
|
||||
changed = true;
|
||||
}
|
||||
if (server.arg("min_temperature").toInt() != minTemperatureSet) {
|
||||
debugln("write minTemperature");
|
||||
minTemperatureSet = server.arg("min_temperature").toInt();
|
||||
EEPROM.put(260, minTemperatureSet);
|
||||
EEPROM.commit();
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
lastTimeCycle = -5000; // trigger timer()
|
||||
reload_settings();
|
||||
server.sendHeader(F("Location"), F("/settings"), true);
|
||||
server.send(302);
|
||||
} else {
|
||||
server.send(204); // nothing changed (except maybe current temperature),
|
||||
// so no page update needed
|
||||
}
|
||||
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) or
|
||||
temperature <= minTemperatureSet) {
|
||||
isOn = true;
|
||||
if (millis() - lastTimeCycle > 5000) {
|
||||
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) or
|
||||
temperature <= minTemperatureSet) {
|
||||
isOn = true;
|
||||
}
|
||||
ri++;
|
||||
}
|
||||
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");
|
||||
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);
|
||||
analogWrite(LED_BUILTIN_AUX, 100); // 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);
|
||||
analogWrite(LED_BUILTIN, 0); // LED
|
||||
if (strcmp(stat[r], "AN") == 0) {
|
||||
debug(r);
|
||||
debugln(F(" is off"));
|
||||
sprintf(stat[r], "AUS");
|
||||
}
|
||||
}
|
||||
}
|
||||
lastTimeCycle = 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);
|
||||
delay(125);
|
||||
analogWrite(LED_BUILTIN, 0);
|
||||
delay(125);
|
||||
analogWrite(LED_BUILTIN, 255);
|
||||
delay(250);
|
||||
}
|
||||
ESP.restart();
|
||||
}
|
||||
|
||||
void getCurrentTemperature() {
|
||||
if (millis() - lastTempCycle > 5000) {
|
||||
sensors.requestTemperatures();
|
||||
temperature = int(sensors.getTempCByIndex(0)) != -127
|
||||
? int(sensors.getTempCByIndex(0))
|
||||
: 255; // TODO: mark as invalid
|
||||
temperature = int(sensors.getTempCByIndex(0));
|
||||
lastTempCycle = millis();
|
||||
debugln(temperature);
|
||||
if (temperature == -127) {
|
||||
emergencyStop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
int p = 0;
|
||||
while (p <= 7) {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
analogWrite(LED_BUILTIN, 255);
|
||||
uint p = 0;
|
||||
while (p <= sizeof(pins) / sizeof(pins[0])) {
|
||||
pinMode(pins[p], OUTPUT);
|
||||
pinMode(2, OUTPUT);
|
||||
digitalWrite(pins[p], LOW);
|
||||
digitalWrite(2, LOW); // LED
|
||||
p++;
|
||||
}
|
||||
Serial.begin(74880);
|
||||
@ -332,12 +369,13 @@ void setup() {
|
||||
|
||||
// WiFI-config
|
||||
// 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);
|
||||
|
||||
uint32_t notConnectedCounter = 0;
|
||||
debug(F("Connecting "));
|
||||
debug(F("Connecting to "));
|
||||
debugln(WiFi.SSID());
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(250);
|
||||
debug(F("."));
|
||||
@ -357,13 +395,14 @@ void setup() {
|
||||
// Time-config
|
||||
myTime.setLocation("de");
|
||||
// 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");
|
||||
ESP.restart();
|
||||
}
|
||||
debug(F("Local time: "));
|
||||
debugln(myTime.dateTime("H:i:s"));
|
||||
setInterval();
|
||||
setInterval(60 * 60 * 11); // + NTP_STALE_AFTER=3602 = sync every ~ 12 h
|
||||
|
||||
// Server-config
|
||||
server.on("/", HTTP_GET, handleHome);
|
||||
@ -373,8 +412,10 @@ void setup() {
|
||||
server.on("/reset", HTTP_GET, handleReset);
|
||||
server.on("/restart", HTTP_GET, handleRestart);
|
||||
server.on("/version", HTTP_GET, [] {
|
||||
server.send(200, "text/html",
|
||||
BUILD_VERSION + BUILD_DEBUG ? "-debug" : "");
|
||||
char softwareVersion[32];
|
||||
sprintf(softwareVersion, "%s%s", BUILD_VERSION,
|
||||
BUILD_DEBUG ? "-debug" : "");
|
||||
server.send(200, "text/html", softwareVersion);
|
||||
});
|
||||
|
||||
getCurrentTemperature();
|
||||
@ -388,5 +429,6 @@ void loop() {
|
||||
timer();
|
||||
yield();
|
||||
delay(50);
|
||||
events();
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user