Transmitting strings of variable length through RF?

Hello! I am trying to send 4 numbers through RF, currently I am doing this using the radiohead library.

The first 3 numbers range from -100 to 100, while the 4th is just 1 or 0. They are all seperated by comma:

  X = analogRead(A0);  
  Y = analogRead(A1);
  Z = analogRead(A2);
  Btn = analogRead(A3);
  X = map(X, 0, 1024, 100, -100);
  if (X <= 5 && X >= -5)
    X = 0;
  Y = map(Y, 0, 1024, 100, -100);
  if (Y <= 5 && Y >= -5)
    Y = 0;
  Z = map(Z, 0, 1024, -100, 100);
  if (Z <= 5 && Z >= -5)
    Z = 0;
  if (Btn <= 1000)
    Btn = 0;
    Btn = 1;

  str_x = String(X);
  str_y = String(Y);
  str_z = String(Z);
  str_btn = String(Btn);
  str_out = str_x + "," + str_y + "," + str_z + "," + str_btn;

  static char *msg = str_out.c_str();
  rf_driver.send((uint8_t *)msg, strlen(msg));


The serial print from the transmitter confirms I am getting the syntax I am expecting.

However, the guides I’ve found expect specific char lengths on the receiving end, and I am unsure of how to handle it, as my string/char can vary from 7 characters to 16.

I’ve tried using the max amount expected and ignoring the nulls when breaking up the received message, but I keep getting strange issues with the message getting “stuck” and repeating until I reset the transmitter when I change the input around. Any ideas?

Like, if I press the button that changes the 4th value from 0 to 1, it works fine. But if move the other inputs around so that the values increase and decrease, it will suddenly “freeze” and repeat the same message received. I’ve had a look at the serial output from the transmitter, and I cannot see any issues, it changes the values fine, just as I expect, but as soon as I try to read lots of changes it on the receiving end, it keeps getting stuck and sending/receiving the same message over and over.

Also, I am having a bit of a brainfart, and have never been good at dealing with syntaxes for how to parse strings etc. This code currently handles the first 2 numbers, but how do I make it parse and seperate all 4? Currently the remaining numbers will be displayed through the Y value.

#include <RH_ASK.h>
#include <SPI.h> 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;

String str_x, str_y, str_z, str_btn, str_out;
void setup()
void loop()
    // Set buffer to size of expected message
    uint8_t buf[16];
    uint8_t buflen = sizeof(buf);
    //Serial.print("Message Received: ");
    // Check if received packet is correct size
    if (rf_driver.recv(buf, &buflen))
      str_out = String((char*)buf);
      for (int i = 0; i < str_out.length(); i++) 
        if (str_out.substring(i, i+1) == ",") 
          str_x = str_out.substring(0, i);
          str_y = str_out.substring(i+1);/*
          str_z = str_out.substring(??);
          str_btn = str_out.substring(??);*/
      Serial.print("X: ");
      Serial.print("  - Y: ");
      Serial.print("  - Z: ");
      Serial.print("  - Btn: ");
      Serial.println(str_btn);    */  


All you need is to transfer four 8-bit signed variables ? You could transmit and receive an array of four bytes: char data[4];

Aha, I suppose that could simplify matters, but I'm afraid I am not used to how to use the char variable.

I successfully converted my inputs into an array of 4 chars, but how do I send it?

Do I still convert and send it as a string, do I just send the array plainly? And how do I deconstruct it on the receiving end?

Like this? ("inputs" is the array of 4 chars):

rf_driver.send((uint8_t *)inputs, sizeof(inputs));


  str_out = String(inputs[0]) + String(inputs[1])+ String(inputs[2]) + String(inputs[3]);
  static char *msg = str_out.c_str();
  rf_driver.send((uint8_t *)msg, strlen(inputs));

Do I convert each digit back into an int on the receiving end?

Edit: Just discovered that the send command supports sending ints, so I don't necessarily need to mess with char, but is sending a single letter simpler for RF transceivers, than up to 4 digits?

Edit: I just got it working by sending and receiving an int array, your tip really sent me on the right track, thanks! But do you still suggest I use Char?

You can send (or receive) anything using this general format, but make sure that "sizeof()" is actually the number of bytes to be sent. Note the "&" -- not needed for arrays.

rf_driver.send((uint8_t *)&inputs, sizeof(inputs));

What does the & do in this setting?

What does the & do in this setting?

Time to learn about Pointers.

What does the & do in this setting?

It gets the address in memory of the variable.