Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 3 months ago.
passing variable into http query
I have all my system set up and it works very well when i pass to param1 in my .php file on my server. the problem i'm having is finding the right syntax to pass - eg. float Lat(it has a lat value) - into the place where i have 60.654321 in my get request. I'm a little bit stumped on this, and i know it's probably very simple.
I would like to pass a variable into this part of the code: line 32
int ret = http.get("http://reskiproject.com/save.php?param1=60.654321", str, 128);
ie. replace 60.654321 with the value of
float Lat;
does anyone have any small code example in reference to this problem. Thanks in advance.
#include "mbed.h" #include "EthernetInterface.h" #include "HTTPClient.h" #include "xbee.h" xbee xbee1(p9,p10,p11); //Initalise xbee_lib Serial pc(USBTX,USBRX); EthernetInterface eth; HTTPClient http; char str[512]; char GPS_DATA[48]; //Xbee buffer size is 202 bytes float Lat = 0.0; int main() { eth.init(); //Use DHCP eth.connect(); while(1) { pc.printf("Waiting for Gps Data from Sensor Xbee\n\r"); xbee1.RecieveData(GPS_DATA,19); //Read data from the XBee pc.printf("%s\n\r",GPS_DATA); //Send data to XBee sscanf(GPS_DATA,"%f,",&Lat); pc.printf("Lat: %2.6f\n\r",Lat); //GET data printf("\n\rTrying to fetch page...\n\r"); int ret = http.get("http://reskiproject.com/save.php?param1=60.654321", str, 128); if (!ret) { printf("Page fetched successfully - read %d characters\n\r", strlen(str)); printf("Result: %s\n\r", str); } else { printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); } }
1 Answer
11 years, 3 months ago.
I found the solution to this problem by adding this code-
HTTPText txt(str, 512); printf("\n\rTrying to fetch page...\n\r"); sprintf(url, "http://reskiproject.com/save.php?param1=%f¶m2=%f", Lon, Lat); int ret = http.get(url, &txt);
and here is the complete code that will send our lat/lon values it gets by xbee from another microcontroller to our webpage .php and shows in google maps api.
#include "mbed.h" #include "EthernetInterface.h" #include "HTTPClient.h" #include "xbee.h" xbee xbee1(p9,p10,p11); //Initalise xbee_lib Serial pc(USBTX,USBRX); EthernetInterface eth; HTTPClient http; char str[512]; char url[100]; char GPS_DATA[48]; //Xbee buffer size is 202 bytes float Lat = 0.0; float Lon = 0.0; int main() { eth.init(); //Use DHCP eth.connect(); while(1) { pc.printf("Waiting for Gps Data from Sensor Xbee\n\r"); xbee1.RecieveData(GPS_DATA,19); //Read data from the XBee pc.printf("%s\n\r",GPS_DATA); //Send data to XBee sscanf(GPS_DATA,"%f,%f",&Lon,&Lat); pc.printf("Lon: %2.6f Lat: %2.6f\n\r",Lon,Lat); //GET data HTTPText txt(str, 512); printf("\n\rTrying to fetch page...\n\r"); sprintf(url, "http://reskiproject.com/save.php?param1=%f¶m2=%f", Lon, Lat); int ret = http.get(url, &txt); //int ret = http.get("http://reskiproject.com/save.php?param1=60.654321¶m2=24.123456", str, 128); if (!ret) { printf("Page fetched successfully - read %d characters\n\r", strlen(str)); printf("Result: %s\n\r", str); } else { printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); } } }
I hope this helps anyone else who has to do this kind of thing from microcontroller to webpage. i have the php file code from the webserer side if somebody would like to know about that to.