Getting INPUTS from HC-12

Hi, i am trying to Send and Receive comands via HC-12.

This is the code that's suposed to receive comands from the HC-12 (command is a String variable), but for some reason it always showes the error message even if the comand that he has just received correspods to one of the options. How can i Solve this issue? Thanks, Luca.

void GetComands() {
  if (radio.available()){
    command = radio.readStringUntil('\n');
    Serial.println("the command is " + command + ".");

    if(command.equals("AutoSequence 1")){
      AutoSequence1();
    }else if(command.equals("AutoSequence 2")){
      AutoSequence2();
    }else if(command.equals("AutoSequence 3")){
      AutoSequence3();
    }else Serial.println("Error");
  }
}

Please copy to the forum the real output from this code that you see in the IDE Monitor

The message is:

the command is AutoSequence 1.
Error

The fact that the condition does not work obviously means that the command line and the line in the condition are not equal to each other.
Since they look the same on print, it means there are non-printable characters in the received string. Try printing the received data character by character in hexadecimal format

How can i do that?

void GetComands() {
  if (radio.available()){
    command = radio.readStringUntil('\n');
    Serial.println("the command is " + command + ".");
    for (int i =0; i < command.length(); i++) {
      Serial.println(command[i], HEX);
       }
    Serial.println();
    if(command.equals("AutoSequence 1")){
      AutoSequence1();
    }else if(command.equals("AutoSequence 2")){
      AutoSequence2();
    }else if(command.equals("AutoSequence 3")){
      AutoSequence3();
    }else Serial.println("Error");
  }
}

This is what it shows now:

the command is AutoSequence 1.
41
75
74
6F
53
65
71
75
65
6E
63
65
20
31
D

Error

As I expected, you have a service character with a code 0xD at the end of the command = CR (carriage return)
You need to either remove it from the command on reception, or not send it on transmission

try this:

void GetComands() {
  if (radio.available()){
    command = radio.readStringUntil('\n');
    Serial.println("the command is " + command + ".");
  
    // removing '\r' if needed
    if ((command.length() > 0) &&
           (command[command.length() -1] == 0xD)) 
              command.remove(command.length() -1); 
  
    if(command.equals("AutoSequence 1")){
      AutoSequence1();
    }else if(command.equals("AutoSequence 2")){
      AutoSequence2();
    }else if(command.equals("AutoSequence 3")){
      AutoSequence3();
    }else Serial.println("Error");
  }
}

Now it seams working but only forr one of the four comands that it's suposed to receive (in the code i shared u can see only 3 because the other one is in another part of the program). How can i integrate the other comands?

Please show all the code and clarify the problem

Problem solved, i simply transmitted shorter comands (like 0 - 1 - 2 - 3) instead of more complex strings and now its working.
Thanks, Luca.

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