hello!
I'm doing GET request on a website with the ethernet shield and I'm having difficulties parsing the result..
the only way I'm getting interesting result is with this bit of code in arduino :
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c= client.read();
Serial.print(c);
}
serial output :
connecting...
connected
HTTP/1.1 200 OK
Date: Thu, 27 Nov 2014 15:29:18 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
7
-1062-
0
disconnecting.
I'm trying to isoalte the '1062' and trimming the rest to be able to use that number in 'int' format...
I tried converting the char to string to use the String indexOf() but no luck
any ideas anyone ?
thanks in advance!
One way is to capture what is sent back to the client, then use various methods to extract the desired data. The textFinder application might also be of use.
//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString
#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "checkip.dyndns.com"; // myIP server 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("client readString test 11/04/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
Serial.println();
}
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 / HTTP/1.1"); //download text
client.println("Host: checkip.dyndns.com");
client.println("Connection: close"); //close 1.1 persistent connection
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
readString += c; //places captured byte in readString
}
//Serial.println();
client.stop(); //stop client
Serial.println("client disconnected.");
Serial.println("Data from server captured in readString:");
Serial.println();
Serial.print(readString); //prints readString to serial monitor
Serial.println();
Serial.println();
Serial.println("End of readString");
Serial.println("==================");
Serial.println();
readString=""; //clear readString variable
}
Hey zoomkat!!
thanks for your reply!
I'm trying to make it work but I'm not there yet
the output seems to be a series of char still so I can't use the function for the string messages.. There might be another way but I'm a beginner in that domain ;)..
let me know if I'm missing something.. I tried this code but I get an error "invalid conversion from 'char' to 'const char'...
any ideas ??
the ouput of the code you gave me is the exact same one as in my previous message..
cheers
Phil
//zoomkat 11-04-13
//simple client test
//for use with IDE 1.0.1
//with DNS, DHCP, and Host
//open serial monitor and send an e to test client GET
//for use with W5100 based ethernet shields
//remove SD card if inserted
//data from myIP server captured in readString
#include <SPI.h>
#include <Ethernet.h>
String readString;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "www.mywebsite.com"; // myIP server 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("client readString test 11/04/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
Serial.println();
}
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 /fb.php HTTP/1.1"); //download text
client.println("Host: www.mywebsite.com");
client.println("Connection: close"); //close 1.1 persistent connection
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
readString += c; //places captured byte in readString
String stringOne = c;
int firstClosingBracket = stringOne.indexOf('-');
}
//Serial.println();
client.stop(); //stop client
Serial.println("client disconnected.");
Serial.println("Data from server captured in readString:");
Serial.println();
Serial.print(firstClosingBracket); //prints readString to serial monitor
Serial.println();
Serial.println();
Serial.println("End of readString");
Serial.println("==================");
Serial.println();
readString=""; //clear readString variable
}
[/code
hey Paul
ok I didn't know that..
all I'm trying to do is to isolate the number '1062' from the wepage output.
I was trying to use String indexOf() to parse the message until the delimiter '-' but I get an error saying it's an invalid conversion between a char and a string in the serial window..
I'm a bit lost then... any idea how I can parse this message ?
thanks
I modified the code you were trying (below). The www.mywebsite.com seems to be bogus, but the below code parses out 8859 from what is returned, and may work for your server based on the return you posted.
//zoomkat 11-28-14
#include <SPI.h>
#include <Ethernet.h>
String readString;
String data;
int start;
int ind1; // - locations
int ind2;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
char serverName[] = "www.mywebsite.com"; // myIP server 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("client readString test 11/04/13"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
Serial.println();
}
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 /fb.php HTTP/1.1"); //download text
client.println("Host: www.mywebsite.com");
client.println("Connection: close"); //close 1.1 persistent connection
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
readString += c; //places captured byte in readString
//String stringOne = c;
//int firstClosingBracket = stringOne.indexOf('-');
}
//Serial.println();
client.stop(); //stop client
Serial.println("client disconnected.");
Serial.println("Data from server captured in readString:");
Serial.println();
Serial.print(readString); //prints readString to serial monitor
Serial.println();
//parse readString for data
start = readString.indexOf("ml"); //finds a unique start location
ind1 = readString.indexOf('-', start+2); //finds location of first -
ind2 = readString.indexOf('-', ind1+1 ); //finds location of second -
data = readString.substring(ind1+1, ind2); //captures data String
Serial.print("my data is: ");
Serial.println(data);
Serial.println();
int n = data.toInt(); //convert data into a number
Serial.print("my number is: ");
Serial.println(n);
Serial.println();
Serial.println("End of readString");
Serial.println("==================");
Serial.println();
readString=""; //clear readString variable
data=""; //clear data variable
}