root / EtherCard / examples / getViaDNS / getViaDNS.pde
History | View | Annotate | Download (1.5 KB)
| 1 | //>>> The latest version of this code can be found at https://github.com/jcw/ !! |
|---|---|
| 2 | |
| 3 | // This demo does web requests via DNS lookup, using a fixed gateway. |
| 4 | // 2010-11-27 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php |
| 5 | // $Id: getViaDNS.pde 7763 2011-12-11 01:28:16Z jcw $ |
| 6 | |
| 7 | #include <EtherCard.h> |
| 8 | |
| 9 | #define REQUEST_RATE 5000 // milliseconds |
| 10 | |
| 11 | // ethernet interface mac address |
| 12 | static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
|
| 13 | // ethernet interface ip address |
| 14 | static byte myip[] = { 192,168,1,203 };
|
| 15 | // gateway ip address |
| 16 | static byte gwip[] = { 192,168,1,1 };
|
| 17 | // remote website name |
| 18 | char website[] PROGMEM = "google.com"; |
| 19 | |
| 20 | byte Ethernet::buffer[300]; // a very small tcp/ip buffer is enough here |
| 21 | static long timer; |
| 22 | |
| 23 | // called when the client request is complete |
| 24 | static void my_result_cb (byte status, word off, word len) {
|
| 25 | Serial.print("<<< reply ");
|
| 26 | Serial.print(millis() - timer); |
| 27 | Serial.println(" ms");
|
| 28 | Serial.println((const char*) Ethernet::buffer + off); |
| 29 | } |
| 30 | |
| 31 | void setup () {
|
| 32 | Serial.begin(57600); |
| 33 | Serial.println("\n[getViaDNS]");
|
| 34 | |
| 35 | if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) |
| 36 | Serial.println( "Failed to access Ethernet controller"); |
| 37 | |
| 38 | ether.staticSetup(myip, gwip); |
| 39 | |
| 40 | if (!ether.dnsLookup(website)) |
| 41 | Serial.println("DNS failed");
|
| 42 | ether.printIp("Server: ", ether.hisip);
|
| 43 | |
| 44 | timer = - REQUEST_RATE; // start timing out right away |
| 45 | } |
| 46 | |
| 47 | void loop () {
|
| 48 | ether.packetLoop(ether.packetReceive()); |
| 49 | |
| 50 | if (millis() > timer + REQUEST_RATE) {
|
| 51 | timer = millis(); |
| 52 | Serial.println("\n>>> REQ");
|
| 53 | ether.browseUrl(PSTR("/foo/"), "bar", website, my_result_cb);
|
| 54 | } |
| 55 | } |