我有一个问题,发送一个HTTP响应到服务器,我必须使用 "sprintf"
while (1) {
float temp = sht31.readTemperature();
float humidity = sht31.readHumidity();
if(temp!=stored_temp || humidity!=stored_humidity){
printf("Change detected!\n");
stored_temp=temp;
stored_humidity=humidity;
lcd.locate(3, 3);
lcd.printf("Temperature: %.2f C", temp);
lcd.locate(3, 13);
lcd.printf("Humidity: %.2f %%", humidity);
{
HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://34.212.30.54:5000/api/transmit_data");
post_req->set_header("Content-Type", "application/json");
const char body[ ] = "{\"student_id\": xxxxxxx, \"sensor-type\": humidity, \"data\": 39.93}";
//float new_data;
这是给我主要问题的关键点,有什么建议吗?
const char new_body[ ] = sprintf(const char body, "Il valore del sensore è: %.2f", humidity);
printf("Il valore selezionato dall'utente è:\n%s\n", new_body);
HttpResponse* post_res = post_req->send(new_body, strlen(new_body));
if (!post_res) {
printf("HttpRequest failed (error code %d)\n", post_req->get_error());
return 1;
}
printf("\n----- HTTP POST response -----\n");
dump_response(post_res);
delete post_req;
}
Details of sprintf can be found at
http://www.cplusplus.com/reference/cstdio/sprintf/
int sprintf ( char * str, const char * format, ... );
1. It returns int not const char[].
2. you can do sprintf as:
char body[MAX_PATH];
sprintf(body, "Il valore del sensore è: %.2f", humidity);
Now body has char array to be used in printf and further.
printf("Il valore selezionato dall'utente è:\n%s\n", body);