Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino joining string and *char

I'm new to arduino and I have stumbled upon a problem. I want to send data via my esp8266 to my php page. But I don't know how to join my data with this GET request.

Here is my code:

String card = "2-3d-fg-d6-12-68-32-3f-35-45-42-53-2a-3";
char *hello = "GET /insert.php?card="+card+"&error=1 HTTP/1.1\r\nHost: testsite.com\r\n\r\n"; 
wifi.send((const uint8_t*)hello, strlen(hello));

And this is what I get in arduino console:

error: cannot convert 'StringSumHelper' to 'char*' in initialization cannot convert 'StringSumHelper' to 'char*' in initialization

like image 921
Jure Avatar asked Jul 24 '15 15:07

Jure


People also ask

What does char * mean in Arduino?

What does char * mean in Arduino? A variable of type "char *" is a pointer. That means it holds the memory address of some other variable or array element or parameter or whatever, and in that address is a char.21-Apr-2020.

How do I concatenate chars in Arduino?

String concatenation in Arduino is quite straightforward and also robust. You simply use the + operator to join strings.

Can you use += on a String?

The += operator can also be used to concatenate the String expression on its right to the String variable or property on its left, and assign the result to the variable or property on its left. When you use the += operator, you might not be able to determine whether addition or string concatenation will occur.

How do I split a character from a String in Arduino?

You can strip off the first character with . substring(1) and you can convert the resulting String from digit characters to a number with . toInt() (or . toFloat() if there is a decimal point).


1 Answers

You can use std::string::c_str() function, which returns a pointer to a const char buffer:

String card = "2-3d-fg-d6-12-68-32-3f-35-45-42-53-2a-3";
char *prefix = "GET /insert.php?card=";
char *postfix ="&error=1 HTTP/1.1\r\nHost: testsite.com\r\n\r\n"; 

String url = prefix +card+ postfix;
const char *url_complete = url.c_str();
//...

See also the related post: How concatenate a string and a const char?

like image 87
user3704293 Avatar answered Oct 20 '22 14:10

user3704293