From 54b551f3df30013a2b6b14f3a6fe5796c763c92d Mon Sep 17 00:00:00 2001 From: Marcus Date: Wed, 1 May 2024 01:29:28 +0200 Subject: [PATCH] firmware wip - rename PIN variables to a more equal naming schema --- Arduino/CanGrow/CanGrow.ino | 464 ++++++++++++++++++------------------ 1 file changed, 238 insertions(+), 226 deletions(-) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 173313b..db49d27 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -325,7 +325,7 @@ function convertDateToEpoch(src, dst) { * D0 - MOSFET Pump * D1, D2 - I2C * D3 - DHT11 - * D4 - PIN_WIPE + * D4 - PinWIPE * D5 - MOSFET Fan * D6 - MOSFET Grow LED, PWM * D7 - waterlevel (set HIGH to read value) @@ -342,7 +342,7 @@ const uint8_t PINdht = D3; // D4 is HIGH at boot, boot fail if pulled to LOW // During Start Screen you can pull D4 to LOW to wipe saved data in EEPROM // DO NOT PULL D4 DOWN AT WHEN POWERING ON !!! BOOT WILL FAIL -const uint8_t PIN_WIPE = D4; +const uint8_t PinWIPE = D4; const uint8_t PinFAN = D5; const uint8_t PinLED = D6; // const uint8_t PINwaterlevel = D7; @@ -593,14 +593,14 @@ void wipeEEPROM() { // wipeMsg is helper variable to know if the Serial.print Message was // already sent byte wipeMsg = 0; - while(digitalRead(PIN_WIPE) == LOW ) { + while(digitalRead(PinWIPE) == LOW ) { // only show the Serial message once if(wipeMsg == 0) { - Serial.println("Please release PIN_WIPE to erase all data saved in EEPROM"); + Serial.println("Please release PinWIPE to erase all data saved in EEPROM"); Serial.println("LAST CHANCE TO KEEP THE DATA BY RESETTING NOW!!"); display.setCursor(0,36); - display.println("RELEASE PIN_WIPE"); + display.println("RELEASE PinWIPE"); display.println("TO WIPE EEPROM"); display.display(); @@ -623,18 +623,18 @@ void wipeEEPROM() { Serial.println("DONE"); - // set D4 PIN_WIPE internal LED to Output to give feedback WIPE + // set D4 PinWIPE internal LED to Output to give feedback WIPE // was done - pinMode(PIN_WIPE, OUTPUT); + pinMode(PinWIPE, OUTPUT); Serial.println("!! Device will restart in 3 seconds !!"); // let the internal led blink fast to signalize wipe is done for(byte i = 0; i <= 24 ; i++) { if(i % 2) { - digitalWrite(PIN_WIPE, LOW); + digitalWrite(PinWIPE, LOW); } else { - digitalWrite(PIN_WIPE, HIGH); + digitalWrite(PinWIPE, HIGH); } delay(125); } @@ -1038,9 +1038,7 @@ void refreshSensors() { * */ void setup() { - // Start EEPROM - EEPROM.begin(512); - + // setup pins pinMode(PinFAN, OUTPUT); pinMode(PINdht, INPUT); @@ -1048,7 +1046,7 @@ void setup() { pinMode(PINsoilmoisture, OUTPUT); pinMode(PinLED, OUTPUT); pinMode(PinPUMP, OUTPUT); - pinMode(PIN_WIPE, OUTPUT); + pinMode(PinWIPE, OUTPUT); // set all OUTPUT to low @@ -1063,12 +1061,14 @@ void setup() { // 220Hz is note A3 btw. analogWriteFreq(220); + // Start EEPROM + EEPROM.begin(512); + // Start Serial Serial.begin(115200); // Write a line before doing serious output, because before there is some garbage in serial // whats get the cursor somewhere over the place - // output Serial.println("420"); Serial.println(".:: CanGrow Start ::."); @@ -1076,7 +1076,7 @@ void setup() { Wire.begin(); // initialise I2C display - display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x64 display.clearDisplay(); display.display(); @@ -1094,7 +1094,7 @@ void setup() { // initialise DHT11 dht.begin(); //TODO: Do only, when configured - Serial.println("To wipe the EEPROM saved data, set D4 (PIN_WIPE) to LOW - NOW! (2 seconds left)"); + Serial.println("To wipe the EEPROM saved data, set D4 (PinWIPE) to LOW - NOW! (2 seconds left)"); // wait a few seconds to let the user pull D4 down to wipe EEPROM // and we can enjoy the boot screen meanwhile :p // meanwhile blink with the led onboad :) @@ -1103,24 +1103,25 @@ void setup() { display.fillRect(0,36,128,64-36, 0); display.setCursor(0,36); display.println("To wipe EEPROM pull"); - display.println("D4 (PIN_WIPE) to GND"); + display.println("D4 (PinWIPE) to GND"); display.display(); - + + // blink with the onboard LED on D4 (PinWIPE) for(byte i = 0; i <= 6 ; i++) { if(i % 2) { - digitalWrite(PIN_WIPE, LOW); + digitalWrite(PinWIPE, LOW); } else { - digitalWrite(PIN_WIPE, HIGH); + digitalWrite(PinWIPE, HIGH); } delay(333); } // set back to HIGH because thats the default - digitalWrite(PIN_WIPE, HIGH); + digitalWrite(PinWIPE, HIGH); //delay(2000); - // read status from PIN_WIPE to WIPE - // when PIN_WIPE is set to LOW, wipe EEPROM - if(digitalRead(PIN_WIPE) == LOW) { + // read status from PinWIPE to WIPE + // when PinWIPE is set to LOW, wipe EEPROM + if(digitalRead(PinWIPE) == LOW) { // wipe EEPROM wipeEEPROM(); } @@ -1190,12 +1191,17 @@ void loop() { // current time gets previous time for new interval outputPrevTime = currentRuntime; } - - - //delay(1000); } +/* + * + * + * Web related stuff + * + * + */ + /* * Web Handler */ @@ -1241,7 +1247,7 @@ void WebHandler() { // favicon.ico is a special one, because its requested everytime and i dont wont to deliver the // failed whole page every call. we can save up this 0,5kb traffic :o) webserver.on("/favicon.ico", [](){ webserver.send(404, "text/html", "404 - not found"); }); - webserver.onNotFound(WEB404); + webserver.onNotFound(Sys404); // switching MOSFETs webserver.on("/switch", HTTP_POST, POSTsetOutput); @@ -1371,6 +1377,13 @@ String returnStrDateFromEpoch(unsigned long epochTime) { return dateStr; } + +/* + * + * System pages like infos, errors + * + */ + void SysRestart() { String body = returnHTMLheader(); // TODO only debug and development solution, remove this later @@ -1414,6 +1427,22 @@ void SysWipe() { } } +void Sys404() { + String body = returnHTMLheader(); + body += "

404 - not found

"; + body += FPSTR(HTMLfooter); + webserver.send(404, "text/html", body); +} + +void Syslogout() { + String body = returnHTMLheader(); + body += "

you are logged out.

"; + body += FPSTR(HTMLfooter); + + // TODO does not work atm + webserver.send(401, "text/html", body); +} + /* * TODO @@ -1448,35 +1477,12 @@ void SysWipe() { /* * - * Web pages + * Main UI pages * */ -void WEB404() { - String body = returnHTMLheader(); - body += "

404 - not found

"; - body += FPSTR(HTMLfooter); - webserver.send(404, "text/html", body); -} - -void WEBlogout() { - String body = returnHTMLheader(); - body += "

you are logged out.

"; - body += FPSTR(HTMLfooter); - - // TODO does not work atm - webserver.send(401, "text/html", body); -} - -void WEBhelp() { - String body = returnHTMLheader("help"); - body += FPSTR(HTMLhelp); - body += FPSTR(HTMLfooter); - webserver.send(200, "text/html", body); -} - /* - * Root pages + * Root page */ void WEBroot() { @@ -1497,19 +1503,19 @@ void WEBroot() { body += ""; body += "Grow started: "; body += returnStrDateFromEpoch(GrowStart); - body += "
"; + body += "
\n"; body += "Day of Grow: "; - body += DayOfGrow; - body += "
"; + body += DayOfGrow; + body += "
\n"; body += "Soil Moisture: "; body += valSoilmoisture; - body += " %
"; + body += " %
\n"; body += "Humidity: "; body += valHumidity; - body += " %
"; + body += " %
\n"; body += "Temperature: "; body += valTemperature; - body += " °C
"; + body += " °C
\n"; if(UsePump == true) { body += "Pump water level: "; switch(getWaterlevel()) { @@ -1524,11 +1530,11 @@ void WEBroot() { break; } } - body += "
"; + body += "
\n"; body += "Growlight brightnes: "; body += ((PinLEDPWM * 100) / 255); - body += " %
"; - body += "
"; + body += " %
\n"; + body += "\n"; body += "MOSFET\n"; body += "
"; - - - - - + body += FPSTR(HTMLfooter); webserver.send(200, "text/html", body); @@ -1558,74 +1560,111 @@ void WEBroot() { } /* - * Config pages + * + * settings pages + * + * */ -void WEBwifiSettings() { - byte ssidsAvail = WiFi.scanNetworks(); - String body = returnHTMLheader("wifiSettings"); - if(FirstRun == true) { - body += "

Welcome!

"; - body += "

CanGrow is actually unconfigured. You need to Setup your WiFi first down below.
"; - body += "
After you entered your WiFi connection details, you need to restart and are step closer to your grow 🥦"; - body += "
"; +/* + * Grow page + */ +void WEBgrowSettings() { + // if system settings are unconfigured, we cannot proceed with growSettings + if(configured == false) { + webserver.sendHeader("Location", String("/systemSettings"), true); + webserver.send(302, "text/plain", "please configure systemSettings first"); + } else { + + String body = returnHTMLheader("growSettings"); + + if(strlen(GrowName) < 1) { + body += "

Final step: Grow settings

"; + body += "

Please configure all settings
"; + body += "

"; + GrowStart = timeClient.getEpochTime(); + } + + body += "

Grow Settings

"; + if(webserver.hasArg("success")) { + body += FPSTR(HTMLsuccess); + } + body += "

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

"; - } - body += "

WiFi config

\n"; - if(NeedRestart == true) { - body += FPSTR(HTMLneedRestart); - } - if(webserver.hasArg("success")) { - body += FPSTR(HTMLsuccess); - } + + body += "
\n"; + - if(FirstRun == false) { - body += "Current Settings:
"; - body += "WiFi SSID: "; - body += WIFIssid; - body += "
\n"; - body += "Use DHCP: "; - body += WIFIuseDHCP; - body += "
\n"; - body += "IP address: "; - body += WiFi.localIP().toString(); - body += "
\n"; - body += "Subnet mask: "; - body += WiFi.subnetMask().toString(); - body += "
\n"; - body += "Gateway: "; - body += WiFi.gatewayIP().toString(); - body += "
\n"; - body += "DNS: "; - body += WiFi.dnsIP().toString(); - body += "

\n"; + + body += "Grow name:
\n"; + + + // the input field, which calls javascript convertDateToEpoch() to write data to transmit to id GrowStart + body += "Grow start date:
\n"; + + body += "\n"; + + + + body += "Days of vegetation:
\n"; + + body += "Days of bloom:
\n"; + + body += "Hours light on vegetation:
\n"; + + body += "Hours light on bloom:
\n"; + + body += "Sunrise: \n"; + body += " :
\n"; + + // SunFade bool + body += "Fade LED on Sunrise/Sunset?:
\n"; + + body += "Fading duration (minutes):
\n"; + + if(UseLEDrelais == false) { + body += "Brightness LED:
\n"; + } + + if(UseFANrelais == false) { + body += "Speed FAN:
\n"; + } + + body += "\n"; + body += "
\n"; + body += FPSTR(JSconvertDateToEpoch); + body += FPSTR(HTMLfooter); + + webserver.send(200, "text/html", body); } - - body += "

Select your wifi network from the SSID list.
To use DHCP leave IP, Subnet, Gateway and DNS fields blank!

"; - body += "
\n"; - body += "SSID:
\n"; - body += "Password:
\n"; - body += "IP:
\n"; - body += "Subnet mask:
\n"; - body += "Gateway:
\n"; - body += "DNS:
\n"; - body += "\n"; - body += "
\n"; - body += FPSTR(HTMLfooter); - - webserver.send(200, "text/html", body); -} +} void WEBsystemSettings() { // if wifi settings are unconfigured, we cannot proceed with systemSettings @@ -1723,104 +1762,77 @@ void WEBsystemSettings() { } } -/* - * Grow pages - */ -void WEBgrowSettings() { - // if system settings are unconfigured, we cannot proceed with growSettings - if(configured == false) { - webserver.sendHeader("Location", String("/systemSettings"), true); - webserver.send(302, "text/plain", "please configure systemSettings first"); - } else { - - String body = returnHTMLheader("growSettings"); - - if(strlen(GrowName) < 1) { - body += "

Final step: Grow settings

"; - body += "

Please configure all settings
"; - body += "

"; - GrowStart = timeClient.getEpochTime(); - } - - body += "

Grow Settings

"; - if(webserver.hasArg("success")) { - body += FPSTR(HTMLsuccess); - } - body += "

Here you can set everything grow related, like light hours, how much water, LED brightness
"; +void WEBwifiSettings() { + byte ssidsAvail = WiFi.scanNetworks(); + String body = returnHTMLheader("wifiSettings"); + if(FirstRun == true) { + body += "

Welcome!

"; + body += "

CanGrow is actually unconfigured. You need to Setup your WiFi first down below.
"; + body += "
After you entered your WiFi connection details, you need to restart and are step closer to your grow 🥦"; + body += "
"; body += "

"; - - body += "
\n"; - - - - body += "Grow name:
\n"; - - - // the input field, which calls javascript convertDateToEpoch() to write data to transmit to id GrowStart - body += "Grow start date:
\n"; - - body += "\n"; - - - - body += "Days of vegetation:
\n"; - - body += "Days of bloom:
\n"; - - body += "Hours light on vegetation:
\n"; - - body += "Hours light on bloom:
\n"; - - body += "Sunrise: \n"; - body += " :
\n"; - - // SunFade bool - body += "Fade in and out LED?:
\n"; - - body += "Fading duration (minutes):
\n"; - - if(UseLEDrelais == false) { - body += "Brightness LED:
\n"; - } - - if(UseFANrelais == false) { - body += "Speed FAN:
\n"; - } - - body += "\n"; - body += "
\n"; - body += FPSTR(JSconvertDateToEpoch); - body += FPSTR(HTMLfooter); - - webserver.send(200, "text/html", body); } + body += "

WiFi config

\n"; + if(NeedRestart == true) { + body += FPSTR(HTMLneedRestart); + } + if(webserver.hasArg("success")) { + body += FPSTR(HTMLsuccess); + } + + if(FirstRun == false) { + body += "Current Settings:
"; + body += "WiFi SSID: "; + body += WIFIssid; + body += "
\n"; + body += "Use DHCP: "; + body += WIFIuseDHCP; + body += "
\n"; + body += "IP address: "; + body += WiFi.localIP().toString(); + body += "
\n"; + body += "Subnet mask: "; + body += WiFi.subnetMask().toString(); + body += "
\n"; + body += "Gateway: "; + body += WiFi.gatewayIP().toString(); + body += "
\n"; + body += "DNS: "; + body += WiFi.dnsIP().toString(); + body += "

\n"; + } + + body += "

Select your wifi network from the SSID list.
To use DHCP leave IP, Subnet, Gateway and DNS fields blank!

"; + body += "
\n"; + body += "SSID:
\n"; + body += "Password:
\n"; + body += "IP:
\n"; + body += "Subnet mask:
\n"; + body += "Gateway:
\n"; + body += "DNS:
\n"; + body += "\n"; + body += "
\n"; + body += FPSTR(HTMLfooter); + + webserver.send(200, "text/html", body); +} + +void WEBhelp() { + String body = returnHTMLheader("help"); + body += FPSTR(HTMLhelp); + body += FPSTR(HTMLfooter); + webserver.send(200, "text/html", body); }