IoT-Based Smart Irrigation System Using RYLR998 LoRa Module and XAMPP Web Application
Introduction: Water Conservation in Agriculture
Water scarcity is a critical challenge in agriculture, where inefficient irrigation can waste resources and reduce crop yields. An IoT-based smart irrigation system addresses this by using real-time soil moisture data to automate watering, ensuring crops receive precisely what they need. In this tutorial, we’ll build a smart irrigation system using the REYAX RYLR998 LoRa module for long-range, low-power communication, paired with a PHP web application hosted on XAMPP to display and manage soil moisture and pump status data. The RYLR998’s built-in antenna and AT command interface make it ideal for remote farming applications. This guide is perfect for farmers, IoT enthusiasts, and developers seeking affordable, sustainable farming solutions.
Keywords: IoT smart irrigation, RYLR998 LoRa module, LoRaWAN agriculture, soil moisture sensor, Arduino irrigation, XAMPP PHP web application, smart farming.
Hardware: Components for Your Smart Irrigation System
To build this system, gather the following components:
- LoRa Module: REYAX RYLR998 LoRa module for long-range communication (~$15–$25). The RYLR998 operates at 868/915 MHz, features a built-in antenna, and supports UART with AT commands, making it easy to integrate.
- Soil Moisture Sensor: Capacitive soil moisture sensor (e.g., SEN0193, ~$5) for reliable, corrosion-resistant readings.
- Microcontroller: Arduino Uno or ESP32 to process sensor data and communicate with the RYLR998 (~$10–$20).
- Relay Module: Single-channel 5V relay to control a water pump (~$3).
- Water Pump: 5V–12V submersible pump for irrigation (~$10).
- Power Supply: 5V USB power bank or solar panel for field deployment.
- LoRa Gateway: Single-channel LoRa gateway (e.g., Dragino LG01-N) to receive RYLR998 data and forward it to your XAMPP server (~$50).
- Miscellaneous: Breadboard, jumper wires, and a waterproof enclosure for outdoor use.
Total Cost: ~$95–$135, depending on sourcing.
Tip: Ensure your RYLR998 and gateway use the correct frequency band (868 MHz for Europe, 915 MHz for North America) to comply with local regulations. The RYLR998’s built-in antenna simplifies setup, but ensure it’s placed in an open area for optimal range.
Software: Arduino Code for RYLR998 LoRa Communication
We’ll use the Arduino IDE to program the microcontroller, sending soil moisture and pump status data via the RYLR998 LoRa module to a gateway, which forwards it to a PHP web application on XAMPP. The RYLR998 is controlled via AT commands over UART, making it straightforward to configure and send data.
Prerequisites
- Install Arduino IDE (download from arduino.cc).
- Install the Adafruit_Sensor library for the soil moisture sensor (via Library Manager).
- Set up XAMPP with Apache and MySQL running on your local machine.
- Configure your LoRa gateway to forward packets to your XAMPP server’s IP address (e.g., via HTTP POST).
- Review the RYLR998 AT Command Guide for configuration commands.
Arduino Code
This code reads soil moisture, controls the pump, and sends data using the RYLR998’s AT commands. The RYLR998 is configured to communicate with a gateway at a specific address and network ID.
#include <WiFi.h>
#include <SoftwareSerial.h>
// Wi-Fi credentials
const char* ssid = "your_SSID"; // Replace with your Wi-Fi SSID
const char* password = "your_PASSWORD"; // Replace with your Wi-Fi password
// Server details
const char* serverUrl = "http://your_server_ip/receive_data.php"; // Replace with your XAMPP server URL
// Pin definitions
#define SOIL_MOISTURE_PIN 35 // Change to the correct pin if needed
#define RELAY_PIN 23
#define LORA_RX_PIN 5
#define LORA_TX_PIN 18
// LoRa module setup
SoftwareSerial loraSerial(LORA_RX_PIN, LORA_TX_PIN);
void setup() {
// Initialize serial communication
Serial.begin(9600);
loraSerial.begin(9600);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize pins
pinMode(SOIL_MOISTURE_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
// Ensure pump is off initially
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
// Read soil moisture level
int soilMoistureValue = analogRead(SOIL_MOISTURE_PIN);
Serial.print("Soil Moisture: ");
Serial.println(soilMoistureValue);
// Send soil moisture data to XAMPP server
if (WiFi.status() == WL_CONNECTED) {
WiFiClient client;
if (client.connect(serverUrl, 80)) {
String postData = "soil_moisture=" + String(soilMoistureValue);
client.println("POST /receive_data.php HTTP/1.1");
client.println("Host: your_server_ip"); // Replace with your server IP
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.println(postData);
}
client.stop();
}
// Check for incoming LoRa messages
if (loraSerial.available()) {
String command = loraSerial.readStringUntil('\n');
Serial.print("Received command: ");
Serial.println(command);
// Control pump based on command
if (command == "PUMP_ON") {
digitalWrite(RELAY_PIN, HIGH);
Serial.println("Pump activated.");
} else if (command == "PUMP_OFF") {
digitalWrite(RELAY_PIN, LOW);
Serial.println("Pump deactivated.");
}
}
// Delay for stability
delay(2000);
}
Notes:
- Calibrate
MOISTURE_THRESHOLD
by testing the sensor in dry and wet soil (e.g., 300–700 range). - Update
FREQUENCY
,ADDRESS
,GATEWAY_ADDRESS
, andNETWORK_ID
to match your gateway’s configuration. The RYLR998 requires matching network IDs and frequencies for communication. - The RYLR998 uses AT commands for simple packet transmission, making it easier to integrate than full LoRaWAN stacks. The
AT+PARAMETER
settings optimize for range and reliability in agricultural environments. - If your gateway expects a different data format, adjust the payload structure accordingly.
Tutorial: Setting Up a PHP Web Application on XAMPP
We’ll configure the LoRa gateway to send RYLR998 data to a PHP web application on XAMPP, which stores soil moisture and pump status in a MySQL database and displays it on a dashboard.
Step 1: Set Up XAMPP
- Install XAMPP: Download and install XAMPP from apachefriends.org. Start Apache and MySQL.
- Create a Database:
- Open phpMyAdmin (http://localhost/phpmyadmin).
- Create a database named
irrigation_db
. - Create a table
sensor_data
with the following SQL:
CREATE TABLE sensor_data (
id INT AUTO_INCREMENT PRIMARY KEY,
moisture INT NOT NULL,
pump_status TINYINT(1) NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Configure the LoRa Gateway
- Set Up Gateway: Configure your LoRa gateway (e.g., Dragino LG01-N) to forward RYLR998 packets to your XAMPP server’s IP address (e.g.,
http://192.168.1.100/irrigation/receive.php
) via HTTP POST. The RYLR998 sends data in a comma-separated format (e.g.,500,1
for moisture and pump status). - Gateway Settings: In the gateway’s web interface, set the uplink to parse the RYLR998’s payload and send it as a JSON object. Ensure the gateway is on the same network as your XAMPP server.
Step 3: Create the PHP Web Application
Create a folder irrigation
in C:\xampp\htdocs
. Add the following files:
receive.php
(Handles incoming LoRa data)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "irrigation_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Decode incoming LoRa payload (assuming JSON from gateway)
$data = json_decode(file_get_contents('php://input'), true);
$payload = explode(",", $data['data']); // RYLR998 sends comma-separated data
// Parse payload (moisture, pump status)
$moisture = (int)$payload[0];
$pump_status = (int)$payload[1];
// Insert into database
$sql = "INSERT INTO sensor_data (moisture, pump_status) VALUES ($moisture, $pump_status)";
if ($conn->query($sql) === TRUE) {
echo "Data saved successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
index.php
(Displays dashboard)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Smart Irrigation Dashboard</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { border-collapse: collapse; width: 100%; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
.status-on { color: green; }
.status-off { color: red; }
</style>
</head>
<body>
<h2>Smart Irrigation System Dashboard</h2>
<table>
<tr>
<th>ID</th>
<th>Soil Moisture</th>
<th>Pump Status</th>
<th>Timestamp</th>
</tr>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "irrigation_db";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, moisture, pump_status, timestamp FROM sensor_data ORDER BY timestamp DESC LIMIT 10";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$status = $row["pump_status"] ? "On" : "Off";
$status_class = $row["pump_status"] ? "status-on" : "status-off";
echo "<tr>
<td>" . $row["id"] . "</td>
<td>" . $row["moisture"] . "</td>
<td class='$status_class'>" . $status . "</td>
<td>" . $row["timestamp"] . "</td>
</tr>";
}
} else {
echo "<tr><td colspan='4'>No data available</td></tr>";
}
$conn->close();
?>
</table>
</body>
</html>
Step 4: Test the System
- Upload Arduino Code: Connect your Arduino, upload the code, and open the Serial Monitor (9600 baud) to verify sensor readings and RYLR998 transmission.
- Start XAMPP: Ensure Apache and MySQL are running.
- Access Dashboard: Open
http://localhost/irrigation/index.php
in a browser to view the dashboard. - Verify Data: Confirm that soil moisture and pump status appear in the table. Check that the gateway correctly forwards RYLR998 data to
receive.php
.
Troubleshooting:
- If no data appears, verify the gateway’s IP, port, and JSON format (e.g.,
{"data": "500,1"}
). - Ensure the RYLR998’s frequency, address, and network ID match the gateway’s settings.
- Test
receive.php
with a sample POST request using Postman (e.g.,{"data": "500,1"}
). - Check RYLR998 responses in the Serial Monitor for errors (e.g.,
+ERR=-2
indicates a configuration issue).
Case Study: Potential Impact on Small Farms
This smart irrigation system, powered by the RYLR998 LoRa module, can transform small-scale farming, especially in water-scarce regions. For a 5-acre vegetable farm:
- Water Savings: Automating irrigation at a 30% moisture threshold can reduce water usage by up to 40%, according to FAO studies.
- Cost Efficiency: The $95–$135 setup cost is recouped within a season through lower water and labor costs.
- Local Accessibility: The XAMPP-based web application runs locally, ideal for farms with limited internet access.
- Long-Range Communication: The RYLR998 supports up to 20 km in open fields, enabling monitoring across large farms without cellular networks.
- Real-World Impact: A 2023 study by the Indian Agricultural Research Institute found similar LoRa-based systems increased crop yields by 15–20% while reducing water consumption.
Farmers can monitor the dashboard on a local laptop, making data-driven decisions to optimize watering and prevent soil erosion.
Conclusion
This IoT-based smart irrigation system, using the REYAX RYLR998 LoRa module and a XAMPP-hosted PHP web application, offers a cost-effective, scalable solution for sustainable agriculture. The RYLR998’s ease of use and long-range capabilities make it ideal for remote farms, while the local dashboard ensures accessibility. Deploy this system in your garden or farm, and share your results in the comments! Explore our other IoT tutorials, like the NPK sensor guide and healthcare IoT projects.
No comments:
if you want any help about arduino and nodemcu you contact me freely