strings, sensors, urls and GSM http

I’m using a SIM900 based GSM shield and trying to POST information to a PHP script on my webserver.

I can’t work out how to send the values from a variable in the POST.

Here’s the function:

  numdata=inet.httpPOST("mywebserver.com", 80, "/webfiles/post-script.php", "?sensor=33",msg, 50);

I want to be able to change the value “33” to whatever the variable is.

I’ve tried to do this using char but had no luck. Could you please provide some guidance?

Thanks

I've tried to do this using char but had no luck. Could you please provide some guidance?

What did you try?

int uselessCrap = 35;
char append[20];
sprintf(append, "?sensor=%d", uselessCrap);

numdata=inet.httpPOST("mywebserver.com", 80, "/webfiles/post-script.php", append, msg, 50);

PaulS:

I've tried to do this using char but had no luck. Could you please provide some guidance?

What did you try?

I was going round in circles with a hundred different combinations all of which were a load of rubbish tbh.

Have inserted that into my code and has worked a treat, thank you.

A couple of questions:

  • Can I set the string "append" to any length given that you set it to "20" or should it be specific?
  • what is sprintf?
  • What is the "%d" for after the "="

Can I set the string "append" to any length given that you set it to "20" or should it be specific?

Yes. The Arduino has limited memory, so don't get carried away. 2000 is not better than 20.

what is sprintf?

A function. Google it.

What is the "%d" for after the "="

A format specifier. The sprintf() documentation should describe all the format specifiers.

Thank you again, in 5 minutes you've managed to fix what's been bugging me for hours!

One final question, how could I add more than one sensor information? Is there a way to add more than one string in the sprintf function, I can't seem to find any info on it.

One final question, how could I add more than one sensor information? Is there a way to add more than one string in the sprintf function, I can't seem to find any info on it.

sprintf() takes a variable number of arguments. The first is always where to put the data. The second is always how to format the data. The third and subsequent arguments are what is to be formatted. Supposed you wanted to store temperature and humidity in a database. You might do something like GET /someScript.php?t=78&h=49. The sprintf() function, assuming you just want to deal with the script arguments would be:

int temp = 78;
int humid = 49;
char append[40];
sprintf(append, "?t=%d&h=%d", temp, humid);