I’m still trying to get to grips with \ and searches but now have a more fundamental problem. Nothing being returned by the web server. I’ve adapted Simon Monk’s “sketch_12_03_web_request” to my MAC and target web server, added monitor prints to try to see what’s going on, and pinched some ideas from the Forum specifically “Using Ethernet Shield to read text from a web page Jul 22, 2012, 08:44 pm”.
Here’s my code:
// Test of GetWeatherData function based upon
// sketch_12_03_web_request
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x9B, 0xAA };//MAC address from my Ethernet shield
EthernetClient client;
void setup()
{
Serial.begin(9600);
while (!Serial){}; // for Leonardo compatability
if (! Ethernet.begin(mac))
{
Serial.println("Could not connect to network");
}
delay(1000);
Serial.println("Connected to Network");
GetWeatherData();
}
void loop()
{
}
void GetWeatherData()
{
if (client.connect("http://datapoint.metoffice.gov.uk/", 80))
{
Serial.println("Connected to http://datapoint.metoffice.gov.uk/");
client.println("GET http://datapoint.metoffice.gov.uk/public/data/val/wxfcs/all/json/324268?res=daily&key=60a4c960-6610-4949-b47e-b0d1d4b40f2c HTTP/1.0");
client.println();
Serial.println("GETting data");
while (client.connected())
{
Serial.println("Client is still connected.");
if (client.available())
{
char clientReturn = client.read();
Serial.println(clientReturn);
client.findUntil("W\":\"", "\0");
Serial.println("Finding until \"W\".");
String weatherType = client.readStringUntil('\r');
Serial.println(weatherType);
}
else
Serial.println("Client unavailable.");
}
if (!client.connected()) Serial.println("Client disconnected.");
client.stop();
}
}
and the Monitor output
Connected to Network
Connected to http://datapoint.metoffice.gov.uk/
GETting data
Client disconnected.
Is there something wrong in the coding that I’m not seeing? Is the difference between Simon and DPac’s use of Setup() and Loop() of any significance? Or should I be looking elswhere for assistance?
Or is the clue in here in the Headers tab in the response in Firefox to the request?
Response Headers
Access-Control-Allow-Origin *
Cache-Control public, no-transform, must-revalidate, max-age=3400
Connection keep-alive
Content-Encoding gzip
Content-Length 704
Content-Type application/json
Date Wed, 11 Jul 2018 20:43:18 GMT
ETag 1531341798728
Expires Wed, 11 Jul 2018 21:39:58 GMT
Server WaveServer 1.0
Vary Accept-Encoding
WebServer -PROD-01
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-GB,en;q=0.5
Cache-Control max-age=0
Connection keep-alive
Cookie _ga=GA1.3.865629209.1494395890; __gads=ID=ce3c06bac300a509:T=1494395894:S=ALNI_Mb8PGopX4CT4OHNFhfADPZ10ylleg; WT_FPC=id=204c23b512cbc3895691494392443238:lv=1531036949464:ss=1531036817421; __qca=P0-1991991959-1529423782491
Host datapoint.metoffice.gov.uk
If-None-Match 1531287629206
Upgrade-Insecure-Requests 1
User-Agent Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0
If you need to see what the JSON raw data looks like, tell me how best to post it, as using code tags doesn’t work. It will only show the first line.