#include <Wire.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include <DHT.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h> // Include the LCD library
// WiFi and MQTT Configuration
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = ""; // WiFi Password
const char* mqtt_server = "broker.emqx.io"; // MQTT Broker address
WiFiClient espClient;
PubSubClient client(espClient);
// Sensor Pin Definitions
#define DHTPIN 5 // DHT22 data pin
#define ONE_WIRE_BUS 4 // DS18B20 data pin
#define LDR_PIN 34 // LDR (analog) pin
// Sensor Setup
DHT dht(DHTPIN, DHT22); // DHT22 sensor
OneWire oneWire(ONE_WIRE_BUS); // DS18B20
DallasTemperature sensors(&oneWire);
// LCD Setup (Assuming you are using an I2C LCD with address 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2); // 16 columns, 2 rows LCD
// Time variables for reducing print redundancy
unsigned long lastPrintTime = 0;
const long printInterval = 5000; // Interval to print data (in milliseconds)
// Function to Connect to WiFi
void setup_wifi() {
if (WiFi.status() == WL_CONNECTED) return; // Skip if already connected
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected");
}
// Function to Maintain MQTT Connection
void reconnect() {
if (client.connected()) return; // Skip if already connected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Greenhouse")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
// Function to Read Sensors
float readTemperature() {
sensors.requestTemperatures(); // Request temperature reading
return sensors.getTempCByIndex(0); // Read from first DS18B20 sensor
}
float readHumidity() {
return dht.readHumidity(); // Read humidity from DHT22
}
float readLightPercentage() {
int rawLight = analogRead(LDR_PIN); // Read raw LDR value (0-4095)
return (1.0 - (rawLight / 4095.0)) * 100.0; // Invert the reading (max light = 100%)
}
void setup() {
Serial.begin(115200);
setup_wifi(); // Establish WiFi connection
client.setServer(mqtt_server, 1883); // Set up MQTT server
// Initialize Sensors
dht.begin();
sensors.begin();
// Initialize LCD
lcd.init();
lcd.backlight(); // Turn on backlight
lcd.setCursor(0, 0);
lcd.print("Greenhouse Data");
Serial.println("Setup complete");
}
void loop() {
unsigned long currentTime = millis();
if (currentTime - lastPrintTime >= printInterval) {
// Read sensor data
float temperature = readTemperature();
float humidity = readHumidity();
float lightPercentage = readLightPercentage();
// Prepare JSON payload for MQTT
String payload = String("{") +
"\"temperature\":" + String(temperature, 1) + "," +
"\"humidity\":" + String(humidity, 1) + "," +
"\"light\":" + String(lightPercentage, 1) +
"}";
// Publish data to MQTT topic
client.publish("greenhouse/sensors", payload.c_str());
Serial.println("Published: " + payload);
// Display data on LCD
lcd.clear(); // Clear previous display
lcd.setCursor(0, 0);
lcd.print("Temp: " + String(temperature, 1) + "C");
lcd.setCursor(0, 1);
lcd.print("Hum: " + String(humidity, 1) + "%");
lastPrintTime = currentTime; // Update the last print time
}
if (!client.connected()) {
reconnect(); // Ensure MQTT connection
}
client.loop(); // Keep MQTT connection alive
delay(100); // Small delay to prevent overloading
}