Compare commits

..

2 commits

2 changed files with 100 additions and 51 deletions

View file

@ -9,8 +9,8 @@
#include "config.h" #include "config.h"
//********* Config block *********// //********* Config block *********//
// ch1, ch2, ch3, ch4
uint8_t pins[LIGHTS_COUNT] = { 12, 15, 13, 14 }; uint8_t pins[LIGHTS_COUNT] = { 14, 12, 15, 13 };
IPAddress strip_ip(192, 168, 0, 26); // choose an unique IP Adress IPAddress strip_ip(192, 168, 0, 26); // choose an unique IP Adress
IPAddress gateway_ip(192, 168, 0, 1); // Router IP IPAddress gateway_ip(192, 168, 0, 1); // Router IP
@ -37,9 +37,9 @@ IPAddress dns(192, 168, 0, 1);
// 10 bit PWM // 10 bit PWM
#define PWM_OFF 0 // 0V #define PWM_OFF 0 // 0V
#define PWM_MIN 640 // 15V - minimum light amount (~1%) #define PWM_MIN 0 // 0V - minimum light amount (~1%)
#define PWM_MAX 1023 // 24V - maximum light amount (100%) #define PWM_MAX 1023 // 24V - maximum light amount (100%)
#define BRI_TO_PWM_FACTOR 1.506 // 24V-15V = 9V range; 9V ≙ 1024-640 = 384 counts; 384/255 counts =~ 1,506 counts #define BRI_TO_PWM_FACTOR 4.012 // 24V-15V = 24V range
//********************************// //********************************//
@ -177,6 +177,7 @@ void read_eeprom_config()
{ {
light_state[light] = true; // set the light state to on light_state[light] = true; // set the light state to on
} }
Serial.println("light[" + (String)light + "] = " + (String)light_state[light]);
} }
uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS);
@ -195,23 +196,29 @@ void read_eeprom_config()
tc_enabled = TIMING_CONTROL_DISABLED; tc_enabled = TIMING_CONTROL_DISABLED;
} }
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 255) { Serial.println("tc_enabled = " + (String)tc_enabled);
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 255)
{
// set the default value on uninitialized EEPROM // set the default value on uninitialized EEPROM
EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, 0); EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, 0);
EEPROM.commit(); EEPROM.commit();
} }
#ifdef USE_STATIC_IP #ifdef USE_STATIC_IP
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255) { if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255)
{
EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 0); EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 0);
EEPROM.commit(); EEPROM.commit();
} }
#else #else
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255) { if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 255)
{
EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 1); EEPROM.write(EEPROM_DYNAMIC_IP_ADDRESS, 1);
EEPROM.commit(); EEPROM.commit();
} }
#endif #endif
} }
//********************************// //********************************//
@ -222,13 +229,15 @@ void setup()
Serial.begin(SERIAL_BAUD_RATE); Serial.begin(SERIAL_BAUD_RATE);
if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 0) { if (EEPROM.read(EEPROM_DYNAMIC_IP_ADDRESS) == 0)
{
WiFi.config(strip_ip, gateway_ip, subnet_mask, dns); WiFi.config(strip_ip, gateway_ip, subnet_mask, dns);
} }
read_eeprom_config(); read_eeprom_config();
for (int j = 0; j < 200; j++) { for (int j = 0; j < 200; j++)
{
lightEngine(); lightEngine();
} }
@ -241,7 +250,8 @@ void setup()
Serial.print("IP: "); Serial.print("IP: ");
Serial.println(myIP); Serial.println(myIP);
if (!light_state[0]) { if (!light_state[0])
{
// Show that we are connected // Show that we are connected
analogWrite(pins[0], 100); analogWrite(pins[0], 100);
delay(500); delay(500);
@ -269,7 +279,9 @@ void loop()
server.handleClient(); server.handleClient();
lightEngine(); lightEngine();
if (tc_enabled == TIMING_CONTROL_ENABLED) { if (tc_enabled == TIMING_CONTROL_ENABLED)
{
//Serial.println("tc_enabled = " + (String)tc_enabled);
tc_update_loop(); tc_update_loop();
} }
} }
@ -287,7 +299,8 @@ void handleNotFound()
message += server.args(); message += server.args();
message += "\n"; message += "\n";
for (uint8_t i = 0; i < server.args(); i++) { for (uint8_t i = 0; i < server.args(); i++)
{
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
} }
server.send(404, "text/plain", message); server.send(404, "text/plain", message);
@ -299,7 +312,8 @@ void init_webserver()
{ {
#ifndef DISABLE_WEB_CONTROL #ifndef DISABLE_WEB_CONTROL
server.on("/state", HTTP_PUT, []() { // HTTP PUT request used to set a new light state server.on("/state", HTTP_PUT, []()
{ // HTTP PUT request used to set a new light state
DynamicJsonDocument root(1024); DynamicJsonDocument root(1024);
DeserializationError error = deserializeJson(root, server.arg("plain")); DeserializationError error = deserializeJson(root, server.arg("plain"));
@ -307,38 +321,46 @@ void init_webserver()
server.send(404, "text/plain", "FAIL. " + server.arg("plain")); server.send(404, "text/plain", "FAIL. " + server.arg("plain"));
} else { } else {
for (JsonPair state : root.as<JsonObject>()) { for (JsonPair state : root.as<JsonObject>())
{
const char* key = state.key().c_str(); const char* key = state.key().c_str();
int light = atoi(key) - 1; int light = atoi(key) - 1;
JsonObject values = state.value(); JsonObject values = state.value();
int transitiontime = 4; int transitiontime = 4;
uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS); uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS);
if (values.containsKey("on")) { if (values.containsKey("on"))
if (values["on"]) { {
if (values["on"])
{
light_state[light] = true; light_state[light] = true;
if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_OFF) { if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_OFF)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON); EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_ON);
} }
} else { } else {
light_state[light] = false; light_state[light] = false;
if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON) { if (tmp == LAST_STATE_STARTUP_LIGHT_LAST_STATE && EEPROM.read(EEPROM_LAST_STATE_ADDRESS + light) == LIGHT_STATE_ON)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF); EEPROM.write(EEPROM_LAST_STATE_ADDRESS + light, LIGHT_STATE_OFF);
} }
} }
} }
if (values.containsKey("bri")) { if (values.containsKey("bri"))
{
bri[light] = values["bri"]; bri[light] = values["bri"];
} }
if (values.containsKey("bri_inc")) { if (values.containsKey("bri_inc"))
{
bri[light] += (int)values["bri_inc"]; bri[light] += (int)values["bri_inc"];
if (bri[light] > 255) bri[light] = 255; if (bri[light] > 255) bri[light] = 255;
else if (bri[light] < 1) bri[light] = 1; else if (bri[light] < 1) bri[light] = 1;
} }
if (values.containsKey("transitiontime")) { if (values.containsKey("transitiontime"))
{
transitiontime = values["transitiontime"]; transitiontime = values["transitiontime"];
} }
process_lightdata(light, transitiontime); process_lightdata(light, transitiontime);
@ -349,7 +371,8 @@ void init_webserver()
} }
}); });
server.on("/state", HTTP_GET, []() { // HTTP GET request used to fetch current light state server.on("/state", HTTP_GET, []()
{ // HTTP GET request used to fetch current light state
uint8_t light = server.arg("light").toInt() - 1; uint8_t light = server.arg("light").toInt() - 1;
DynamicJsonDocument root(1024); DynamicJsonDocument root(1024);
root["on"] = light_state[light]; root["on"] = light_state[light];
@ -361,7 +384,8 @@ void init_webserver()
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
}); });
server.on("/detect", []() { // HTTP GET request used to discover the light type server.on("/detect", []()
{ // HTTP GET request used to discover the light type
char macString[32] = { 0 }; char macString[32] = { 0 };
sprintf(macString, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); sprintf(macString, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
DynamicJsonDocument root(1024); DynamicJsonDocument root(1024);
@ -377,7 +401,8 @@ void init_webserver()
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
}); });
server.on("/tc_data_blocks", []() { server.on("/tc_data_blocks", []()
{
String output = tc_getJsonData(); String output = tc_getJsonData();
server.send(200, "application/json", output); server.send(200, "application/json", output);
@ -385,24 +410,29 @@ void init_webserver()
#endif // DISABLE_WEB_CONTROL #endif // DISABLE_WEB_CONTROL
server.on("/", []() { server.on("/", []()
{
#ifndef DISABLE_WEB_CONTROL #ifndef DISABLE_WEB_CONTROL
static float transitiontime = 4.0; static float transitiontime = 4.0;
if (server.hasArg("transition")) { if (server.hasArg("transition"))
{
transitiontime = server.arg("transition").toFloat(); transitiontime = server.arg("transition").toFloat();
} }
// startup behavior switch handling // startup behavior switch handling
if (server.hasArg("startup")) { if (server.hasArg("startup"))
{
int startup = server.arg("startup").toInt(); int startup = server.arg("startup").toInt();
if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) != startup) { if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) != startup)
{
EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, startup); EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, startup);
for (uint8_t i = 0; i < LIGHTS_COUNT; i++) { for (uint8_t i = 0; i < LIGHTS_COUNT; i++) {
uint8_t tmp = (light_state[i] == true ? LIGHT_STATE_ON : LIGHT_STATE_OFF); uint8_t tmp = (light_state[i] == true ? LIGHT_STATE_ON : LIGHT_STATE_OFF);
if (EEPROM.read(EEPROM_LAST_STATE_ADDRESS + i) != tmp) { if (EEPROM.read(EEPROM_LAST_STATE_ADDRESS + i) != tmp)
{
EEPROM.write(EEPROM_LAST_STATE_ADDRESS + i, tmp); EEPROM.write(EEPROM_LAST_STATE_ADDRESS + i, tmp);
} }
} }
@ -415,10 +445,14 @@ void init_webserver()
#endif // DISABLE_WEB_CONTROL #endif // DISABLE_WEB_CONTROL
// timing controller switch handling // timing controller switch handling
if (server.hasArg("tc")) { if (server.hasArg("tc"))
if (server.arg("tc") == "true") { {
if (tc_enabled == TIMING_CONTROL_DISABLED) { if (server.arg("tc") == "true")
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED) { {
if (tc_enabled == TIMING_CONTROL_DISABLED)
{
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED)
{
tc_enabled = TIMING_CONTROL_ENABLED; tc_enabled = TIMING_CONTROL_ENABLED;
EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_ENABLED); EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_ENABLED);
EEPROM.commit(); EEPROM.commit();
@ -429,9 +463,11 @@ void init_webserver()
} else { // tc is set to false or something else } else { // tc is set to false or something else
if (tc_enabled == TIMING_CONTROL_ENABLED) { if (tc_enabled == TIMING_CONTROL_ENABLED)
{
tc_enabled = TIMING_CONTROL_DISABLED; tc_enabled = TIMING_CONTROL_DISABLED;
if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_DISABLED) { if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_DISABLED)
{
EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED); EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_DISABLED);
EEPROM.commit(); EEPROM.commit();
Serial.print("Timing control = "); Serial.print("Timing control = ");
@ -468,7 +504,8 @@ void init_webserver()
// process the received data for every light // process the received data for every light
for (int light = 0; light < LIGHTS_COUNT; light++) { for (int light = 0; light < LIGHTS_COUNT; light++) {
if (server.hasArg("bri" + (String)light)) { if (server.hasArg("bri" + (String)light))
{
bri[light] = (int)server.arg("bri" + (String)light).toInt(); bri[light] = (int)server.arg("bri" + (String)light).toInt();
Serial.print("Brightness "); Serial.print("Brightness ");
Serial.print(light); Serial.print(light);
@ -715,6 +752,7 @@ void init_webserver()
http_content += "<script>" http_content += "<script>"
"function loadData() {" // load tc data and generate graph "function loadData() {" // load tc data and generate graph
"console.log('----> generate graph <----');"
"$.getJSON('/tc_data_blocks', function(data) {" "$.getJSON('/tc_data_blocks', function(data) {"
"var currenttime = [];" "var currenttime = [];"
"var time = [];" "var time = [];"
@ -785,8 +823,9 @@ void init_webserver()
"if (indexFloat > index) {" "if (indexFloat > index) {"
"index = indexFloat;" "index = indexFloat;"
"}" "}"
"console.log(\">>>\" + index);" "console.log(\"index in graph >>>\" + index);"
// TODO in array dynamisch erzeugt umschreiben
"var trace1 = {" "var trace1 = {"
"x: time," "x: time,"
"y: channel1," "y: channel1,"
@ -815,6 +854,7 @@ void init_webserver()
"type: 'scatter'," "type: 'scatter',"
"mode: 'lines+markers'," "mode: 'lines+markers',"
"};" "};"
"var layout = {" "var layout = {"
"title: 'Timing Control Data Blocks'," "title: 'Timing Control Data Blocks',"
"xaxis: {" "xaxis: {"
@ -838,12 +878,14 @@ void init_webserver()
"}" "}"
"}]" "}]"
"};" "};"
"Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);" "Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);" // TODO array der traces dynamisch erzeugen
"});" "});"
"}" "}"
"setInterval(loadData, 10000);" "setInterval(loadData, 10000);"
"loadData();" "loadData();"
"function updateLightState() {" // load the light data from server and set on state and brightness "function updateLightState() {" // load the light data from server and set on state and brightness
"console.log('----> setting bri and power switch <----');"
"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)"
@ -855,12 +897,15 @@ void init_webserver()
"const onLinkOff = document.getElementById(`on${i - 1}_off`);" "const onLinkOff = document.getElementById(`on${i - 1}_off`);"
"briSlider.value = data.bri;" "briSlider.value = data.bri;"
"briSliderVal.innerHTML = (Math.round((data.bri * 100.0 / 255.0) * 100) / 100).toFixed(2);" "briSliderVal.innerHTML = (Math.round((data.bri * 100.0 / 255.0) * 100) / 100).toFixed(2);"
"if (data.on == 'true') {" "console.log('data.on ' + i + ' = ' + data.on);"
"onLinkOn.classList.remove('pure-button-primary');" "if (data.on == true) {"
"onLinkOff.classList.add('pure-button-primary');" //"console.log('true');"
"} else {"
"onLinkOn.classList.add('pure-button-primary');" "onLinkOn.classList.add('pure-button-primary');"
"onLinkOff.classList.remove('pure-button-primary');" "onLinkOff.classList.remove('pure-button-primary');"
"} else {"
//"console.log('false');"
"onLinkOn.classList.remove('pure-button-primary');"
"onLinkOff.classList.add('pure-button-primary');"
"}" "}"
"})" "})"
".catch(error => console.error(error));" ".catch(error => console.error(error));"
@ -871,6 +916,7 @@ void init_webserver()
// show pwm values in webinterface // show pwm values in webinterface
"function updatePWMValues() {" "function updatePWMValues() {"
"console.log('----> setting pwm data <----');"
"for (let i = 0; i < " + (String)LIGHTS_COUNT + "; i++) {" "for (let i = 0; i < " + (String)LIGHTS_COUNT + "; i++) {"
"const lightID = i + 1;" "const lightID = i + 1;"
"const pwmElement = document.getElementById(`light${i}_pwm`);" "const pwmElement = document.getElementById(`light${i}_pwm`);"
@ -880,7 +926,7 @@ void init_webserver()
"fetch(url)" "fetch(url)"
".then(response => response.json())" ".then(response => response.json())"
".then(data => {" ".then(data => {"
"const pwmValue = ((Math.round((data.curpwm - ((data.curpwm >= 640.0) ? 640.0 : 0)) / 383.0 * 100.0) * 100.0) / 100.0).toFixed(2);" // pwm as % 640 to 1023 "const pwmValue = ((Math.round((data.curpwm - ((data.curpwm >= " + (String)PWM_MIN+ ") ? " + (String)PWM_MIN + " : 0)) / " + (String)PWM_MAX + " * 100.0) * 100.0) / 100.0).toFixed(2);" // pwm as % PWM_MIN to PWM_MAX
"console.log('curpwm[' + i + '] = ' + data.curpwm + ' = ' + pwmValue);" "console.log('curpwm[' + i + '] = ' + data.curpwm + ' = ' + pwmValue);"
"pwmElement.innerText = pwmValue.toString();" "pwmElement.innerText = pwmValue.toString();"
"pwmElement.value = pwmValue;" "pwmElement.value = pwmValue;"

View file

@ -117,8 +117,11 @@ void tc_init()
} }
} }
if (tc_enabled == TIMING_CONTROL_ENABLED)
{
Serial.println("tc_enabled = " + (String)tc_enabled);
tc_update_main(); tc_update_main();
}
} }
//********************************// //********************************//