need help why my code

al3xrud3r
Posts: 2
Joined: Mon Nov 20, 2023 6:31 am

need help why my code

Postby al3xrud3r » Mon Nov 20, 2023 6:38 am

so i have made a code for a greenhouse control with a esp32 control for greenhouse
monitor/control code with the following
temperature/humidity in fahrenheit

control temperature monitoring off a dht11

Fan/vent control with schedule on html and rtc with manual on/off button

Swamp cooler with schedule on html and rtc with manual on/off button

2 different light control one veg one flower with schedule on html and rtc with manual on/off button

Useing rtc,dht,light sensor and rtc
but i keep getting this This site can’t be reached when i try to get into the web page


here my code if anyone can help be nice thank you
  1. #include <WiFi.h>
  2. #include <HTTPClient.h>
  3. #include <WebServer.h>
  4. #include <RTClib.h>
  5. #include <DHT.h>
  6.  
  7. #define DHTPIN 2 // Pin connected to the DHT11 sensor
  8. #define DHTTYPE DHT11 // DHT sensor type
  9.  
  10. #define FAN_PIN 4 // Pin connected to the relay module for fan/vent control
  11. #define SWAMP_COOLER_PIN 5 // Pin connected to the relay module for swamp cooler control
  12. #define VEG_LIGHT_PIN 6 // Pin connected to the relay module for veg light control
  13. #define FLOWER_LIGHT_PIN 7 // Pin connected to the relay module for flower light control
  14. #define LIGHT_SENSOR_PIN A0 // Pin connected to the light sensor (LDR)
  15.  
  16. DHT dht(DHTPIN, DHTTYPE);
  17. RTC_DS3231 rtc;
  18.  
  19. WebServer server(80);
  20.  
  21. int fanOnHour = 8; // Fan/vent on hour
  22. int fanOffHour = 18; // Fan/vent off hour
  23.  
  24. int swampCoolerOnHour = 9; // Swamp cooler on hour
  25. int swampCoolerOffHour = 17; // Swamp cooler off hour
  26.  
  27. int vegLightOnHour = 6; // Veg light on hour
  28. int vegLightOffHour = 18; // Veg light off hour
  29.  
  30. int flowerLightOnHour = 8; // Flower light on hour
  31. int flowerLightOffHour = 20; // Flower light off hour
  32.  
  33. void handleRoot() {
  34.   String html = "<html><body>";
  35.   html += "<h1>Greenhouse Control</h1>";
  36.   html += "<h2>Temperature and Humidity</h2>";
  37.   html += "<p>Temperature: ";
  38.   html += String(dht.readTemperature() * 1.8 + 32); // Convert temperature to Fahrenheit
  39.   html += " °F</p>";
  40.   html += "<p>Humidity: ";
  41.   html += String(dht.readHumidity());
  42.   html += " %</p>";
  43.   html += "<h2>Fan/Vent Control</h2>";
  44.   html += "<form action='/fan' method='POST'>";
  45.   html += "Fan/Vent Schedule: <input type='time' name='onTime' value='08:00'> - <input type='time' name='offTime' value='18:00'><br>";
  46.   html += "<input type='submit' value='Set Schedule'>";
  47.   html += "</form>";
  48.   html += "<br>";
  49.   html += "<form action='/fan' method='POST'>";
  50.   html += "<input type='submit' name='fanOn' value='Fan On'>";
  51.   html += "<input type='submit' name='fanOff' value='Fan Off'>";
  52.   html += "</form>";
  53.   html += "<h2>Swamp Cooler Control</h2>";
  54.   html += "<form action='/swampCooler' method='POST'>";
  55.   html += "Swamp Cooler Schedule: <input type='time' name='onTime' value='09:00'> - <input type='time' name='offTime' value='17:00'><br>";
  56.   html += "<input type='submit' value='Set Schedule'>";
  57.   html += "</form>";
  58.   html += "<br>";
  59.   html += "<form action='/swampCooler' method='POST'>";
  60.   html += "<input type='submit' name='swampCoolerOn' value='Swamp Cooler On'>";
  61.   html += "<input type='submit' name='swampCoolerOff' value='Swamp Cooler Off'>";
  62.   html += "</form>";
  63.   html += "<h2>Veg Light Control</h2>";
  64.   html += "<form action='/vegLight' method='POST'>";
  65.   html += "Veg Light Schedule: <input type='time' name='onTime' value='06:00'> - <input type='time' name='offTime' value='18:00'><br>";
  66.   html += "<input type='submit' value='Set Schedule'>";
  67.   html += "</form>";
  68.   html += "<br>";
  69.   html += "<form action='/vegLight' method='POST'>";
  70.   html += "<input type='submit' name='vegLightOn' value='Veg Light On'>";
  71.   html += "<input type='submit' name='vegLightOff' value='Veg Light Off'>";
  72.   html += "</form>";
  73.   html += "<h2>Flower Light Control</h2>";
  74.   html += "<form action='/flowerLight' method='POST'>";
  75.   html += "Flower Light Schedule: <input type='time' name='onTime' value='08:00'> - <input type='time' name='offTime' value='20:00'><br>";
  76.   html += "<input type='submit' value='Set Schedule'>";
  77.   html += "</form>";
  78.   html += "<br>";
  79.   html += "<form action='/flowerLight' method='POST'>";
  80.   html += "<input type='submit' name='flowerLightOn' value='Flower Light On'>";
  81.   html += "<input type='submit' name='flowerLightOff' value='Flower Light Off'>";
  82.   html += "</form>";
  83.   html += "</body></html>";
  84.   server.send(200, "text/html", html);
  85. }
  86.  
  87. void handleFan() {
  88.   if (server.hasArg("onTime") && server.hasArg("offTime")) {
  89.     String onTime = server.arg("onTime");
  90.     String offTime = server.arg("offTime");
  91.     fanOnHour = onTime.substring(0, 2).toInt();
  92.     fanOffHour = offTime.substring(0, 2).toInt();
  93.     server.send(200, "text/plain", "Fan schedule updated");
  94.   } else if (server.hasArg("fanOn")) {
  95.     digitalWrite(FAN_PIN, HIGH);
  96.     server.send(200, "text/plain", "Fan turned on");
  97.   } else if (server.hasArg("fanOff")) {
  98.     digitalWrite(FAN_PIN, LOW);
  99.     server.send(200, "text/plain", "Fan turned off");
  100.   }
  101. }
  102.  
  103. void handleSwampCooler() {
  104.   if (server.hasArg("onTime") && server.hasArg("offTime")) {
  105.     String onTime = server.arg("onTime");
  106.     String offTime = server.arg("offTime");
  107.     swampCoolerOnHour = onTime.substring(0, 2).toInt();
  108.     swampCoolerOffHour = offTime.substring(0, 2).toInt();
  109.     server.send(200, "text/plain", "Swamp cooler schedule updated");
  110.   } else if (server.hasArg("swampCoolerOn")) {
  111.     digitalWrite(SWAMP_COOLER_PIN, HIGH);
  112.     server.send(200, "text/plain", "Swamp cooler turned on");
  113.   } else if (server.hasArg("swampCoolerOff")) {
  114.     digitalWrite(SWAMP_COOLER_PIN, LOW);
  115.     server.send(200, "text/plain", "Swamp cooler turned off");
  116.   }
  117. }
  118.  
  119. void handleVegLight() {
  120.   if (server.hasArg("onTime") && server.hasArg("offTime")) {
  121.     String onTime = server.arg("onTime");
  122.     String offTime = server.arg("offTime");
  123.     vegLightOnHour = onTime.substring(0, 2).toInt();
  124.     vegLightOffHour = offTime.substring(0, 2).toInt();
  125.     server.send(200, "text/plain", "Veg light schedule updated");
  126.   } else if (server.hasArg("vegLightOn")) {
  127.     digitalWrite(VEG_LIGHT_PIN, HIGH);
  128.     server.send(200, "text/plain", "Veg light turned on");
  129.   } else if (server.hasArg("vegLightOff")) {
  130.     digitalWrite(VEG_LIGHT_PIN, LOW);
  131.     server.send(200, "text/plain", "Veg light turned off");
  132.   }
  133. }
  134.  
  135. void handleFlowerLight() {
  136.   if (server.hasArg("onTime") && server.hasArg("offTime")) {
  137.     String onTime = server.arg("onTime");
  138.     String offTime = server.arg("offTime");
  139.     flowerLightOnHour = onTime.substring(0, 2).toInt();
  140.     flowerLightOffHour = offTime.substring(0, 2).toInt();
  141.     server.send(200, "text/plain", "Flower light schedule updated");
  142.   } else if (server.hasArg("flowerLightOn")) {
  143.     digitalWrite(FLOWER_LIGHT_PIN, HIGH);
  144.     server.send(200, "text/plain", "Flower light turned on");
  145.   } else if (server.hasArg("flowerLightOff")) {
  146.     digitalWrite(FLOWER_LIGHT_PIN, LOW);
  147.     server.send(200, "text/plain", "Flower light turned off");
  148.   }
  149. }
  150.  
  151. void setup() {
  152.   pinMode(FAN_PIN, OUTPUT);
  153.   pinMode(SWAMP_COOLER_PIN, OUTPUT);
  154.   pinMode(VEG_LIGHT_PIN, OUTPUT);
  155.   pinMode(FLOWER_LIGHT_PIN, OUTPUT);
  156.  
  157.   dht.begin();
  158.   rtc.begin();
  159.  
  160.   WiFi.begin("ASUS", "AlexRuder1997"); // Replace with your WiFi credentials
  161.  
  162.   while (WiFi.status() != WL_CONNECTED) {
  163.     delay(1000);
  164.     Serial.println("Connecting to WiFi...");
  165.   }
  166.  
  167.   Serial.println("WiFi connected");
  168.   Serial.print("IP address: ");
  169.   Serial.println(WiFi.localIP());
  170.  
  171.   server.on("/", handleRoot);
  172.   server.on("/fan", handleFan);
  173.   server.on("/swampCooler", handleSwampCooler);
  174.     server.on("/vegLight", handleVegLight);
  175.   server.on("/flowerLight", handleFlowerLight);
  176.  
  177.   server.begin();
  178.   Serial.println("Server started");
  179. }
  180.  
  181. void loop() {
  182.   server.handleClient();
  183.  
  184.   // Check if it's time to turn on/off the fan
  185.   DateTime now = rtc.now();
  186.   int currentHour = now.hour();
  187.  
  188.   if (currentHour >= fanOnHour && currentHour < fanOffHour) {
  189.     digitalWrite(FAN_PIN, HIGH);
  190.   } else {
  191.     digitalWrite(FAN_PIN, LOW);
  192.   }
  193.  
  194.   // Check if it's time to turn on/off the swamp cooler
  195.   if (currentHour >= swampCoolerOnHour && currentHour < swampCoolerOffHour) {
  196.     digitalWrite(SWAMP_COOLER_PIN, HIGH);
  197.   } else {
  198.     digitalWrite(SWAMP_COOLER_PIN, LOW);
  199.   }
  200.  
  201.   // Check if it's time to turn on/off the veg light
  202.   if (currentHour >= vegLightOnHour && currentHour < vegLightOffHour) {
  203.     digitalWrite(VEG_LIGHT_PIN, HIGH);
  204.   } else {
  205.     digitalWrite(VEG_LIGHT_PIN, LOW);
  206.   }
  207.  
  208.   // Check if it's time to turn on/off the flower light
  209.   if (currentHour >= flowerLightOnHour && currentHour < flowerLightOffHour) {
  210.     digitalWrite(FLOWER_LIGHT_PIN, HIGH);
  211.   } else {
  212.     digitalWrite(FLOWER_LIGHT_PIN, LOW);
  213.   }
  214. }

lbernstone
Posts: 831
Joined: Mon Jul 22, 2019 3:20 pm

Re: need help why my code

Postby lbernstone » Mon Nov 20, 2023 4:37 pm

Turn on verbose logging in the Tools menu and upload your code again.
If you don't see any information on the serial monitor when you try to connect, then your issue is outside of the esp32. Check for firewalls or restrictions on your AP that would prevent a route there from your PC. This can also be due to incorrect network configuration.
You can test access to the webserver by pinging it.

sivar2311
Posts: 3
Joined: Sun Jun 12, 2022 3:48 pm

Re: need help why my code

Postby sivar2311 » Tue Nov 21, 2023 2:30 pm

While i was rebuilding the circiut in Woki i noticed that you're using GPIOs 6 and 7.

These GPIO's are connected to the SPI FLASH and can not be used!
(see https://randomnerdtutorials.com/esp32-p ... nce-gpios/)
I'm wondering that your Devkit exposes these GPIOs !?

You never called Serial.begin().
I wonder how you can see an output on the serial monitor?

You also should rework the main loop() function.

Every if / else statement lead's to a digitalWrite!
You should check for a state change and only if the state has changed do a digitalWrite.

al3xrud3r
Posts: 2
Joined: Mon Nov 20, 2023 6:31 am

Re: need help why my code

Postby al3xrud3r » Thu Nov 23, 2023 9:18 pm

i have changed the pin out to
  1. #define DHTPIN 2             // Pin connected to the DHT11 sensor
  2. #define DHTTYPE DHT11        // DHT sensor type
  3. #define FAN_PIN 27           // Pin connected to the relay module for fan/vent control
  4. #define SWAMP_COOLER_PIN 26  // Pin connected to the relay module for swamp cooler control
  5. #define VEG_LIGHT_PIN 25     // Pin connected to the relay module for veg light control
  6. #define FLOWER_LIGHT_PIN 33  // Pin connected to the relay module for flower light control
  7. #define LIGHT_SENSOR_PIN 36  // Pin connected to the light sensor (LDR)

sivar2311
Posts: 3
Joined: Sun Jun 12, 2022 3:48 pm

Re: need help why my code

Postby sivar2311 » Sat Nov 25, 2023 8:03 am

I have a few questions so that I can better understand your project.
At the moment it seems to me that only a time control is implemented.

- What is the external RTC module used for?
If the ESP32 is connected to the Internet, it can get the time from an SNTP server. This makes the use of an external RTC module obsolete.
- What is the DHT module used for?
Is it to control the fan as soon as a certain temperature is exceeded?
- What is the LDR needed for?
Should it only control the lights when the temperature falls below a certain threshold?

Can you describe in detail what exactly your project should be able to do?

Who is online

Users browsing this forum: Nicola31 and 43 guests