Arduino Ethernet

From ProjectPages
Jump to: navigation, search

Introduction

I bought this module from Ebay for a measly £2.60 from emarket4un.

ENC28J60 Ethernet Breakout Board


I used J. C. Wippler's EtherCard library available here. Within the library there are many examples and the one I used is the 'backSoon', which acts as a web server that gives an outage message.

Connecting Up

Just plug in a few wires to get going
Pin Connections
Pin Name Arduino
SO Digital 12
SI Digital 11
SCK Digital 13
CS Digital 8
VCC 3v3 pin
GND Gnd pin

Code

<syntaxhighlight line> // Present a "Will be back soon web page", as stand-in webserver. // 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

  1. include <EtherCard.h>
  1. define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
  1. if STATIC

// ethernet interface ip address static byte myip[] = { 192,168,22,222 }; // gateway ip address static byte gwip[] = { 192,168,22,1 };

  1. endif

// ethernet mac address - must be unique on your network static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer

char page[] PROGMEM = "HTTP/1.0 503 Service Unavailable\r\n" "Content-Type: text/html\r\n" "Retry-After: 600\r\n" "\r\n" "" "" "Service Temporarily Unavailable" "" "" "

This service is currently unavailable

" "

" "The main server is currently off-line.
" "Please try again later." "

" "" ""

void setup(){

 Serial.begin(57600);
 Serial.println("\n[backSoon]");
 
 if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
   Serial.println( "Failed to access Ethernet controller");
  1. if STATIC
 ether.staticSetup(myip, gwip);
  1. else
 if (!ether.dhcpSetup())
   Serial.println("DHCP failed");
  1. endif
 ether.printIp("IP:  ", ether.myip);
 ether.printIp("GW:  ", ether.gwip);  
 ether.printIp("DNS: ", ether.dnsip);  

}

void loop(){

 // wait for an incoming TCP packet, but ignore its contents
 if (ether.packetLoop(ether.packetReceive())) {
   memcpy_P(ether.tcpOffset(), page, sizeof page);
   ether.httpServerReply(sizeof page - 1);
 }

}

</syntaxhighlight>

The End Result

The web page delivered to the browser


Another Library

I have just found UIPEthernet: A plugin-replacement of the stock Arduino Ethernet library for ENC28J60 shields and breakout boards.
I used the Adrduino WebClient example and changed #include <Ethernet.h> to #include <UIPEthernet.h>. It worked straight away.
NOTE: You must change the CS pin to D10 from D8!

<syntaxhighlight line highlight=18> /*

 Web client

This sketch connects to a website (http://www.google.com)
using an Arduino Wiznet Ethernet shield. 

Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13

created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe, based on work by Adrian McEwen

*/
  1. include <SPI.h>
  2. include <UIPEthernet.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[] = "www.google.com"; // name address for Google (using DNS)

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

// 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;

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:
   // try to congifure using IP address instead of DHCP:
   Ethernet.begin(mac, ip);
 }
 // give the Ethernet shield a second to initialize:
 delay(1000);
 Serial.println("connecting...");
 // if you get a connection, report back via serial:
 if (client.connect(server, 80)) {
   Serial.println("connected");
   // Make a HTTP request:
   client.println("GET /search?q=arduino HTTP/1.1");
   client.println("Host: www.google.com");
   client.println("Connection: close");
   client.println();
 } 
 else {
   // kf you didn't get a connection to the server:
   Serial.println("connection failed");
 }

}

void loop() {

 // if there are incoming bytes available 
 // from the server, read them and print them:
 if (client.available()) {
   char c = client.read();
   Serial.print(c);
 }
 // if the server's disconnected, stop the client:
 if (!client.connected()) {
   Serial.println();
   Serial.println("disconnecting.");
   client.stop();
   // do nothing forevermore:
   while(true);
 }

} </syntaxhighlight>

Serial Monitor Screenshot

Here is the result of a Google query in the Arduino serial monitor

Links