HC-12 two way communication transmission time too long

Hi everyone,

I'm relatively new to the Arduino world and have limited experience with programming. My undergraduate course is not in the field of programming or electronics. I only started to use Arduino because of a Uni program which involves the use of Arduino.

Essentially, I'm trying to test the capability of radio in two-way air-water communication. Before that, I need to test it in an air-air situation. I bought several radio modules: HC-12, nrf905, RFM69HCW, which all run at 433MHz. I can find two-way communication sample code for nrf905 and RFM69HCW in their respective libraries (ping-client/server from nrf905 library and RadioHead69_RawDemo_TX/RX from Radiohead). And I modified both code to send and receive a 32-character long message 32 times. And in these 32 times, record the number of successful/unsuccessful transmissions. Unsuccessful means the message that's being sent back is not the same as the message being sent out.

I intend to do the same for the HC-12 module (wiring following this) and wrote the following code with my teammate for both RX and TX.

This is for sender:

#include <SoftwareSerial.h>

#define BAUD 9600               //radio baud rate

#define PAKLEN 32               //length of data packets
#define PAKNUM 32               //size of data packets
#define PAKSIZE (PAKLEN*PAKNUM) //total size of data

char packetbuffer[PAKNUM][PAKLEN];//buffer to store packets in
char recbuffer[PAKLEN];//buffer to store recieved data

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(BAUD);               // Serial port to HC12

  HC12.setTimeout(1000); //time to wait for packets//adjust as small as possible
  randomSeed(analogRead(0));
}

void loop() {
  
  makePackets();//generate random data to send
  
  Serial.println("Sending Packets");
  
  uint8_t fail = 0;//count number of packets droped
  uint32_t tik = micros();//get time at start of transmission
  for(uint8_t i = 0; i < PAKNUM; i++){//send all packets and get response
      HC12.write(packetbuffer[i], PAKLEN);//send packet
      
      uint8_t bytesRead = 0; 
      bytesRead = HC12.readBytes(recbuffer, PAKLEN);//read reply
      
      if(bytesRead != PAKLEN){
        fail++;
      }//check for correct length

      else if(not checkPacket(i)){
        fail++;
        }//check for correct contence
  
  }
  uint32_t tok = micros();//get time at end of tranmission
  uint32_t time = tok - tik;//fine time taken
  
  //print results
  Serial.println("=========Results=========");
  Serial.println("Time: ");
  Serial.print(time);
  Serial.println(" us");
  Serial.println("Packet Transmitted: ");
  Serial.print((PAKNUM - fail));
  Serial.println();
  Serial.println("Packet Dropped: ");
  Serial.print(fail);
  Serial.println();
  Serial.println();
  
  delay(2000);

}

bool checkPacket(uint8_t paknum){//checks if recieved packet matches sent
    for(uint8_t i = 0; i< PAKLEN; i++){
        if(recbuffer[i] != packetbuffer[paknum][i]){
          return false;
          }
    }
    return true;
}

void makePackets(void){//makes random packets to send
    
    for(uint8_t i = 0; i < PAKNUM; i++){
    for(uint8_t j = 0; j < PAKLEN; j++){
        packetbuffer[i][j] = (uint8_t) random(65,90);
    }    
    }    
}

This is for receiver:

#include <SoftwareSerial.h>

#define BAUD 9600               //radio baud rate
#define PAKLEN 32               //length of data packets  

char buffer[PAKLEN];

SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  //Serial.begin(9600);
  HC12.begin(BAUD);               // Serial port to HC12
  
  HC12.setTimeout(100);          //time to wait for packets//adjust as small as possible
}

void loop() {
    uint8_t bytes = HC12.readBytes(buffer, PAKLEN);
    if (bytes != 0){
      //Serial.println(buffer);
      HC12.write(buffer, PAKLEN);
      memset(buffer, 0,32);
    }
}

So with this code, I got a transmission time at the scale of seconds, around 4s for the HC-12 module (when the two modules are close to each other) which seems very long. Because with the other two modules, 32 times of transmission only took around several hundred milliseconds (also close to each other). My teammate and I cannot figure out why as we are both new to this area.

Any help would be appreciated. Thanks!

The HC-12 is intended to be used as an asynchronous, wireless serial link, not packet-oriented like some of the other radios.

It is an excellent idea to check whether data are actually available to be read, before attempting to perform the read operation. See the Serial Input Basics tutorial, elsewhere on the forum, and study the code examples in the tutorial you linked.

void loop() {
      uint8_t bytesRead = 0; 
      bytesRead = HC12.readBytes(recbuffer, PAKLEN);//read reply

Hi jremington,

Thanks so much for the reply. I added in the lines to check the availability and got rather strange results. But I'll study the Serial Input Basics tutorial and try again.

As for testing the transmission quality of the module, since HC-12 is not packet-oriented, what can be a more suitable way of quantifying the transmission quality?

Thank you so much again.

Hi,
Why so many modules to test 433MHz.
Shouldn't just one pair be sufficient?
What parameters are you studying?

Have you read the data on the HC-12,you can control the TX baud .

What antennas are you using?

Tom... :grinning: :+1: :coffee: :australia:

Have you read the HC12 module datasheet ?

You need to define "transmission quality", before you can quantify it.

What is this about?

I'm trying to test the capability of radio in two-way air-water communication.

Thank you Tom for your response.

Why so many modules to test 433MHz.
Shouldn't just one pair be sufficient?t

We are going to implement the module onto another device we have built. So we were just testing which module could communicate the fastest.

Have you read the data on the HC-12,you can control the TX baud .

I don't currently have any HC-12 with me. Waiting for a new shipment since I broke my old ones. However I tested this on the HC-05 module and changing the baud rate indeed works. Though the highest baud rate supported is 57600 (I was using a mega).

I don't know what antennas I was using, but the length of it is slightly less than half of the module.

Hi,

You had better research what antenna to use on your modules at that frequency as the antenna gain, radiation pattern and polarization at 433 will have a major effect on your results.

Google;

antennas 433 MHz arduino

Tom.. :grinning: :+1: :coffee: :australia:

I think for transmission quality, I was just using a packet-oriented approach, just to see how many packets would get lost during my 32 times of transmission.

What is this about?

We have a device that is slightly submerged in water and another device above water, just want to make them talk to each other using radio.

Thanks Tom. I'll look into it. I didn't know antennas could play such an important role in this.