Arduino Ethernet Shield
Shield-compatible Arduino board
The Ethernet shield allows you to connect a WizNet Ethernet controller to the Arduino boards via the SPI bus. It uses pins 10, 11, 12, and 13 for the SPI connection to the WizNet. Later models of the Ethernet shield also have an SD Card on board. Digital pin 4 is used to control the slave select pin on the SD card.
The shield should be connected to a network with an ethernet cable. You will need to change the network settings in the program to correspond to your network.
In the above image, the Arduino board would be stacked below the Ethernet shield.
1/*2
3 Telnet client4
5 This sketch connects to a a telnet server (http://www.google.com)6
7 using an Arduino Wiznet Ethernet shield. You'll need a telnet server8
9 to test this with.10
11 Processing's ChatServer example (part of the network library) works well,12
13 running on port 10002. It can be found as part of the examples14
15 in the Processing application, available at16
17 http://processing.org/18
19 Circuit:20
21 * Ethernet shield attached to pins 10, 11, 12, 1322
23 created 14 Sep 201024
25 modified 9 Apr 201226
27 by Tom Igoe28
29 */30
31#include <SPI.h>32#include <Ethernet.h>33
34// Enter a MAC address and IP address for your controller below.35// The IP address will be dependent on your local network:36byte mac[] = {37
38 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED39};40
41IPAddress ip(192, 168, 1, 177);42
43// Enter the IP address of the server you're connecting to:44
45IPAddress server(1, 1, 1, 1);46
47// Initialize the Ethernet client library48// with the IP address and port of the server49// that you want to connect to (port 23 is default for telnet;50// if you're using Processing's ChatServer, use port 10002):51
52EthernetClient client;53
54void setup() {55
56 // You can use Ethernet.init(pin) to configure the CS pin57
58 //Ethernet.init(10); // Most Arduino shields59
60 //Ethernet.init(5); // MKR ETH shield61
62 //Ethernet.init(0); // Teensy 2.063
64 //Ethernet.init(20); // Teensy++ 2.065
66 //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet67
68 //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet69
70 // start the Ethernet connection:71
72 Ethernet.begin(mac, ip);73
74 // Open serial communications and wait for port to open:75
76 Serial.begin(9600);77
78 while (!Serial) {79
80 ; // wait for serial port to connect. Needed for native USB port only81
82 }83
84 // Check for Ethernet hardware present85
86 if (Ethernet.hardwareStatus() == EthernetNoHardware) {87
88 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");89
90 while (true) {91
92 delay(1); // do nothing, no point running without Ethernet hardware93
94 }95
96 }97
98 while (Ethernet.linkStatus() == LinkOFF) {99
100 Serial.println("Ethernet cable is not connected.");101
102 delay(500);103
104 }105
106 // give the Ethernet shield a second to initialize:107
108 delay(1000);109
110 Serial.println("connecting...");111
112 // if you get a connection, report back via serial:113
114 if (client.connect(server, 10002)) {115
116 Serial.println("connected");117
118 } else {119
120 // if you didn't get a connection to the server:121
122 Serial.println("connection failed");123
124 }125}126
127void loop() {128
129 // if there are incoming bytes available130
131 // from the server, read them and print them:132
133 if (client.available()) {134
135 char c = client.read();136
137 Serial.print(c);138
139 }140
141 // as long as there are bytes in the serial queue,142
143 // read them and send them out the socket if it's open:144
145 while (Serial.available() > 0) {146
147 char inChar = Serial.read();148
149 if (client.connected()) {150
151 client.print(inChar);152
153 }154
155 }156
157 // if the server's disconnected, stop the client:158
159 if (!client.connected()) {160
161 Serial.println();162
163 Serial.println("disconnecting.");164
165 client.stop();166
167 // do nothing:168
169 while (true) {170
171 delay(1);172
173 }174
175 }176}