Code Example


/*
 Modified from original by Brian Patton
 Modified for use with a Teensy 3.2 and a
 Ethernet LAN Module based on a ENC28J60 chip
 UPIEthernet.h library downloaded from:
 https://github.com/ntruchsess/arduino_uip

 Circuit:
 Circuit modified by Brian Patton for use with the Teensy

 Teensy  ENC28J60
 pin 10    CS
 pin 11    ST (I concluded it was a poorly translated SI)
 pin 12    SO
 pin 13    SCK
 
 if using 5 volts...
 5V        5V
 GND       GND
 
 if using 3.3 volts...
 3.3       Q3 (I have no idea why...it just is)
 GND       GND

 Original code Created by Rui Santos
 http://randomnerdtutorials.com/arduino-webserver-with-an-arduino-ethernet-shield/
 Teensy with Ethernet Shield
 */

#include <SPI.h>
#include <UIPEthernet.h>
int led0 = 0;
int led1 = 1;
int led2 = 2;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address Must be unique on your network
byte ip[] = { 10, 0, 0, 16 };                          //ip of device (see video here to figure it out)
byte gateway[] = { 10, 0, 0, 1 };                      //router address (default is .1 on last IP byte)((notice .16 from above))
byte subnet[] = { 255, 255, 255, 0 };                  //subnet mask
EthernetServer server(80);                             //web server port
String readString;

void setup() {
  Serial.begin(9600); // open Serial port
  while (!Serial) {
    ; //wait for Serial window to open
  }
  pinMode(A0, INPUT_PULLUP);  // Activate internal pullup resistor 
  pinMode(A1, INPUT_PULLUP);  // Activate internal pullup resistor
  pinMode(led0, OUTPUT);  //Set pin to output
  pinMode(led1, OUTPUT);  //Set pin to output
  pinMode(led2, OUTPUT);  //Set pin to output

  // start the Ethernet connection and the server:
  //   Ethernet.begin(mac, ip, gateway, subnet); // Use this if unique
  Ethernet.begin(mac, ip); // use this if default

  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop() {
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {
          //store characters to string
          readString += c;
         //   Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {
          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Teensy Arduino with Ethernet</TITLE>");
          client.println("</HEAD>");
          //Collect data to be displayed each refresh
          int sensor0 = analogRead(A0);
          int sensor1 = analogRead(A1);

          client.println("<BODY background=\"http://www.publicdomainpictures.net/pictures/10000/velka/_MG_3317.jpg\">");

          client.println("<table border = \"5\"  style = \"width: 70%\" cellpadding = \"8\" BGCOLOR = \"silver\" align = \"center\" cellspacing = \"0\" summary = \"Arduino Teensy\">");
          client.println("<tr><td><H1>Teensy Arduino with Ethernet </H1 > </td > </tr>");
          client.println("</table>");
          client.println("<table border = \"2\" align = \"center\"  style = \"width: 70%\" cellpadding = \"8\" BGCOLOR = \"gray\" cellspacing = \"0\" summary = \"Arduino Data\">");
          client.println("<tr>");
          client.println("<td>Analog Sensor 1 </td>");
          client.print("<td><H2>");
          client.print(sensor0);
          client.println("</H2></td>");
          client.println("</tr>");
          client.println("<tr>");
          client.println("<td>Analog Sensor 2 </td>");
          client.print("<td><H2>");
          client.print(sensor1);
          client.println("</H2></td>");
          client.println("</tr>");
          client.println("</table>");
          client.println("<table border = \"2\"  style = \"width: 30%\" cellpadding = \"8\" BGCOLOR = \"white\" cellspacing = \"0\" align = \"center\" summary = \"Arduino Control\">");
         
          client.println("<tr>");
          client.println("<td><H2>Red LED </H2> </td>");       
          client.println("<td BGCOLOR = \"red\"><a href = \"/?0on\"><font color=\"white\">Red On </a></td>");
          client.println("<td BGCOLOR = \"grey\"><a href = \"/?0off\"><font color=\"red\">Red Off </a> <br> </td>");
          client.println("</tr>");
        
          client.println("<tr>");
          client.println("<td><H2>Green LED </H2> </td>");
          client.println("<td BGCOLOR = \"green\"><a href = \"/?1on\"><font color=\"white\">Green On </a></td>");
          client.println("<td BGCOLOR = \"grey\"><a href = \"/?1off\"><font color=\"green\">Green Off </a> <br> </td>");
          client.println("</tr>");
        
          client.println("<tr>");
          client.println("<td><H2>Blue LED</H2> </td>");
          client.println("<td BGCOLOR = \"blue\"><a href = \"/?2on\"><font color=\"white\">Blue On </a></td>");
          client.println("<td BGCOLOR = \"grey\"><a href = \"/?2off\"><font color=\"blue\">Blue Off </a> <br> </td>");
          client.println("</tr>");
          
          client.println("</table>");
          client.println("<br>");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();
          //controls the Arduino if you press the buttons
          if (readString.indexOf("?0on") > 0) {
            digitalWrite(led0, HIGH);
          }
          if (readString.indexOf("?0off") > 0) {
            digitalWrite(led0, LOW);
          }
          if (readString.indexOf("?1on") > 0) {
            digitalWrite(led1, HIGH);
          }
          if (readString.indexOf("?1off") > 0) {
            digitalWrite(led1, LOW);
          }
          if (readString.indexOf("?2on") > 0) {
            digitalWrite(led2, HIGH);
          }
          if (readString.indexOf("?2off") > 0) {
            digitalWrite(led2, LOW);
          }
          //clearing string for next read
          readString = "";

        }
      }
    }
  }
}