Plant Monitoring System with Soil Moisture and Light Sensors via MQTT Communication
11.12.2024 - Engine: Gemini

Setting Up a Plant Monitoring System with Soil Moisture and Light Sensors Using MQTT Communication
Components
- Microcontroller (e.g., Arduino Uno, ESP32)
- Soil moisture sensor (e.g., YL-69)
- Light sensor (e.g., BH1750)
- MQTT broker (e.g., Mosquitto)
- MQTT client (e.g., MQTT library for Arduino)
Steps
1. Hardware Connection:
- Connect the soil moisture sensor to the microcontroller's analog pins.
- Connect the light sensor to the microcontroller's I2C bus.
2. Software Configuration:
- Install the MQTT library on the microcontroller.
- Establish a connection to the MQTT broker.
- Define MQTT topics for publishing sensor readings.
3. Soil Sensor Measurement:
- Read the analog value from the soil moisture sensor.
- Convert the value to a soil moisture measurement.
4. Light Sensor Measurement:
- Read the lux value from the light sensor over I2C.
5. MQTT Publishing:
- Publish soil moisture and light sensor readings to MQTT topics with appropriate message payloads.
6. MQTT Subscription:
- Subscribe to the MQTT topics on another device or a dashboard to receive sensor readings in real time.
Sample Code:
// Arduino code for plant monitoring using MQTT
#include <MQTT.h>
const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "mqtt_server";
const int mqtt_port = 1883;
const char* mqtt_topic_moisture = "moisture";
const char* mqtt_topic_light = "light";
MQTTClient client(256);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
// Connect to the MQTT broker
client.begin(mqtt_server, mqtt_port, client);
// Subscribe to MQTT topics
client.subscribe(mqtt_topic_moisture);
client.subscribe(mqtt_topic_light);
}
void loop() {
// Read sensor values
int moisture_value = analogRead(A0);
float light_value = bh1750.readLightLevel();
// Publish the values
client.publish(mqtt_topic_moisture, String(moisture_value).c_str());
client.publish(mqtt_topic_light, String(light_value).c_str());
delay(1000);
}