Compare commits

..

No commits in common. "ebd742bd219470933ce123848287fd6a480d1c2c" and "523292a2fed1dc278a93ed1aec3bc1d4513c282b" have entirely different histories.

2 changed files with 51 additions and 100 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] = { 14, 12, 15, 13 }; uint8_t pins[LIGHTS_COUNT] = { 12, 15, 13, 14 };
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
@ -22,24 +22,24 @@ IPAddress dns(192, 168, 0, 1);
#define LIGHT_VERSION 2.1 #define LIGHT_VERSION 2.1
#define LAST_STATE_STARTUP_LIGHT_LAST_STATE 0 #define LAST_STATE_STARTUP_LIGHT_LAST_STATE 0
#define LAST_STATE_STARTUP_LIGHT_ON_STATE 1 #define LAST_STATE_STARTUP_LIGHT_ON_STATE 1
#define LAST_STATE_STARTUP_LIGHT_OFF_STATE 2 #define LAST_STATE_STARTUP_LIGHT_OFF_STATE 2
#define LIGHT_STATE_ON 1 #define LIGHT_STATE_ON 1
#define LIGHT_STATE_OFF 0 #define LIGHT_STATE_OFF 0
#define TIMING_CONTROL_ENABLED 1 #define TIMING_CONTROL_ENABLED 1
#define TIMING_CONTROL_DISABLED 0 #define TIMING_CONTROL_DISABLED 0
#define SCENE_RELEAX 0 #define SCENE_RELEAX 0
#define SCENE_BRIGHT 1 #define SCENE_BRIGHT 1
#define SCENE_NIGHTLY 2 #define SCENE_NIGHTLY 2
// 10 bit PWM // 10 bit PWM
#define PWM_OFF 0 // 0V #define PWM_OFF 0 // 0V
#define PWM_MIN 0 // 0V - minimum light amount (~1%) #define PWM_MIN 640 // 15V - 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 4.012 // 24V-15V = 24V range #define BRI_TO_PWM_FACTOR 1.506 // 24V-15V = 9V range; 9V ≙ 1024-640 = 384 counts; 384/255 counts =~ 1,506 counts
//********************************// //********************************//
@ -177,7 +177,6 @@ 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);
@ -196,29 +195,23 @@ void read_eeprom_config()
tc_enabled = TIMING_CONTROL_DISABLED; tc_enabled = TIMING_CONTROL_DISABLED;
} }
Serial.println("tc_enabled = " + (String)tc_enabled); if (EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS) == 255) {
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
} }
//********************************// //********************************//
@ -229,15 +222,13 @@ 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();
} }
@ -250,8 +241,7 @@ 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);
@ -279,9 +269,7 @@ 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();
} }
} }
@ -299,8 +287,7 @@ 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);
@ -312,8 +299,7 @@ void init_webserver()
{ {
#ifndef DISABLE_WEB_CONTROL #ifndef DISABLE_WEB_CONTROL
server.on("/state", HTTP_PUT, []() server.on("/state", HTTP_PUT, []() { // HTTP PUT request used to set a new light state
{ // 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"));
@ -321,46 +307,38 @@ 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);
@ -371,8 +349,7 @@ void init_webserver()
} }
}); });
server.on("/state", HTTP_GET, []() server.on("/state", HTTP_GET, []() { // HTTP GET request used to fetch current light state
{ // 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];
@ -384,8 +361,7 @@ void init_webserver()
server.send(200, "text/plain", output); server.send(200, "text/plain", output);
}); });
server.on("/detect", []() server.on("/detect", []() { // HTTP GET request used to discover the light type
{ // 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);
@ -401,8 +377,7 @@ 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);
@ -410,29 +385,24 @@ 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);
} }
} }
@ -445,14 +415,10 @@ 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 (server.arg("tc") == "true") if (tc_enabled == TIMING_CONTROL_DISABLED) {
{ 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();
@ -463,11 +429,9 @@ 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 = ");
@ -504,8 +468,7 @@ 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);
@ -752,7 +715,6 @@ 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 = [];"
@ -823,9 +785,8 @@ void init_webserver()
"if (indexFloat > index) {" "if (indexFloat > index) {"
"index = indexFloat;" "index = indexFloat;"
"}" "}"
"console.log(\"index in graph >>>\" + index);" "console.log(\">>>\" + index);"
// TODO in array dynamisch erzeugt umschreiben
"var trace1 = {" "var trace1 = {"
"x: time," "x: time,"
"y: channel1," "y: channel1,"
@ -854,7 +815,6 @@ 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: {"
@ -878,14 +838,12 @@ void init_webserver()
"}" "}"
"}]" "}]"
"};" "};"
"Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);" // TODO array der traces dynamisch erzeugen "Plotly.newPlot('plot_chart', [trace1, trace2, trace3, trace4], layout);"
"});" "});"
"}" "}"
"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)"
@ -897,15 +855,12 @@ 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);"
"console.log('data.on ' + i + ' = ' + data.on);" "if (data.on == 'true') {"
"if (data.on == true) {"
//"console.log('true');"
"onLinkOn.classList.add('pure-button-primary');"
"onLinkOff.classList.remove('pure-button-primary');"
"} else {"
//"console.log('false');"
"onLinkOn.classList.remove('pure-button-primary');" "onLinkOn.classList.remove('pure-button-primary');"
"onLinkOff.classList.add('pure-button-primary');" "onLinkOff.classList.add('pure-button-primary');"
"} else {"
"onLinkOn.classList.add('pure-button-primary');"
"onLinkOff.classList.remove('pure-button-primary');"
"}" "}"
"})" "})"
".catch(error => console.error(error));" ".catch(error => console.error(error));"
@ -916,7 +871,6 @@ 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`);"
@ -926,7 +880,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 >= " + (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 "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
"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,11 +117,8 @@ void tc_init()
} }
} }
if (tc_enabled == TIMING_CONTROL_ENABLED) tc_update_main();
{
Serial.println("tc_enabled = " + (String)tc_enabled);
tc_update_main();
}
} }
//********************************// //********************************//