A comprehensive IoT-based smart farming system featuring ESP32 microcontrollers, environmental monitoring sensors, automated irrigation control, and real-time web dashboard. The system uses MQTT communication via HiveMQ Cloud for real-time data exchange and remote control capabilities.
ESP32 (Publisher) → HiveMQ Cloud → Backend (Subscriber) → Supabase PostgreSQL
↘ ↗
Frontend (Publisher) → Relay Commands → ESP32 (Subscriber)

| Component | ESP32 Pin | Connection Type |
|---|---|---|
| DHT22 Sensor | GPIO16 | Digital |
| Soil Moisture | GPIO35 | Analog |
| Rain Detector | GPIO18 | Digital |
| Relay Control | GPIO17 | Digital Output |
| Water Pump 12V | Relay | Switched Load |
| OLED SDA | GPIO21 | I2C |
| OLED SCL | GPIO22 | I2C |
| Soil Temperature (DS18B20) | GPIO23 | OneWire |
| SIM7670C GSM RX | GPIO25 | UART |
| SIM7670C GSM TX | GPIO26 | UART |
| Water Level | GPIO34 | Analog |
| Power Supply Module | Breadboard | VCC/GND Distribution |

Complete schematic showing ESP32 connections to sensors (DHT22, soil moisture, rain detector, soil temperature probe), actuators (relay-controlled water pump), display (OLED), SIM7670C GSM module, and power distribution system.
// MQTT Topics Used by ESP32
#define SENSOR_TOPIC "sf/esp32-01/sensor" // Publishes sensor data
#define RELAY_TOPIC "sf/esp32-01/relay" // Publishes relay status
#define COMMAND_TOPIC "sf/devices/relay/command" // Subscribes to commands
#define STATUS_TOPIC "sf/esp32-01/status" // Publishes device status
{
"device_id": "esp32-01",
"timestamp": "2024-01-15T10:30:00Z",
"soil_moisture": 65.2,
"soil_temperature": 22.5,
"air_temperature": 28.3,
"humidity": 70.1,
"rain_detected": false,
"water_level": 85.0
}
{
"device_id": "esp32-01",
"command": "toggle_relay",
"relay_id": 1,
"state": "on",
"duration": 300,
"timestamp": "2024-01-15T10:30:00Z"
}
GET /api/health - API health check with database connection statusGET /api/db-test - Database connection testGET /api/info - API information and endpoint documentationPOST /api/sensor-data - Create new sensor data entryGET /api/sensor-data - Retrieve sensor data with paginationGET /api/sensor-data/latest - Get latest sensor readingsGET /api/sensor-data/stats - Get sensor data statisticsGET /api/sensor-data/health - Sensor data service health checkGET /api/sensor-data/:id - Get specific sensor data by IDDELETE /api/sensor-data/cleanup - Cleanup old sensor data recordsPOST /api/relay-log - Create new relay log entryGET /api/relay-log - Retrieve relay logs with paginationGET /api/relay-log/latest - Get latest relay log entryGET /api/relay-log/status - Get current relay statusGET /api/relay-log/stats - Get relay operation statisticsGET /api/relay-log/duration - Get relay operation duration metricsGET /api/relay-log/health - Relay log service health checkPOST /api/relay-log/state-change - Log relay state change eventGET /api/relay-log/:id - Get specific relay log by IDDELETE /api/relay-log/cleanup - Cleanup old relay log recordsGET /api/mqtt/health - MQTT connection status and healthPOST /api/mqtt/publish - Publish message to specific MQTT topicsmart-farming-iot/
├── README.md
├── assets/
│ └── Wiring_ESP32.png
├── iot-backend/ # Node.js Backend API
│ ├── src/
│ │ ├── controllers/ # API route handlers
│ │ ├── services/ # Business logic & MQTT
│ │ ├── repositories/ # Database operations
│ │ ├── routes/ # Express routes
│ │ └── schemas/ # Validation schemas
│ ├── prisma/ # Database schema & migrations
│ └── package.json
├── iot-dashboard/ # Next.js Frontend
│ ├── src/
│ │ ├── app/ # App router pages
│ │ ├── components/ # React components
│ │ ├── hooks/ # Custom React hooks
│ │ └── lib/ # Utilities & API client
│ └── package.json
└── iot-devices/ # ESP32 PlatformIO Project
├── src/ # Main application code
├── include/ # Header files
├── lib/ # Custom libraries
└── platformio.ini # Platform configuration
# Install PlatformIO
pip install platformio
# Navigate to device folder
cd iot-devices
# Upload to ESP32
pio run --target upload
cd iot-backend
npm install
npm run dev
cd iot-dashboard
npm install
npm run dev
The backend requires several environment variables for database connection, MQTT configuration, and other settings:
# Supabase Database Configuration
# Connect to Supabase via connection pooling
DATABASE_URL="postgresql://[username]:[password]@[host]:[port]/[database]?pgbouncer=true"
# Direct connection to the database. Used for migrations
DIRECT_URL="postgresql://[username]:[password]@[host]:[port]/[database]"
# MQTT Configuration
MQTT_PORT=8883
MQTT_PROTOCOL="mqtts"
MQTT_HOST="[your-hivemq-cloud-instance].s1.eu.hivemq.cloud"
MQTT_USERNAME="[your-mqtt-username]"
MQTT_PASSWORD="[your-mqtt-password]"
ENABLE_MQTT=1
# MQTT topic subscriptions (comma-separated lists supported)
# Wildcards: + for single level, # for multi-level
MQTT_SENSOR_TOPICS="sf/+/sensor"
MQTT_RELAY_TOPICS="sf/+/relay"
The frontend requires configuration for API and MQTT service endpoints:
NEXT_PUBLIC_API_BASE_URL='https://[your-backend-api-url]/api'
NEXT_PUBLIC_MQTT_SERVICE_BASE_URL='https://[your-mqtt-service-url]/api'
Create a config.h file in the include directory with the following settings:
#ifndef CONFIG_H
#define CONFIG_H
// WiFi Configuration
#define WIFI_SSID "YourWiFiSSID"
#define WIFI_PASSWORD "YourWiFiPassword"
// MQTT Configuration
#define MQTT_SERVER "ee769d47a04a469da8fe04f0427dfd0b.s1.eu.hivemq.cloud"
#define MQTT_PORT 8883
#define MQTT_USERNAME "smart_farming_iot"
#define MQTT_PASSWORD "YourMQTTPassword"
#define CLIENT_ID "ESP32_SmartFarming_01"
// MQTT Topics
#define SENSOR_TOPIC "sf/esp32-01/sensor"
#define RELAY_TOPIC "sf/esp32-01/relay"
#define COMMAND_TOPIC "sf/devices/relay/command"
#define STATUS_TOPIC "sf/esp32-01/status"
// Pin Configuration
#define DHT_PIN 16
#define SOIL_MOISTURE_PIN 35
#define RAIN_SENSOR_PIN 18
#define RELAY_PIN 17
#define ONE_WIRE_BUS 23
#define WATER_LEVEL_PIN 34
#endif // CONFIG_H
This project showcases a complete IoT solution for smart farming, integrating hardware, software, and cloud services. It is designed for scalability and adaptability to various agricultural scenarios. Contributions and improvements are welcome!
$ claude mcp add Smart_Farming \
-- python -m otcore.mcp_server <graph>