Arduino data type confusion. Have string, need const char?

203 views Asked by At

I'm working with an UNO and I'm using maniacbug's RF24 library with soft spi, and his rf24network library. https://github.com/maniacbug/RF24Network

All I'm trying to do is send data but I'm stuck with the wrong data type.

{
last_sent = now;

printf("Sending...\r\n");
const char* hello = "Hello, world!";
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,hello,strlen(hello));
if (ok)
  printf("\tok.\r\n");
else
{
  printf("\tfailed.\r\n");
  delay(250); // extra delay on fail to keep light on longer
}
}

If I change it to

bool ok = network.write(header,myString,strlen(myString));

I get an error on data type. So how do I get my string to fit this structure. My thought was to to a myString.toCharArray then maybe loop through that?

1

There are 1 answers

0
P.P On BEST ANSWER

Convert the String into a char array first:

size_t len = myString.length();
char buf[len+1]; // +1 for the trailing zero terminator
myString.toCharArray(buf, len);

and then you can pass buf:

bool ok = network.write(header,buf,strlen(buf));