Home > Uncategorized > Getting started with the Ethernet Shield in #Arduino @rdegges

Getting started with the Ethernet Shield in #Arduino @rdegges


In order to extend the capabilities of the Arduino, you add what are known as Shields. which are stackable boards that plug into the top of the Arduino board.

Ethernet support is one key feature missing in the Arduino board, so I bought a HiLetGo Ethernet shield from Amazon for £8. It’s based on the W5100 chip, and pretty standard kit.

You plug the shield into the top of the arduino, then connect the USB to the computer, and the ethernet cable to your router.

My project was to get the public IP address of my router – using the ipify API. The code is based on the TCP/IP code listed on https://www.arduino.cc/en/Tutorial/WebClient

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

// if you don’t want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = “api.ipify.org”; // name address for ipify.com (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement

void setup() {
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet

// Open serial communications and wait for port to open:
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only

// start the Ethernet connection:
Serial.println(“Initialize Ethernet with DHCP:”);
if (Ethernet.begin(mac) == 0) {
Serial.println(“Failed to configure Ethernet using DHCP”);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println(“Ethernet shield was not found. Sorry, can’t run without hardware. :(“);
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println(“Ethernet cable is not connected.”);
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip, myDns);
} else {
Serial.print(” DHCP assigned IP “);
// give the Ethernet shield a second to initialize:
Serial.print(“connecting to “);

// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.print(“connected to “);
// Make a HTTP request:
client.println(“GET /?format=json HTTP/1.1”);
client.println(“Host: api.ipify.org”);
client.println(“Connection: close”);
} else {
// if you didn’t get a connection to the server:
Serial.println(“connection failed”);
beginMicros = micros();

void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80) len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
byteCount = byteCount + len;

// if the server’s disconnected, stop the client:
if (!client.connected()) {
endMicros = micros();
Serial.print(“Received “);
Serial.print(” bytes in “);
float seconds = (float)(endMicros – beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(“, rate = “);
Serial.print(” kbytes/second”);

// do nothing forevermore:
while (true) {

Note, that the Mac address listed in the code is really just a random number, as long as it’s unique on your network, it doesn’t cause a problem… and it’s extremely likely to be unique.

The output appears in the serial window such as the following;


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: