title title title title

Ethernet Shield


Here is the sketch I used for the project. Note that the MAC address is unique for your shield. If you don't know your MAC address, try the one used here.

      #include <Ethernet.h>
      #include <SPI.h>
      
      const int BLUE    =5;
      const int GREEN   =6;
      const int RED     =7;
      const int SPEAKER =3;
      
      //For controlling LEDS and the speaker
      int freq = 0;
      int pin;
      
      // MAC address of the SainSmart Ethernet Shield
      byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
      
      //Start the server on port 80
      EthernetServer server = EthernetServer(80);
      
      boolean receiving = false; //To keep track of whether we are getting data.
      
      void setup()
      {
        Serial.begin(9600);
        pinMode(RED, OUTPUT);
        pinMode(GREEN, OUTPUT);
        pinMode(BLUE, OUTPUT);
      
        //Connect with DHCP
        if (!Ethernet.begin(mac))
        {
          Serial.println("Could not Configure Ethernet with DHCP.");
          return;
        }
        else
        {
          Serial.println("Ethernet Configured!");
        }
        //Start the server and print out the local network IP address
        server.begin();
        Serial.print("Server Started.\nLocal IP: ");
        Serial.println(Ethernet.localIP());
      }
      
      void loop()
      {
        EthernetClient client = server.available();
      
        if (client)
        {
          //An HTTP request ends with a blank line
          boolean currentLineIsBlank = true;
          boolean sentHeader = false;
      
          while (client.connected())
          {
            if (client.available())
            {
              char c = client.read(); //Read from the incoming buffer
      
              if(receiving && c == ' ') receiving = false; //done receiving
              if(c == '?') receiving = true; //found arguments
      
              //This looks at the GET requests
              if(receiving)
              {
                //An LED command is specified with an L
                if (c == 'L')
                {
                  Serial.print("Toggling Pin ");
                  pin = client.parseInt();
                  Serial.println(pin);
                  digitalWrite(pin, !digitalRead(pin));
                  break;
                }
                //A speaker command is specified with an S
                else if (c == 'S')
                {
                  Serial.print("Setting Frequency to ");
                  freq = client.parseInt();
                  Serial.println(freq);
                  if (freq == 0)
                    noTone(SPEAKER);
                  else
                    tone(SPEAKER, freq);
                  break;
                }
                //Add similarly formatted else if statements here
                //TO CONTROL ADDITIONAL THINGS
              }
      
              //Print out the response header and the HTML page
              if(!sentHeader)
              {
                //Send a standard HTTP response header
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html\n"); // Note the blank line after header - required
      
                client.println("<!DOCTYPE HTML>");
                client.println("<!--[if lt IE 7]>      <html class='no-js lt-ie9 lt-ie8 lt-ie7'> <![endif]-->");
                client.println("<!--[if IE 7]>         <html class='no-js lt-ie9 lt-ie8'> <![endif]-->");
                client.println("<!--[if IE 8]>         <html class='no-js lt-ie9'> <![endif]-->");
                client.println("<!--[if gt IE 8]><!--> <html class='no-js'> <!--<![endif]-->");
                client.println("<html>");
                client.println("<head>");
                client.println("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
                client.println("<title>Remote Arduino Control</title>");
                client.println("</head>");
                client.println("<body leftmargin='100px' topmargin='100px' bgcolor='#CCFFFF'>");
                client.println("<h1>");
                client.println("Push to turn on. Push again to turn off.");
                client.println("</h1>");
                //Red toggle button
                client.println("<form action='' method='get'>");
                client.println("<input type='hidden' name='L' value='7' />");
                client.println("<input type='submit' value='Toggle Red' />");
                client.println("</form>");
                client.println("<br />");
                //Green toggle button
                client.println("<form action='' method='get'>");
                client.println("<input type='hidden' name='L' value='6' />");
                client.println("<input type='submit' value='Toggle Green' />");
                client.println("</form>");
                client.println("<br />");
                //Blue toggle button
                client.println("<form action='' method='get'>");
                client.println("<input type='hidden' name='L' value='5' />");
                client.println("<input type='submit' value='Toggle Blue' />");
                client.println("</form>");
                client.println("<br />");
                //Speaker frequency slider
                client.println("<form action='' method='get'>");
                client.print("<input type='range' name='S' min='0' max='1000' step='100' value='0'/>");
                client.println("<input type='submit' value='Set Frequency' />");
                client.println("</form>");
                client.println("</html>");
      
                sentHeader = true;
              }
      
              if (c == '\n' && currentLineIsBlank) break;
      
              if (c == '\n')
              {
                currentLineIsBlank = true;
              }
              else if (c != '\r')
              {
                currentLineIsBlank = false;
              }
            }
          }
          delay(5); //Give the web browser time to receive the data
          client.stop(); //Close the connection:
        }
      } // end of loop()
    

The Ethernet and SPI libraries are necessary. Both load with the standard Arduino IDE software. The SPI library is used to talk to the shield. In the setup(), once the local network IP address is found, you can comment out this portion of the code. The Ethernet library code provides a return value for each LED and what frequency to set the tone generator using the GET protocol that is added to the URL line when the client browser sends the form data. The L value returned matches the pin number for the prospective LED and the tone value is returned using the S parameter and a value between 0 and 1000 Hz. The range slider will work in newer browsers or default to a value list in older browsers. It's important to include 0 Hz to be able to silence the speaker. If you select more than one LED before toggling off, the light blends in the 3-color LED.

Heres some of the serial monitor screen output:

    	Ethernet Configured!
      Server Started.
      Local IP: 192.168.1.76
      Toggling Pin 7
      Toggling Pin 7
      Toggling Pin 6
      Toggling Pin 6
      Toggling Pin 5
      Toggling Pin 5
      Setting Frequency to 500
      Setting Frequency to 0
      Toggling Pin 7
      Toggling Pin 7
      Toggling Pin 5
      Toggling Pin 5
      Toggling Pin 6
		

One hundred and fifty ohm resistors were used to current limit the LEDs and the speaker, so the hardware setup was simple. You could, of course, use the control signals to operate anything remotely you desired. The only requirement is the circuit be on and attached to the Internet when you want to use it.

I'll leave the heavy lifting of working with your router protocol up to you, but with my modem/router arrangement, selecting the local IP address to be frozen for the Arduino shield was easy. There was a dialog box with a check box for that, providing you named the link. Then I went to another area to set up a "pinhole" in the firewall for this link name and address. Basically, the router added the Arduino shield to its DNS list for my modem and routed any port 80 data to it. There is zero security in using this system, so be forewarned. Any Internet data on the IP address arranged during this set-up will go to your Arduino board. Only values for L and S would be responsive. Everything else would be ignored.


Return to: Projects Page