skip navigational linksPJRC
Shopping Cart Checkout Shipping Cost Download Website
Home MP3 Player 8051 Tools All Projects PJRC Store Site Map
You are here: Teensy Teensyduino Libraries Ethernet Search PJRC

PJRC Store
Teensy, $18
Teensy Pins, $21
Teensy++, $24
Teensy++ Pins, $27
USB Cable, $4
Teensy
Main Page
Getting Started
How-To Tips
Code Library
Projects
Teensyduino
Reference

Ethernet Library

The Ethernet library lets you connect to the Internet or a local network. You can create applications like web servers, or access web pages, send email or a variety of other internet functions.

Download: Ethernet is included with the Arduino software

Ethernet works with Teensy, and Teensy's "print" function includes block write optimizations that allow more efficient data transmission than standard Arduino.

Hardware Requirements

The simplest way to use Ethernet with Teensy is the WIZ812MJ module.

Basic Usage

Ethernet.begin(mac, ip, gateway, subnet);

Initialize the Ethernet library and hardware. "mac" and "ip" are required. "gateway" and "subnet" are optional.

Allowing Other Computers to Connect (Acting as a Server)

Server myserver = Server(port)

Create a server that will listen for incoming connections on a specific port number.

myserver.begin()

Begin listening for incoming connections.

Client myclient = myserver.available();

Wait until somebody connects to the server port, then return a Client object that can be used for communicating.

Connecting to Other Computers (Acting as a Cerver)

Client myclient = Client(ip, port)

Create a client object that will connect to another computer at a specific IP number and port number.

myclient.connect()

Make the connection. Returns true if the other computer accepted the connection.

Communicating While Connected

myclient.print(data)

Send data.

myclient.available()

Check if any data has been received and is available to read.

myclient.read()

Read one byte received. -1 is return if no data received.

myclient.connected()

Check if the other computer is still connected. Disconnecton is reported only after all data has been read.

myclient.stop()

Disconnect from the other computer.

Example Program

The Ethernet Library comes with examples, like this simple web server which can be accessed from any browser.

This example code can be loaded from the File > Examples > Ethernet > WebServer menu.

You must edit the IP number to a number which is appropriate for your local Ethernet network.

/*
 * Web Server
 *
 * A simple web server that shows the value of the analog input pins.
 */

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 178 };

Server server(80);

void setup()
{
  delay(100);  // allow chip to hardware reset
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if we've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so we can send a reply
        if (c == '\n' && current_line_is_blank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          // output the value of each analog input pin
          for (int i = 0; i < 6; i++) {
            client.print("analog input ");
            client.print(i);
            client.print(" is ");
            client.print(analogRead(i));
            client.println("<br />");
          }
          break;
        }
        if (c == '\n') {
          // we're starting a new line
          current_line_is_blank = true;
        } else if (c != '\r') {
          // we've gotten a character on the current line
          current_line_is_blank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
}

WIZ812MJ Module Connections

You will need the WIZnet WIZ812MJ Ethernet module, which is available from several distributors at very affordable prices.

WIZ812MJ Ethernet Module

Only 4 signals (MISO, MOSI, SCLK, SS), plus 3.3 volt power and a reset are required to make the WIZ812MJ work with Teensy or Teensy++. The WIZ812MJ has many pins, but nearly all connect to ground or 3.3 volts, or aren't used at all.

Pin FunctionTeensy 1.0Teensy 2.0Teensy++ 1.0Teensy++ 2.0
MISO1132323
MOSI1022222
SCLK912121
SS802020

This bottom side view of the WIZ812MJ can make connecting the wires much easier.

Here is the wiring used for the board used to test Ethernet on Teensy. The 4 signals lines are connected to both Teensy 2.0 and Teensy++ 2.0 pins, so either board may be plugged in. Normally you would only connect to the correct 4 pins to avoid losing use of the other 4 in your application.

Details

For more detailed information about the Ethernet library, please refer to the official Ethernet page.