HC-12 receiver freezing when powered up

Hello,

I'm working with a set of arduinos with HC-12 modules and have found a strange behavior in the Receiving module when powered up after the Transmitter.

My code simply tests the connection between the 2 arduinos by which the Tx module repeatedly sends a string of "Testing" and the Rx module blinks when it receives the string.
My problem is that when the Rx module is powered on after the Tx module is already on, it freezes and does not react to the signal. When the Tx module is powered on second, they both work just fine. It's as if the Tx is interrupting the Rx.
Thanks for your help!

Here is my code:
---Transmitter---

#include <SoftwareSerial.h>
#define setPin 4

SoftwareSerial HC12(3,2);         // HC12 TX Pin, HC-12 RX Pin
byte incomingByte;
String readBuffer = "";


void setup() {
  pinMode(LED_BUILTIN, OUTPUT); 
  
  HC12.begin(9600);               // Open serial port to HC12
  
  pinMode(setPin, OUTPUT);
  
  digitalWrite(setPin, LOW);      // Put HC12 in Command Mode
  delay(250); 
  HC12.write("AT+DEFAULT");       // Set default settings
  delay(250);
  digitalWrite(setPin, HIGH);     // Put HC12 in Transparent Mode
  delay(100);
}

void loop() {
  HC12.write("Testing");
  delay(100); 
  
    digitalWrite(LED_BUILTIN, HIGH);      // turn the LED on
    delay(50);                                // wait
    digitalWrite(LED_BUILTIN, LOW);       // turn the LED off
    delay(70); 

    delay(500);
}

---Receiver---

#define setPin 4

byte incomingByte;
String readBuffer = "";


void setup() {
  pinMode(LED_BUILTIN, OUTPUT); 

  Serial.begin(9600);             // Open serial port to computer
  
  pinMode(setPin, OUTPUT);
  
  digitalWrite(setPin, LOW);      // Put HC12 in Command Mode
  delay(250); 
  Serial.write("AT+DEFAULT");       // Set default settings
  delay(250);
  digitalWrite(setPin, HIGH);     // Put HC12 in Transparent Mode
  delay(100);
}

void loop() {
  while(Serial.available()){               // If HC12 has data
    incomingByte = Serial.read();         // Store each incoming byte from HC12
    readBuffer += char(incomingByte);   // Add each byte to readBuffer string variable
  }
  delay(100);
  
  if (readBuffer == "Testing"){
    digitalWrite(LED_BUILTIN, HIGH);      // turn the LED on
    delay(50);                                // wait
    digitalWrite(LED_BUILTIN, LOW);       // turn the LED off
    delay(70);
  }
  
   readBuffer = "";
  delay(500); 
}

You need to get the transmitter and receiver synchronized.
Read the data until 'T' is found then read the rest of the data.

In the long run you will need to send a sync byte like an 0xFF and a terminating charter like a 'cr'.

I'm not sure if I understand what you mean in the last line but I think I get the idea.

Rather than sending "Testing", I should send something like"" where the beginning and end are clearly marked to avoid the receiver seeing a jumbled message like "stingTe".

You get the idea.
This may be overkill for your application but it will packetize your data and will enable you to send any kind of data.

Some protocols send a start byte like an 0xFF then after that a byte that specifies the number of bytes to follow so the receiver know how much data to read

Avoid using String objects, as they cause program malfunctions and crashes on Arduinos with small amounts of memory.

The Serial Input Basics tutorial is a good place to start, and to learn how to communicate without using Strings.

This is simple and works

// Transmitter needs to send 0xFF followed by a byte count then the data bytes
// FF 5 Test9

#define setPin 4

byte incomingByte = 0;
String readBuffer = "";


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);             // Open serial port to computer

  pinMode(setPin, OUTPUT);

  digitalWrite(setPin, LOW);      // Put HC12 in Command Mode
  delay(250);
  Serial.write("AT+DEFAULT");     // Set default settings
  delay(250);
  digitalWrite(setPin, HIGH);     // Put HC12 in Transparent Mode
  delay(100);
}

void loop() {

  while (incomingByte != 0xFF) incomingByte = Serial.read();
  //Serial.print(incomingByte);

  while (Serial.available() == 0);
  int num_of_bytes = Serial.read();

  for (int i = 0; i < num_of_bytes; i++)
  {
    while (Serial.available() == 0);
    incomingByte = Serial.read();
    readBuffer += char(incomingByte);
  }

  //Serial.print(readBuffer);
  //Serial.println();

  if (readBuffer == "Testing") {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(50);
    digitalWrite(LED_BUILTIN, LOW);
    delay(70);
  }

  readBuffer = "";
  incomingByte = 0;
  delay(500);
}

Hello raze1347

Take a view here to gain the knowledge.

1 Like