1

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]);
  }
}
김성빈
  • 11
  • 2
  • Which sensor are you using? Which RS485 interface are you using? – fpiette Jul 01 '21 at 07:45
  • @김성빈 - This language is not C. – Armali Jul 01 '21 at 08:52
  • 1
    @Armali - I'm sorry. Thank you. – 김성빈 Jul 01 '21 at 10:44
  • @fpiette - KSM-8900 is using a soil sensor from a Korean company and RS-485 has ESP-8266 attached to a regular board. https://github.com/Kimseongbeen/korea-digital/blob/main/KSM-8900/KSM8900%20%EC%82%AC%EC%9A%A9%EC%9E%90%EC%84%A4%EB%AA%85%EC%84%9C.pdf This address is the soil sensor manual I put on my github. – 김성빈 Jul 01 '21 at 10:47

1 Answers1

0
    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.

Armali
  • 18,255
  • 14
  • 57
  • 171
  • 1
    Thank you. As you said, the sensor value will be printed normally as you revised it. Thank you again. – 김성빈 Jul 01 '21 at 10:41