Lecture Topic 3.2.1 Arduino With Ethernet Shield
Lecture Topic 3.2.1 Arduino With Ethernet Shield
Setting it up is as simple as plugging the header pins from the shield into your Arduino. This
generic ethernet shield available on Amazon. It is meant to be used with the Arduino Uno Rev. 3
boards (or later). It has too many pins to plug into earlier version Arduino boards.
Step 2: Shield Features
The Ethernet Shield is based upon the W51000 chip, which has an internal 16K buffer. It has a
connection speed of up to 10/100Mb. This is not the fastest connection around, but it still works well.
It relies on the Arduino Ethernet library, which comes bundled with the development
environment.
There is also an on-board micro SD slot which enables you to store a heck-of-a-lot of data, and serve
up entire websites using just your Arduino. This requires the use of an external SD library, which does
not come bundled with the software. It is a different topic to be covered later on.
The board also has space for the addition of a Power over Ethernet (PoE) module, which allows you to
power your Arduino over an Ethernet connection.
Step 3: Get Started
Plug the Arduino into your computer's USB port, and the Ethernet shield into your router (or direct
internet connection).
Next, open the Arduino development environment. I highly recommend upgrading to Arduino 1.0 or
later (if you have not done so already). This version of the software has built in DHCP support, and
does not require manually configuring an IP address.
To figure out what IP address has been assigned to your board, open the DhcpAddressPrinter sketch.
This can be found at:
The following code changes the web page served based on a button press:
<pre>/*
A simple web server that changes the page that is served, triggered by a button press. Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Connect a button between Pin D2 and 5V
* Connect a 10K resistor between Pin D2 and ground
*/
#include <SPI.h>
#include <Ethernet.h>
// Initialize the Ethernet server library with the IP address and port
you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
int buttonPress = 1;
void setup()
{
pinMode(2, INPUT);
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
buttonPress = digitalRead(2);
The following code lights up an LED depending on the URL that is sent to the Arduino:
<pre>/*
Allows you to turn on and off an LED by entering different
urls. To turn it on: http://your-IP-address/$1
To turn it off: http://your-IP-address/$2
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Connect an LED to pin D2 and put it in series with a 220 ohm resistor to ground
*/
#include <SPI.h>
#include <Ethernet.h>
boolean incoming = 0;
void setup()
{
pinMode(2, OUTPUT);
// start the Ethernet connection and the server: Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// 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
//reads URL string from $ to first blank space
if(incoming && c == ' ‘){
incoming = 0;
}
else {
incoming = 1;
}
//Checks for the URL string $1 or $2
if(incoming == 1){ Serial.println(c); if(c == '1')
{
Serial.println("ON");
digitalWrite(2, HIGH);
}
if(c == '2'){
Serial.println("OFF");
digitalWrite(2, LOW);
}
}
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();
}
}
To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a 220
ohm resistor to ground.
To turn on the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$1
To turn off the LED enter this into your browser:
http://[YOUR IP ADDRESS HERE]/$2
Note: You should obviously replace [YOUR IP ADDRESS
HERE] with your IP address.
Step 5: Client
You can also use the Ethernet Shield as a client. In other words, you can use it to read websites like a
web browser.
Websites have a lot of text both visible and hidden, which makes programming on the client side very
tricky. Reading information from websites typically involves parsing a lot of strings.
We can write some code to read Twitter messages and such a code already exists as an example
within the Arduino programmer.
We will write a code to turn on an LED if a special message is read.
To make this work connect the positive lead an LED to pin D2, and the negative lead in series with a
220 ohm resistor to ground.
Don't forget to enter your own IP address into the code below, or it will not work. Here is
the code:
<pre>
/*
Twitter Client with Strings
This sketch connects to Twitter using an Ethernet shield. It parses the XML returned, and looks for
<text>this is a tweet</text>
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield, either one will work, as long as
it's got a Wiznet Ethernet module on board.
This example uses the DHCP routines in the Ethernet library which is part of the Arduino core from
version 1.0 beta 1
This example uses the String library, which is part of the Arduino core from version 0019.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
*/
#include <SPI.h>
#include <Ethernet.h>
// initialize serial:
Serial.begin(9600);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, ip);
}
// connect to Twitter: connectToServer();
}
void loop() {
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(serverName, 80))
{ Serial.println("making HTTP
request...");
// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=RandyMcTester&count=1
HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}
Presumably you are going to want to read
something other than the recent post on the
RandyMcTester Twitter feed. To read other Twitter feeds, change the following bit of text:
client.println("GET /1/statuses/user_timeline.xml?screen_name=[NEW TWITTER NAME
HERE]&count=1 HTTP/1.1");