#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
OneWire oneWire(13); // Connect the DH122 sensor (DS18B20) to GPIO 13
DallasTemperature DH122(&oneWire); // Rename the object for clarity
const int moistureSensorPin = A0; // Connect the potentiometer to analog pin A0
const int buzzerPin = 12; // Connect the buzzer to GPIO 12
const float tempThresholdHigh = 30.0; // Adjust as needed
const int moistureThresholdLow = 400; // Adjust as needed
void setup() {
pinMode(buzzerPin, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Use correct I2C address
display.display();
delay(2000);
display.clearDisplay();
DH122.begin(); // Initialize the DH122 sensor
}
void loop() {
float temperature = DH122.getTempCByIndex(0); // Read temperature from DH122
int moisture = analogRead(moistureSensorPin); // Read potentiometer value
if (temperature > tempThresholdHigh || moisture < moistureThresholdLow) {
digitalWrite(buzzerPin, HIGH); // Activate the buzzer for low moisture
} else {
digitalWrite(buzzerPin, LOW);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Temp: ");
display.print(temperature);
display.println(" C");
display.print("Moisture: ");
display.print(moisture);
display.display();
delay(1000); // Adjust as needed
}