All I want is the actual data part. In this example only "1525112225".
I'm using the code mentioned on the arduino website:
/*
Web client
This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
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);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
All I want is the actual data part. In this example only "1525112225".
How is the Arduino supposed to know what "the actual data part" is? As far as it is concerned, no part of the server output is more important or useful than any other part.
Personally, I find it hard to believe that you got that response from making a GET request to google.
Once again, I'll remind you that "the actual data" is the ENTIRE server response.
If only SOME of the data is important, YOU must define what marks the start of important data and what marks the end of important data.
Then, when the start marker arrives, you know to start storing data in a char array, and when the end marker arrives you know to stop storing the data, use the stored data, and reset the array, and index, for the next server reply.
Why on earth would you post code that is NOT the code you are having problems with?
I may be way too late, but what I assume you are trying to do is locate the body of the response and then choose which line to read. If so then yes it can most certainly be done.
while (client.available())
{
char c = client.read();
Serial.print(c);
if(c == '\n')
{
c = client.read();
Serial.print(c);
if(c == '\r')
{
c = client.read();
Serial.print(c);
if(c == '\n')
{
c = client.read();
Serial.print(c);
digitalWrite(MACHINE, c);
Serial.println();
Serial.print(F("Machine state: "));
Serial.println(c);
}
}
}
The code above is what I used to locate the response body by finding the empty line that always separates the body from the rest of the response as the blank line '\r\n' will always be preceded by a '\n'. Finding the right line in the body is not that much different either, you can differentiate each line by finding the '\n' at the end of each line. The code below should be the basic structure to parse out specific lines in the body:
for(int i; i < body_line; i++)
{
//Cycle through the body preceding your desired line
while(/*Loop until end of line is reached*/)
{
//Print out body data until end of line is reached
}
}
while(/*Loop until end of line is reached*/)
{
//Use this while loop to process desired body line
}
while(/*Loop until end of response is reached*/)
{
//Read out the remaining data
}
It may not be clear, but what they're saying is that the problem your encountering is more to do with the HTTP response from the server, not the client (Arduino device). You need to wrap the response in something. In web programming the server would spit out a rest JSON formatted response like:
{ "Time": 1001010}
Not ideal to deserialize JSON, but if you just spit it out by itself, then you could say the entire reponse body is the time. Or a comma or pipe delimited string that Arduino could turn into an object and it's properties. I do REST API's for a living basically. Usually serving data to Angular, Vue or whatever framework. They all handle JSON docs, I've played with the JSON library for Arduino, but it seems like it's CPU heavy where it's most useful or overkill in a scenario like yours.