Arduino Soil Sensor RS-485 Protocol Response Value Conversion Problem

269 Views Asked by At

I'm studying protocol with a soil sensor, but the value is not numerical or texted.
TT
code

 Serial.print(inChar,HEX);
  for(int i=0;i<9;i++){
    Serial.println(inputString.charAt(i));
   }

result

14600AD700D373
1
4
6
F [3]
A7 [4]
B [5]
54 [6]
0 [7]
6 [8]
D3 //CRC
73 //CRC

code

ss=((int)inputString.charAt(3)*256+(int)inputString.charAt(4))/100;
    Serial.println("MO : "+ss);
    ss=((int)inputString.charAt(5)*256+(int)inputString.charAt(6))/100;
    Serial.println("TEM : "+ss);
    ss=((int)inputString.charAt(7)*256+(int)inputString.charAt(8))/100;
    Serial.println("EC : "+ss);
    inputString="";
    Serial.println("");

result

MO : 
TEM :  → //Characters such as arrows, not numbers
EC :  //no answer

The Arduino serial monitor omitted the previous zero value.

inputString.charAt[3]=0F,
inputString.charAt[4]=A7,  
inputString.charAt[5]=0B,
inputString.charAt[6]=54,  
inputString.charAt[7]=00,
inputString.charAt[8]=06

Combining inputString[3],[4] and dividing it by 100, the rest [5][6], [7][8] are temperature and EC, respectively.
ex) 0FA7(16) -> 4007(10) 4007/100 -> 40.07%VMC
0B54(16) -> 2900(100) 2900/100 -> 29'C

I wrote a code to convert sensor values to values we know, but the values don't output normally.
Maybe the size of the char variable is up to 256 so there is an error. I tried to change int to float and tried again, but the value didn't come out.
Do you have any idea what the problem may be? Please help me!

My Arduino Sketch

#include <Ticker.h>
#include "CRC.h"
#include <SoftwareSerial.h>
Ticker ticker;
SoftwareSerial mySerial(D7, D4); // RX, TX

String inputString = "";         
int counter=0;

void tick()
{
 // Serial.println ( WiFi.localIP() );
  counter++;
  crd16Rtu();
}

void setup() {
  Serial.begin(115200);
  mySerial.begin(115200);
  ticker.attach(3, tick); 

}

void loop() {
  serialEvent();
}

void serialEvent() {
  if(mySerial.available() == false)
    return;
  while (mySerial.available()) {
    // get the new byte:
    char inChar = (char)mySerial.read();
    Serial.print(inChar,HEX);
    // add it to the inputString:
    inputString += inChar;
  }
  Serial.println("");
  if(inputString.length() >= 11) {
    String ss="";
   for(int i=0;i<9;i++){
    Serial.println(inputString.charAt(i));
   }
    
    ss=((int)inputString.charAt(3)*256+(int)inputString.charAt(4))/100;
    Serial.println("MO : "+ss);
    ss=((int)inputString.charAt(5)*256+(int)inputString.charAt(6))/100;
    Serial.println("TEM : "+ss);
    ss=((int)inputString.charAt(7)*256+(int)inputString.charAt(8))/100;
    Serial.println("EC : "+ss);
    inputString="";
    Serial.println("");

  } 
}

// RS485 out
void crd16Rtu() {
  char str[24] =  {0x01,0x04,0x00,0x07,0x00,0x03,0x00,0x00};  //[1,4,0,7,0,3,0,0],
  String s;
  int si,sj,len;

  len=6;
  
  uint8_t * data = (uint8_t *) &str[0];
  si=crc16(data, len, 0x8005, 0xFFFF, 0x0000, true,  true  );
  sj=si&0xff;
  str[len]=sj;
  sj=si>>8;
  str[len+1]=sj;

  for(int i=0;i<len+2;i++) {
    mySerial.print(str[i]);
    //Serial.println((int)str[i]);
  }
}
1

There are 1 best solutions below

1
On
    Serial.println("MO : "+ss);

It seems you missed this in the String Addition Operator tutorial:

Caution: You should be careful about concatenating multiple variable types on the same line, as you may get unexpected results. For example:

int sensorValue = analogRead(A0);
String stringOne = "Sensor value: ";
String stringThree = stringOne + sensorValue;
Serial.println(stringThree);

results in "Sensor Value: 402" or whatever the analogRead() result is, but

int sensorValue = analogRead(A0);
String stringThree = "Sensor value: " + sensorValue;
Serial.println(stringThree);

gives unpredictable results because stringThree never got an initial value before you started concatenating different data types.

This is indeed an unintelligible behavior of the strange Arduino programming language.