virtualWire String subStrings problems /!\

Hi, nice to meet you !

I'm currently building a transmitter with VirtuaWire for a little submarine Drone, i want to tell him for example "for this period of time you will go up" and some other stuff. To do that i use some radio emitter-receiver (low frequence usualy work underwater, as long i'm not too deep).

But i have a problem: when you send the message, you do not send a simple String, it is more a char buffer with the size of a String or something right?

So when i receive the message i want to do this for example (this is in loop):

if (coms.substring(3) == "M1r" && mode == 1) {
    sayM("M1r reçus", MOTOR1); // Detected but never works
    nombres = coms.substring(5, 8); //I'm transfering the number to an other String to be sure
    M1r = nombres.toInt(); //Motor will go "on" for this amount of time
    sayM("COMS recus = ", COMS); //Finish the lines so i know everything went fine
  }

coms is the string that i receive from this in my virtualWire fonction :

I've got this before my setup:

typedef struct {
  String commande;
  int valeur;
} MaStructure;
  MaStructure message;
  byte taille_message = sizeof(MaStructure);
  
  vw_wait_rx_max(5000);
  //vw_wait_rx();

  if (vw_get_message((byte *) &message, &taille_message)) {
    radioToComs = message.commande; //I simply put the string from the message into a string more stable
  }

I know this fonction is working because i try some stuff like:

if (coms == "bonjour"){
Serial.println("hello world");
}

But when i use the substring, nothing is working correctly... so i think it is a little bit tricky no?

If you have any thinking about that, i would be happy to here them :slight_smile:

Yes, i forgot: i have this before my setup:

typedef struct {
String commande;
int valeur;
} MaStructure;

But i have a problem: when you send the message, you do not send a simple String, it is more a char buffer with the size of a String or something right?

Wrong. Forget that the stupid String class exists. What you send, and receive, is an array of bytes.

A byte is the same size as a char, so you could think of sending and receiving arrays of chars. But an array of chars is NOT a String, and does not have a substring method.

You could make the array of chars into a string (which is NOT the same thing as a String), by adding a NULL terminator in the proper position in the array. That's why the number of elements in the array is required on the sending end and provided on the receiving end.

Do NOT use Strings in your code. Learn to use strings, instead.

Okay i see... more like this:
string - Arduino Reference ?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

You will also make life easier for yourself if you make the messages as short as possible. Just using a single alphanumeric character gives you 62 options.

...R