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.
#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)
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));
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).
@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.