Introduction
Use this DFRduino Ethernet W5100S Shield to connect your Arduino to the Internet with a RJ45 cable. Plug the shield onto the board, burn the Ethernet sample codes into the controller, then an easy IoT project with stable communication is done. Based on WizNet WS5100S ethernet chip, this board integrates full hardwired TCP/IP protocol stacks and has 4 independent hardware SOCKETs. Your Arduino board can communicate with WS5100S and SD card through the SPI interface(use ICSP pin). The DFRduino Ethernet W5100S expansion board supports both Arduino UNO and Mege series.
Current hardware version V3.0: the chip has been upgraded from W5100 to W5100S, which comes with lower power consumption, lower heat and higher performance.
Features
- Full hardwired TCP/IP protocol stacks
- Support 4 independent hardware SOCKETs simultaneously
- Internal 16K bytes memory TX/ RX Buffers for TCP/IP packet processing
- Support auto-negotiation
Specification
- Operating Voltage: 5V
- 16KB TX/RX Buffers
- 4 Independent hardware sockets
- Full-hardware TCP/IP protocol stacks + MAC +PHY
- Operating Temperature: -40℃ ~ 85℃
- Dimension: 70×55×30mm (2. 76×2. 15×1. 18")
Indicator Description
Silkscreen | Description |
---|---|
L | Pin D13 signal indicator |
PWR | Power indicator |
COL | Connection conflict detection indicator Low level: conflict occurs High level: no conflict |
ACT | Activity indicator No flash: connected, but no data is being transmitted or received Blinking: connected, blinking according to data transmission |
DPX | Full duplex indicator Low level: full duplex High level: half duplex |
100M | Connection speed indicator Low level: 100Mbps High level: 10Mbps |
LINK | Connection status indicator Low level: connected High level: disconnected |
Tutorial
Requirements
- Hardware
- DFRduino UNO Controller x 1
- DFRduino Ethernet W5100S Expansion Board x 1
- Network Cable x 1
- Router or Switch x 1
- M-M/F-M/F-F Jumper wires
- Software
- Arduino IDE
- Download and install the W5100S Library (About how to install the library?)
Please replace the original W5100. CPP file with the downloaded file before use (the purpose of replacement is to be compatible with W5100S chip).
File path: Arduino\libraries\Ethernet\SRC\Utility in the Arduino installation directory.
Connection Diagram
Sample Code 1 - Get the Internet Time
This sample shows how to get network time from the NTP time server. Upload the sample, and open the serial monitor to see the time.
/*
Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
modified 02 Sept 2015
by Arturo Guadalupi
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address for your controller below.
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
unsigned int localPort = 8888; // local port to listen for UDP packets
const char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
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:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for native USB port only
// start Ethernet and UDP
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. :(");
} else if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// no point in carrying on, so do nothing forevermore:
while (true) {
delay(1);
}
}
Udp.begin(localPort);
}
void loop() {
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
// the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, extract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = ");
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// Subtract 70 years and change to China time zone:
unsigned long epoch = secsSince1900 - seventyYears + 28800;
// print Unix time:
Serial.println(epoch);
// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if (((epoch % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch % 60); // print the second
}
// wait ten seconds before asking for the time again
delay(10000);
Ethernet.maintain();
}
// send an NTP request to the time server at the given address
void sendNTPpacket(const char * address) {
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); // NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}
Result 1
Sample Code 2 - View IO Status on the Web Server
This sample turns UNO into a Web server that other devices in the same LAN can access to view IO status.
/*!
* @file webServer.ino
* @brief A simple web server with DHPC capbabilty.
* @n 1)Get IP address from router automatically
* @n 2)Show the value of the analog input pins
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [yangfeng]<feng.yang@dfrobot.com>
* @version V1.0
* @date 2021-07-14
* @get from https://www.dfrobot.com
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xDE, 0xCD, 0xAE, 0x0F, 0xFE, 0xED };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for Leonardo only
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;);
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// start the Ethernet connection and the server:
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.dfrobot.com/ihome/stylesheet/stylesheet.css\" />");
client.println("<center> <a href=\"http://www.dfrobot.com\"><img src=\"http://alturl.com/qf6vz\"></a> </center> ");
client.println("<br />");
client.println("<div>");
/********************************Page display section***********************************/
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
// output the value of each digital input pin
for (int digitalChannel = 2; digitalChannel < 10; digitalChannel++) {
int sensorReading = digitalRead(digitalChannel);
if(digitalChannel!=7&&digitalChannel!=8)
{
client.print("Digital input ");
client.print(digitalChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
}
/****************************************************************************/
client.println("</div>");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Result 2
Open the serial monitor after uploading the code, and enter the IP address in the browser of another device on the same LAN to access the Web page and view the IO status on it.
Sample Code 3 - Control IO Status through Web Page
This sample allows other devices on the same LAN to control the status of IO2 and IO3 via the Web page.
/*!
* @file Webcontrol.ino
* @brief This demo implements the change of IO port 2 and 3 output state through the web page
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [yangfeng]<feng.yang@dfrobot.com>
* @version V1.0
* @date 2021-07-14
* @get from https://www.dfrobot.com
*/
#include <SPI.h>
#include <Ethernet.h>
//Define digital output IO port
uint8_t D2 =2;
uint8_t D3 =3;
int value = LOW;
int value1 = LOW;
// Enter a MAC address and IP address for your controller below.
byte mac[] = {0xDE, 0xCD, 0xAE, 0x0F, 0xFE, 0xED };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for Leonardo only
//Set digital IO port to output mode
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
// start the Ethernet connection:
if(Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;);
}
// print your local IP address:
Serial.print("My IP address: ");
for(byte thisByte = 0; thisByte < 4; thisByte++){
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// start the Ethernet connection and the server:
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
String request;
if(client){
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while(client.connected()){
if(client.available()){
char c = client.read();
Serial.write(c);
request +=c;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank){
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"http://www.dfrobot.com/ihome/stylesheet/stylesheet.css\" />");
client.println("<center> <a href=\"http://www.dfrobot.com\"><img src=\"http://alturl.com/qf6vz\"></a> </center> ");
client.println("<br />");
client.println("<div>");
client.flush();
/********************************Page display section***********************************/
//Determines whether there is information in the HTTP request that controls the output pins and, if so, sets the output state of the outlet based on the information
if(request.indexOf("GET /D2=ON") != -1){
digitalWrite(D2, HIGH);
value = HIGH;
}
if(request.indexOf("GET /D2=OFF") != -1){
digitalWrite(D2, LOW);
value = LOW;
}
//Reply the status of the set digital IO port according to the request message
client.print("Digital_2 output: ");
if(value == HIGH){
client.print("ON");
} else{
client.print("OFF");
}
client.println("<br><br>");
//In the web page set control D2 port hyperlink, used for web control development board digital output IO port state
client.println("Turn <a href=\"/D2=ON\">ON</a><br>");
client.println("Turn <a href=\"/D2=OFF\">OFF</a><br>");
if(request.indexOf("GET /D3=ON") != -1) {
digitalWrite(D3, HIGH);
value1 = HIGH;
}
if(request.indexOf("GET /D3=OFF") != -1){
digitalWrite(D3, LOW);
value1 = LOW;
}
client.println("<br />");
client.print("Digital_3 output: ");
if(value1 == HIGH){
client.print("ON");
} else{
client.print("OFF");
}
client.println("<br><br>");
client.println("Turn <a href=\"/D3=ON\">ON</a><br>");
client.println("Turn <a href=\"/D3=OFF\">OFF</a><br>");
/****************************************************************************/
client.println("</html>");
break;
}
if (c == '\n'){
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r'){
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
//close the connection:
client.stop();
Serial.println("client disonnected");
}
}
Result 3
Open the serial monitor after uploading the code, and enter the IP address in the browser of another device on the same LAN to access the Web page and view or control the IO status through it.
More Ethernet Libraries and Tutorials
FAQ
Q&A | Some general Arduino Problems/FAQ/Tips |
---|---|
Q | The browser webpage could not be loaded, what should I do? |
A | 1. Open the Control Panel -> Network and Internet -> Network and Sharing Center -> Change Adapter Settings 2. Find the network to which you are connected -> right-click -> Properties -> Find "Internet Protocol Version 4 (TCP/IPv4)" in the Network TAB and double-click to access properties settings 3. Change the IP address to make the computer IP address and the controller IP address are in the same LAN |
Q | What if getting DHCP fails? |
A | Check whether the switch or router supports DHCP, if not, set an IP address by specifying an IP address. Please refer to more Ethernet libraries and tutorials |
A | For any questions, advice or cool ideas to share, please visit the DFRobot Forum. |