The aquaculture monitoring system includes an automated SMS alerting system that monitors sensor readings in real-time and sends SMS alerts when values exceed safe operating parameters.
-
Middleware Integration: The
sensorAlertMiddlewareis integrated into the Express.js application and monitors all POST requests to sensor reading endpoints. -
Automatic Monitoring: When new sensor readings are posted via the API, the middleware automatically checks if the values are within acceptable limits.
-
SMS Alerts: If a reading exceeds the configured thresholds, the system automatically sends SMS alerts to configured phone numbers.
-
Spam Prevention: The system includes a cooldown period (default: 30 minutes) to prevent alert spam for the same sensor/pool combination.
Sensor thresholds are configured in /config/sensor-limits.js. Current limits include:
| Sensor Type | Minimum | Maximum | Unit | Field Name |
|---|---|---|---|---|
| pH | 6.5 | 8.5 | - | ph_value |
| Ammonia | 0 | 0.25 | ppm | ammonia_ppm |
| Nitrite | 0 | 0.5 | ppm | nitrite_ppm |
| Nitrate | 0 | 40 | ppm | nitrate_ppm |
| Dissolved Oxygen | 5.0 | 15.0 | ppm | do_ppm |
| ORP | 300 | 500 | mV | orp_mv |
| Salinity | 0 | 35 | ppt | salinity_ppt |
| Temperature | 15 | 30 | °C | temperature_celsius |
| Turbidity | 0 | 10 | NTU | turbidity_ntu |
| Water Level | 50 | 200 | cm | water_level_cm |
| TOC | 0 | 5 | ppm | toc_ppm |
| Fish Activity | 20 | 100 | % | activity_level |
| Feeding Response | 50 | 100 | % | strike_rate_percent |
SMS alert recipients are configured in the ALERT_CONTACTS array in /config/sensor-limits.js:
const ALERT_CONTACTS = [
"213784127256", // Default contact
// Add more phone numbers as needed
];The alert cooldown period is configurable (default: 30 minutes):
const ALERT_COOLDOWN = 30; // minutesThe system uses Infobip SMS API. Configuration is in /controllers/SMS-API.js:
- Authorization: Pre-configured API key
- From Number: 447491163443
- API Endpoint: https://api.infobip.com/sms/2/text/advanced
SMS alerts include:
- Alert type and severity indicator (🚨)
- Pool ID
- Sensor name
- Current value and status (too high/too low)
- Acceptable range
- Call to action
Example:
🚨 AQUACULTURE ALERT 🚨
Pool ID: 1
Sensor: pH Level
Status: TOO HIGH (9.5 > 8.5)
Acceptable Range: 6.5-8.5
Immediate attention required!
Run the test script to verify components:
node test-alerts.jsTest with actual API calls (requires running server):
node demo-alerts.jsSend a test reading that exceeds limits:
curl -X POST http://localhost:3000/api/ph-readings \
-H "Content-Type: application/json" \
-d '{
"pool_id": 1,
"ph_value": 9.5,
"notes": "Test alert - pH too high"
}'mainapi/
├── controllers/
│ └── SMS-API.js # SMS sending functionality
├── middleware/
│ └── sensor-alerts.js # Alert monitoring middleware
├── config/
│ └── sensor-limits.js # Sensor thresholds and configuration
├── test-alerts.js # Unit test script
├── demo-alerts.js # Integration demo script
└── app.js # Main app with middleware integration
The system provides comprehensive logging:
- Success logs: Confirmation when alerts are sent
- Error logs: Failed SMS attempts or system errors
- Debug logs: Sensor value checks and cooldown status
- Add sensor limits to
SENSOR_LIMITSin/config/sensor-limits.js - Update the route mapping in
getSensorTypeFromPath()function - Test with new sensor readings
Edit the values in /config/sensor-limits.js and restart the server.
Add phone numbers to the ALERT_CONTACTS array in /config/sensor-limits.js.
Modify the ALERT_COOLDOWN value in /config/sensor-limits.js.
- API keys should be moved to environment variables in production
- Phone numbers should be validated before adding to contact list
- Consider implementing rate limiting for API endpoints
- Log all alert activities for audit purposes
-
SMS not sending
- Check API key validity
- Verify phone number format (international format without +)
- Check network connectivity
-
No alerts triggered
- Verify sensor limits configuration
- Check if values actually exceed thresholds
- Confirm middleware is properly integrated
-
Too many alerts
- Increase cooldown period
- Review threshold values
- Check for sensor calibration issues
Enable debug logging by modifying the middleware to log all sensor checks:
console.log(`Checking ${sensorType}: ${sensorValue} (limits: ${limits.min}-${limits.max})`);- Email alerts in addition to SMS
- Alert escalation (multiple attempts)
- Alert acknowledgment system
- Dashboard for alert history
- Integration with external monitoring systems
- Predictive alerting based on trends