I'm working with a 7 parameter soil sensor (JXCT-IoT) Manual SOIL SENSOR . I did tests to print each parameter and it prints the correct value but when I put all the code, the values are mixed for example the soil moisture Temperature: 0.7 %
value is printed as the PH Ph: 24.3 PH
value.
It should be clarified that I am using arduino uno, rs485, the sensor works with 5v, the altsoft library and the pins (6, 7, 8, 9) for the communication of the arduino uno with the sensor.
Please, if any of you managed to solve this problem, could you guide me or share the code. I will be very grateful for your help.
#include <AltSoftSerial.h>
// RO to pin 8 & DI to pin 9 when using AltSoftSerial
#define RE 6
#define DE 7
const byte temp[] = {0x01,0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf};//
const byte mois[] = {0x01,0x03,0x00,0x12,0x00,0x01,0x24,0x0F};
const byte econ[] = {0x01,0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xce};
const byte ph[] = {0x01,0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};//0x0B64
const byte nitro[] = { 0x01, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C };
const byte phos[] = { 0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc };
const byte pota[] = { 0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0 };
byte values[11];
AltSoftSerial mod;
float envhumidity = 0.0, envtemperature = 0.0, soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0;
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0,val5 = 0, val6 = 0, val7 = 0;
void setup() {
Serial.begin(9600);
mod.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
// put RS-485 into receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
delay(3000);
}
void loop() {
val1 = moisture();
soil_mois = val1/10.0;
delay(1000);
soil_temp = temperature()/10.0;
delay(1000);
val3 = econduc();
delay(1000);
val4 = phydrogen()/10;
soil_ph = val4;
delay(1000);
val5 = nitrogen();
delay(1000);
val6 = phosphorous();
delay(1000);
val7 = potassium();
delay(1000);
Serial.print("Moisture: ");Serial.print(soil_mois);Serial.println(" %");
delay(1000);
Serial.print("Temperature: ");Serial.print(soil_temp);Serial.println(" C");
delay(1000);
Serial.print("EC: ");Serial.print(val3);Serial.println(" us/cm");
delay(1000);
Serial.print("ph: ");Serial.print(soil_ph);Serial.println(" ph");
delay(1000);
Serial.print("Nitrogen: "); Serial.print(val5);Serial.println(" mg/kg");
delay(1000);
Serial.print("Phosphorous: ");Serial.print(val6);Serial.println(" mg/kg");
delay(1000);
Serial.print("Potassium: ");Serial.print(val7);Serial.println(" mg/kg");
Serial.println();
delay(3000);
}
byte moisture() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte temperature() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[3]<<8|values[4];
}
byte econduc() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(econ); i++) mod.write(econ[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte phydrogen() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte nitrogen() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte phosphorous() {
mod.flushInput();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte potassium() {
mod.flushInput();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}