1

I'm working on RF transmitter and receiver modules using two Arduino UNOs. I'm getting an additional noise on the receiver side in serial.println. To be more clear about the question. Hre is my transmitter code

// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup()
{
    // Initialize ASK Object
    rf_driver.init();
    Serial.begin(9600);
}
 
void loop()
{
  
    const char *msg = "Hello World";
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
    Serial.println((char *)msg);
    delay(1000);
}

Here is my receiver code

// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup()
{
    // Initialize ASK Object
    rf_driver.init();
    // Setup Serial Monitor
    Serial.begin(9600);
}
 
void loop()
{
    // Set buffer to size of expected message
    uint8_t buf[11];
    uint8_t buflen = sizeof(buf);
    // Check if received packet is correct size
    if (rf_driver.recv(buf, &buflen))
    {
      
      // Message received with valid checksum
      Serial.print("Message Received: ");
      Serial.println((char*)buf);         
    }
}

And here is my Serial monitor in receiver side enter image description here

So, can somebody help me to sort out this issue

1 Answer 1

1

You're treating a raw buffer as a string.

In C a string is an array of characters (11 in your case) terminated by a NULL character. You receive 11 bytes into an 11 byte array and then try and print it - without any terminating character - so the output "overruns" into the memory following your array.

You need a buffer of 12 bytes not 11, and you have to make sure that the 12th byte (byte number 11) is zero.

Or use Serial.write(buf, buflen); function to write exactly 11 bytes to the serial.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.