How to get data via GET using arduino?
exemplo: 192.168.1.10/?cel=+5512345678&msg=hello+im+the+msg
I would like to take this data and save it in a variable. I've been reading a lot but everything seems extremely complicated, someone could provide an example or tutorial for simple understanding?
basic client GET test code.
//zoomkat 9-22-12
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//remove SD card if inserted
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "web.comporium.net"; // zoomkat's test web page server
EthernetClient client;
//////////////////////
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
Serial.begin(9600);
Serial.println("Better client test 9/22/12"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}
void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}
//////////////////////////
void sendGET() //client function to send/receive GET request data.
{
if (client.connect(serverName, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
client.println("Host: web.comporium.net");
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}
Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client
}
I found this code here, it handles HTTP GET, if anyone needs ....
#define TAMANHO_QUERY_STRING 320
#define TAMANHO_SEGUNDA_PARTE 315
#define TAMANHO_TEXT 300
#define TAMANHO_NUMERO 15
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void loop(){
boolean na_query = false;
char queryString[TAMANHO_QUERY_STRING];
while (client.connected()) {
char c = client.read();
//Pulamos até a query string (apos o primeiro espaço)
if(c!=' ' && !na_query) {
continue;
} else {
na_query = true;
}
//Colocamos o byte na query
queryString.concat(c);
//Verifica se terminou a query string
if (c==' ' && na_query) {
break;
}
}
char segundaParte[TAMANHO_SEGUNDA_PARTE];
segundaParte = getValue(queryString, '?', 1);
char text_undecode[TAMANHO_TEXT];
char numero[TAMANHO_NUMERO];
text_undecode = getValue(segundaParte, '&', 0);
numero = getValue(segundaParte, '&', 1);
}