From ebd742bd219470933ce123848287fd6a480d1c2c Mon Sep 17 00:00:00 2001 From: Kai Lauterbach Date: Sat, 29 Apr 2023 12:05:13 +0200 Subject: [PATCH] Fixed light power state in webinterface. Fixed PWM range. Added some output to show the eeprom values read o startup. Newline before several { fixed. --- firmware/firmware.ino | 144 ++++++++++++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 49 deletions(-) diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 6f3cd8e..2f885fa 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -9,8 +9,8 @@ #include "config.h" //********* Config block *********// - -uint8_t pins[LIGHTS_COUNT] = { 12, 15, 13, 14 }; +// ch1, ch2, ch3, ch4 +uint8_t pins[LIGHTS_COUNT] = { 14, 12, 15, 13 }; IPAddress strip_ip(192, 168, 0, 26); // choose an unique IP Adress 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 LAST_STATE_STARTUP_LIGHT_LAST_STATE 0 -#define LAST_STATE_STARTUP_LIGHT_ON_STATE 1 -#define LAST_STATE_STARTUP_LIGHT_OFF_STATE 2 +#define LAST_STATE_STARTUP_LIGHT_ON_STATE 1 +#define LAST_STATE_STARTUP_LIGHT_OFF_STATE 2 -#define LIGHT_STATE_ON 1 +#define LIGHT_STATE_ON 1 #define LIGHT_STATE_OFF 0 -#define TIMING_CONTROL_ENABLED 1 +#define TIMING_CONTROL_ENABLED 1 #define TIMING_CONTROL_DISABLED 0 -#define SCENE_RELEAX 0 -#define SCENE_BRIGHT 1 +#define SCENE_RELEAX 0 +#define SCENE_BRIGHT 1 #define SCENE_NIGHTLY 2 // 10 bit PWM #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 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 } + Serial.println("light[" + (String)light + "] = " + (String)light_state[light]); } uint8_t tmp = EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS); @@ -195,23 +196,29 @@ void read_eeprom_config() 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 EEPROM.write(EEPROM_LAST_STATE_STARTUP_ADDRESS, 0); EEPROM.commit(); } #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.commit(); } #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.commit(); } #endif + } //********************************// @@ -222,13 +229,15 @@ void setup() 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); } read_eeprom_config(); - for (int j = 0; j < 200; j++) { + for (int j = 0; j < 200; j++) + { lightEngine(); } @@ -241,7 +250,8 @@ void setup() Serial.print("IP: "); Serial.println(myIP); - if (!light_state[0]) { + if (!light_state[0]) + { // Show that we are connected analogWrite(pins[0], 100); delay(500); @@ -269,7 +279,9 @@ void loop() server.handleClient(); lightEngine(); - if (tc_enabled == TIMING_CONTROL_ENABLED) { + if (tc_enabled == TIMING_CONTROL_ENABLED) + { + //Serial.println("tc_enabled = " + (String)tc_enabled); tc_update_loop(); } } @@ -287,7 +299,8 @@ void handleNotFound() message += server.args(); 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"; } server.send(404, "text/plain", message); @@ -299,7 +312,8 @@ void init_webserver() { #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); DeserializationError error = deserializeJson(root, server.arg("plain")); @@ -307,38 +321,46 @@ void init_webserver() server.send(404, "text/plain", "FAIL. " + server.arg("plain")); } else { - for (JsonPair state : root.as()) { + for (JsonPair state : root.as()) + { const char* key = state.key().c_str(); int light = atoi(key) - 1; JsonObject values = state.value(); int transitiontime = 4; uint8_t tmp = EEPROM.read(EEPROM_LAST_STATE_STARTUP_ADDRESS); - if (values.containsKey("on")) { - if (values["on"]) { + if (values.containsKey("on")) + { + if (values["on"]) + { 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); } } else { 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); } } } - if (values.containsKey("bri")) { + if (values.containsKey("bri")) + { bri[light] = values["bri"]; } - if (values.containsKey("bri_inc")) { + if (values.containsKey("bri_inc")) + { bri[light] += (int)values["bri_inc"]; if (bri[light] > 255) bri[light] = 255; else if (bri[light] < 1) bri[light] = 1; } - if (values.containsKey("transitiontime")) { + if (values.containsKey("transitiontime")) + { transitiontime = values["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; DynamicJsonDocument root(1024); root["on"] = light_state[light]; @@ -361,7 +384,8 @@ void init_webserver() 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 }; sprintf(macString, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); DynamicJsonDocument root(1024); @@ -377,7 +401,8 @@ void init_webserver() server.send(200, "text/plain", output); }); - server.on("/tc_data_blocks", []() { + server.on("/tc_data_blocks", []() + { String output = tc_getJsonData(); server.send(200, "application/json", output); @@ -385,24 +410,29 @@ void init_webserver() #endif // DISABLE_WEB_CONTROL - server.on("/", []() { + server.on("/", []() + { #ifndef DISABLE_WEB_CONTROL static float transitiontime = 4.0; - if (server.hasArg("transition")) { + if (server.hasArg("transition")) + { transitiontime = server.arg("transition").toFloat(); } // startup behavior switch handling - if (server.hasArg("startup")) { + if (server.hasArg("startup")) + { 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); for (uint8_t i = 0; i < LIGHTS_COUNT; i++) { 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); } } @@ -415,10 +445,14 @@ void init_webserver() #endif // DISABLE_WEB_CONTROL // timing controller switch handling - if (server.hasArg("tc")) { - if (server.arg("tc") == "true") { - if (tc_enabled == TIMING_CONTROL_DISABLED) { - if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED) { + if (server.hasArg("tc")) + { + if (server.arg("tc") == "true") + { + if (tc_enabled == TIMING_CONTROL_DISABLED) + { + if (EEPROM.read(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS) != TIMING_CONTROL_ENABLED) + { tc_enabled = TIMING_CONTROL_ENABLED; EEPROM.write(EEPROM_TIMING_CONTROL_ENABLED_ADDRESS, TIMING_CONTROL_ENABLED); EEPROM.commit(); @@ -429,9 +463,11 @@ void init_webserver() } 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; - 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.commit(); Serial.print("Timing control = "); @@ -468,7 +504,8 @@ void init_webserver() // process the received data for every 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(); Serial.print("Brightness "); Serial.print(light); @@ -715,6 +752,7 @@ void init_webserver() http_content += "