firmware wip - time stuff

This commit is contained in:
Marcus 2024-04-17 22:10:02 +02:00
parent 4fb0a6fdb7
commit f52941b1b3

View file

@ -8,18 +8,30 @@
*
*/
// external Libraries
// Libraries
// https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/SPI
#include <SPI.h>
// https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/Wire
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/EEPROM
#include <EEPROM.h>
#include <ArduinoJson.h>
#include <NTPClient.h>
// https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WiFi
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
// https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266WebServer
#include <ESP8266WebServer.h>
// https://github.com/adafruit/Adafruit-GFX-Library
#include <Adafruit_GFX.h>
// https://github.com/adafruit/Adafruit_SSD1306
#include <Adafruit_SSD1306.h>
// https://github.com/adafruit/DHT-sensor-library
#include "DHT.h"
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
// https://github.com/arduino-libraries/NTPClient
#include <NTPClient.h>
// https://github.com/PaulStoffregen/Time
#include <TimeLib.h>
/*
*
@ -264,7 +276,7 @@ const char HTMLheader[] PROGMEM = R"EOF(
<li><a id="systemSettings" href="/systemSettings">System settings</a></li>
<li><a href="/wifiSettings">WiFi settings</a></li>
<li><a href="/help">Help</a></li>
<li><span id="GrowName"></span></li>
<li><span id="MenuGrowName"></span></li>
</ul>
<div class="center">
@ -303,6 +315,23 @@ const char HTMLhelp[] PROGMEM = R"EOF(
Here you will get some helpful help.
)EOF";
const char JSreplaceStr[] PROGMEM = R"EOF(
<script>
function replaceStr(dst, content) {
document.getElementById(dst).innerHTML = content;
}
</script>
)EOF";
const char JSconvertDateToEpoch[] PROGMEM = R"EOF(
<script>
function convertDateToEpoch(src, dst) {
var valGrowStart = document.getElementById(src).value ;
document.getElementById(dst).value = new Date(valGrowStart).getTime() / 1000;
}
</script>
)EOF";
/*
*
* Pin assignments
@ -687,6 +716,7 @@ bool loadEEPROM() {
// size is 32 byte
EEPROM.get(170, GrowName);
if(strlen(GrowName) < 1) {
// size is 4 byte
EEPROM.get(202, GrowStart);
// size is 1 byte
@ -701,14 +731,16 @@ bool loadEEPROM() {
EEPROM.get(210, SunriseHour);
// size is 1 byte
EEPROM.get(211, SunriseMinute);
// size is 1 byte
EEPROM.get(212, DayOfGrow);
}
// to ensure PINled_PWM always set to 255 when UseLEDrelais is true
// we set it here again, does not matter whats stored.
// size is 1 byte
if(UseLEDrelais == true) {
PINled_PWM = 255;
} else {
EEPROM.get(212, PINled_PWM);
EEPROM.get(213, PINled_PWM);
}
@ -757,6 +789,8 @@ bool loadEEPROM() {
Serial.println(SunriseHour);
Serial.print("SunriseMinute: ");
Serial.println(SunriseMinute);
Serial.print("DayOfGrow: ");
Serial.println(DayOfGrow);
Serial.print("PINled_PWM: ");
Serial.println(PINled_PWM);
@ -1045,11 +1079,17 @@ String returnHTMLfooter() {
String footer;
// show the GrowName in the menu if set
if(strlen(GrowName) > 0){
// add a seedling to the growname :)
String divGrowName = "&#x1F331; ";
divGrowName += GrowName;
// replace content of html ID GrowName
footer += JSreplaceStr("GrowName", divGrowName);
// add replaceStr javascript function from PROGMEM
// first is dst ID, second is content (&#.. is a seedling emoji )
footer += FPSTR(JSreplaceStr);
footer += "<script>replaceStr('MenuGrowName', '&#x1F331; ";
footer += GrowName;
footer += "');\n";
// include the GrowName in the title
footer += "document.title = 'CanGrow - ";
footer += GrowName;
footer += "';</script>";
}
footer += FPSTR(HTMLfooter);
@ -1075,6 +1115,34 @@ String returnStrSelected(byte savedValue, byte selectId) {
return returnStr;
}
String returnStrDateFromEpoch(unsigned long epochTime) {
String dateStr;
byte Day = day(epochTime);
byte Month = month(epochTime);
unsigned int Year = year(epochTime);
dateStr = Year;
dateStr += "-";
if(Month < 10) {
dateStr += "0";
dateStr += Month;
} else {
dateStr += Month;
}
dateStr += "-";
if(Day < 10) {
dateStr += "0";
dateStr += Day;
} else {
dateStr += Day;
}
return dateStr;
}
/*
*
* Web pages
@ -1285,6 +1353,7 @@ void WEBgrowSettings() {
body += "<h1>Final step: Grow settings</h1>";
body += "<p>Please configure all settings<br>";
body += "</p>";
GrowStart = timeClient.getEpochTime();
}
body += "<h2>Grow Settings</h2>";
@ -1302,9 +1371,17 @@ void WEBgrowSettings() {
body += GrowName;
body+= "' required><br>\n";
body += "Grow start date: <input type='text' name='GrowStart' value='";
// the input field, which calls javascript convertDateToEpoch() to write data to transmit to id GrowStart
body += "Grow start date: <input type='date' id='GrowStart_sel' onChange='convertDateToEpoch(\"GrowStart_sel\", \"GrowStart\");' value='";
body += returnStrDateFromEpoch(GrowStart);
body += "' required><br>\n";
body += "<input type='hidden' id='GrowStart' name='GrowStart' value='";
body += GrowStart;
body+= "' required><br>\n";
body+= "' required>\n";
body += "Days of vegetation: <input type='number' name='DaysVeg' value='";
body += DaysVeg;
@ -1337,6 +1414,7 @@ void WEBgrowSettings() {
body += "<input type='submit' value='Save'>\n";
body += "</form>\n";
body += FPSTR(JSconvertDateToEpoch);
body += returnHTMLfooter();
webserver.send(200, "text/html", body);
@ -1389,7 +1467,7 @@ void POSTgrowSettings() {
// size is 1 byte
EEPROM.put(211, SunriseMinute);
// size is 1 byte
EEPROM.put(212, PINled_PWM);
EEPROM.put(213, PINled_PWM);
EEPROM.commit();
@ -1543,11 +1621,12 @@ void POSTwifiSettings() {
}
/*
String JSreplaceStr(String elementID, String content) {
String jsReturn = "<script>document.getElementById('" + elementID + "').innerHTML='" + content + "';</script>";
return jsReturn;
}
*/