From 8922a8f9e9fac3e9703be154e5c8243af2f286bf Mon Sep 17 00:00:00 2001 From: Marcus Date: Tue, 16 Apr 2024 12:31:03 +0200 Subject: [PATCH] my first plants are coming out of their seed - i need some light control urgently, lol --- Arduino/CanGrow/CanGrow.ino | 250 +++++++++++++++++++++++++++++++----- 1 file changed, 220 insertions(+), 30 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 2b9dc7a..b4b53ae 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -39,16 +39,10 @@ float valHumidity; /* - * EEPROM variables + * EEPROM saved variables */ -// -// System -// -// configured - if true, run setup assistant -bool configured; -// NTP Offset -int ntpOffset; + // @@ -64,9 +58,32 @@ IPAddress WIFIgateway(192,168,4,254); IPAddress WIFIdns(0,0,0,0); char WebUiUsername[16] = "cangrow"; char WebUiPassword[32] = "cangrow"; + +// +// System +// +// configured - if true, run setup assistant +bool configured; +// NTP Offset +int ntpOffset; +// MoistureSensor_Type - contains which moisture sensor to use +// 0: analog capacitive sensor +// 1: I2C chirp sensor from catnip electronics +byte MoistureSensor_Type; +// SoilmoistureLow - contains the value , when soil moisture is assumed to be low, +byte SoilmoistureLow; +// UsePump - is the pump used? bool +bool UsePump; +// UseFan - is the fan used? bool +byte PumpOnTime; +bool UseFan; + // // Grow Stuff // + +unsigned int NtpOffset; + // GrowName - contains the name of the grow/plant. Up to 32 byte char GrowName[32]; // GrowStart - contains unix timestamp from date where grow starts (00:00) @@ -88,17 +105,9 @@ byte SunriseHour; byte SunriseMinute; // PINled_PWM - contains the PWM value for dimming the grow light byte PINled_PWM; -// MoistureSensor_Type - contains which moisture sensor to use -// 0: analog capacitive sensor -// 1: I2C chirp sensor from catnip electronics -byte MoistureSensor_Type; -// UsePump - is the pump used? bool -bool UsePump; -// UseFan - is the fan used? bool -byte PumpOnTime; -bool UseFan; -// SoilmoistureLow - contains the value , when soil moisture is assumed to be low, -byte SoilmoistureLow; + + + /* * @@ -587,8 +596,8 @@ bool loadEEPROM() { EEPROM.get(128, WIFIgateway); // read var WIFIgateway from address 128, 16 byte long EEPROM.get(144, WIFIdns); - // read var WIFIuseDHCP from Address 510, 1 byte long - EEPROM.get(510, WIFIuseDHCP); + // read var WIFIuseDHCP from Address 160, 1 byte long + EEPROM.get(160, WIFIuseDHCP); /* @@ -603,9 +612,26 @@ bool loadEEPROM() { * * All boolean variables are at the end of the EEPROM */ - EEPROM.get(511, configured); - EEPROM.get(160, WebUiUsername); - EEPROM.get(176, WebUiPassword); + + + // size is 1 byte + EEPROM.get(161, configured); + // size is 1 byte + EEPROM.get(162, UseFan); + // size is 1 byte + EEPROM.get(163, UsePump); + // size is 1 byte + EEPROM.get(164, PumpOnTime); + // size is 1 byte + EEPROM.get(165, MoistureSensor_Type); + // size is 1 byte + EEPROM.get(166, SoilmoistureLow); + // size is 2 byte + EEPROM.get(167, ntpOffset); + + // TODO auth does not work atm + // EEPROM.get(160, WebUiUsername); + // EEPROM.get(176, WebUiPassword); /* * Grow settings @@ -618,8 +644,25 @@ bool loadEEPROM() { // print values to Serial output Serial.print("WIFIssid: "); Serial.println(WIFIssid); + Serial.print("WIFIpassword: "); + Serial.println(WIFIpassword); Serial.print("Use DHCP: "); Serial.println(WIFIuseDHCP); + + Serial.print("UseFan: "); + Serial.println(UseFan); + Serial.print("UsePump: "); + Serial.println(UsePump); + Serial.print("PumpOnTime: "); + Serial.println(PumpOnTime); + Serial.print("MoistureSensor_Type: "); + Serial.println(MoistureSensor_Type); + Serial.print("SoilmoistureLow: "); + Serial.println(SoilmoistureLow); + Serial.print("ntpOffset: "); + Serial.println(ntpOffset); + + Serial.print("configured: "); Serial.println(configured); } else { @@ -833,20 +876,29 @@ void WebHandler() { webserver.on("/growSettings", HTTP_GET, WEBwifiSettings); } else { + // now we need to receive systemSettings + webserver.on("/systemSettings/save", HTTP_POST, POSTsystemSettings); + // when system settings are unconfigured , then system settings are root if(configured == false) { webserver.on("/", HTTP_GET, WEBsystemSettings); webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings); webserver.on("/growSettings", HTTP_GET, WEBsystemSettings); + + } else { webserver.on("/systemSettings", HTTP_GET, WEBsystemSettings); } + + webserver.on("/growSettings", HTTP_GET, WEBgrowSettings); + // now we need to receive growSettings + webserver.on("/growSettings/save", HTTP_POST, POSTgrowSettings); // when grow is unconfigured (GrowStart <1) then Grow settings are root if(GrowStart < 1) { webserver.on("/", HTTP_GET, WEBgrowSettings); } else { - webserver.on("/growSettings", HTTP_GET, WEBgrowSettings); + webserver.on("/", HTTP_GET, WEBroot); } } } @@ -946,7 +998,7 @@ void WEBwifiSettings() { body += "
"; body += "

"; } - body += "

WiFi config

\n"; + body += "

WiFi config

\n"; if(webserver.hasArg("success")) { body += "
Successfully saved!
Please restart the device.
"; } @@ -980,9 +1032,66 @@ void WEBwifiSettings() { void WEBsystemSettings() { String body = FPSTR(HTMLheader); - body += "

System settings

"; + + if(configured == false) { + body += "

Step 2: System settings

"; + body += "

Please configure all settings
"; + body += "

"; + } + + body += "

System settings

"; + if(webserver.hasArg("success")) { + body += "
Successfully saved!
Please restart the device.
"; + } body += "

here you can set which features and sensors you use
"; body += "

"; + + /* + * // size is 1 byte + EEPROM.put(162, UseFan); + // size is 1 byte + EEPROM.put(163, UsePump); + // size is 1 byte + EEPROM.put(164, PumpOnTime); + // size is 1 byte + EEPROM.put(165, MoistureSensor_Type); + // size is 1 byte + EEPROM.put(166, SoilmoistureLow); + // size is 2 byte + EEPROM.put(167, ntpOffset); + * */ + + + body += "
\n"; + + body += "Use fan:
\n"; + + + body += "Use pump:
\n"; + + body += "Pump on time:
\n"; + + body += "Moisture sensor type:
\n"; + + body += "Soil moisture low:
\n"; + + body += "NTP offset:
\n"; + + body += "\n"; + body += "
\n"; + body += FPSTR(HTMLfooter); webserver.send(200, "text/html", body); @@ -993,9 +1102,26 @@ void WEBsystemSettings() { */ void WEBgrowSettings() { String body = FPSTR(HTMLheader); - body += "

Grow Settings

"; - body += "

Here you can set everything grow related, like light hours, how much water, LED brightnes
"; + + if(strlen(GrowName) > 0) { + body += "

Step 3: Grow settings

"; + body += "

Please configure all settings
"; + body += "

"; + } + + body += "

Grow Settings

"; + if(webserver.hasArg("success")) { + body += "
Successfully saved!
Please restart the device.
"; + } + body += "

Here you can set everything grow related, like light hours, how much water, LED brightness
"; body += "

"; + + body += "
\n"; + + body += "Brightness "; + + body += "\n"; + body += "
\n"; body += FPSTR(HTMLfooter); webserver.send(200, "text/html", body); @@ -1007,7 +1133,71 @@ void WEBgrowSettings() { * POSTs * */ + +void POSTgrowSettings() { + PINled_PWM = webserver.arg("PINled_PWM").toInt(); + analogWrite(D4, PINled_PWM); + + Serial.println(":: POSTgrowSettings ::"); + + Serial.print("PINled_PWM: "); + Serial.println(PINled_PWM); + + webserver.sendHeader("Location", String("/growSettings?success"), true); + webserver.send(302, "text/plain", "growSettings/save: success!"); +} + + +void POSTsystemSettings() { + ntpOffset = webserver.arg("ntpOffset").toInt(); + MoistureSensor_Type = webserver.arg("MoistureSensor_Type").toInt(); + SoilmoistureLow = webserver.arg("SoilmoistureLow").toInt(); + UsePump = webserver.arg("UsePump").toInt(); + PumpOnTime = webserver.arg("PumpOnTime").toInt(); + UseFan = webserver.arg("UseFan").toInt(); + + configured = true; + + // size is 1 byte + EEPROM.put(161, configured); + // size is 1 byte + EEPROM.put(162, UseFan); + // size is 1 byte + EEPROM.put(163, UsePump); + // size is 1 byte + EEPROM.put(164, PumpOnTime); + // size is 1 byte + EEPROM.put(165, MoistureSensor_Type); + // size is 1 byte + EEPROM.put(166, SoilmoistureLow); + // size is 2 byte + EEPROM.put(167, ntpOffset); + + EEPROM.commit(); + + Serial.println(":: POSTsystemSettings ::"); + + Serial.print("configured: "); + Serial.println(configured); + Serial.print("UseFan: "); + Serial.println(UseFan); + Serial.print("UsePump: "); + Serial.println(UsePump); + Serial.print("PumpOnTime: "); + Serial.println(PumpOnTime); + Serial.print("MoistureSensor_Type: "); + Serial.println(MoistureSensor_Type); + Serial.print("SoilmoistureLow: "); + Serial.println(SoilmoistureLow); + Serial.print("ntpOffset: "); + Serial.println(ntpOffset); + + webserver.sendHeader("Location", String("/systemSettings?success"), true); + webserver.send(302, "text/plain", "systemSettings/save: success!"); +} + + void POSTwifiSettings() { String WIFIssid_new = webserver.arg("WIFIssid"); String WIFIpassword_new = webserver.arg("WIFIpassword"); @@ -1040,7 +1230,7 @@ void POSTwifiSettings() { EEPROM.put(112, WIFInetmask); EEPROM.put(128, WIFIgateway); EEPROM.put(144, WIFIdns); - EEPROM.put(510, WIFIuseDHCP); + EEPROM.put(160, WIFIuseDHCP); EEPROM.commit();