Auto Veggie & Flowering Mode

This commit is contained in:
2024-12-11 21:07:19 +01:00
parent 528e862838
commit ccb1caec32
7 changed files with 387 additions and 239 deletions

View File

@@ -71,6 +71,7 @@ void ui_Screen4_screen_init(void);
lv_obj_t * ui_Screen4;
lv_obj_t * ui_wifistatus;
lv_obj_t * ui_lblgrowmode;
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;
@@ -157,6 +158,14 @@ void ui_event_btnfanhome(lv_event_t * e)
_ui_screen_change(&ui_Screen1, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_Screen1_screen_init);
}
}
void ui_event_wifibtn(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_Screen5, LV_SCR_LOAD_ANIM_FADE_ON, 500, 0, &ui_Screen5_screen_init);
}
}
void ui_event_growmodebtn(lv_event_t * e)
{
lv_event_code_t event_code = lv_event_get_code(e);

View File

@@ -81,6 +81,7 @@ void ui_Screen4_screen_init(void);
extern lv_obj_t * ui_Screen4;
extern lv_obj_t * ui_wifistatus;
extern lv_obj_t * ui_lblgrowmode;
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;

View File

@@ -128,7 +128,7 @@ void ui_Screen1_screen_init(void)
lv_obj_set_align(ui_settings, LV_ALIGN_CENTER);
ui_growmode = lv_dropdown_create(ui_Screen1);
lv_dropdown_set_options(ui_growmode, "Manuell\nVeggie\nFlower\n");
lv_dropdown_set_options(ui_growmode, "Manuell\nVeggie\nFlower");
lv_obj_set_width(ui_growmode, 150);
lv_obj_set_height(ui_growmode, LV_SIZE_CONTENT); /// 1
lv_obj_set_x(ui_growmode, 0);

View File

@@ -67,6 +67,7 @@ void ui_Screen4_screen_init(void)
lv_obj_set_y(ui_homebtn, 80);
lv_obj_set_align(ui_homebtn, LV_ALIGN_CENTER);
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);
}

View File

@@ -105,7 +105,7 @@ void ui_Screen6_screen_init(void)
lv_obj_set_style_text_font(ui_sunsetcheck, &lv_font_montserrat_22, LV_PART_MAIN | LV_STATE_DEFAULT);
ui_setmode = lv_dropdown_create(ui_Screen6);
lv_dropdown_set_options(ui_setmode, "Veggie\nFlower\n");
lv_dropdown_set_options(ui_setmode, "Veggie\nFlower");
lv_obj_set_width(ui_setmode, 150);
lv_obj_set_height(ui_setmode, LV_SIZE_CONTENT); /// 1
lv_obj_set_x(ui_setmode, -75);

View File

@@ -0,0 +1,13 @@
#include <nvs_flash.h>
void setup() {
nvs_flash_erase(); // erase the NVS partition and...
nvs_flash_init(); // initialize the NVS partition.
while (true);
}
void loop() {
;
}

492
ui/ui.ino
View File

@@ -16,12 +16,11 @@ Preferences preferences;
const char* ntp = "fritz.box";
const int gmt_offset_sec = 3600;
const int daylight_offset_sec = 7200;
ESP32Time rtc(3600);
//Variablen für PWM
int pwm_freq = 5000;
int pwm_resolution = 8;
int pwm_light_resolution = 10;
int pwm_resolution = 10;
//Variablen PWM fan
int fan_pwm_channel = 0;
@@ -39,21 +38,14 @@ char led1cycle[5] = "led1";
char led2cycle[5] = "led2";
char fancycle[4] = "fan";
//Variable zum Speichern des Status des Schalters
bool fan_active = false;
bool led1active = false;
bool led2active = false;
/* Ändern Sie dies auf die Auflösung Ihres Bildschirms */
static const uint16_t screenWidth = 320;
static const uint16_t screenHeight = 240;
enum { SCREENBUFFER_SIZE_PIXELS = screenWidth * screenHeight / 10 };
static lv_color_t buf [SCREENBUFFER_SIZE_PIXELS];
@@ -68,9 +60,10 @@ void my_print(const char * buf)
}
#endif
//WIFI
void scanWifi(lv_event_t * e)
{
lv_dropdown_clear_options(ui_wifinetlist);
int n = WiFi.scanNetworks();
if (n == 0)
@@ -80,8 +73,6 @@ void scanWifi(lv_event_t * e)
}
else
{
for (int i = 0; i < n; ++i)
{
lv_dropdown_add_option(ui_wifinetlist, WiFi.SSID(i).c_str(), i);
@@ -94,48 +85,40 @@ void setWifi(lv_event_t * e)
char ssid[32];
lv_dropdown_get_selected_str(ui_wifinetlist, ssid, sizeof(ssid));
const char * pw = (const char*)lv_textarea_get_text(ui_wifipw);
Serial.println(ssid);
Serial.println(pw);
WiFi.begin(ssid, pw);
int8_t counter;
while (WiFi.status() != WL_CONNECTED && counter != 10)
while (WiFi.status() != WL_CONNECTED && counter != 21)
{
lv_label_set_text(ui_wifistatus, "Connecting...");
// Serial.println("AHHHHHH die Schleife");
delay(500);
++counter;
}
lv_label_set_text(ui_wifistatus, "Connected");
preferences.begin("wifi", false);
preferences.putString("ssid", ssid);
preferences.putString("pw", pw);
preferences.end();
}
// FAN AND LIGHT CONTROL
void switchEventHandler(lv_event_t * e)
{
lv_obj_t *obj = (lv_obj_t *)lv_event_get_target(e);
char * user_data = (char*)lv_event_get_user_data(e);
preferences.begin("dutycycles", false);
int light_off_duty_cycle = 1024;
int fan_duty_cycle;
int led1_duty_cycle;
int led2_duty_cycle;
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;
turnOn(led1_pwm_pin, led1_duty_cycle, ui_light1statuslbl, ui_lbllightstatus, ui_light1percent);
}
else
@@ -148,7 +131,9 @@ void switchEventHandler(lv_event_t * e)
{
if (lv_obj_has_state(obj, LV_STATE_CHECKED))
{
preferences.begin("g_phase", true);
led2_duty_cycle = preferences.getInt(user_data, 860);
preferences.end();
led2active = true;
turnOn(led2_pwm_pin, led2_duty_cycle, ui_light2statuslbl, ui_lbllightstatus, ui_light2percent);
}
@@ -162,8 +147,11 @@ void switchEventHandler(lv_event_t * e)
{
if (lv_obj_has_state(obj, LV_STATE_CHECKED))
{
fan_duty_cycle = preferences.getInt(user_data, 76);
preferences.begin("g_phase", true);
fan_duty_cycle = preferences.getInt(user_data, 310);
preferences.end();
fan_active = true;
turnOn(fan_pwm_pin, fan_duty_cycle, ui_lblfanstatus, ui_lblfanspeed, ui_lblfanfanspeed);
}
else
@@ -173,7 +161,6 @@ void switchEventHandler(lv_event_t * e)
turnOff(fan_pwm_pin, off_duty_cycle, ui_lblfanstatus, ui_lblfanspeed, ui_lblfanfanspeed);
}
}
preferences.end();
}
void turnOn(int pin, int duty_cycle, lv_obj_t * statuslbl, lv_obj_t * statusmainlbl, lv_obj_t * percent)
@@ -182,21 +169,28 @@ void turnOn(int pin, int duty_cycle, lv_obj_t * statuslbl, lv_obj_t * statusmain
int percentage;
if (statusmainlbl == ui_lblfanspeed)
{
percentage = round((duty_cycle * 100.0) / 255.0);
percentage = round((duty_cycle * 100.0) / 1024.0);
lv_label_set_text_fmt(statusmainlbl, "%d%%", percentage);
lv_label_set_text_fmt(percent, "%d%%", percentage);
}
else
{
percentage = round(100 - (duty_cycle * 100.0) / 1024.0);
lv_label_set_text(statusmainlbl, "ON");
lv_label_set_text_fmt(percent, "%d%%", percentage);
preferences.begin("g_phase", false);
preferences.putBool("light", true);
preferences.end();
}
lv_label_set_text(statuslbl, "ON");
}
void turnOff(int pin, int duty_cycle, lv_obj_t * statuslbl, lv_obj_t * statusmainlbl, lv_obj_t * percent)
{
ledcWrite(pin, duty_cycle);
if (statusmainlbl == ui_lblfanspeed) lv_label_set_text(statusmainlbl, "0%");
lv_label_set_text(statuslbl, "OFF");
@@ -204,16 +198,24 @@ void turnOff(int pin, int duty_cycle, lv_obj_t * statuslbl, lv_obj_t * statusmai
if (led1active == false && led2active == false)
{
preferences.begin("g_phase", false);
preferences.putBool("light", false);
preferences.end();
lv_label_set_text(statusmainlbl, "OFF");
}
}
void dimm(int pin, int duty_cycle, lv_obj_t * percent, lv_obj_t * percent2 = NULL)
{
int percentage = 0;
ledcWrite(pin, duty_cycle);
if (percent == ui_lblfanspeed)
{
percentage = round((duty_cycle * 100.0) / 255.0);
percentage = round((duty_cycle * 100.0) / 1024.0);
lv_label_set_text_fmt(percent2, "%d%%", percentage);
}
else
@@ -226,33 +228,35 @@ void dimm(int pin, int duty_cycle, lv_obj_t * percent, lv_obj_t * percent2 = NUL
static void dimmUpBtnEventHandler(lv_event_t * e)
{
int fan_duty_cycle = 3;
int fan_duty_cycle = 10;
int led1_duty_cycle = -10;
int led2_duty_cycle = -10;
lv_obj_t *obj = (lv_obj_t *)lv_event_get_target(e);
char* user_data = (char*)lv_event_get_user_data(e);
preferences.begin("dutycycles", false);
Serial.println(user_data);
if (obj == ui_light1up)
{
if (led1active)
{
preferences.begin("g_phase", false);
led1_duty_cycle += preferences.getInt(user_data, 860);
if (led1_duty_cycle <= 0) led1_duty_cycle = 0;
Serial.println(led1_duty_cycle);
preferences.putInt(user_data, led1_duty_cycle);
preferences.end();
dimm(led1_pwm_pin, led1_duty_cycle, ui_light1percent);
}
}
if (obj == ui_light2up)
{
if (led2active)
{
preferences.begin("g_phase", false);
led2_duty_cycle += preferences.getInt(user_data, 860);
if (led2_duty_cycle <= 0) led2_duty_cycle = 0;
Serial.println(led2_duty_cycle);
preferences.putInt(user_data, led2_duty_cycle);
preferences.end();
dimm(led2_pwm_pin, led2_duty_cycle, ui_light2percent);
}
}
@@ -260,33 +264,34 @@ static void dimmUpBtnEventHandler(lv_event_t * e)
{
if (fan_active)
{
preferences.begin("g_phase", false);
fan_duty_cycle += preferences.getInt(user_data, 76);
if (fan_duty_cycle >= 255) fan_duty_cycle = 255;
Serial.println(fan_duty_cycle);
if (fan_duty_cycle >= 1024) fan_duty_cycle = 1024;
preferences.putInt(user_data, fan_duty_cycle);
preferences.end();
dimm(fan_pwm_pin, fan_duty_cycle, ui_lblfanspeed, ui_lblfanfanspeed);
}
}
preferences.end();
}
static void dimmDownBtnEventHandler(lv_event_t * e)
{
lv_obj_t *obj = (lv_obj_t *)lv_event_get_target(e);
char* user_data = (char*)lv_event_get_user_data(e);
int fan_duty_cycle = -3;
int fan_duty_cycle = -10;
int led1_duty_cycle = 10;
int led2_duty_cycle = 10;
preferences.begin("dutycycles", false);
Serial.println(user_data);
if (obj == ui_light1down)
{
if (led1active)
{
preferences.begin("g_phase", false);
led1_duty_cycle += preferences.getInt(user_data);
if (led1_duty_cycle >= 860) led1_duty_cycle = 860;
Serial.println(led1_duty_cycle);
preferences.putInt(user_data, led1_duty_cycle);
preferences.end();
dimm(led1_pwm_pin, led1_duty_cycle, ui_light1percent);
}
}
@@ -294,10 +299,11 @@ static void dimmDownBtnEventHandler(lv_event_t * e)
{
if (led2active)
{
preferences.begin("g_phase", false);
led2_duty_cycle += preferences.getInt(user_data);
if (led2_duty_cycle >= 860) led2_duty_cycle = 860;
Serial.println(led2_duty_cycle);
preferences.putInt(user_data, led2_duty_cycle);
preferences.end();
dimm(led2_pwm_pin, led2_duty_cycle, ui_light2percent);
}
}
@@ -305,13 +311,14 @@ static void dimmDownBtnEventHandler(lv_event_t * e)
{
if (fan_active)
{
preferences.begin("g_phase", false);
fan_duty_cycle += preferences.getInt(user_data);
if (fan_duty_cycle <= 64) fan_duty_cycle = 64;
if (fan_duty_cycle <= 310) fan_duty_cycle = 310;
preferences.putInt(user_data, fan_duty_cycle);
preferences.end();
dimm(fan_pwm_pin, fan_duty_cycle, ui_lblfanspeed, ui_lblfanfanspeed);
}
}
preferences.end();
}
@@ -354,7 +361,6 @@ void my_touchpad_read (lv_indev_t * indev_driver, lv_indev_data_t * data)
if (data->point.x < 0) data->point.x = 0;
if (data->point.y < 0) data->point.y = 0;
// Debugging-Ausgaben
/*
Serial.print("Touch X: ");
@@ -368,7 +374,23 @@ void my_touchpad_read (lv_indev_t * indev_driver, lv_indev_data_t * data)
/* Tick-Funktion für LVGL interne Timings */
static uint32_t my_tick_get_cb (void) { return millis(); }
//Setup Preferences
void initPreferences()
{
preferences.begin("wifi", false);
preferences.putString("ssid", "MeinZuhause");
preferences.putString("pw", "D4n1e7Y4s3m1nUnd7ey7a");
preferences.end();
preferences.begin("g_phase", false);
preferences.putBool("light", false);
preferences.putInt(led1cycle, 860);
preferences.putInt(led2cycle, 860);
preferences.putInt(fancycle, 510);
preferences.putBool("veggie", false);
preferences.putBool("flowering", false);
preferences.end();
}
@@ -378,8 +400,9 @@ struct tm targettime = {0};
time_t realtime;
time_t target_sec;
time_t endtime;
time_t diff_time;
int updateVeggieMode = 5000;
bool startup;
@@ -388,15 +411,13 @@ void setup ()
{
Serial.begin( 115200 ); /* Vorbereitung für mögliches serielles Debugging */
//INIT NEEDED PREFERENCES FILES
// initPreferences();
String LVGL_Arduino = "Hello Arduino! ";
LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
Serial.println( LVGL_Arduino );
Serial.println( "I am LVGL_Arduino" );
lv_init();
#if LV_USE_LOG != 0
@@ -411,16 +432,13 @@ void setup ()
tft.setTouch(calData);
// PWM-Kanal konfigurieren
ledcAttachChannel(fan_pwm_pin, pwm_freq, pwm_resolution, fan_pwm_channel);
ledcAttachChannel(led1_pwm_pin, pwm_freq, pwm_light_resolution, led1_pwm_channel);
ledcAttachChannel(led2_pwm_pin, pwm_freq, pwm_light_resolution, led2_pwm_channel);
ledcAttachChannel(led1_pwm_pin, pwm_freq, pwm_resolution, led1_pwm_channel);
ledcAttachChannel(led2_pwm_pin, pwm_freq, pwm_resolution, led2_pwm_channel);
// Standardmäßig den Lüfter auf 0% Duty Cycle setzen
ledcWrite(fan_pwm_pin, 76);
ledcWrite(led1_pwm_pin, 255);
ledcWrite(led2_pwm_pin, 255);
static lv_disp_t* disp;
disp = lv_display_create( screenWidth, screenHeight );
@@ -431,16 +449,10 @@ void setup ()
indev = lv_indev_create();
lv_indev_set_type( indev, LV_INDEV_TYPE_POINTER );
lv_indev_set_read_cb( indev, my_touchpad_read );
lv_tick_set_cb( my_tick_get_cb );
ui_init();
Serial.println( "Setup abgeschlossen" );
// DHT11-Sensor initialisieren
// dht.begin();
// Verknüpfung Funktionen UI
lv_obj_add_event_cb(ui_switchfanstatus, switchEventHandler, LV_EVENT_VALUE_CHANGED, fancycle); // Verknüpfen des Switches
lv_obj_add_event_cb(ui_btnfanup, dimmUpBtnEventHandler, LV_EVENT_CLICKED, fancycle); // Up-Button
@@ -455,7 +467,6 @@ void setup ()
lv_obj_add_event_cb(ui_wifikeyboard, setWifi, LV_EVENT_READY, NULL);
lv_obj_add_event_cb(ui_savemode, saveModeSettings, LV_EVENT_CLICKED, NULL);
// Setze initial den Lüfter auf "Off" und den Schalter auf unchecked
lv_label_set_text(ui_lblfanstatus, "OFF");
lv_label_set_text(ui_lblfanfanspeed, "0%");
@@ -467,8 +478,12 @@ void setup ()
//Speichern der var von DutyCycle led1, led2, fan
lv_dropdown_clear_options(ui_wifinetlist);
// Standardmäßig den Lüfter auf 50% Duty Cycle setzen
lv_obj_add_state(ui_switchfanstatus, LV_STATE_CHECKED);
lv_obj_send_event(ui_switchfanstatus, LV_EVENT_VALUE_CHANGED, fancycle);
preferences.begin("wifi", false);
//Setup Wifi
preferences.begin("wifi", true);
String ssid = preferences.getString("ssid", "");
String pw = preferences.getString("pw", "");
int8_t counter = 0;
@@ -493,149 +508,73 @@ void setup ()
}
preferences.end();
int target_h;
int target_m;
bool lights;
preferences.begin("g_phase", false);
//Test GrowProgram
preferences.begin("g_phase", true);
bool light_status = preferences.getBool("light_status", false);
int led1 = preferences.getInt(led1cycle, 860);
int led2 = preferences.getInt(led2cycle, 860);
bool veggie = preferences.getBool("veggie");
bool flowering = preferences.getBool("flowering");
bool veggie_lights_on = preferences.getBool("veggie_on");
bool veggie_lights_off = preferences.getBool("veggie_off");
bool flower_lights_on = preferences.getBool("flower_on");
bool flower_lights_off = preferences.getBool("flower_off");
if (veggie)
{
lv_dropdown_set_selected(ui_growmode, 1);
if (veggie_lights_off)
{
target_h = preferences.getInt("veggie_start_h");
target_m = preferences.getInt("veggie_start_m");
lights = true;
}
else
{
target_h = preferences.getInt("veggie_end_h");
target_m = preferences.getInt("veggie_end_m");
lights = false;
}
}
else if (flowering)
{
lv_dropdown_set_selected(ui_growmode, 2);
}
else
{
lv_dropdown_set_selected(ui_growmode, 0);
}
int start_flower_h = preferences.getInt("flower_start_h");
int start_flower_m = preferences.getInt("flower_start_m");
int end_flower_h = preferences.getInt("flower_end_h");
int end_flower_m = preferences.getInt("flower_end_m");
preferences.end();
Serial.print("Starttime Flower: ");
Serial.print(start_flower_h);
Serial.print(":");
Serial.println(start_flower_m);
Serial.print("Endtime: ");
Serial.print(end_flower_h);
Serial.print(":");
Serial.println(end_flower_m);
Serial.println("VON SETUP");
Serial.print("MODUS VEGGIE: ");
Serial.println(veggie);
Serial.print("MODUS FLOWERING: ");
Serial.println(flowering);
Serial.print("LICHTSTATUS: ");
Serial.println(light_status);
Serial.print("LED1: ");
Serial.println(led1);
Serial.print("LED2: ");
Serial.println(led2);
configTime(gmt_offset_sec, daylight_offset_sec, ntp);
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");
return;
}
realtime = mktime(&timeinfo);
printTime();
targettime = timeinfo;
targettime.tm_hour = target_h;
targettime.tm_min = target_m;
targettime.tm_sec = 0;
target_sec = mktime(&targettime);
if (realtime >= target_sec)
{
}
Serial.println(&timeinfo, "Testtime: %A, %B %d %Y %H:%M:%S");
Serial.println(&targettime, "Testtime: %A, %B %d %Y %H:%M:%S");
startup = true;
lv_obj_add_event_cb(ui_growmode, setMode, LV_EVENT_VALUE_CHANGED, NULL);
WiFi.disconnect();
}
void loop ()
{
lv_timer_handler(); /* Lässt die GUI ihre Arbeit verrichten */
static unsigned long lastUpdate = 0;
const unsigned long updateInterval = 60000; //60 sekunden
static unsigned long lastUpdate2 = 0;
const unsigned long updateInterval2 = 1000;
static unsigned int lastUpdate = 0;
static unsigned int lastUpdate2 = 0;
const unsigned int updateInterval = 1000; //60 sekunden
unsigned long currentMillis = millis();
if (currentMillis - lastUpdate2 >= updateInterval2)
{
printTime();
realtime = mktime(&timeinfo);
//onTime();
lastUpdate2 = currentMillis;
}
if (currentMillis - lastUpdate >= updateInterval)
{
// if (realtime >= testtime2)
// {
// }
printTime();
lastUpdate = currentMillis;
}
}
void onTime()
{
// testtime.tm_min += 5;
// int seconds = 60 * 5;
// testtime2 = mktime(&testtime);
// testtime2 += seconds;
// struct tm * test = gmtime(&testtime2);
// Serial.println(test, "Test: ,%A, %B %d %Y %H:%M:%S");
if (currentMillis - lastUpdate2 >= updateVeggieMode)
{
Serial.print("UPDATE LOOP: ");
Serial.println(updateVeggieMode);
growMode();
lastUpdate2 = currentMillis;
}
}
void saveModeSettings(lv_event_t * e)
@@ -648,8 +587,8 @@ void saveModeSettings(lv_event_t * e)
lv_dropdown_get_selected_str(ui_setmode, mode, sizeof(mode));
lv_roller_get_selected_str(ui_starthour, start_h, sizeof(start_h));
lv_roller_get_selected_str(ui_startmin, start_m, sizeof(start_h));
lv_roller_get_selected_str(ui_endhour, end_h, sizeof(end_h));
lv_roller_get_selected_str(ui_startmin, start_m, sizeof(start_h));
lv_roller_get_selected_str(ui_endmin, end_m, sizeof(end_h));
int start_hour = atoi(start_h);
@@ -689,31 +628,216 @@ void setMode(lv_event_t * e)
{
preferences.putBool("veggie", true);
preferences.putBool("flowering", false);
targettime = timeinfo;
growMode();
}
else
{
preferences.putBool("veggie", false);
preferences.putBool("flowering", true);
targettime = timeinfo;
growMode();
}
preferences.end();
}
void veggie()
void modeOn()
{
lv_obj_add_state(ui_light1switch, LV_STATE_CHECKED);
lv_obj_add_state(ui_light2switch, LV_STATE_CHECKED);
lv_obj_send_event(ui_light1switch, LV_EVENT_VALUE_CHANGED, led1cycle);
lv_obj_send_event(ui_light2switch, LV_EVENT_VALUE_CHANGED, led2cycle);
}
void modeOff()
{
lv_obj_remove_state(ui_light1switch, LV_STATE_CHECKED);
lv_obj_remove_state(ui_light2switch, LV_STATE_CHECKED);
lv_obj_send_event(ui_light1switch, LV_EVENT_VALUE_CHANGED, led1cycle);
lv_obj_send_event(ui_light2switch, LV_EVENT_VALUE_CHANGED, led2cycle);
}
void growMode()
{
Serial.println("GROWMODE");
preferences.begin("g_phase", true);
bool lights = preferences.getBool("light");
bool veggie_state = preferences.getBool("veggie");
bool flower_state = preferences.getBool("flowering");
Serial.print("Veggie: ");
Serial.println(veggie_state);
Serial.print("Flowering: ");
Serial.println(flower_state);
Serial.print("Status Licht:");
Serial.println(lights);
Serial.println("###################");
preferences.end();
if (veggie_state || flower_state)
{
preferences.begin("g_phase", true);
Serial.println("IF VEGGIE OR FLOWER ACTIVE");
int start_h;
int start_m;
int end_h;
int end_m;
Serial.println("###################");
if (veggie_state)
{
Serial.println("IF VEGGIE");
start_h = preferences.getInt("veggie_start_h");
start_m = preferences.getInt("veggie_start_m");
end_h = preferences.getInt("veggie_end_h");
end_m = preferences.getInt("veggie_end_m");
Serial.println("###################");
}
else
{
Serial.println("IF FLOWER");
start_h = preferences.getInt("flower_start_h");
start_m = preferences.getInt("flower_start_m");
end_h = preferences.getInt("flower_end_h");
end_m = preferences.getInt("flower_end_m");
Serial.println("###################");
}
preferences.end();
time_t diff_start = getDiffTime(start_h, start_m);
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)
{
Serial.println("LIGHTS ARE ON");
diff_time = getDiffTime(end_h, end_m);
Serial.println(diff_time);
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)
{
modeOff();
diff_time = getDiffTime(start_h, start_m);
updateVeggieMode = (diff_time * 1000) + 1000;
}
else
{
updateVeggieMode = (diff_time * 1000) + 1000;
modeOn();
Serial.print("UPDATE LIGHTS ON: ");
Serial.print(updateVeggieMode);
Serial.println("###################");
}
}
else
{
Serial.println("LIGHTS OFF: ");
diff_time = getDiffTime(start_h, start_m);
Serial.println(diff_time);
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;
}
else
{
modeOff();
updateVeggieMode = (diff_time * 1000) + 1000;
Serial.print("UPDATE LIGHTS OFF: ");
Serial.print(updateVeggieMode);
Serial.println("###################");
}
}
}
}
preferences.end();
}
void flowering()
time_t getDiffTime(int time_h, int time_m)
{
targettime.tm_hour = time_h;
targettime.tm_min = time_m;
targettime.tm_sec = 0;
target_sec = mktime(&targettime);
realtime = mktime(&timeinfo);
diff_time = difftime(target_sec, realtime);
return diff_time;
}
void printTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo))
{
Serial.println("Failed to obtain time");