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!
