root / EtherCard / examples / getStaticIP / getStaticIP.pde
History | View | Annotate | Download (1.7 KB)
| 1 | //>>> The latest version of this code can be found at https://github.com/jcw/ !! |
|---|---|
| 2 | |
| 3 | // This demo does web requests to a fixed IP address, using a fixed gateway. |
| 4 | // 2010-11-27 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php |
| 5 | // $Id: getStaticIP.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 ip address and port |
| 18 | static byte hisip[] = { 74,125,79,99 };
|
| 19 | // remote website name |
| 20 | char website[] PROGMEM = "google.com"; |
| 21 | |
| 22 | byte Ethernet::buffer[300]; // a very small tcp/ip buffer is enough here |
| 23 | static long timer; |
| 24 | |
| 25 | // called when the client request is complete |
| 26 | static void my_result_cb (byte status, word off, word len) {
|
| 27 | Serial.print("<<< reply ");
|
| 28 | Serial.print(millis() - timer); |
| 29 | Serial.println(" ms");
|
| 30 | Serial.println((const char*) Ethernet::buffer + off); |
| 31 | } |
| 32 | |
| 33 | void setup () {
|
| 34 | Serial.begin(57600); |
| 35 | Serial.println("\n[getStaticIP]");
|
| 36 | |
| 37 | if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) |
| 38 | Serial.println( "Failed to access Ethernet controller"); |
| 39 | |
| 40 | ether.staticSetup(myip, gwip); |
| 41 | |
| 42 | ether.copyIp(ether.hisip, hisip); |
| 43 | ether.printIp("Server: ", ether.hisip);
|
| 44 | |
| 45 | while (ether.clientWaitingGw()) |
| 46 | ether.packetLoop(ether.packetReceive()); |
| 47 | Serial.println("Gateway found");
|
| 48 | |
| 49 | timer = - REQUEST_RATE; // start timing out right away |
| 50 | } |
| 51 | |
| 52 | void loop () {
|
| 53 | ether.packetLoop(ether.packetReceive()); |
| 54 | |
| 55 | if (millis() > timer + REQUEST_RATE) {
|
| 56 | timer = millis(); |
| 57 | Serial.println("\n>>> REQ");
|
| 58 | ether.browseUrl(PSTR("/foo/"), "bar", website, my_result_cb);
|
| 59 | } |
| 60 | } |