]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/sketch_12_03_web_request/sketch_12_03_web_request.ino
first commit
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / sketch_12_03_web_request / sketch_12_03_web_request.ino
1
2 // sketch_12_03_web_request
3
4 #include <SPI.h>
5 #include <Ethernet.h>
6
7 byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
8
9 EthernetClient client;
10
11 void setup() 
12 {
13   Serial.begin(9600);
14   while (!Serial){}; // for Leonardo compatability
15
16   if (! Ethernet.begin(mac)) 
17   {
18     Serial.println("Could not connect to network");
19   }
20   delay(1000);
21   hitWebPage();
22 }
23
24 void loop() 
25 {
26 }
27
28 void hitWebPage()
29 {
30   if (client.connect("api.openweathermap.org", 80))
31   {
32     client.println("GET /data/2.5/weather?q=Manchester,uk HTTP/1.0");
33     client.println();
34     while (client.connected())
35     {
36       if (client.available())
37       {
38         client.findUntil("description\":\"", "\0");
39         String description = client.readStringUntil('\"');
40         Serial.println(description);
41       }
42     }
43     client.stop();
44   }
45 }