sending a message with 433 RF Modules - extra character at the end

hi!

I am testing some RF modules using this video as guidance: https://youtu.be/b5C9SPVlU4U

Full article: Using 433MHz RF Modules with Arduino | DroneBot Workshop

In the example, an arduino board is sending a message to another. Thing is, when I try it, I have a "Null" at the end of the message, and I would like to get rid of it. (see image attached)

Any ideas?

The guy in the video is using the same code for his example, and he has no "Null" character at the end of the message...

code transmitter:

/*
  433 MHz RF Module Transmitter Demonstration 1
  RF-Xmit-Demo-1.ino
  Demonstrates 433 MHz RF Transmitter Module
  Use with Receiver Demonstration 1

  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/

// 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();
}

void loop()
{
    const char *msg = "Welcome to the Workshop!";
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
    delay(1000);
}

code receiver:

/*
  433 MHz RF Module Receiver Demonstration 1
  RF-Rcv-Demo-1.ino
  Demonstrates 433 MHz RF Receiver Module
  Use with Transmitter Demonstration 1

  DroneBot Workshop 2018
  https://dronebotworkshop.com
*/

// 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[24];
    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);         
    }
}

In the example, an arduino board is sending a message to another. Thing is, when I try it, I have a "Null" at the end of the message, and I would like to get rid of it. (see image attached)

No, the problem is that you did not transmit a Null at the end of the message and the received message is not terminated in the receive buffer with a Null. When working with character arrays for printing and using the c-string functions, the terminating Null is critical.

const char *msg = "Welcome to the Workshop!";

This message is 24 characters long, plus a null terminator. The declaration with the quotation marks add the null terminator. The message to be sent is 25 bytes. Strlen does not include the null terminator, so use

rf_driver.send((uint8_t *)msg, strlen(msg) + 1);

On the receiver side, declare

uint8_t buf[25];

Then you will print out a clean message.

Message Received: Welcome to the Workshop!

thank you cattledog

Now I get a clean message.

I wonder why the example in the video shows a clean message without having these modifications in the code.

If there happens to be a value 0 in the memory address right after where the string is stored, that will be seen as the null terminator by the print function, which itself has no idea how long the string is supposed to be so it's just reading the memory byte by byte until a 0 is found.

Of course you can not rely on this to be the case, either way. Different compilers can give different results for the same piece of code, depending on how that compiler happens to organise the memory.

I wonder why the example in the video shows a clean message without having these modifications in the code.

Few people post videos showing code or circuits working incorrectly.

The author got lucky. If the code fails to zero-terminate a character string, it IS wrong, but might work sometimes.

thanks guys!