void loop() {
byte queryData[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
byte receivedData[19];
float nit_val, phos_val, potas_val, ph_val, ec_val, mois_val;
int loopCounter = 15; // Counter to keep track of loop iterations with data
const int maxIterations = 20;
mySerial.write(queryData, sizeof(queryData)); // Send the query data to the NPK sensor
delay(1000); // Wait for 1 second
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
// Parse and print the received data in decimal format
unsigned int soilHumidity = (receivedData[3] << 8) | receivedData[4];
// unsigned int soilTemperature = (receivedData[5] << 8) | receivedData[6];
unsigned int soilConductivity = (receivedData[7] << 8) | receivedData[8];
unsigned int soilPH = (receivedData[9] << 8) | receivedData[10];
unsigned int nitrogen = (receivedData[11] << 8) | receivedData[12];
unsigned int phosphorus = (receivedData[13] << 8) | receivedData[14];
unsigned int potassium = (receivedData[15] << 8) | receivedData[16];
float moisture = soilHumidity / 10.0;
float ec = soilConductivity / 1000.0;
float pH = soilPH / 10.0;
float nitro = nitrogen / 1000.0 * 10.0;
float phos = phosphorus;
float potas = potassium / 1000.0 / 39.0983 * 100.0;
I have a code here to get the Nitrogen, Phosphorus, and Potassium but I have a doubt in computation on how to get the exact value of nitrogen in percentage also in other phos, potas,ec,pH and moisture, can anybody help me this is my thesis, Our team compare the result in soils lab in our device but in the end the result is inacccurate, can somebody help me, thanks.