2012/10/20

Freeduino/Arduino Web Sensors And Switches

Arduino Uno/Duemilanove is an useful gadget, combined with an W5100 Ethernet Shield , linked with some analog sensors and relays by wires, one can easily implements an simple remote web sensor monitoring and switch controlling system using an browser.

Pins Used

W5100 Ethernet Shield -- D10, D11, D12, D13
Analog Sensors -- A0, A1, A2, A3, A4, A5
Switches -- D2, D3, D4, D5, D6, D7, D8, D9

Output On Chrome Browser


Sketch


// modified from Instructables Arduino Led Server and Arduino Example WebServer 
// by Befun Hung on Oct. 20, 2012
// the sketch works on Arduino IDE 1.01

#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module. 
#define sensorPinStart 0 //change to 2 if using Arduino Ethernet Shield 05 or 06
#define noOfSensors 2
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(35);
String Led;
int led[] = {00, 2, 3, 4 ,5 ,6 ,7 ,8,9  }; //Led pins num 0 in arry is not used
int numofleds = 8; //numofleds
String value[] = {"off","off","off","off","off","off","off","off","off"}; //startup all led are off
EthernetServer server(80);
String data;

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip,gateway,subnet); 
  server.begin();
  //set pin mode
  for (int j = 1; j < (numofleds + 1); j++)
  {
    pinMode(led[j], OUTPUT);
  }
  Serial.println("Serial READY");
  Serial.println("Ethernet READY");
  Serial.println("Server READY");
}

void loop()
{
  EthernetClient 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 (inString.length() < 35) 
        {
          inString.concat(c);
        } 
        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();
          client.println("<html>");
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<body><form method=get>");
          client.println("<center><p><h1>Freeduino/Arduino Web Sensor And Switch</h1></p><hr></center>"); 
          client.println("<h2>");
          client.println("Sensor Reading:<br>");
          for (int analogChannel = sensorPinStart ; analogChannel < sensorPinStart+noOfSensors ; analogChannel++)
          {
            int sensorReading = analogRead(analogChannel);
            client.print("Analog Input ");
            client.print(analogChannel);
            client.print(" : ");
            client.print(sensorReading);
            client.println("<br>");
          }
          client.println("<p></p>Switches Status:<br>");
          for(int i=1;i < (numofleds + 1) ;i++) 
          { 
            Led = String("Switch") + i;
            if(inString.indexOf(Led+"=off")>0 || inString.indexOf("all=on")>0)
            {
              Serial.println(Led+"on");
              digitalWrite(led[i], HIGH);
              value[i] = "on"; 
            }
            else if(inString.indexOf(Led+"=on")>0 || inString.indexOf("all=off")>0 )
            {          
              Serial.println(Led+"off");
              digitalWrite(led[i], LOW);
              value[i] = "off";
            }
            client.println(Led+"  <input type=submit name="+Led+" value="+value[i]+">"+"<br>");
          }
          client.println("<p></p>Switches Action:<br>");
          client.println("All <input type=submit name=all value=on><input type=submit name=all value=off>");
          client.println("</form><hr></h2>");
          client.println("<center>The web page will automatically refresh every 5 seconds.</center>");
          client.println("</html></body>");
          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;
        }
      }  // End of if (client.available)
    }  // End of while (client.connected())
    // give the web browser time to receive the data
    delay(1);
    inString = "";
    client.stop();
  } //End of if (client)
}