#include <SoftwareSerial.h>
#define RE 7
#define DE 8
const byte ph[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
byte values[11]; // Array to store received data
SoftwareSerial mod(1, 0); // Using pins 2 and 3 for communication
void setup() {
Serial.begin(9600); // Start serial communication with the Serial Monitor
mod.begin(4800); // Start communication with the sensor
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
Serial.println("Sensor Initialized");
}
void loop() {
digitalWrite(DE, HIGH); // Enable transmission mode
digitalWrite(RE, HIGH);
delay(10);
if (mod.write(ph, sizeof(ph)) == 8) { // Send request to the sensor
digitalWrite(DE, LOW); // Switch to receive mode
digitalWrite(RE, LOW);
delay(10); // Allow time for response
// Read response from the sensor
if (mod.available() >= 11) { // Make sure we have enough data
for (byte i = 0; i < 11; i++) {
values[i] = mod.read(); // Store all the received bytes
Serial.print(values[i], HEX); // Print the raw byte values in hexadecimal
Serial.print(" ");
}
Serial.println(); // Print a newline after the raw data
// Interpret the data (based on your sensor's documentation)
// pH value (assuming it's in values[4] and values[5])
int16_t pH_raw = (values[4] << 8) | values[5];
float soil_ph = float(pH_raw) / 100.0; // Adjust divisor as necessary (e.g., dividing by 100)
Serial.print("Soil pH: ");
Serial.println(soil_ph, 1); // Print pH value
// Temperature value (assuming it's in values[6] and values[7])
int16_t temperature_raw = (values[6] << 8) | values[7];
float temperature = float(temperature_raw) / 10.0; // Adjust divisor based on your sensor
Serial.print("Temperature: ");
Serial.println(temperature, 1); // Print temperature value
// Humidity value (assuming it's in values[8] and values[9])
int16_t humidity_raw = (values[8] << 8) | values[9];
float humidity = float(humidity_raw) / 10.0; // Adjust divisor if necessary
Serial.print("Humidity: ");
Serial.println(humidity, 1); // Print humidity value
// EC value (assuming it's in values[10] and possibly another byte)
// For example, EC could be in two bytes if applicable
int16_t ec_raw = (values[10] << 8) | values[11]; // Adjust if more bytes are available
float ec = float(ec_raw) / 1000.0; // Adjust scaling if needed (divide by 1000)
Serial.print("EC: ");
Serial.println(ec); // Print EC value
} else {
Serial.println("Error: Incomplete data received.");
}
}
delay(3000); // Wait before the next reading
}
RESULT:
Humidity: 861.6
EC: 3.15
4B 98 68 C 4C 98 98 D F4 F4 F4
Soil pH: 196.1
Temperature: -2661.1
Humidity: -282.8
EC: -3.03
F0 C 8A 78 A C FD FA C0 88 78
Soil pH: 25.7
Temperature: -51.8
Humidity: -1624.8
EC: 30.84
RS485 Soil Sensor(Temperature&Humidity&EC&PH) Arduino WiKi- DFRobot
this link are similar to my sensor but he has different hardware im only using the senor, rs485 to tll and arduino uno r3
PLEASE HELP ME. I dont know how to use this forum