Need help solving multiple errors for garden control code with ESP8622

Hi all was wondering if you could help me with these errors?











/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In function 'void setup()':
Garden_2:86:3: error: 'dht' was not declared in this scope
   86 |   dht.begin();
      |   ^~~
Garden_2:90:3: error: 'rtc' was not declared in this scope
   90 |   rtc.begin();
      |   ^~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In function 'void loop()':
Garden_2:114:23: error: 'dht' was not declared in this scope
  114 |   float temperature = dht.readTemperature();
      |                       ^~~
Garden_2:201:21: error: 'moistureLow' was not declared in this scope
  201 |   if (rain == 1 || !moistureLow) {
      |                     ^~~~~~~~~~~
Garden_2:208:23: error: 'dht' was not declared in this scope
  208 |   float temperature = dht.readTemperature();
      |                       ^~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In lambda function:
Garden_2:214:47: error: 'temperature' is not captured
  214 |     String message = "Temperature: " + String(temperature) + " C\n" +
      |                                               ^~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:212:19: note: the lambda has no capture-default
  212 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:208:9: note: 'float temperature' declared here
  208 |   float temperature = dht.readTemperature();
      |         ^~~~~~~~~~~
Garden_2:215:44: error: 'humidity' is not captured
  215 |                      "Humidity: " + String(humidity) + " %";
      |                                            ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:212:19: note: the lambda has no capture-default
  212 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:209:9: note: 'float humidity' declared here
  209 |   float humidity = dht.readHumidity();
      |         ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In function 'void loop()':
Garden_2:243:7: error: 'rain' was not declared in this scope
  243 |   if (rain == 1 || !moistureLow) {
      |       ^~~~
Garden_2:243:21: error: 'moistureLow' was not declared in this scope
  243 |   if (rain == 1 || !moistureLow) {
      |                     ^~~~~~~~~~~
Garden_2:251:23: error: 'dht' was not declared in this scope
  251 |   float temperature = dht.readTemperature();
      |                       ^~~
Garden_2:256:18: error: 'rtc' was not declared in this scope
  256 |   DateTime now = rtc.now();
      |                  ^~~
Garden_2:257:49: error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'
  257 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
      |                                  |                |
      |                                  const char*      const char [2]
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In lambda function:
Garden_2:263:47: error: 'temperature' was not declared in this scope
  263 |     String message = "Temperature: " + String(temperature) + " C\n" +
      |                                               ^~~~~~~~~~~
Garden_2:264:44: error: 'humidity' was not declared in this scope
  264 |                      "Humidity: " + String(humidity) + " %\n" +
      |                                            ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In function 'void loop()':
Garden_2:299:23: error: 'dht' was not declared in this scope
  299 |   float temperature = dht.readTemperature();
      |                       ^~~
Garden_2:303:18: error: 'rtc' was not declared in this scope
  303 |   DateTime now = rtc.now();
      |                  ^~~
Garden_2:304:49: error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'
  304 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
      |                                  |                |
      |                                  const char*      const char [2]
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In lambda function:
Garden_2:309:47: error: 'temperature' is not captured
  309 |     String message = "Temperature: " + String(temperature) + " C\n" +
      |                                               ^~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:307:19: note: the lambda has no capture-default
  307 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:299:9: note: 'float temperature' declared here
  299 |   float temperature = dht.readTemperature();
      |         ^~~~~~~~~~~
Garden_2:310:44: error: 'humidity' is not captured
  310 |                      "Humidity: " + String(humidity) + " %\n" +
      |                                            ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:307:19: note: the lambda has no capture-default
  307 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:300:9: note: 'float humidity' declared here
  300 |   float humidity = dht.readHumidity();
      |         ^~~~~~~~
Garden_2:311:33: error: 'time' is not captured
  311 |                      "Time: " + time;
      |                                 ^~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:307:19: note: the lambda has no capture-default
  307 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:304:10: note: 'String time' declared here
  304 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |          ^~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In lambda function:
Garden_2:330:45: error: 'temperature' is not captured
  330 |   String message = "Temperature: " + String(temperature) + " C\n" +
      |                                             ^~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:328:21: note: the lambda has no capture-default
  328 | server.on("/data", []() {
      |                     ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:299:9: note: 'float temperature' declared here
  299 |   float temperature = dht.readTemperature();
      |         ^~~~~~~~~~~
Garden_2:331:42: error: 'humidity' is not captured
  331 |                    "Humidity: " + String(humidity) + " %\n" +
      |                                          ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:328:21: note: the lambda has no capture-default
  328 | server.on("/data", []() {
      |                     ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:300:9: note: 'float humidity' declared here
  300 |   float humidity = dht.readHumidity();
      |         ^~~~~~~~
Garden_2:332:31: error: 'time' is not captured
  332 |                    "Time: " + time;
      |                               ^~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:328:21: note: the lambda has no capture-default
  328 | server.on("/data", []() {
      |                     ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:304:10: note: 'String time' declared here
  304 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |          ^~~~
Garden_2:334:67: error: 'moistureLevels' is not captured
  334 |     message += "\nMoisture sensor " + String(i+1) + ": " + String(moistureLevels[i]);
      |                                                                   ^~~~~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:328:21: note: the lambda has no capture-default
  328 | server.on("/data", []() {
      |                     ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:274:7: note: 'int moistureLevels [4]' declared here
  274 |   int moistureLevels[numSensors];
      |       ^~~~~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In function 'void loop()':
Garden_2:372:21: error: 'moistureLow' was not declared in this scope
  372 |   if (rain == 1 || !moistureLow) {
      |                     ^~~~~~~~~~~
Garden_2:379:23: error: 'dht' was not declared in this scope
  379 |   float temperature = dht.readTemperature();
      |                       ^~~
Garden_2:383:18: error: 'rtc' was not declared in this scope
  383 |   DateTime now = rtc.now();
      |                  ^~~
Garden_2:384:49: error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'
  384 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
      |                                  |                |
      |                                  const char*      const char [2]
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In lambda function:
Garden_2:389:47: error: 'temperature' is not captured
  389 |     String message = "Temperature: " + String(temperature) + " C\n" +
      |                                               ^~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:387:19: note: the lambda has no capture-default
  387 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:379:9: note: 'float temperature' declared here
  379 |   float temperature = dht.readTemperature();
      |         ^~~~~~~~~~~
Garden_2:390:44: error: 'humidity' is not captured
  390 |                      "Humidity: " + String(humidity) + " %\n" +
      |                                            ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:387:19: note: the lambda has no capture-default
  387 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:380:9: note: 'float humidity' declared here
  380 |   float humidity = dht.readHumidity();
      |         ^~~~~~~~
Garden_2:391:33: error: 'time' is not captured
  391 |                      "Time: " + time;
      |                                 ^~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:387:19: note: the lambda has no capture-default
  387 |   server.on("/", []() {
      |                   ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:384:10: note: 'String time' declared here
  384 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |          ^~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In lambda function:
Garden_2:409:47: error: 'temperature' is not captured
  409 |     String message = "Temperature: " + String(temperature) + " C\n" +
      |                                               ^~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:407:23: note: the lambda has no capture-default
  407 |   server.on("/data", []() {
      |                       ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:379:9: note: 'float temperature' declared here
  379 |   float temperature = dht.readTemperature();
      |         ^~~~~~~~~~~
Garden_2:410:44: error: 'humidity' is not captured
  410 |                      "Humidity: " + String(humidity) + " %\n" +
      |                                            ^~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:407:23: note: the lambda has no capture-default
  407 |   server.on("/data", []() {
      |                       ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:380:9: note: 'float humidity' declared here
  380 |   float humidity = dht.readHumidity();
      |         ^~~~~~~~
Garden_2:411:33: error: 'time' is not captured
  411 |                      "Time: " + time;
      |                                 ^~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:407:23: note: the lambda has no capture-default
  407 |   server.on("/data", []() {
      |                       ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:384:10: note: 'String time' declared here
  384 |   String time = now.hour() + ":" + now.minute() + ":" + now.second();
      |          ^~~~
Garden_2:413:69: error: 'moistureLevels' was not declared in this scope
  413 |       message += "\nMoisture sensor " + String(i+1) + ": " + String(moistureLevels[i]);
      |                                                                     ^~~~~~~~~~~~~~
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino: In function 'void loop()':
Garden_2:417:1: error: expected '}' at end of input
  417 | }
      | ^
/Volumes/Ardunio_Projects/Garden_2/Garden_2.ino:101:13: note: to match this '{'
  101 | void loop() {
      |             ^
Multiple libraries were found for "RTClib.h"
 Used: /Users/darksplat/Documents/Arduino/libraries/RTClib
 Not used: /Users/darksplat/Documents/Arduino/libraries/RTCLib_by_NeiroN
exit status 1
'dht' was not declared in this scope
/*
 * Here is a sketch outline for an ESP8622 with 4 moisture sensors, 1 DHT sensor, 1 rain sensor, 
 * 1 RTC module (DS3231), and a relay that can turn on and off a water valve:
 * Initialize the ESP8622 and set up the WiFi connection.
 * Initialize the DHT sensor and RTC module.
 * Initialize the 4 moisture sensors and the rain sensor.
 * Set up a server on the ESP8622 to allow access to the device through a web interface.
 * In the main loop of the sketch, continually read the values from the moisture sensors, DHT sensor, and rain sensor.
 * If the moisture level of any of the sensors falls below a certain threshold, turn on the relay to open the water valve.
 * If the rain sensor detects rain, turn off the relay to close the water valve.
 * Use the DHT sensor to read the temperature and humidity and display these values on the web interface.
 * Use the RTC module to display the current time on the web interface.
 * Use the web interface to manually turn the relay on or off and to access the data from the sensors and RTC
 * Use the web interface to access data from www.bom.gov.au to retrieve the current weather conditions in the local area and display this information on the web interface.
 * This code sets up the server to handle incoming client requests by defining three different server routes: /, /on, and /off.
 * The / route is accessed when the client sends a request to the root URL of the server. When this happens, the server will send back a message containing the current temperature, humidity, and time values.
 * The /on and /off routes are accessed when the client sends a request to the /on or `/
 */

#define A0 0
#define A1 14
#define A2 15
#define A3 16
#define DHTPIN 2  // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22  // DHT 22 (AM2302)

// Initialize the ESP8622 and set up the WiFi connection:

#include <ESP8266WiFi.h>

// Initialize the DHT sensor and RTC module:

#include <DHT.h>
#include <Wire.h>
#include <RTClib.h>

  // Set up a server on the ESP8622 to allow access to the device through a web interface:

#include <ESP8266WebServer.h>

const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

// Create an instance of the server
// specify the port to listen on as an argument
ESP8266WebServer server(80);

// Initialize the 4 moisture sensors and the rain sensor:

const int numSensors = 4;  // Number of moisture sensors
const int sensors[] = {A0, A1, A2, A3};  // Analog pins connected to the sensors
const int rainPin = 4;  // Digital pin connected to the rain sensor

void setup() {

{
  // Initialize the ESP8622
  WiFi.begin(ssid, password);

  // Wait for the WiFi connection to be established
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
    
    // Set up the server
  server.begin();
  Serial.println("Server started");
  }
  {
  // Set up the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
  }
  {
// Set up the RTC module
RTC_DS3231 rtc;
  }
  {
  // Initialize the DHT sensor
  Serial.begin(9600);
  dht.begin();
  }
  {
  // Initialize the RTC module
  rtc.begin();
  }
  {
// Initialize the moisture sensors and rain sensor
  for (int i = 0; i < numSensors; i++) {
    pinMode(sensors[i], INPUT);
  }
  pinMode(rainPin, INPUT);
}
}

void loop() {
  // Check for incoming client requests
  server.handleClient();
{
  // In the main loop of the sketch, continually read the values from the moisture sensors, DHT sensor, and rain sensor:
  
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Read the temperature and humidity from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read the value from the rain sensor
  int rain = digitalRead(rainPin);
}

// If the moisture level of any of the sensors falls below a certain threshold, turn on the relay to open the water valve:

const int moistureThreshold = 700;  // Adjust this value based on the range of your sensors
const int relayPin = 5;  // Digital pin connected to the relay

{
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Check if the moisture level of any of the sensors is below the threshold
  bool moistureLow = false;
  for (int i = 0; i < numSensors; i++) {
    if (moistureLevels[i] < moistureThreshold) {
      moistureLow = true;
      break;
    }
  }

  // If the moisture is low, turn on the relay to open the water valve
  if (moistureLow) {
    digitalWrite(relayPin, HIGH);
  } else {
    digitalWrite(relayPin, LOW);
  }
}

// If the rain sensor detects rain, turn off the relay to close the water valve:

{
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Check if the moisture level of any of the sensors is below the threshold
  bool moistureLow = false;
  for (int i = 0; i < numSensors; i++) {
    if (moistureLevels[i] < moistureThreshold) {
      moistureLow = true;
      break;
    }
  }

  // Read the value from the rain sensor
  int rain = digitalRead(rainPin);

  // If it is raining or the moisture is not low, turn off the relay to close the water valve
  if (rain == 1 || !moistureLow) {
    digitalWrite(relayPin, LOW);
  } else {
    digitalWrite(relayPin, HIGH);
  }
}
// Use the DHT sensor to read the temperature and humidity and display these values on the web interface:

{
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Check if the moisture level of any of the sensors is below the threshold
  bool moistureLow = false;
  for (int i = 0; i < numSensors; i++) {
    if (moistureLevels[i] < moistureThreshold) {
      moistureLow = true;
      break;
    }
  }
}
{
  // Read the value from the rain sensor
  int rain = digitalRead(rainPin);

  // If it is raining or the moisture is not low, turn off the relay to close the water valve
  if (rain == 1 || !moistureLow) {
    digitalWrite(relayPin, LOW);
  } else {
    digitalWrite(relayPin, HIGH);
  }
}
{ // Read the temperature and humidity from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Set up the server to handle incoming client requests
  server.on("/", []() {
    // Send the temperature and humidity values to the client
    String message = "Temperature: " + String(temperature) + " C\n" +
                     "Humidity: " + String(humidity) + " %";
    server.send(200, "text/plain", message);
  });
}
// Use the RTC module to display the current time on the web interface:

{
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Check if the moisture level of any of the sensors is below the threshold
  bool moistureLow = false;
  for (int i = 0; i < numSensors; i++) {
    if (moistureLevels[i] < moistureThreshold) {
      moistureLow = true;
      break;
    }
  }
}
{
  // Read the value from the rain sensor
  int rain = digitalRead(rainPin);
}
{
  // If it is raining or the moisture is not low, turn off the relay to close the water valve
  if (rain == 1 || !moistureLow) {
    digitalWrite(relayPin, LOW);
  } else {
    digitalWrite(relayPin, HIGH);
  }
}
{
// Read the temperature and humidity from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
}
{
  // Read the current time from the RTC module
  DateTime now = rtc.now();
  String time = now.hour() + ":" + now.minute() + ":" + now.second();
}
{
  // Set up the server to handle incoming client requests
  server.on("/", []() {
    // Send the temperature, humidity, and time values to the client
    String message = "Temperature: " + String(temperature) + " C\n" +
                     "Humidity: " + String(humidity) + " %\n" +
                     "Time: " + time;
    server.send(200, "text/plain", message);
  });

}
// Use the web interface to manually turn the relay on or off and to access the data from the sensors and RTC:

{
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Check if the moisture level of any of the sensors is below the threshold
  bool moistureLow = false;
  for (int i = 0; i < numSensors; i++) {
    if (moistureLevels[i] < moistureThreshold) {
      moistureLow = true;
      break;
    }
  }

  // Read the value from the rain sensor
  int rain = digitalRead(rainPin);

  // If it is raining or the moisture is not low, turn off the relay to close the water valve
  if (rain == 1 || !moistureLow) {
    digitalWrite(relayPin, LOW);
  } else {
    digitalWrite(relayPin, HIGH);
  }

  // Read the temperature and humidity from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read the current time from the RTC module
  DateTime now = rtc.now();
  String time = now.hour() + ":" + now.minute() + ":" + now.second();

  // Set up the server to handle incoming client requests
  server.on("/", []() {
    // Send the temperature, humidity, and time values to the client
    String message = "Temperature: " + String(temperature) + " C\n" +
                     "Humidity: " + String(humidity) + " %\n" +
                     "Time: " + time;
    server.send(200, "text/plain", message);
  });

  server.on("/on", []() {

  // Turn on the relay
  digitalWrite(relayPin, HIGH);
  server.send(200, "text/plain", "Relay turned on");
});

server.on("/off", []() {
  // Turn off the relay
  digitalWrite(relayPin, LOW);
  server.send(200, "text/plain", "Relay turned off");
});

server.on("/data", []() {
  // Send the temperature, humidity, and time values to the client
  String message = "Temperature: " + String(temperature) + " C\n" +
                   "Humidity: " + String(humidity) + " %\n" +
                   "Time: " + time;
  for (int i = 0; i < numSensors; i++) {
    message += "\nMoisture sensor " + String(i+1) + ": " + String(moistureLevels[i]);
  }
  server.send(200, "text/plain", message);
});

}

// Use the web interface to access data from www.bom.gov.au to retrieve the current weather conditions in the local area and display this information on the web interface.
{
server.begin();

Serial.println("Server started");

// Set up the connection to www.bom.gov.au
const char* serverName = "www.bom.gov.au";
WiFiClient client;
}
{
  // Read the values from the moisture sensors
  int moistureLevels[numSensors];
  for (int i = 0; i < numSensors; i++) {
    moistureLevels[i] = analogRead(sensors[i]);
  }

  // Check if the moisture level of any of the sensors is below the threshold
  bool moistureLow = false;
  for (int i = 0; i < numSensors; i++) {
    if (moistureLevels[i] < moistureThreshold) {
      moistureLow = true;
      break;
    }
  }
}
{ 
  // Read the value from the rain sensor
  int rain = digitalRead(rainPin);

  // If it is raining or the moisture is not low, turn off the relay to close the water valve
  if (rain == 1 || !moistureLow) {
    digitalWrite(relayPin, LOW);
  } else {
    digitalWrite(relayPin, HIGH);
  }
}
// Read the temperature and humidity from the DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read the current time from the RTC module
  DateTime now = rtc.now();
  String time = now.hour() + ":" + now.minute() + ":" + now.second();
{
  // Set up the server to handle incoming client requests
  server.on("/", []() {
    // Send the temperature, humidity, and time values to the client
    String message = "Temperature: " + String(temperature) + " C\n" +
                     "Humidity: " + String(humidity) + " %\n" +
                     "Time: " + time;
    server.send(200, "text/plain", message);
  });

  server.on("/on", []() {
    // Turn on the relay
    digitalWrite(relayPin, HIGH);
    server.send(200, "text/plain", "Relay turned on");
  });

  server.on("/off", []() {
    // Turn off the relay
    digitalWrite(relayPin, LOW);
    server.send(200, "text/plain", "Relay turned off");
  });

  server.on("/data", []() {
    // Send the temperature, humidity, and time values to the client
    String message = "Temperature: " + String(temperature) + " C\n" +
                     "Humidity: " + String(humidity) + " %\n" +
                     "Time: " + time;
    for (int i = 0; i < numSensors; i++) {
      message += "\nMoisture sensor " + String(i+1) + ": " + String(moistureLevels[i]);
    }
    server.send(200, "text/plain", message);
  });
}

Cheers

Start with declaring the objects. Most of the errors where gone.

Why {} around?

Must BEFORE setup()

Only

is correct declared.

Look at the examples from each library to make it correct.

look at each error message in turn and fix the obvious errors - sometimes an error will generate several error messages
in setup() you have far too many { and }, try

void setup() {
  // Initialize the ESP8622
  WiFi.begin(ssid, password);

  // Wait for the WiFi connection to be established
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
    
    // Set up the server
  server.begin();
  Serial.println("Server started");
  // Set up the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
 // Set up the RTC module
RTC_DS3231 rtc;
  // Initialize the DHT sensor
  Serial.begin(9600);
  dht.begin();
 // Initialize the RTC module
  rtc.begin();
// Initialize the moisture sensors and rain sensor
  for (int i = 0; i < numSensors; i++) {
    pinMode(sensors[i], INPUT);
  }
  pinMode(rainPin, INPUT);
}

But then you'll run into scope issues for "dht" and "RTC".
Best move these out of setup.

1 Like

Hi, @Arduino_Noob23
Did you write this code?

Did you write this code in stages?

If not, TIP;
Start code with JUST the DHT working.
Then JUST the RTC working.
Then JUST the moisture sensor working.
Then JUST the WiFi working.

Then begin to combine them one at a time, getting the combination to work before adding the next.

Why, because you will not get the list of errors you have now.

Back to basics to do some constructive troubleshooting.

Tom.. :smiley: :+1: :coffee: :australia:

1 Like

Your indentation is all wrong. Indentation does not affect the way code works at all, but it does help us humans to spot errors in the structure of code. It's very easy to get your indentation correct, just click Tools->Auto Format. Get into the habit of doing that regularly and especially before you post your code on the forum.

Once your indentation is correct, errors in the structure of your code become easy to spot. If you see this anywhere

}
}

(two closing brackets vertically aligned) you know you have an error in the structure somewhere above that.

Another tip:

  }
  {

the above is almost 100% certain to indicate a problem with the structure of your code. It will not necessarily generate an error, but it is not something you would ever see in normal, correctly written code.

I noticed something else strange about your code. The code cannot start running if it cannot connect to WiFi. It will get stuck in setup() if it cannot connect, and so loop() will never run.

But that is quite normal for esp code. So what strange thing did I notice? Generally speaking, a WiFi connection is there to provide internet access. Is that true in your case? If so, you do not need an RTC in your project! The esp can get the time from a network time server and maintain the time using it's internal clock. To keep accurate, it can re-synchronise with the network time server once per day, for example. That will be more than accurate enough for gardening purposes.

Hi thanks for the feedback I have modified the code and have got it to compile with these features

  1. Using a ESP8622
  2. DHT
  3. Relay
  4. Moisture sensors
  5. Rain Sensor
  6. Use internet time instead of a RTC (Thanks @PaulRB)
  7. Connect to the BOM

Just need help with a webpage interface for these things

  1. Manually control the relay
  2. Show the BOM information for around my area (Bendigo, VIC, Australia)
  3. Show the time
  4. Show the DHT Temperature and Humidity every 30-60 mins
  5. end goal is to have home assistant integration as well when I get that setup when RPI4 are more available.

Can you guys help me? I have been using the new openAI for a little help but it only posts so far it has limited character responses.


#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <DHT.h>
#include <ArduinoJson.h>

// Replace with your network credentials
const char* ssid     = "your-ssid";
const char* password = "your-password";

// Replace with your DHT sensor pin and type
#define DHT_PIN 2
#define DHT_TYPE DHT11

// Replace with your relay pin
#define RELAY_PIN 5

// Replace with your moisture sensor pins
#define MOISTURE_SENSOR_1_PIN 0
#define MOISTURE_SENSOR_2_PIN 1
#define MOISTURE_SENSOR_3_PIN 2
#define MOISTURE_SENSOR_4_PIN 3

// Replace with your rain sensor pin
#define RAIN_SENSOR_PIN 4

// Create DHT sensor object
DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println("Connected to the WiFi network");

  // Sync clock to internet
  configTime(0, 0, "pool.ntp.org", "time.nist.gov");
  Serial.println("Waiting for time sync...");
  while (time(nullptr) < 1500000000) {
    delay(500);
  }
  Serial.println("Time synced");

  // Initialize DHT sensor
  dht.begin();

  // Set relay pin as output
  pinMode(RELAY_PIN, OUTPUT);
  
  // Set moisture sensor pins as input
pinMode(MOISTURE_SENSOR_1_PIN, INPUT);
pinMode(MOISTURE_SENSOR_2_PIN, INPUT);
pinMode(MOISTURE_SENSOR_3_PIN, INPUT);
pinMode(MOISTURE_SENSOR_4_PIN, INPUT);

// Set rain sensor pin as input
pinMode(RAIN_SENSOR_PIN, INPUT);
}

void loop() {
  // Read moisture sensor values
  int moistureSensor1Value = analogRead(MOISTURE_SENSOR_1_PIN);
  int moistureSensor2Value = analogRead(MOISTURE_SENSOR_2_PIN);
  int moistureSensor3Value = analogRead(MOISTURE_SENSOR_3_PIN);
  int moistureSensor4Value = analogRead(MOISTURE_SENSOR_4_PIN);

  // Calculate average moisture sensor value
  int moistureSensorValue = (moistureSensor1Value + moistureSensor2Value + moistureSensor3Value + moistureSensor4Value) / 4;

  // Read rain sensor value
  int rainSensorValue = digitalRead(RAIN_SENSOR_PIN);

  // Check if it's raining
  bool isRaining = rainSensorValue == LOW;

  // Send HTTP GET request to www.bom.gov.au
  WiFiClient client;
  HTTPClient http;
  http.begin(client, "http://www.bom.gov.au/fwo/IDV60901/IDV60901.94767.json");
  int httpCode = http.GET();

  // Check for success
  if (httpCode > 0) {
    // Parse response
    String payload = http.getString();
    DynamicJsonDocument doc(1024);
    DeserializationError error = deserializeJson(doc, payload);

    if (error) {
      Serial.println("Error parsing JSON");
    } 
    else {
      
      // Extract temperature and humidity from response
      float temperature = doc["temperature"];
      float humidity = doc["humidity"];

      // Get current time
      time_t now = time(nullptr);
      struct tm* timeinfo = localtime(&now);

      // Get current hour
      int currentHour = timeinfo->tm_hour;

      // Check if it's currently nighttime
      bool isNighttime = currentHour < 6 || currentHour >= 18;

      // Check if soil is dry and it's not raining or nighttime
      if (moistureSensorValue < 500 && !isRaining && !isNighttime) {
        // Turn on water valve
        digitalWrite(RELAY_PIN, HIGH);

        // Wait for 15 minutes
        delay(900000);

        // Turn off water valve
        digitalWrite(RELAY_PIN, LOW);
      }
    }
  } else {
    Serial.println("Error sending HTTP GET request");
  }

  // Wait for 1 minute before checking again
  delay(60000);
}

Cheers

Hi, @Arduino_Noob23

Before you do this;

If your code compiles, does it run and do everything it is supposed to?
If not then at this point do not go any further until it is.

Did you read the advice in post #5, did you develop your code in stages or have you copy pasted blocks of code?

Do you have the hardware ready to build your prototype?

Tom... :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.