Hello, the code below allows you to measure pH, conductivity, temperature, humidity, nitrogen, phosphorus, and potassium using the JXBS-3001-RS485-SOIL SENSOR. On the serial monitor, all the values are null. I would need your help to display the real values, thank you.
#include <SoftwareSerial.h>
#define RE 7
#define DE 6
const uint32_t TIMEOUT = 1500UL; // Timeout à 1.5s
const uint32_t INTERVAL = 10000UL; // Intervalle de 10s entre mesures
// Commandes Modbus
const byte MOISTURE[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
const byte TEMPERATURE[] = {0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xD5, 0xCA};
const byte EC[] = {0x01, 0x03, 0x00, 0x02, 0x00, 0x01, 0x25, 0xCA};
const byte PH[] = {0x01, 0x03, 0x00, 0x03, 0x00, 0x01, 0x74, 0x0A};
const byte NITROGEN[] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x01, 0xC5, 0xCB};
const byte PHOSPHORUS[] = {0x01, 0x03, 0x00, 0x05, 0x00, 0x01, 0x94, 0x0B};
const byte POTASSIUM[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0B};
SoftwareSerial mod(2, 3); // RX=2, TX=3
uint8_t currentSensor = 0; // 0:Humidité, 1:Temp, 2:EC, 3:pH, 4:N, 5:P, 6:K
// Déclaration anticipée des fonctions
int16_t readSensor(const byte *command, byte commandSize);
void printSensorData(const char* name, const byte* command, byte size, const char* unit, float factor, bool isSigned = false);
void setup() {
Serial.begin(4800);
mod.begin(4800);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
digitalWrite(RE, LOW);
digitalWrite(DE, LOW);
Serial.println("Système prêt. Début des mesures...");
}
void loop() {
switch (currentSensor) {
case 0: // Humidité
printSensorData("Humidité", MOISTURE, sizeof(MOISTURE), "%", 0.1f);
break;
case 1: // Température
printSensorData("Température", TEMPERATURE, sizeof(TEMPERATURE), "°C", 0.1f, true);
break;
case 2: // Conductivité
printSensorData("Conductivité", EC, sizeof(EC), "µS/cm", 1.0f);
break;
case 3: // pH
printSensorData("pH", PH, sizeof(PH), "", 0.1f);
break;
case 4: // Azote (N)
printSensorData("Azote (N)", NITROGEN, sizeof(NITROGEN), "mg/kg", 1.0f);
break;
case 5: // Phosphore (P)
printSensorData("Phosphore (P)", PHOSPHORUS, sizeof(PHOSPHORUS), "mg/kg", 1.0f);
break;
case 6: // Potassium (K)
printSensorData("Potassium (K)", POTASSIUM, sizeof(POTASSIUM), "mg/kg", 1.0f);
break;
}
currentSensor = (currentSensor + 1) % 7; // Cycle entre 0 et 6
delay(INTERVAL);
}
// Fonction générique pour lire et afficher un capteur
void printSensorData(const char* name, const byte* command, byte size, const char* unit, float factor, bool isSigned) {
Serial.print(name); Serial.print(": ");
int16_t rawValue = readSensor(command, size);
float value = isSigned ? (int16_t)rawValue * factor : rawValue * factor;
Serial.print(value); Serial.println(unit);
Serial.println("-----");
}
// Fonction de lecture générique
int16_t readSensor(const byte *command, byte commandSize) {
uint8_t response[11];
memset(response, 0, sizeof(response));
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10);
mod.write(command, commandSize);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
uint32_t startTime = millis();
uint8_t byteCount = 0;
while (millis() - startTime <= TIMEOUT && byteCount < 11) {
if (mod.available()) {
response[byteCount++] = mod.read();
}
}
if (byteCount >= 5) {
return (int16_t)(response[3] << 8 | response[4]);
}
return 0;
}

