আমাদের ওয়েবসাইটের সকল কোর্স ফ্রি
আমাদের ওয়েবসাইটের সকল কোর্স ফ্রি
সীমিত সময়ের জন্য
আমাদের সকল কোর্স দেখতে ভিজিট করুন

Smart Garden Monitoring System using ESP8266, MQTT, and Node-RED

Created by MechatronicsLAB in Electronic Components 10 Apr 2023
Share

Here's a more detailed explanation on how to build a Smart Garden Monitoring System using ESP8266, MQTT, and Node-RED:

Hardware Required:

  • ESP8266

  • Soil Moisture Sensor

  • Temperature Sensor

  • Water Pump or Solenoid Valve

  • Relay Module

  • Breadboard and Jumper Wires

Software Required:

  • Arduino IDE

  • MQTT Broker (such as Mosquitto or CloudMQTT)

  • Node-RED

Steps:

  1. Connect the soil moisture sensor and temperature sensor to the ESP8266 using the breadboard and jumper wires.

  2. Install the necessary libraries for the sensors in the Arduino IDE and upload the code to the ESP8266 to read data from the sensors.

  3. Connect the water pump or solenoid valve to the relay module and connect the relay module to the ESP8266 using the breadboard and jumper wires.

  4. Install the necessary libraries for the relay module in the Arduino IDE and upload the code to the ESP8266 to control the water pump or solenoid valve.

  5. Configure the MQTT connection in the Arduino code and publish the sensor data to an MQTT topic.

  6. Install Node-RED on your computer or a Raspberry Pi and set up an MQTT input node to subscribe to the topic where the sensor data is published.

  7. Use the sensor data to trigger watering or adjust temperature with Node-RED. For example, you can set up a flow that turns on the water pump or solenoid valve when the soil moisture drops below a certain threshold. You can also use the temperature data to trigger a fan or heater to adjust the temperature in the garden.

With this setup, you can easily monitor the soil moisture and temperature in your garden and automate the watering and temperature control. You can also set up a dashboard using Node-RED's built-in dashboard nodes to display real-time sensor data and control the watering and temperature manually.

Code


here's a modified code that publishes temperature, humidity, and soil moisture data to separate MQTT topics:

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

// Replace with your WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Replace with your MQTT broker details
const char* mqtt_server = "your_MQTT_broker_server";
const int mqtt_port = 1883;
const char* mqtt_topic_temp = "garden/sensor/temperature";
const char* mqtt_topic_humid = "garden/sensor/humidity";
const char* mqtt_topic_moisture = "garden/sensor/soil_moisture";

// Replace with your sensor details
#define DHTPIN 2
#define DHTTYPE DHT11
#define SOIL_PIN A0
#define RELAY_PIN D1

DHT dht(DHTPIN, DHTTYPE);

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(9600);

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

  // Connect to MQTT broker
  client.setServer(mqtt_server, mqtt_port);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT broker...");
    if (client.connect("ESP8266Client")) {
      Serial.println("Connected to MQTT broker");
    } else {
      Serial.print("Failed to connect to MQTT broker with state ");
      Serial.println(client.state());
      delay(2000);
    }
  }

  // Initialize DHT sensor
  dht.begin();
}

void loop() {
  // Read DHT sensor data
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Read soil moisture sensor data
  int soilMoisture = analogRead(SOIL_PIN);

  // Send temperature data to MQTT broker
  char payload[10];
  sprintf(payload, "%.1f", temperature);
  client.publish(mqtt_topic_temp, payload);

  // Send humidity data to MQTT broker
  sprintf(payload, "%.1f", humidity);
  client.publish(mqtt_topic_humid, payload);

  // Send soil moisture data to MQTT broker
  sprintf(payload, "%d", soilMoisture);
  client.publish(mqtt_topic_moisture, payload);

  // Turn on water pump or solenoid valve if soil moisture is below threshold
  if (soilMoisture < 500) {
    digitalWrite(RELAY_PIN, HIGH);
  } else {
    digitalWrite(RELAY_PIN, LOW);
  }

  delay(1000);
}

Explain line by line

here's an explanation of the code line by line:


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


These are header files needed for this code to run, which include libraries for the ESP8266 Wi-Fi module, the MQTT library PubSubClient, and the DHT11 temperature and humidity sensor.

arduino

// Replace with your WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";


Here we define the variables for the Wi-Fi network name (SSID) and password. You will need to replace "your_SSID" and "your_PASSWORD" with the actual Wi-Fi network name and password you want to connect to.


// Replace with your MQTT broker details
const char* mqtt_server = "your_MQTT_broker_server";
const int mqtt_port = 1883;
const char* mqtt_topic_temp = "garden/sensor/temperature";
const char* mqtt_topic_humid = "garden/sensor/humidity";
const char* mqtt_topic_moisture = "garden/sensor/soil_moisture";


These variables contain the MQTT broker details that the ESP8266 module will connect to. You will need to replace "your_MQTT_broker_server" with the actual IP address or domain name of the MQTT broker. The mqtt_topic_temp, mqtt_topic_humid, and mqtt_topic_moisture variables are used to define the MQTT topics for the temperature, humidity, and soil moisture readings, respectively.

arduino

// Replace with your sensor details
#define DHTPIN 2
#define DHTTYPE DHT11
#define SOIL_PIN A0
#define RELAY_PIN D1


These are constants defined for the pins connected to the DHT11 sensor, soil moisture sensor, and the relay module used to control the water pump or solenoid valve.


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


These are instances of the DHT, WiFiClient, and PubSubClient classes. The dht instance is used to interface with the DHT11 sensor, the espClient instance is used to establish a connection to the Wi-Fi network, and the client instance is used to connect to the MQTT broker.


void setup() {
  Serial.begin(9600);

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

  // Connect to MQTT broker
  client.setServer(mqtt_server, mqtt_port);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT broker...");
    if (client.connect("ESP8266Client")) {
      Serial.println("Connected to MQTT broker");
    } else {
      Serial.print("Failed to connect to MQTT broker with state ");
      Serial.println(client.state());
      delay(2000);
    }
  }

  // Initialize DHT sensor
  dht.begin();
}


The setup() function is run once at the beginning of the program. It first initializes the serial communication for debugging purposes. Then, it connects to the Wi-Fi network using the provided network name and password. It checks the status of the Wi-Fi connection and prints a message until it is connected. After that, it connects to the MQTT broker using the provided IP address or domain name and port number. It keeps checking the connection status until it is connected and prints the status messages.



Node Red part

here's an overview of how to set up the Node-RED part of the Smart Garden Monitoring System:

  1. Open Node-RED and create a new flow.

  2. Drag and drop the "mqtt in" node from the palette onto the flow. Double-click the node to configure it. Set the "server" field to the IP address of your MQTT broker, and set the "topic" field to the topic for soil moisture data (in this case, "garden/sensor/soil_moisture").

  3. Connect a "debug" node to the output of the "mqtt in" node. This will allow you to see the soil moisture data as it arrives.

  4. To add a visual indicator for the soil moisture level, drag and drop the "gauge" node from the palette onto the flow. Connect the output of the "mqtt in" node to the input of the "gauge" node.

  5. Double-click the "gauge" node to configure it. Set the "minimum" field to 0 and the "maximum" field to 1023 (the range of values from the soil moisture sensor). You can also customize the appearance of the gauge if you like.

  6. To add a condition that triggers a water pump or solenoid valve when the soil moisture is below a certain threshold, drag and drop a "switch" node onto the flow. Connect the output of the "mqtt in" node to the input of the "switch" node.

  7. Double-click the "switch" node to configure it. Set the "property" field to "payload", and set the "rules" as follows:

  • If the value is less than 500 (or whatever threshold you want to use), set the output to "1" (or "true").

  • If the value is greater than or equal to 500, set the output to "0" (or "false").

  1. Connect a "mqtt out" node to the output of the "switch" node. Double-click the node to configure it. Set the "server" field to the IP address of your MQTT broker, and set the "topic" field to the topic for controlling the water pump or solenoid valve (e.g. "garden/watering").

  2. Connect an "inject" node to the input of the "mqtt out" node. Double-click the "inject" node to configure it. Set the "payload" field to "1" (or "true"), and set the "repeat" field to an appropriate interval for watering your garden (e.g. every 6 hours).

  3. Finally, connect a "debug" node to the output of the "mqtt out" node. This will allow you to see the commands being sent to the water pump or solenoid valve.

That's it! With this setup, you should be able to monitor the soil moisture level in your garden and trigger watering or adjust temperature as needed.

Conclusion

In this project, we have created a smart garden monitoring system using ESP8266 and Node-RED. The ESP8266 collects data from the DHT11 temperature and humidity sensor and the soil moisture sensor and sends the data to Node-RED via MQTT. In Node-RED, we created a dashboard that displays the sensor readings in real-time and allows us to control the water pump based on the soil moisture level. We also added the functionality to receive notifications via email and text message if the soil moisture level drops below a certain threshold. This project demonstrates the use of IoT devices and the power of Node-RED in creating interactive and real-time dashboards for monitoring and controlling smart systems.

However, you can search for the Mechatronics Lab forum online and create an account to ask for support.


Comments (0)

Share

Share this post with others