convert String to uint8_t *

Hello I want send a String with virtualwire but the message must be uint8_t *. The String is msg = "267" + analogRead(A1);

and the command to send the message : vw_send((uint8_t *)msg, strlen(msg));

How to convert a String to uint8_t * ?

1 Like

Is there supposed to be anything between the “267” and the number coming back from analogRead() when you send it?

no :)

So, why is it necessary to send the “267”?

Using a string (a NULL terminated array of chars), not a stupid String.

PaulS: So, why is it necessary to send the "267"?

Using a string (a NULL terminated array of chars), not a stupid String.

Perhaps you should say "c-string (a NULL terminated array of chars)".

zoomkat: Perhaps you should say "c-string (a NULL terminated array of chars)".

Or not. PHP uses the same type of NULL-terminated array of chars, and I'm not going to drag the language into the term, when it is irrelevant.

267 is the id of the arduino. Another arduino will have the id 268 or 269 ... The arduino must send this id and the analogRead

Neilmine: How to convert a String to uint8_t * ?

With CStr.

By the way, I didn't know that. I looked at the String documentation. It took me about 20 seconds. You should get in the habit.

i tried :

String message; const char *msg; message="267" + analogRead(A1); msg = message.c_str(); vw_send((uint8_t *)msg, strlen(msg));

but the message send is "267" witout the value of the analogread

String myMessage = "xx";
char charMsgToSend[3];
myMessage.toCharArray(charMsgToSend, myMessage.length()+1);
vw_send((uint8_t *)charMsgToSend, strlen(charMsgToSend));

You didn't initialize the String message properly. This works:

String message;
const char *msg;

void setup()
{
  Serial.begin(9600);
  delay(100);
  message = "267";
  message = message + analogRead(A1);
  msg = message.c_str();
  Serial.println("message:");
  Serial.println(msg);
}

void loop()
{}

An alternative to aarg's solution might be:

char msg[10];

void setup()
{
  char temp[5];
  
  Serial.begin(9600);
  delay(100);
  strcpy(msg, "267");
  itoa(analogRead(A1), temp, 10);
  strcat(msg, temp);
  Serial.println("message:");
  Serial.println(msg);
}

void loop()
{}

The c string version above uses 2250 bytes versus 4062 for the String class version.

econjack: An alternative to aarg's solution might be: [...]

It would be my solution too. I never use the String class.

the econjack’s program, modified, works :slight_smile: :

#include <VirtualWire.h>
char msg[10];
int value;

void setup()
{
  vw_setup(2000);
  vw_set_tx_pin(3);
  Serial.begin(9600);
}

void loop(){
  char temp[5];
  value=analogRead(A1);
  
  if (value>=1000){    strcpy(msg, "267");}
  else if (value>=100){strcpy(msg, "2670");}
  else if (value>=10){ strcpy(msg, "26700");}
  else{                    strcpy(msg, "267000");}
  
  itoa(value, temp, 10);
  delay(500);
  strcat(msg, temp);
  
  Serial.print("message : ");
  Serial.println(msg);
  
  vw_send((uint8_t *)msg, strlen(msg));
  vw_wait_tx();
}

argg’s program sent just 267

Neilmine: argg's program sent just 267

I tested it here, I don't know why it didn't work for you. Did you change it at all? The output is:

message:
267467

aarg's code worked for me, too, albeit with a larger footprint because of the String class.

the message in serial is 267467 or 2671023 or …
But the message sent by virtualwire, or rather the received message by another Arduino, is 267

That's your code, not mine. You obviously missed something in adapting it.

Neilmine: The String is msg = "267" + analogRead(A1);

This will not do what you think it does, and that's why I dislike C++ operator overloading. "267" is not a C++ String object, and so the + operator does not do a string append.