RF virtual wire sending string issue

I want to send a String written through Serial.read() to the arduino via RF virtual wire to another arduino but i get the error message:
invalid cast from type 'String' to type 'uint8_t* {aka unsigned char*}'

so what do i need to change in order to be able to send variable strings which i send to the arduino via Serial?

TRANSMITTER

#include <VirtualWire.h>


#undef int
#undef abs
#undef double
#undef float
#undef round




String msg;


void setup() {
  Serial.begin(9600);
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
  // Initialise the IO and ISR
  vw_set_ptt_inverted(true); // Required for RF Link module
  vw_setup(2000);                 // Bits per sec
  vw_set_tx_pin(7);                // pin 3 is used as the transmit data out into the TX Link module, change this as per your needs
}



void loop() {

  delay(1000);


  if (Serial.available() > 0) {

    msg = "";
    
    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }
    
    

    vw_send((uint8_t *)msg, strlen(msg) );
    vw_wait_tx();                             
    delay(200);
  }

}

RECEIVER (working fine)

#include <VirtualWire.h>    // you must download and install the VirtualWire.h to your hardware/libraries folder

#undef int
#undef abs
#undef double
#undef float
#undef round

void setup()
{
  pinMode(7, OUTPUT);
  digitalWrite(7, LOW);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  Serial.begin(9600);

  // Initialise the IO and ISR
  vw_set_ptt_inverted(true);    // Required for RX Link Module
  vw_setup(2000);                   // Bits per sec
  vw_set_rx_pin(6);           // We will be receiving on pin 4 i.e the RX pin from the module connects to this pin.
  vw_rx_start();                      // Start the receiver
}

void loop() {
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;

  if (vw_get_message(buf, &buflen)){
   
    for (int i = 0; i < buflen; i++) {

      Serial.print((char)buf[i]); 
    }
    
    Serial.println("");
  }
}

Convert your String to a char buffer (check the documentation), or better still, don't use String in the first place.

if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char *tosend[strlen(msg) + 1];

    msg.toCharArray(tosend[strlen(msg) + 1], strlen(msg + 1));



    vw_send((uint8_t *)tosend, strlen(msg) + 1 );
    vw_wait_tx();
    delay(200);
  }

gives me following error:
cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'
cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'

char *tosend[strlen(msg) + 1];

What's that?

AWOL:

char *tosend[strlen(msg) + 1];

What's that?

a char buffer ? named tosend with the length of the message (msg) plus 1 to hold the NULL.

By my reading, it's an array of pointers to char.

okay, so how do i convert the string msg into something which the line

vw_send((uint8_t *)

can work with?

all i am trying to do is send the string msg via virtual wire.......

if (Serial.available() > 0) {

    msg = "";
   
    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

Before we go on, I need to ask how many times you expect that while loop to iterate?
i.e. how many characters do you think "msg" will contain?

it is variable because it depends on the length of the message. let's start with 100 characters maximum.

No, let's start with one character.

I asked how many time you thought the while loop would run, not how many character in total you would receive.

okay, starting with one character. means i have one byte and that means the while loop will run one time?

You extract data from a String like so:

   String pissingAwayResources = "Do NOT use Strings!";

   char doThis[80];
   pissingAwayResources.toCharArray(doThis, sizeof(doThis));

A good read:
https://forum.arduino.cc/index.php?topic=288234.0

if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char *tosend[msg.length() + 1];

    msg.toCharArray(tosend[msg.length() + 1], sizeof(tosend));


    vw_send((uint8_t *)tosend, msg.length() + 1);
    vw_wait_tx();

    delay(200);
  }

this code still doesn't work. the receiver prints out only questionmarks, if anything.

this code still doesn't work.

Of course not, because you are NOT paying attention.

You are creating an array of pointers to chars, NOT an array of chars. Why the hell are you doing that?

if i create an array of chars i get "invalid conversion from 'char' to 'char*'"

if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char tosend[msg.length() + 1];

    msg.toCharArray(tosend[msg.length() + 1], sizeof(tosend));


    vw_send((uint8_t *)tosend, msg.length() + 1);
    vw_wait_tx();

    delay(200);
  }

problem solved. thank you!!!

if (Serial.available() > 0) {

    msg = "";

    while (Serial.available()) {
      char received = Serial.read();
      msg += received;
    }

    char tosend[msg.length() + 1];

    msg.toCharArray(tosend, sizeof(tosend));

    


    vw_send((uint8_t *)tosend, msg.length() + 1);
    vw_wait_tx();

    delay(200);
  }

TobiasCrackz:
if i create an array of chars i get "invalid conversion from 'char' to 'char*'"

if (Serial.available() > 0) {

msg = "";

while (Serial.available()) {
     char received = Serial.read();
     msg += received;
   }

char tosend[msg.length() + 1];

msg.toCharArray(tosend[msg.length() + 1], sizeof(tosend));

vw_send((uint8_t *)tosend, msg.length() + 1);
   vw_wait_tx();

delay(200);
 }

If you do it right, you won't. Look at your call to toCharArray() and my call to toCharArray() in reply #11. The first argument to toCharArray() is NOT the first element beyond the array (which is a char). It is the array.

yupp, works. thanks pauls!!!