SunSim not working yet

This commit is contained in:
2024-12-22 11:50:23 +01:00
parent aa488de4ec
commit 4bcb3a18af
5 changed files with 329 additions and 83 deletions

View File

@@ -75,6 +75,7 @@ void ui_event_wifibtn(lv_event_t * e);
lv_obj_t * ui_wifibtn;
void ui_event_growmodebtn(lv_event_t * e);
lv_obj_t * ui_growmodebtn;
void ui_event_homebtn(lv_event_t * e);
lv_obj_t * ui_homebtn;
@@ -174,6 +175,14 @@ void ui_event_growmodebtn(lv_event_t * e)
_ui_screen_change(&ui_Screen6, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_Screen6_screen_init);
}
}
void ui_event_homebtn(lv_event_t * e)
{
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_target(e);
if(event_code == LV_EVENT_CLICKED) {
_ui_screen_change(&ui_Screen1, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_Screen1_screen_init);
}
}
void ui_event_wifikeyboard(lv_event_t * e)
{
lv_event_code_t event_code = lv_event_get_code(e);

View File

@@ -85,6 +85,7 @@ void ui_event_wifibtn(lv_event_t * e);
extern lv_obj_t * ui_wifibtn;
void ui_event_growmodebtn(lv_event_t * e);
extern lv_obj_t * ui_growmodebtn;
void ui_event_homebtn(lv_event_t * e);
extern lv_obj_t * ui_homebtn;
// SCREEN: ui_Screen5
void ui_Screen5_screen_init(void);

View File

@@ -69,5 +69,6 @@ void ui_Screen4_screen_init(void)
lv_obj_add_event_cb(ui_wifibtn, ui_event_wifibtn, LV_EVENT_ALL, NULL);
lv_obj_add_event_cb(ui_growmodebtn, ui_event_growmodebtn, LV_EVENT_ALL, NULL);
lv_obj_add_event_cb(ui_homebtn, ui_event_homebtn, LV_EVENT_ALL, NULL);
}

View File

@@ -14,12 +14,16 @@ void ui_Screen6_screen_init(void)
lv_roller_set_options(ui_starthour,
"00\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n",
LV_ROLLER_MODE_NORMAL);
lv_roller_set_selected(ui_starthour, 18, LV_ANIM_OFF);
lv_obj_set_height(ui_starthour, 50);
lv_obj_set_width(ui_starthour, LV_SIZE_CONTENT); /// 1
lv_obj_set_x(ui_starthour, -60);
lv_obj_set_y(ui_starthour, -65);
lv_obj_set_align(ui_starthour, LV_ALIGN_CENTER);
lv_obj_set_style_bg_image_recolor(ui_starthour, lv_color_hex(0xFFFFFF), LV_PART_SELECTED | LV_STATE_DEFAULT);
lv_obj_set_style_bg_image_recolor_opa(ui_starthour, 255, LV_PART_SELECTED | LV_STATE_DEFAULT);
ui_startmin = lv_roller_create(ui_Screen6);
lv_roller_set_options(ui_startmin,
"00\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n",
@@ -62,6 +66,7 @@ void ui_Screen6_screen_init(void)
lv_roller_set_options(ui_endhour,
"00\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n",
LV_ROLLER_MODE_NORMAL);
lv_roller_set_selected(ui_endhour, 12, LV_ANIM_OFF);
lv_obj_set_height(ui_endhour, 50);
lv_obj_set_width(ui_endhour, LV_SIZE_CONTENT); /// 1
lv_obj_set_x(ui_endhour, -60);

396
ui/ui.ino
View File

@@ -7,6 +7,7 @@
#include <WiFi.h>
#include <ESP32Time.h>
#include "time.h"
TaskHandle_t Task1;
Preferences preferences;
// #define DHTPIN 4 // GPIO-Pin, an den der Data-Pin des DHT11 angeschlossen ist
@@ -110,12 +111,16 @@ void switchEventHandler(lv_event_t * e)
int fan_duty_cycle;
int led1_duty_cycle;
int led2_duty_cycle;
bool veggie = preferences.getBool("veggie");
bool flower = preferences.getBool("flowering");
if(obj == ui_light1switch)
{
if (lv_obj_has_state(obj, LV_STATE_CHECKED))
{
preferences.begin("g_phase", true);
led1_duty_cycle = preferences.getInt(user_data, 860);
preferences.end();
led1active = true;
@@ -389,6 +394,7 @@ void initPreferences()
preferences.putInt(fancycle, 510);
preferences.putBool("veggie", false);
preferences.putBool("flowering", false);
preferences.putBool("sun", false);
preferences.end();
}
@@ -402,8 +408,13 @@ time_t target_sec;
time_t endtime;
time_t diff_time;
int updateVeggieMode = 5000;
bool startup;
bool sunrise = false;
bool sunset = false;
int sim_duty_cycle;
int led1_pref;
int led2_pref;
int updateSimMode = 24 * 60 * 60 * 1000;
int updateSimLbl = 24 * 60 * 60 * 1000;
@@ -412,7 +423,7 @@ void setup ()
Serial.begin( 115200 ); /* Vorbereitung für mögliches serielles Debugging */
//INIT NEEDED PREFERENCES FILES
// initPreferences();
//initPreferences();
String LVGL_Arduino = "Hello Arduino! ";
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
@@ -466,6 +477,10 @@ void setup ()
lv_obj_add_event_cb(ui_wifibtn, scanWifi, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(ui_wifikeyboard, setWifi, LV_EVENT_READY, NULL);
lv_obj_add_event_cb(ui_savemode, saveModeSettings, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(ui_growmodebtn, setGrowModeLbl, LV_EVENT_CLICKED, NULL);
// Setze initial den Lüfter auf "Off" und den Schalter auf unchecked
lv_label_set_text(ui_lblfanstatus, "OFF");
@@ -549,10 +564,40 @@ void setup ()
printTime();
targettime = timeinfo;
startup = true;
lv_obj_add_event_cb(ui_growmode, setMode, LV_EVENT_VALUE_CHANGED, NULL);
xTaskCreatePinnedToCore(
loopC2, /* Task function. */
"Task1", /* name of task. */
1000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0);
WiFi.disconnect();
}
void loopC2(void* pvParameters)
{
static unsigned int lastUpdate = 0;
for (;;)
{
unsigned long currentMillis = millis();
if (currentMillis - lastUpdate >= updateSimMode)
{
vTaskDelay(1000);
Serial.println("sunSim running ");
sunSim();
lastUpdate = currentMillis;
}
vTaskDelay(1);
}
}
void loop ()
{
@@ -562,6 +607,7 @@ void loop ()
const unsigned int updateInterval = 1000; //60 sekunden
unsigned long currentMillis = millis();
if (currentMillis - lastUpdate >= updateInterval)
{
printTime();
@@ -577,6 +623,8 @@ void loop ()
}
}
void saveModeSettings(lv_event_t * e)
{
char mode[32];
@@ -595,21 +643,46 @@ void saveModeSettings(lv_event_t * e)
int start_min = atoi(start_m);
int end_hour = atoi(end_h);
int end_min = atoi(end_m);
bool sun_checked;
preferences.begin("g_phase", false);
if (!strncmp(mode, "Veggie", 6))
{
preferences.putInt("veggie_start_h", start_hour);
preferences.putInt("veggie_start_m", start_min);
preferences.putInt("veggie_end_h", end_hour);
preferences.putInt("veggie_end_m", end_min);
if (lv_obj_has_state(ui_sunsetcheck, LV_STATE_CHECKED))
{
Serial.println("CHECKBOX SUN ACTIVATED");
preferences.putBool("ve_sun", true);
preferences.putInt("veggie_end_h", (end_hour - 1));
}
else
{
preferences.putBool("ve_sun", false);
preferences.putInt("veggie_end_h", end_hour);
}
}
else if (!strncmp(mode, "Flower", 6))
{
preferences.putInt("flower_start_h", start_hour);
preferences.putInt("flower_start_m", start_min);
preferences.putInt("flower_end_h", end_hour);
preferences.putInt("flower_end_m", end_min);
if (lv_obj_has_state(ui_sunsetcheck, LV_STATE_CHECKED))
{
Serial.println("CHECKBOX SUN ACTIVATED");
preferences.putBool("fl_sun", true);
preferences.putInt("flower_end_h", (end_hour - 1));
}
else
{
preferences.putBool("fl_sun", false);
preferences.putInt("flower_end_h", end_hour);
}
}
preferences.end();
}
@@ -657,6 +730,91 @@ void modeOff()
lv_obj_send_event(ui_light2switch, LV_EVENT_VALUE_CHANGED, led2cycle);
}
void sunSim()
{
Serial.println("SUNSIM WORKING");
if (sunrise)
{
if (sim_duty_cycle == led1_pref)
{
updateSimMode = 24 * 60 * 60 * 1000;
sunrise = false;
}
sim_duty_cycle -= 1;
Serial.println("SUNSIM SUNRISE WORKING");
Serial.println(sim_duty_cycle);
dimm(led1_pwm_pin, sim_duty_cycle, ui_light1percent);
dimm(led2_pwm_pin, sim_duty_cycle, ui_light2percent);
}
else if (sunset)
{
if (led1_pref == sim_duty_cycle)
{
updateSimMode = 24 * 60 * 60 * 1000;
lv_obj_remove_state(ui_light1switch, LV_STATE_CHECKED);
lv_obj_remove_state(ui_light2switch, LV_STATE_CHECKED);
turnOff(led1_pwm_pin, led1_pref, ui_light1statuslbl, ui_lbllightstatus, ui_light1percent);
turnOff(led2_pwm_pin, led2_pref, ui_light2statuslbl, ui_lbllightstatus, ui_light2percent);
sunset = false;
}
led1_pref += 1;
Serial.println("SUNSIM SUNSET WORKING");
Serial.println(led1_pref);
dimm(led1_pwm_pin, led1_pref, ui_light1percent);
dimm(led2_pwm_pin, led1_pref, ui_light2percent);
}
}
void setGrowModeLbl(lv_event_t * e)
{
preferences.begin("g_phase", true);
bool veggie_state = preferences.getBool("veggie");
bool flower_state = preferences.getBool("flowering");
char* start_h;
char* start_m;
char* end_h;
char* end_m;
if (flower_state)
{
lv_dropdown_set_selected(ui_setmode, 1);
start_h = "flower_start_h";
start_m = "flower_start_m";
end_h = "flower_end_h";
end_m = "flower_end_h";
}
else
{
lv_dropdown_set_selected(ui_setmode, 0);
start_h = "veggie_start_h";
start_m = "veggie_start_m";
end_h = "veggie_end_h";
end_m = "veggie_end_h";
}
int s_hour = preferences.getInt(start_h);
int s_min = preferences.getInt(start_m);
int e_hour = preferences.getInt(end_h);
int e_min = preferences.getInt(end_m);
char start_hour = s_hour;
char start_min = s_min;
char end_hour = e_hour;
char end_min = e_min;
lv_roller_set_selected(ui_starthour, start_hour, LV_ANIM_ON);
lv_roller_set_selected(ui_startmin, start_min, LV_ANIM_ON);
lv_roller_set_selected(ui_endhour, end_hour, LV_ANIM_ON);
lv_roller_set_selected(ui_endmin, end_min, LV_ANIM_ON);
preferences.end();
}
void growMode()
{
Serial.println("GROWMODE");
@@ -665,6 +823,8 @@ void growMode()
bool lights = preferences.getBool("light");
bool veggie_state = preferences.getBool("veggie");
bool flower_state = preferences.getBool("flowering");
led1_pref = preferences.getInt("led1");
led2_pref = preferences.getInt("led2");
Serial.print("Veggie: ");
@@ -683,6 +843,8 @@ void growMode()
int start_m;
int end_h;
int end_m;
bool sim_sun;
Serial.println("###################");
if (veggie_state)
{
@@ -691,6 +853,7 @@ void growMode()
start_m = preferences.getInt("veggie_start_m");
end_h = preferences.getInt("veggie_end_h");
end_m = preferences.getInt("veggie_end_m");
sim_sun = preferences.getBool("ve_sun");
Serial.println("###################");
}
else
@@ -700,6 +863,7 @@ void growMode()
start_m = preferences.getInt("flower_start_m");
end_h = preferences.getInt("flower_end_h");
end_m = preferences.getInt("flower_end_m");
sim_sun = preferences.getBool("fl_sun");
Serial.println("###################");
}
preferences.end();
@@ -708,86 +872,48 @@ void growMode()
time_t diff_end = getDiffTime(end_h, end_m);
if (diff_start < 0 && diff_end < 0 && diff_start < diff_end)
{
Serial.println("IF DIFFS KLEINER THEN 0");
Serial.println("DIFFSTART IST KLEINER");
modeOff();
targettime.tm_mday += 1;
diff_start = getDiffTime(start_h, start_m);
updateVeggieMode = (diff_start * 1000) + 1000;
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.println(&targettime, "Targettime: %A, %B %d %Y %H:%M:%S");
Serial.println(diff_start);
Serial.println("###################");
}
else if (diff_start < 0 && diff_end < 0 && diff_start > diff_end)
{
Serial.println("IF DIFFS KLEINER THEN 0");
Serial.println("DIFFSTART IST GRÖßER");
modeOn();
targettime.tm_mday += 1;
diff_end = getDiffTime(end_h, end_m);
updateVeggieMode = (diff_end *1000) + 1000;
Serial.println(diff_end);
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
Serial.println(&targettime, "Targettime: %A, %B %d %Y %H:%M:%S");
Serial.println("###################");
}
else if (diff_start > 0 && diff_end > 0 && diff_start > diff_end)
{
Serial.println("IF DIFFS GRÖßER THEN 0");
Serial.println("DIFFSTART IST GRÖßER");
Serial.print("DIFF START: ");
Serial.println(diff_start);
Serial.print("DIFF END: ");
Serial.println(diff_end);
modeOn();
updateVeggieMode = (diff_end* 1000) + 1000;
Serial.println(updateVeggieMode);
Serial.println("###################");
}
else if (diff_start > 0 && diff_end > 0 && diff_start < diff_end)
{
Serial.println("IF DIFFS GRÖßER THEN 0");
Serial.println("DIFFSTART IST KLEINER");
Serial.print("DIFF START: ");
Serial.println(diff_start);
Serial.print("DIFF END: ");
Serial.println(diff_end);
modeOff();
updateVeggieMode = (diff_start* 1000) + 1000;
Serial.println(updateVeggieMode);
Serial.println("###################");
}
else
{
if (lights)
if (!lights)
{
Serial.println("LIGHTS ARE ON");
diff_time = getDiffTime(end_h, end_m);
Serial.println(diff_time);
Serial.println("LIGHTS ARE OFF");
Serial.println(&timeinfo, "Testtime: %A, %B %d %Y %H:%M:%S");
Serial.println(&targettime, "Testtime: %A, %B %d %Y %H:%M:%S");
if (diff_time < 0)
// if (diff_start < 0 && diff_start > diff_end)
if (diff_start < 0)
{
modeOff();
diff_time = getDiffTime(start_h, start_m);
updateVeggieMode = (diff_time * 1000) + 1000;
Serial.println("TRUE");
if(sim_sun)
{
Serial.println("SIMSUN TRUE");
diff_time = getDiffTime(end_h, end_m);
updateVeggieMode = (diff_time * 1000) + 1000;
lv_obj_add_state(ui_light1switch, LV_STATE_CHECKED);
lv_obj_add_state(ui_light2switch, LV_STATE_CHECKED);
sim_duty_cycle = 860;
sunrise = true;
updateSimMode = (5 * 60 * 1000) / (sim_duty_cycle - led1_pref);
Serial.println(updateSimMode);
}
else
{
modeOn();
targettime.tm_mday += 1;
diff_time = getDiffTime(end_h, end_m);
updateVeggieMode = (diff_time * 1000) + 1000;
}
}
else
{
Serial.println("FALSE");
diff_time = getDiffTime(start_h, start_m);
updateVeggieMode = (diff_time * 1000) + 1000;
modeOn();
modeOff();
Serial.print("UPDATE LIGHTS ON: ");
Serial.print(updateVeggieMode);
@@ -797,21 +923,32 @@ void growMode()
}
else
{
Serial.println("LIGHTS OFF: ");
diff_time = getDiffTime(start_h, start_m);
Serial.println(diff_time);
Serial.println("LIGHTS ARE ON: ");
Serial.println(&timeinfo, "Testtime: %A, %B %d %Y %H:%M:%S");
Serial.println(&targettime, "Testtime: %A, %B %d %Y %H:%M:%S");
if (diff_time < 0)
{
modeOn();
diff_time = getDiffTime(end_h, end_m);
updateVeggieMode = (diff_time * 1000) + 1000;
if(sim_sun)
{
diff_time = getDiffTime(start_h, start_m);
updateVeggieMode = (diff_time * 1000) + 1000;
sim_duty_cycle = 860;
sunset = true;
updateSimMode = (60 * 60 * 1000) / (sim_duty_cycle - led1_pref);
}
else
{
modeOff();
diff_time = getDiffTime(end_h, end_m);
updateVeggieMode = (diff_time * 1000) + 1000;
}
}
else
{
modeOff();
modeOn();
updateVeggieMode = (diff_time * 1000) + 1000;
Serial.print("UPDATE LIGHTS OFF: ");
Serial.print(updateVeggieMode);
@@ -819,7 +956,7 @@ void growMode()
}
}
}
}
preferences.end();
@@ -847,3 +984,96 @@ void printTime()
strftime(clock,80,"Uhrzeit: %H:%M:%S",&timeinfo);
lv_label_set_text(ui_lblclock, clock);
}
// if (diff_start < 0 && diff_end < 0 && diff_start < diff_end)
// {
// Serial.println("IF DIFFS KLEINER THEN 0");
// Serial.println("DIFFSTART IST KLEINER");
// modeOff();
// targettime.tm_mday += 1;
// diff_start = getDiffTime(start_h, start_m);
// updateVeggieMode = (diff_start * 1000) + 1000;
// Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
// Serial.println(&targettime, "Targettime: %A, %B %d %Y %H:%M:%S");
// Serial.println(diff_start);
// Serial.println("###################");
// }
// else if (diff_start < 0 && diff_end < 0 && diff_start > diff_end)
// {
// Serial.println("IF DIFFS KLEINER THEN 0");
// Serial.println("DIFFSTART IST GRÖßER");
// if(sim_sun)
// {
// diff_time = getDiffTime(end_h, end_m);
// updateVeggieMode = (diff_time * 1000) + 1000;
// lv_obj_add_state(ui_light1switch, LV_STATE_CHECKED);
// lv_obj_add_state(ui_light2switch, LV_STATE_CHECKED);
// sim_duty_cycle = 860;
// turnOn(led1_pwm_pin, sim_duty_cycle, ui_light1statuslbl, ui_lbllightstatus, ui_light1percent);
// turnOn(led2_pwm_pin, sim_duty_cycle, ui_light2statuslbl, ui_lbllightstatus, ui_light2percent);
// sunrise = true;
// updateSimMode = (60 * 60 * 1000) / (sim_duty_cycle - led1);
// }
// else
// {
// modeOn();
// }
// targettime.tm_mday += 1;
// diff_end = getDiffTime(end_h, end_m);
// updateVeggieMode = (diff_end *1000) + 1000;
// Serial.println(diff_end);
// Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
// Serial.println(&targettime, "Targettime: %A, %B %d %Y %H:%M:%S");
// Serial.println("###################");
// }
// else if (diff_start > 0 && diff_end > 0 && diff_start > diff_end)
// {
// Serial.println("IF DIFFS GRÖßER THEN 0");
// Serial.println("DIFFSTART IST GRÖßER");
// Serial.print("DIFF START: ");
// Serial.println(diff_start);
// Serial.print("DIFF END: ");
// Serial.println(diff_end);
// modeOn();
// updateVeggieMode = (diff_end* 1000) + 1000;
// Serial.println(updateVeggieMode);
// Serial.println("###################");
// }
// else if (diff_start > 0 && diff_end > 0 && diff_start < diff_end)
// {
// Serial.println("IF DIFFS GRÖßER THEN 0");
// Serial.println("DIFFSTART IST KLEINER");
// Serial.print("DIFF START: ");
// Serial.println(diff_start);
// Serial.print("DIFF END: ");
// Serial.println(diff_end);
// modeOff();
// updateVeggieMode = (diff_start* 1000) + 1000;
// Serial.println(updateVeggieMode);
// Serial.println("###################");
// }
// else
// {
// }