RS 485 Multi sensor for Temperature, moisture, Conductivity and Ph

Thank you so much Sir, it worked!!!!!!

1 Like

Thanks ogom and markd833, I tried as well and it works :star_struck:

Hello

With the help of this forum, I was able to successfully use the Arduino IDE to read the values of a composite sensor over RS485. The command and response frames are the same for the sensors I used.

When I uploaded it again through cloud editor to control it through Arduino Cloud, I could not read the sensor values. I changed several variables and imported the latest ESPSoftwareSerial library used in PC IDE for ESP32 in Cloud Editor. However, I cannot read the sensor values.




Please advise Thanks

My advice: do not post screenshots of your code!

Just to clarify, your code works when compiled using the IDE but the exact same code does not work when compiled via the cloud.

I don't use the cloud setup, but can you download the SoftwareSerial library the cloud is using and try it via the desktop IDE?

Hello

I used the exact same code. I've posted screenshots to confirm other information. Here's my code.
Libraries downloaded from the cloud are not imported into the IDE. I imported the same library into the IDE and Cloud. The latest version is 1.8.2, which can be checked in the IDE and in the cloud.

#include <SoftwareSerial.h>

#define RE 7
#define DE 6

const uint32_t TIMEOUT = 500UL;

const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
const byte temp[] = {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 nitro[] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x01, 0xc5, 0xcb};
const byte phos[] = {0x01, 0x03, 0x00, 0x05, 0x00, 0x01, 0x94, 0x0b};
const byte pota[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};

byte values[11];

SoftwareSerial mod(3, 2); // Rx pin, Tx pin // Software serial for RS485 communication

void setup() {
  Serial.begin(4800);
  mod.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

  delay(500);
}

void loop() {
  float  moist_val, temp_val, ph_val;
  uint16_t ec_val, nitro_val, phos_val, pota_val;

  Serial.println("Moisture: ");
  moist_val = moisture();
  //float Val1 = val1 * 0.1;
  delay(1000);
  Serial.print(moist_val);
  Serial.println(" %\n");
    
  Serial.println("Temperature: ");
  temp_val = temperature();
  //float Val2 = val2 * 0.1;
  delay(1000);
  Serial.print(temp_val);
  Serial.println(" C\n");
  
  Serial.println("Conductivity: ");
  ec_val = conductivity();
  delay(1000);
  Serial.print(ec_val);
  Serial.println(" us/cm\n");
  
  Serial.println("Ph: ");
  ph_val = ph();
  //float Val4 = val4 * 0.1;
  delay(1000);
  Serial.print(ph_val);
  Serial.println(" pH\n");
  
  Serial.println("Nitrogen (N): ");
  nitro_val = nitrogen();
  Serial.print(nitro_val);
  Serial.println(" mg/kg\n");
  delay(1000);

  Serial.println("Phosphorous (P): ");
  phos_val = phosphorous();
  Serial.print(phos_val);
  Serial.println(" mg/kg\n");
  delay(1000);

  Serial.println("Potassium (K): ");
  pota_val = potassium();
  delay(1000);
  Serial.print(pota_val);
  Serial.println(" mg/kg\n");
  
  delay(5000);
}

int16_t moisture() {
  float MOIST3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(moist, sizeof(moist));
    mod.flush();
    digitalWrite(DE, LOW);
    digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    MOIST3and4 = (((values[3] * 256.0) + values[4])/10); // converting hexadecimal to decimal
  }
    Serial.println();
    return MOIST3and4;
   
}

int16_t temperature() {
  float TEMP3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;
// switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
// write out the message
  mod.write(temp, sizeof(temp));
// wait for the transmission to complete
  mod.flush();
// switch RS-485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    TEMP3and4 = (((values[3] * 256.0) + values[4])/10); // converting hexadecimal to decimal
  }
    Serial.println();
    return TEMP3and4;
 
}

int16_t conductivity() {
  int16_t EC3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(EC, sizeof(EC));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    EC3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
   Serial.println();
   return EC3and4;
 
}

int16_t ph() {
  float PH3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(PH, sizeof(PH));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
     values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    PH3and4 = (((values[3] * 256.0) + values[4])/10); // converting hexadecimal to decimal
  }
  Serial.println();
  return PH3and4;
 
}

int16_t nitrogen() {
  int16_t NITRO3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;
  
  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(nitro, sizeof(nitro));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount<sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount-1]);
    }
    NITRO3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
  Serial.println();
  return NITRO3and4;
 }

int16_t phosphorous() {
  int16_t PHOS3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(phos, sizeof(phos));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount<sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount-1]);
    }
    PHOS3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
  Serial.println();
  return PHOS3and4;
  //return (int16_t)(values[4] << 8 | values[5]);
}

int16_t potassium() {
  int16_t POTA3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;
  
// switch RS-485 to transmit mode
  digitalWrite(DE, HIGH);   
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(pota, sizeof(pota));
  mod.flush(); // wait for the transmission to complete
// switch RS-485 to receive mode
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount<sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount-1]);
    }
    POTA3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
  Serial.println();
  return POTA3and4;
}

void printHexByte(byte b)
{
  Serial.print((b >> 4) & 0xF, HEX);
  Serial.print(b & 0xF, HEX);
  Serial.print(' ');
}



esp_ver
I've seen in other forums that the compiler results of IDE and Web Editor are different. It seems to be mainly a library issue. I really want to try the compiler in the cloud.

1 Like

Hello

I referenced the code suggested in another thread in the forum as a solution to a nearly identical problem and changed the SoftwareSerial to HardwareSerial as shown in the code below and was able to successfully read the sensor values from both the IDE and Cloud.

#include "HardwareSerial.h"
//
const byte RXD2 = 6;
const byte TXD2 = 7;


HardwareSerial mod(1); 

const uint32_t TIMEOUT = 500UL;

const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
const byte temp[] = {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 nitro[] = {0x01, 0x03, 0x00, 0x04, 0x00, 0x01, 0xc5, 0xcb};
const byte phos[] = {0x01, 0x03, 0x00, 0x05, 0x00, 0x01, 0x94, 0x0b};
const byte pota[] = {0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};

byte values[11];


void setup() {

  Serial.begin(9600);
  mod.begin(4800,SERIAL_8N1, RXD2,TXD2); //D6,D7
  delay(5000);
}

void loop() {
  float  moist_val, temp_val, ph_val;
  uint16_t ec_val, nitro_val, phos_val, pota_val;

  Serial.println("Moisture: ");
  moist_val = moisture();
  delay(1000);
  Serial.print(moist_val);
  Serial.println(" %\n");
    
  Serial.println("Temperature: ");
  temp_val = temperature();
  delay(1000);
  Serial.print(temp_val);
  Serial.println(" C\n");
  
  Serial.println("Conductivity: ");
  ec_val = conductivity();
  delay(1000);
  Serial.print(ec_val);
  Serial.println(" us/cm\n");
  
  Serial.println("Ph: ");
  ph_val = ph();
  delay(1000);
  Serial.print(ph_val);
  Serial.println(" pH\n");
  
  Serial.println("Nitrogen (N): ");
  nitro_val = nitrogen();
  Serial.print(nitro_val);
  Serial.println(" mg/kg\n");
  delay(1000);

  Serial.println("Phosphorous (P): ");
  phos_val = phosphorous();
  Serial.print(phos_val);
  Serial.println(" mg/kg\n");
  delay(1000);

  Serial.println("Potassium (K): ");
  pota_val = potassium();
  delay(1000);
  Serial.print(pota_val);
  Serial.println(" mg/kg\n");
  
  delay(3000);
}

int16_t moisture() {
  float MOIST3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  mod.write(moist, sizeof(moist));
 
  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    MOIST3and4 = (((values[3] * 256.0) + values[4])/10); // converting hexadecimal to decimal
  }
    Serial.println();
    return MOIST3and4;
   
}

int16_t temperature() {
  float TEMP3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;
// write out the message
  mod.write(temp, sizeof(temp));

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    TEMP3and4 = (((values[3] * 256.0) + values[4])/10); // converting hexadecimal to decimal
  }
    Serial.println();
    return TEMP3and4;
 
}

int16_t conductivity() {
  int16_t EC3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  //digitalWrite(DE, HIGH);
  //digitalWrite(RE, HIGH);
  //delay(10);
  mod.write(EC, sizeof(EC));
  mod.flush();
  //digitalWrite(DE, LOW);
  //digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    EC3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
   Serial.println();
   return EC3and4;
 
}

int16_t ph() {
  float PH3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  //digitalWrite(DE, HIGH);
  //digitalWrite(RE, HIGH);
  //delay(10);
  mod.write(PH, sizeof(PH));
  mod.flush();
  //digitalWrite(DE, LOW);
  //digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
     values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
    PH3and4 = (((values[3] * 256.0) + values[4])/10); // converting hexadecimal to decimal
  }
  Serial.println();
  return PH3and4;
 
}

int16_t nitrogen() {
  int16_t NITRO3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;
  
  //digitalWrite(DE, HIGH);
  //digitalWrite(RE, HIGH);
  //delay(10);
  mod.write(nitro, sizeof(nitro));
  mod.flush();
  //digitalWrite(DE, LOW);
  //digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount<sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount-1]);
    }
    NITRO3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
  Serial.println();
  return NITRO3and4;
 }

int16_t phosphorous() {
  int16_t PHOS3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  //digitalWrite(DE, HIGH);
  //digitalWrite(RE, HIGH);
  //delay(10);
  mod.write(phos, sizeof(phos));
  mod.flush();
  //digitalWrite(DE, LOW);
  //digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount<sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount-1]);
    }
    PHOS3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
  Serial.println();
  return PHOS3and4;
  //return (int16_t)(values[4] << 8 | values[5]);
}

int16_t potassium() {
  int16_t POTA3and4;
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;
  
// switch RS-485 to transmit mode
  //digitalWrite(DE, HIGH);   
  //digitalWrite(RE, HIGH);
  //delay(10);
  mod.write(pota, sizeof(pota));
  mod.flush(); // wait for the transmission to complete
// switch RS-485 to receive mode
  //digitalWrite(DE, LOW);
  //digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount<sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount-1]);
    }
    POTA3and4 = (((values[3] * 256.0) + values[4])); // converting hexadecimal to decimal
  }
  Serial.println();
  return POTA3and4;
}

void printHexByte(byte b)
{
  Serial.print((b >> 4) & 0xF, HEX);
  Serial.print(b & 0xF, HEX);
  Serial.print(' ');
}

Hi , im using a soil four parameter sensor, rs485, and arduino mega 2560. i tried the code but it didn't work, it only shows zero

this is the code i used

#include <SoftwareSerial.h>

#define RE 7
#define DE 6

const uint32_t TIMEOUT = 500UL;

const byte moist[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A};
const byte temp[] = {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};

byte values[11];
SoftwareSerial mod(2, 3); // Rx pin, Tx pin

void setup() {
  //  Serial.begin(9600);
  Serial.begin(4800);
  mod.begin(4800);
  pinMode(RE, OUTPUT);
  pinMode(DE, OUTPUT);

  delay(500);
}

void loop() {
  uint16_t val1, val2, val3, val4;

  Serial.println("Moisture: ");
  val1 = moisture();
  float Val1 = val1 * 0.1;
  delay(1000);
  //Serial.print(val1);
  Serial.print(Val1);
  Serial.println(" %");
  Serial.println("-----");
  //delay(1000);

  Serial.println("Temperature: ");
  val2 = temperature();
  float Val2 = val2 * 0.1;
  delay(1000);
  //Serial.print(val2);
  Serial.print(Val2);
  Serial.println(" *C");
  Serial.println("-----");
  //delay(1000);

  Serial.println("Conductivity: ");
  val3 = conductivity();
  delay(1000);
  Serial.print(val3);
  Serial.println(" us/cm");
  Serial.println("-----");

  Serial.println("Ph: ");
  val4 = ph();
  float Val4 = val4 * 0.1;
  delay(1000);
  //Serial.print(val4);
  Serial.print(Val4);
  Serial.println(" ph");
  Serial.println("-----");

  delay(5000);
}

int16_t moisture() {
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(moist, sizeof(moist));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
  }
  Serial.println();
  return (int16_t)(values[4] << 8 | values[5]);

}

int16_t temperature() {
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(temp, sizeof(temp));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
  }
  Serial.println();
  return (int16_t)(values[4] << 8 | values[5]);

}

int16_t conductivity() {
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(EC, sizeof(EC));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
  }
  Serial.println();
  return (int16_t)(values[4] << 8 | values[5]);

}

int16_t ph() {
  uint32_t startTime = 0;
  uint8_t  byteCount = 0;

  digitalWrite(DE, HIGH);
  digitalWrite(RE, HIGH);
  delay(10);
  mod.write(PH, sizeof(PH));
  mod.flush();
  digitalWrite(DE, LOW);
  digitalWrite(RE, LOW);

  startTime = millis();
  while ( millis() - startTime <= TIMEOUT ) {
    if (mod.available() && byteCount < sizeof(values) ) {
      values[byteCount++] = mod.read();
      printHexByte(values[byteCount - 1]);
    }
  }
  Serial.println();
  return (int16_t)(values[4] << 8 | values[5]);

}

void printHexByte(byte b)
{
  Serial.print((b >> 4) & 0xF, HEX);
  Serial.print(b & 0xF, HEX);
  Serial.print(' ');
}

Do you have the datasheet for your particular sensor?
What does it say about the register addresses for the various soil parameters that it can measure?
Some sensors use 4800 baud instead of 9600 baud.



Hi @solron31 ,

Welcome to the forum..

Really would be best to create a new topic as this one is marked solved already..

the mega has 4 hardware serial ports, start by dropping the software serial as it's not needed..

Arduino Serial Reference..

good luck.. ~q

1 Like

thank you for the info! , I already posted a new topic

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