HC12 receives weird stuff

Hi! I wrote a little sketch to try out wireless communication between two Arduinos using HC12 modules, where the Master sends every 2 seconds this string: M,0,255,0,E. The slave Arduino also receives the message everytime, but for some reason, it mostly isn't 100% the same I sent, and contains weird letters.
Can anyone help me?

Master code:

if(sending == true){
    Serial.println("sending");
    send();
    delay(2000);
  }
}

void send() {
  String Main = "M," + String(first[0]) + "," + String(first[1]) + "," + String(first[2])+ ","+"E";
  //7char c[100];
  //Main.toCharArray(c, Main.length());
  HC12.print(Main);
}

Slave code:

void loop() {
  receive();
  delay(20);
}



void receive() {

  if (HC12.available()) {
    delay(10);
    Main = "";
    while (HC12.available()) {
      byte b = HC12.read();
      Main += char(b);
      //Main = HC12.readString();
    }
    Serial.println(Main);

Using the methods of the excellent tutorial Serial Input Basics will solve your issues.

I don‘t want to be mean, but have you even read what I wrote? I know how Serial communication works, even tried the same code with two Arduinos connected to each other by cable, and it worked perfectly. I think that the problem has something to do with the HC12 modules.

Can you post all of the code for both sending and receiving.

Do the baud rates match?

Your receive code does not handle serial data very well. At 9600 baud, the data is coming in slowly... and the Arduino will process it faster than it arrives, so you won't always have a complete message before printing it out.

It is quite a bad idea to use Strings on AVR-based Arduinos. They corrupt memory and cause program crashes.

Follow the advice given in post #2 to avoid using Strings.

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