Arduino joining string and *char
Asked Answered
D

2

5

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

Dramaturgy answered 24/7, 2015 at 15:27 Comment(0)
P
8

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?

Propeller answered 24/7, 2015 at 20:10 Comment(0)
D
0

Use c_str() function, which returns a pointer to a const char

String id = "14";
char *hello = "GET /api/weather/specific.php?id=";
char *hello2 = " HTTP/1.1\r\nHost: evive.000webhostapp.com\r\nConnection: close\r\n\r\n";
String url = hello + id + hello2;

const char *send_url = url.c_str();
wifi.send((const uint8_t*)send_url, strlen(send_url));  // For Arduino Mega and ESP8266
Dummy answered 4/12, 2019 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.