|
| 1 | +/* |
| 2 | + Web client |
| 3 | + |
| 4 | + This sketch demonstrate Ethernet lib cooperative multitasking. |
| 5 | + |
| 6 | + It open two parallel connections to a website using an Arduino |
| 7 | + Ethernet shield, and prints the results when finished. |
| 8 | + |
| 9 | + A third task keep blinking the LED on pin 13. |
| 10 | + |
| 11 | + Circuit: |
| 12 | + * Ethernet shield |
| 13 | + |
| 14 | + created 23 Nov 2012 |
| 15 | + by Cristian Maglie |
| 16 | + |
| 17 | + */ |
| 18 | + |
| 19 | +#include <SPI.h> |
| 20 | +#include <Ethernet.h> |
| 21 | +#include <Scheduler.h> |
| 22 | + |
| 23 | +byte mac[] = { |
| 24 | + 0x2E, 0xA2, 0xB1, 0x3F, 0xF6, 0x56 }; |
| 25 | + |
| 26 | +// Change with your target server. |
| 27 | +IPAddress server(192,168,1,20); |
| 28 | + |
| 29 | +void setup() { |
| 30 | + // Set LED pin as OUTPUT and start blinking task. |
| 31 | + pinMode(13, OUTPUT); |
| 32 | + Scheduler.startLoop(blinkLed); |
| 33 | + |
| 34 | + Serial.begin(9600); |
| 35 | + while (!Serial) |
| 36 | + ; |
| 37 | + |
| 38 | + // Configure ethernet |
| 39 | + if (Ethernet.begin(mac) == 0) { |
| 40 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 41 | + for(;;) |
| 42 | + ; |
| 43 | + } |
| 44 | + Serial.print("My IP address: "); |
| 45 | + Serial.println(Ethernet.localIP()); |
| 46 | + Serial.print("Subnet mask : "); |
| 47 | + Serial.println(Ethernet.subnetMask()); |
| 48 | + Serial.print("Gateway IP : "); |
| 49 | + Serial.println(Ethernet.gatewayIP()); |
| 50 | + Serial.print("DNS IP : "); |
| 51 | + Serial.println(Ethernet.dnsServerIP()); |
| 52 | + Serial.println(); |
| 53 | +} |
| 54 | + |
| 55 | +EthernetClient client1; |
| 56 | +EthernetClient client2; |
| 57 | + |
| 58 | +char buffer1[4096]; |
| 59 | +char buffer2[4096]; |
| 60 | + |
| 61 | +boolean done1 = false; |
| 62 | +boolean done2 = false; |
| 63 | + |
| 64 | +void loop() { |
| 65 | + Scheduler.start(task1); |
| 66 | + Scheduler.start(task2); |
| 67 | + |
| 68 | + // Wait for clients to finish jobs |
| 69 | + while (!done1) |
| 70 | + yield(); |
| 71 | + while (!done2) |
| 72 | + yield(); |
| 73 | + |
| 74 | + Serial.println(); |
| 75 | + Serial.println("Client 1 received:"); |
| 76 | + Serial.println("=================="); |
| 77 | + Serial.println(buffer1); |
| 78 | + Serial.println(); |
| 79 | + Serial.println("Client 2 received:"); |
| 80 | + Serial.println("=================="); |
| 81 | + Serial.println(buffer2); |
| 82 | + |
| 83 | + // Halt |
| 84 | + for (;;) |
| 85 | + yield(); |
| 86 | +} |
| 87 | + |
| 88 | +void task1() { |
| 89 | + Serial.println("CONNECTING-1"); |
| 90 | + client1.connect(server, 80); |
| 91 | + Serial.println("CONNECTED-1"); |
| 92 | + |
| 93 | + client1.println("GET /site1 HTTP/1.0"); |
| 94 | + client1.println(); |
| 95 | + |
| 96 | + int i=0; |
| 97 | + while (client1.connected() && i<4096) { |
| 98 | + if (client1.available()) |
| 99 | + buffer1[i++] = client1.read(); |
| 100 | + if ((i%16)==0) |
| 101 | + Serial.print("."); |
| 102 | + // slow down to show that other task can run in parallel |
| 103 | + delay(1); |
| 104 | + } |
| 105 | + buffer1[i]=0; |
| 106 | + |
| 107 | + client1.stop(); |
| 108 | + Serial.println("DISCONNECTED-1"); |
| 109 | + |
| 110 | + done1 = true; |
| 111 | +} |
| 112 | + |
| 113 | +void task2() { |
| 114 | + Serial.println("CONNECTING-2"); |
| 115 | + client2.connect(server, 80); |
| 116 | + Serial.println("CONNECTED-2"); |
| 117 | + |
| 118 | + client2.println("GET /site2 HTTP/1.0"); |
| 119 | + client2.println(); |
| 120 | + |
| 121 | + int i=0; |
| 122 | + while (client2.connected() && i<4096) { |
| 123 | + if (client2.available()) |
| 124 | + buffer2[i++] = client2.read(); |
| 125 | + if ((i%16)==0) |
| 126 | + Serial.print("x"); |
| 127 | + delay(1); |
| 128 | + } |
| 129 | + buffer2[i]=0; |
| 130 | + |
| 131 | + client2.stop(); |
| 132 | + Serial.println("DISCONNECTED-2"); |
| 133 | + |
| 134 | + done2 = true; |
| 135 | +} |
| 136 | + |
| 137 | +// Monitor loop to see if scheduling is running properly. |
| 138 | +// Just blink an LED. |
| 139 | +void blinkLed() { |
| 140 | + digitalWrite(13, HIGH); |
| 141 | + delay(100); |
| 142 | + digitalWrite(13, LOW); |
| 143 | + delay(100); |
| 144 | +} |
0 commit comments