A crop monitoring system made using a Raspberry Pi 4B, a 7-in-1 NPK sensor, an ultrasonic sensor, and an ESP32 Wi-Fi module.
- Real-time Soil Monitoring: pH, moisture, temperature, conductivity, nitrogen, phosphorus, potassium (NPK)
- Plant Growth Tracking: Ultrasonic distance sensor for growth rate calculation
- WiFi Connectivity: ESP32 module provides WiFi access to Raspberry Pi
- Cloud Integration: Data stored in Firebase Firestore
- Telegram Notifications: Real-time alerts and data reports
- Automated Scheduling: Data collection at configurable intervals (8-10 AM, 12-2 PM, 8-10 PM)
- Raspberry Pi 4B
- 7-in-1 NPK Soil Sensor (RS485)
- HC-SR04 Ultrasonic Distance Sensor
- ESP32 WiFi Module
- USB to RS485 Converter
pip install -r requirements.txtOr manually:
pip install python-dotenv RPi.GPIO pyserial geocoder requests firebase-admin pyTelegramBotAPI telethon google-cloud-firestore-
Copy the example environment file:
cp .env.example .env
-
Edit
.envand fill in your actual configuration:- Firebase credentials and API key
- Telegram API credentials
- Serial port settings
- Location and plant IDs
| Variable | Description | Example |
|---|---|---|
GOOGLE_APPLICATION_CREDENTIALS |
Path to Firebase service account key | /path/to/serviceAccountKey.json |
FIREBASE_API_KEY |
Firebase API key | AIza... |
FIREBASE_EMAIL |
Firebase auth email | user@example.com |
FIREBASE_PASSWORD |
Firebase auth password | your_password |
TELEGRAM_API_ID |
Telegram API ID | 12345678 |
TELEGRAM_API_HASH |
Telegram API hash | abcd1234... |
TELEGRAM_PHONE |
Telegram phone number | +1234567890 |
TELEGRAM_USERNAME |
Telegram username | yourusername |
TELEGRAM_CHAT_ID |
Telegram chat/channel ID | @yourchannel |
LOCATION_ID |
Location identifier | ABC123 |
PLANT_ID |
Plant identifier | XYZ789 |
FRUIT_TYPE |
Type of fruit/crop | mango |
SERIAL_PORT |
Serial port for NPK sensor | /dev/ttyUSB1 |
SERIAL_BAUDRATE |
Serial baudrate | 4600 |
-
Navigate to the wififile directory:
cd wififile -
Copy the config template:
cp config.h.example config.h
-
Edit
config.hand add your WiFi credentials:const char* WIFI_SSID = "your_wifi_ssid"; const char* WIFI_PASSWORD = "your_wifi_password";
- Open
wififile.inoin Arduino IDE - Select your ESP32 board (Tools β Board β ESP32 Dev Module)
- Select the correct port (Tools β Port)
- Upload the sketch
The ESP32 accepts the following commands from Raspberry Pi over serial:
| Command | Description |
|---|---|
CONNECT |
Connect to configured WiFi network |
DISCONNECT |
Disconnect from WiFi |
STATUS |
Get connection status, SSID, signal strength, IP |
IP |
Get current IP address |
SCAN |
Scan for available WiFi networks |
On the Raspberry Pi:
python CMPEProject.pyThe system will:
- Connect to Firebase and authenticate
- Monitor soil conditions every 24 minutes (5 readings per collection window)
- Calculate and send average readings to Firestore
- Send notifications via Telegram
- Track plant growth rate using ultrasonic sensor
Security Note: Never commit .env, config.h, or serviceAccountKey.json to version control.
This website is part of the MoniCrop Smart Crop Monitoring System:
- MoniCrop Website - Functional Website
- MoniCrop iOS App - Mobile application for iOS
If you use this work, please cite:
@article{Afi2023,
author = "Eman Sarah Afi",
title = "{Development of a Smart Crop Monitoring System}",
year = "2023",
month = "10",
url = "https://aubh.figshare.com/articles/thesis/_b_Development_of_a_Smart_Crop_Monitoring_System_b_/30580751",
doi = "10.58014/aubh.24314056.v2"
}Published thesis: Development of a Smart Crop Monitoring System (DOI: 10.58014/aubh.24314056.v2)

