Here's a more detailed explanation on how to build a Smart Garden Monitoring System using ESP8266, MQTT, and Node-RED:
Soil Moisture Sensor
Water Pump or Solenoid Valve
Breadboard and Jumper Wires
MQTT Broker (such as Mosquitto or CloudMQTT)
Connect the soil moisture sensor and temperature sensor to the ESP8266 using the breadboard and jumper wires.
Install the necessary libraries for the sensors in the Arduino IDE and upload the code to the ESP8266 to read data from the sensors.
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.
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.
Configure the MQTT connection in the Arduino code and publish the sensor data to an MQTT topic.
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.
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.
here's a modified code that publishes temperature, humidity, and soil moisture data to separate MQTT topics:
here's an explanation of the code line by line:
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.
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.
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.
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.
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.
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:
Open Node-RED and create a new flow.
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").
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.
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.
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.
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.
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").
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").
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).
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.
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.