Greenhouse Monitoring and Control System using IoT

This guide will help you build an IoT-based Greenhouse Monitoring and Control System. The system will monitor environmental conditions such as temperature, humidity, and soil moisture and control greenhouse equipment like fans, heaters, and irrigation systems through the Internet.

Components Required

  • Microcontroller or development board (e.g., Arduino, ESP8266, or ESP32)
  • Temperature and humidity sensor (e.g., DHT22)
  • Soil moisture sensor
  • Relay module (for controlling fans, heaters, and irrigation)
  • Fans, heaters, and irrigation pumps
  • Wi-Fi module (if not integrated into the microcontroller)
  • Power supply (e.g., 5V DC adapter or battery)
  • Cloud service account (e.g., ThingSpeak, Blynk, or AWS IoT)
  • Connecting wires and breadboard

Setting Up the Hardware

Follow these steps to set up the hardware:

  1. Connect the Sensors

    Connect the DHT22 sensor to measure temperature and humidity, and the soil moisture sensor to the microcontroller. Use appropriate pins for data, power, and ground connections.

  2. Connect the Relay Module

    Connect the relay module to the microcontroller. The relay will control the fans, heaters, and irrigation pumps. Connect the relay's control pins to the microcontroller and the high-voltage terminals to the appliances.

  3. Power the System

    Ensure that the microcontroller and all connected components are powered correctly. Use a stable power supply to avoid fluctuations.

Programming the Microcontroller

Write the code to read sensor data and control the greenhouse equipment based on the environmental conditions. Below is a basic outline of the code using Arduino:


                #include <DHT.h>
                #include <WiFi.h>
                #include <PubSubClient.h>

                #define DHTPIN 2
                #define DHTTYPE DHT22
                #define MOISTURE_PIN A0
                #define RELAY_FAN_PIN 4
                #define RELAY_HEATER_PIN 5
                #define RELAY_IRRIGATION_PIN 6

                DHT dht(DHTPIN, DHTTYPE);
                WiFiClient espClient;
                PubSubClient client(espClient);

                const char* ssid = "your_SSID";
                const char* password = "your_PASSWORD";
                const char* mqtt_server = "broker_address";
                const char* mqtt_topic = "greenhouse/control";

                void setup() {
                    Serial.begin(9600);
                    dht.begin();
                    
                    pinMode(RELAY_FAN_PIN, OUTPUT);
                    pinMode(RELAY_HEATER_PIN, OUTPUT);
                    pinMode(RELAY_IRRIGATION_PIN, OUTPUT);
                    
                    setup_wifi();
                    client.setServer(mqtt_server, 1883);
                }

                void setup_wifi() {
                    delay(10);
                    Serial.println();
                    Serial.print("Connecting to ");
                    Serial.print(ssid);
                    
                    WiFi.begin(ssid, password);
                    while (WiFi.status() != WL_CONNECTED) {
                        delay(500);
                        Serial.print(".");
                    }
                    Serial.println(" connected");
                }

                void reconnect() {
                    while (!client.connected()) {
                        Serial.print("Attempting MQTT connection...");
                        if (client.connect("GreenhouseClient")) {
                            Serial.println("connected");
                            client.subscribe(mqtt_topic);
                        } else {
                            Serial.print("failed, rc=");
                            Serial.print(client.state());
                            Serial.println(" try again in 5 seconds");
                            delay(5000);
                        }
                    }
                }

                void loop() {
                    if (!client.connected()) {
                        reconnect();
                    }
                    client.loop();
                    
                    float temperature = dht.readTemperature();
                    float humidity = dht.readHumidity();
                    int moisture = analogRead(MOISTURE_PIN);

                    if (temperature > 30) {
                        digitalWrite(RELAY_FAN_PIN, HIGH); // Turn on fan
                    } else {
                        digitalWrite(RELAY_FAN_PIN, LOW); // Turn off fan
                    }

                    if (humidity < 40) {
                        digitalWrite(RELAY_IRRIGATION_PIN, HIGH); // Turn on irrigation
                    } else {
                        digitalWrite(RELAY_IRRIGATION_PIN, LOW); // Turn off irrigation
                    }

                    if (moisture < 300) {
                        digitalWrite(RELAY_HEATER_PIN, HIGH); // Turn on heater
                    } else {
                        digitalWrite(RELAY_HEATER_PIN, LOW); // Turn off heater
                    }

                    String payload = "Temperature: " + String(temperature) + " C, Humidity: " + String(humidity) + " %, Moisture: " + String(moisture);
                    client.publish(mqtt_topic, payload.c_str());
                    
                    delay(10000); // Wait 10 seconds before the next loop
                }
            

This code connects to a Wi-Fi network, reads data from the sensors, and controls the greenhouse equipment based on the environmental conditions. The data is also published to an MQTT broker for remote monitoring.

Setting Up Cloud Integration

To visualize and analyze the data, integrate the system with a cloud service. For instance, you can use ThingSpeak, Blynk, or AWS IoT. Configure your cloud service to receive and display the data published by the microcontroller.

Testing and Calibration

Test the system by simulating different environmental conditions and verifying that the equipment responds correctly. Adjust the thresholds in the code as needed to ensure proper control and monitoring.

Conclusion

The Greenhouse Monitoring and Control System using IoT provides a comprehensive solution for managing environmental conditions in a greenhouse. By integrating sensors, microcontrollers, and cloud services, you can automate and monitor the greenhouse effectively, improving plant growth and resource management.