This document provides a detailed overview of a beehive monitoring system. The system uses an ESP32 microcontroller to interface with various sensors and modules, including a DHT22 temperature and humidity sensor, an HX711 weighing sensor module, an MQ135 air quality sensor, an SW-420 vibration sensor, and a buzzer module. The system is powered by a 18650 Li-ion battery managed by a TP4056 charging module.
ESP32 (30 pin)
18650 Li-ion Battery
TP4056
SW-420 Vibration Sensor
HX711 Weighing Sensor Module
DHT22
MQ135
Buzzer Module
Load Cell - Red/white/black/green
#include "HX711.h" // HX711 library for load cell
#include "DHT.h" // DHT sensor library
// --- Pin Definitions ---
// DHT22 Sensor
#define DHTPIN 4 // GPIO 4 for DHT22
#define DHTTYPE DHT22 // Define the type of DHT sensor
// HX711 Load Cell
#define HX711_DT 21 // GPIO 21 for DT (DOUT) of HX711
#define HX711_SCK 22 // GPIO 22 for SCK (CLK) of HX711
// MQ135 Air Quality Sensor
#define MQ135_PIN A0 // Analog pin A0 for MQ135
// SW-420 Vibration Sensor
#define VIBRATION_PIN 5 // GPIO 5 for vibration sensor output
// Buzzer
#define BUZZER_PIN 23 // GPIO 23 for buzzer control
// --- Sensor Objects ---
HX711 scale; // Initialize HX711
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
// --- Threshold Values ---
float weightThreshold = 50.0; // Example weight threshold in kg
float tempThreshold = 35.0; // Temperature threshold in °C
float humidityThreshold = 70.0; // Humidity threshold in %
int airQualityThreshold = 300; // Air quality threshold (arbitrary ADC value)
// --- Setup Function ---
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
// Initialize HX711 load cell
scale.begin(HX711_DT, HX711_SCK);
scale.set_scale(); // Calibrate your load cell and set the scale factor here
scale.tare(); // Reset scale to zero
// Initialize pins
pinMode(VIBRATION_PIN, INPUT); // Vibration sensor as input
pinMode(BUZZER_PIN, OUTPUT); // Buzzer as output
// Ready message
Serial.println("Beehive Monitoring System Initialized!");
}
// --- Loop Function ---
void loop() {
// --- Read DHT22 Sensor Data ---
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT22 sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
// --- Read Load Cell Data ---
float weight = 0.0;
if (scale.is_ready()) {
weight = scale.get_units(10); // Average of 10 readings
Serial.print("Weight: ");
Serial.print(weight);
Serial.println(" kg");
} else {
Serial.println("HX711 not ready");
}
// --- Read MQ135 Air Quality Sensor ---
int airQuality = analogRead(MQ135_PIN);
Serial.print("Air Quality: ");
Serial.println(airQuality);
// --- Read Vibration Sensor ---
int vibration = digitalRead(VIBRATION_PIN);
Serial.print("Vibration Detected: ");
Serial.println(vibration == HIGH ? "Yes" : "No");
// --- Trigger Buzzer on Threshold Breach ---
if (temperature > tempThreshold ||
humidity > humidityThreshold ||
weight > weightThreshold ||
airQuality > airQualityThreshold ||
vibration == HIGH) {
Serial.println("ALERT! Threshold Breached!");
digitalWrite(BUZZER_PIN, HIGH); // Turn buzzer ON
delay(1000