Fixed time reading and timezone settings. Fixed vertical position in webinterface tc data graph.

This commit is contained in:
Kai Lauterbach 2023-04-28 10:56:26 +02:00
parent 161a7e2a78
commit c7c90ff4b0
2 changed files with 86 additions and 34 deletions

View file

@ -198,7 +198,8 @@ void read_eeprom_config() {
//********************************// //********************************//
void setup() { void setup()
{
EEPROM.begin(256); EEPROM.begin(256);
Serial.begin(SERIAL_BAUD_RATE); Serial.begin(SERIAL_BAUD_RATE);
@ -245,7 +246,8 @@ void setup() {
//********************************// //********************************//
void loop() { void loop()
{
server.handleClient(); server.handleClient();
lightEngine(); lightEngine();
@ -332,7 +334,7 @@ void init_webserver() {
DynamicJsonDocument root(1024); DynamicJsonDocument root(1024);
root["on"] = light_state[light]; root["on"] = light_state[light];
root["bri"] = bri[light]; root["bri"] = bri[light];
root["bricur"] = (int)current_bri[light]; root["curbri"] = (int)current_bri[light];
String output; String output;
serializeJson(root, output); serializeJson(root, output);
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
@ -532,7 +534,7 @@ void init_webserver() {
http_content += "<form class=\"pure-form pure-form-aligned\" action=\"/\" method=\"post\">"; http_content += "<form class=\"pure-form pure-form-aligned\" action=\"/\" method=\"post\">";
http_content += "<script>" http_content += "<script>"
"let timeoutId;" "let timeoutId;" // slider value change submit to server
"function sendSliderValue(x) {" "function sendSliderValue(x) {"
"x = x - 1;" "x = x - 1;"
"clearTimeout(timeoutId);" "clearTimeout(timeoutId);"
@ -664,6 +666,7 @@ void init_webserver() {
http_content += "\" href=\"/?dip=false\">OFF</a>"; http_content += "\" href=\"/?dip=false\">OFF</a>";
http_content += "</div>"; http_content += "</div>";
// ip config
if (dip == 0) { if (dip == 0) {
http_content += "<div class=\"pure-control-group\">"; http_content += "<div class=\"pure-control-group\">";
http_content += "<label for=\"ip\">IP</label>"; http_content += "<label for=\"ip\">IP</label>";
@ -685,9 +688,9 @@ void init_webserver() {
http_content += "</div>"; http_content += "</div>";
http_content += "<script>" http_content += "<script>"
"function loadData() {" "function loadData() {" // load tc data and generate graph
"$.getJSON('/tc_data_blocks', function(data) {" "$.getJSON('/tc_data_blocks', function(data) {"
"var currenttime = '';" "var currenttime = [];"
"var time = [];" "var time = [];"
"var channel1 = [];" "var channel1 = [];"
"var channel2 = [];" "var channel2 = [];"
@ -700,7 +703,41 @@ void init_webserver() {
"channel3.push(data['tcdata'][i]['ch3']);" "channel3.push(data['tcdata'][i]['ch3']);"
"channel4.push(data['tcdata'][i]['ch4']);" "channel4.push(data['tcdata'][i]['ch4']);"
"}" "}"
"currenttime=data['currenttime'];" "currenttime.push(data['currenttime']['hour']);"
"currenttime.push(data['currenttime']['min']);"
//"console.log(currenttime);"
// index suche für die vertikale linie
"var currentTimeStr = currenttime[0] + ':' + (currenttime[1] < 10 ? '0' : '') + currenttime[1];"
"var index = time.indexOf(currentTimeStr);"
"if (index === -1) {"
"var lowerIndex = -1;"
"var upperIndex = -1;"
"for (var i = 0; i < time.length - 1; i++) {"
"if (time[i] <= currentTimeStr && currentTimeStr <= time[i+1]) {"
"lowerIndex = i;"
"upperIndex = i + 1;"
"break;"
"}"
"}"
"if (lowerIndex === -1 || upperIndex === -1) {"
"console.log(\"Error: Current time not found in time array and not between two elements in time array.\");"
"return;"
"}"
"var lowerTime = time[lowerIndex].split(\":\");"
"var upperTime = time[upperIndex].split(\":\");"
"var timeDiff = (currenttime[0] - lowerTime[0]) + ((currenttime[1] - lowerTime[1]) / 60);"
"var indexFloat = lowerIndex + timeDiff / ((upperTime[0] - lowerTime[0]) + ((upperTime[1] - lowerTime[1]) / 60));"
//"console.log(\"Index (float): \" + indexFloat);"
"} else {"
//"console.log(\"Index (integer): \" + index);"
//"console.log(\"Index (float): \" + index);"
"}"
"if (indexFloat > index) {"
"index = indexFloat;"
"}"
//"console.log(\">>>\" + index);"
"var trace1 = {" "var trace1 = {"
"x: time," "x: time,"
"y: channel1," "y: channel1,"
@ -741,9 +778,9 @@ void init_webserver() {
"}," "},"
"shapes: [{" "shapes: [{"
"type: 'line'," "type: 'line',"
"x0: currenttime," "x0: index,"
"y0: 0," "y0: 0,"
"x1: currenttime," "x1: index,"
"y1: 255," "y1: 255,"
"line: {" "line: {"
"color: 'lightgrey'," "color: 'lightgrey',"
@ -757,7 +794,7 @@ void init_webserver() {
"}" "}"
"setInterval(loadData, 10000);" "setInterval(loadData, 10000);"
"loadData();" "loadData();"
"function updateLightState() {" "function updateLightState() {" // load the light data from server and set on state and brightness
"for (let i = 1; i <= 4; i++) {" "for (let i = 1; i <= 4; i++) {"
"const lightURL = `http://192.168.0.26/state?light=${i}`;" "const lightURL = `http://192.168.0.26/state?light=${i}`;"
"fetch(lightURL)" "fetch(lightURL)"

View file

@ -1,6 +1,7 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <WiFiUdp.h> #include <WiFiUdp.h>
#include <NTPClient.h> #include <NTPClient.h>
#include <TimeLib.h>
#include "config.h" #include "config.h"
@ -24,10 +25,8 @@ bool tc_testOngoing = false;
uint32_t tc_last_check = 60000; uint32_t tc_last_check = 60000;
long utcOffsetInSeconds = 3600;
WiFiUDP ntpUDP; WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, MY_NTP_SERVER, utcOffsetInSeconds); NTPClient timeClient(ntpUDP, MY_NTP_SERVER);
struct tc_data_st { struct tc_data_st {
uint8_t hh; uint8_t hh;
@ -87,6 +86,10 @@ void tc_init()
tc_last_check = millis(); tc_last_check = millis();
// Set the timezone to Europe/Berlin
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
tzset();
timeClient.begin(); timeClient.begin();
Serial.println("TC: Read data block from eeprom"); Serial.println("TC: Read data block from eeprom");
@ -145,8 +148,10 @@ void tc_update_main()
{ {
//Serial.println((String)tc_data[i].hh + ":" + (String)tc_data[i].mm); //Serial.println((String)tc_data[i].hh + ":" + (String)tc_data[i].mm);
if (tc_data[i].hh <= timeClient.getHours() && /*if (tc_data[i].hh <= timeClient.getHours() &&
tc_data[i].mm <= timeClient.getMinutes()) tc_data[i].mm <= timeClient.getMinutes())*/
if (tc_data[i].hh <= hour() &&
tc_data[i].mm <= minute())
{ {
target_data_block = i+1; // found the next block to load target_data_block = i+1; // found the next block to load
} }
@ -193,7 +198,11 @@ void tc_update_main()
} }
Serial.println("-----\ntdb = " + (String)target_data_block); Serial.println("-----\ntdb = " + (String)target_data_block);
Serial.print("target time: "); Serial.print(tc_data[target_data_block].hh); Serial.print(":"); Serial.print(tc_data[target_data_block].mm); Serial.println(); Serial.print("target time: ");
Serial.print(tc_data[target_data_block].hh);
Serial.print(":");
Serial.print(tc_data[target_data_block].mm);
Serial.println();
// set the channels current and target brightness // set the channels current and target brightness
bri[0] = tc_data[target_data_block].ch1; bri[0] = tc_data[target_data_block].ch1;
@ -273,33 +282,34 @@ void tc_updateTime()
if (timeClient.update() || millis() > (tc_last_check + TIME_CHECK_INTERVAL_MS)) if (timeClient.update() || millis() > (tc_last_check + TIME_CHECK_INTERVAL_MS))
{ {
tc_last_check = millis(); tc_last_check = millis();
//Serial.println("TC: Read time from server...");
//Serial.println(timeClient.getFormattedTime());
/* adjustTimeForDST();
if (dow == 7 && mo == 10 && d >= 25 && h == 3 && DST==1)
{
// DST=0;
timeClient.setTimeOffset(utcOffsetInSeconds);
timeClient.forceUpdate();
} else if (dow == 7 && mo == 3 && d >= 25 && h ==2 && DST==0)
{
// DST = 1
timeClient.setTimeOffset(utcOffsetInSeconds * 2);
timeClient.forceUpdate();
}
*/
Serial.print("=====\nLocal time: "); Serial.print("=====\nLocal time: ");
Serial.print(timeClient.getHours()); Serial.print(hour());
Serial.print(":"); Serial.print(":");
Serial.println(timeClient.getMinutes()); Serial.println(minute());
} }
/**/ /**/
} }
//********************************// //********************************//
void adjustTimeForDST()
{
// Get the time and date information
time_t now = timeClient.getEpochTime();
struct tm timeinfo;
localtime_r(&now, &timeinfo);
// set the local time
setTime(timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, timeinfo.tm_mday, timeinfo.tm_mon + 1, timeinfo.tm_year + 1900);
}
//********************************//
void tc_readConfig() void tc_readConfig()
{ {
for (uint8_t i = 0; i < NUMBER_OF_TIMER_DATA_BLOCKS; i++) for (uint8_t i = 0; i < NUMBER_OF_TIMER_DATA_BLOCKS; i++)
@ -383,7 +393,12 @@ String tc_getJsonData()
output += ","; output += ",";
} }
} }
output += "], \"currenttime\": \"" + (String)timeClient.getHours() + ":" + (String)timeClient.getMinutes() + "\"}"; if (tc_testOngoing == false)
{
output += "], \"currenttime\":{\"hour\":" + (String)timeClient.getHours() + ",\"min\":" + (String)timeClient.getMinutes() + "}}";
} else {
output += "], \"currenttime\":{\"hour\":16,\"min\":0}}";
}
return output; return output;
} }