7 in 1 soil sensor giving wrong hex data

to whom it may concern good day! im here seeking for help since im not that very expert when it comes to programming and im having a problem with my project. Im trying to get the same serial output in the youtube but i think there was a problem since when the code is uploaded in the arduino several outputs is not the same until its being stable for a several minutes.

here is my code which i get from Saravanan AL (https://www.youtube.com/watch?v=nGvaAZSIQaA)

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);  // RX, TX

void setup() {
  Serial.begin(9600);
  mySerial.begin(4800); // NPK communicates at 4800 bps
}

void loop()
{
  byte queryData[] {0x01,0x03,0x00,0x00,0x00,0x07,0x04,0x08};
  byte receivedData[19];
  mySerial.write(queryData,sizeof(queryData)); // send query data to NPK
  delay(1000);
 if (mySerial.available() >= sizeof(receivedData)) {  // Check if there are enough bytes available to read
    mySerial.readBytes(receivedData, sizeof(receivedData));  // Read the received data into the receivedData array

  // Print the received data in HEX format
for (int i = 0; i < sizeof(receivedData); i++) {
      Serial.print(receivedData[i], HEX);
      Serial.print(" ");
    }
    Serial.println();
  }
}

this is the output from the serial monitor:
23:41:51.822 -> 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87 FF 1 3 E
23:41:52.875 -> 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87 FF 1 3
23:41:53.899 -> E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87 FF 1
23:41:54.939 -> 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87 FF
23:41:55.963 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87
23:41:57.011 -> FF 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C
23:41:58.048 -> 87 FF 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0
23:41:59.052 -> 2C 87 FF 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0
23:42:00.117 -> 0 2C 87 FF 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0
23:42:01.140 -> 0 0 2C 87 FF 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0
23:42:02.190 -> 0 0 0 2C 87 FF 1 3 E 0 0 1 3C 0 0 0 1E 0 0
23:42:03.221 -> 0 0 0 0 2C 87 FF 1 3 E 0 0 1 3C 0 0 0 1E 0
23:42:04.257 -> 0 0 0 0 0 2C FF 1 3 E 0 0 1 3C 0 0 0 1E 0
23:42:05.294 -> 0 0 0 0 0 2C FF 1 3 E 0 0 1 3C 0 0 0 1E 0 (same output because i think its already stable)

Is there a marker or a an indication where the start of the data is ?

im not sure about that sir but this is the datasheets and i just follow it cause it seems to be exact the same with the youtube video.





Think about what happens if that is not the case; e.g. you received 8 bytes followed by 11 bytes (total 19 bytes)

Your if statement will not be true and you will write again to the soil sensor.

If your code can be blocking, you can try the below

mySerial.write(queryData,sizeof(queryData)); // send query data to NPK
// wait till a full reply is received
while(mySerial.available() < sizeof(receivedData)) {}
// read the data
mySerial.readBytes(receivedData, sizeof(receivedData));
1 Like

should i need to put a 1 second delay between myserial.write and while or no need?

Hello francis_cofreros

Take a search engine of your choice and ask the WWW for 'npk protocol +arduino' to collect some data to be sorted out to get the needed information.

Have a nice day and enjoy coding in C++.

No need with the given approach. It's easy to test :wink:

its very convenient bro!!!!

00:37:57.848 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87
00:37:58.852 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87
00:37:59.851 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87
00:38:00.855 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87
00:38:01.858 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87
00:38:02.870 -> 1 3 E 0 0 1 3C 0 0 0 1E 0 0 0 0 0 0 2C 87

that what i get now and its stable. ill ask you again if im having a trouble thank you a lot!

Hello there bro, i found a problem again. i think the coding only send inquiry once in the sensor thats why in the preceding lines of serial output it has the same output.

No, every time at the beginning of loop() it will send the command. Next it will wait till it has the 19 bytes reply, read 19 bytes once it knows that there are 19 bytes and lastly displays them.

I don't have your sensor so can't test. I (and others) can only look at your code; please post it (complete).

1 Like

@francis_cofreros it looks like you are getting valid Modbus responses from your sensor. That's usually the hardest to achieve - based on discussions around the various NPK sensors in these forums.

I would suggest you switch over to using the ModbusMaster library as it will take care of all the message handling and generally make it a lot easier for you.

This is the video of the arduino and the rs485 converter (video1.mp4 - Google Drive)

After i uploaded the code in the arduino, the rs485 converter just respond once.

For RS485 you might have to enable/disable transmit and receive using the pins on the converter.

Can't watch your video; I get a message "An error occurred. Please try again later".

rs485 converter
i use this kind of converter

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.