Are there people here who have used soil Ph sensor? How did you make it work? Ours is stuck to 25.5 as value.
#include <SoftwareSerial.h>
#include <Wire.h>
#define RE 30
#define DE 31
const byte ph[] = {0x01, 0x03, 0x00, 0x00, 0x06, 0x01, 0x84, 0x0A};
byte values[11];
SoftwareSerial modbus(52, 53);
void setup()
{
Serial.begin(9600);
modbus.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
}
void loop()
{
float ph_val;
ph_val = soil_ph();
delay(250);
Serial.print("Soil Ph: ");
Serial.println(ph_val,1);
delay(3000);
}
float soil_ph(){
byte val;
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(modbus.write(ph,sizeof(ph))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<11;i++){
// Serial.print(modbus.read(),HEX);
values[i] = modbus.read();
// Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4]/10;
}
reinnnn:
return values[4]/10;
What do you expect the above code to do?
My guess you want what Python could do and sum all the values in an array. It does not work that way with C++.
float soil_ph(){
byte val;
int total=0
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(modbus.write(ph,sizeof(ph))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<11;i++){
// Serial.print(modbus.read(),HEX);
values[i] = modbus.read();
total += values[i];
// Serial.print(values[i],HEX);
}
Serial.println();
}
}
return total/10;
}
Good day! We have already tried the code that you have given but the value only changes to 280 and the sensor can't still read the ph of soil
What does your sensor datasheet say about the modbus request for pH? What address does it use? Is the pH at address 00?
here it is. Thank you so much!
We have already tried this communication protocol but still the value is stuck to 25.0
I suspect that the reason your reading is stuck at 25.5 is that the value being returned from the SoftwareSerial read function is -1 (i.e. 255). That would indicate that there is no data to be read - i.e. your sensor is not responding.
reinnnn:
const byte ph[] = {0x01, 0x03, 0x00, 0x00, 0x06, 0x01, 0x84, 0x0A};
This is not correct for a canned modbus message. I think it is asking your sensor for 388 words of data starting from address 0000. The CRC-16 checksum is also incorrect.
In the image you posted, i'm not sure what the difference is between the the pH enquiry in 4.4.1 and 4.4.2 apart from the address.
To use the example in 4.4.1, I think your canned modbus message would be:
const byte ph[] = {0x01, 0x03, 0x00, 0x0D, 0x00, 0x01, 0x15, 0xC9};
and for the example in 4.4.2, I think your canned modbus message would be:
const byte ph[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0B};
system
Closed
October 1, 2023, 12:56pm
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.