wiki:wade/embedded/arduino/example/example_7_1_ethernet_web_admin

Version 4 (modified by wade, 14 years ago) (diff)

--

回到 Arduino

Demo Video

  • http://www.youtube.com/watch?v=w_4o_QxHpMI
    /*
     * Web Admin 透過 web service 提供網頁監控功能。
     * 接法:
     * analog pin1:CT sersor
     * 
     */
    
    #include <Ethernet.h>
    //#include <string.h>
    
    byte mac[] = { 
      0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
    byte ip[] = { 
      192, 168, 1, 240 };
    
    Server server(80);
    
    void setup()
    {
      Ethernet.begin(mac, ip);
      server.begin();
      Serial.begin(9600);
      pinMode(2, OUTPUT);
      pinMode(3, OUTPUT);
    }
    
    void loop()
    {
      Client client = server.available();
      // detect if current is the first line
      boolean current_line_is_first = true;
      // command code
      char command[3] = "\0";
      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 />");
              }
              client.println("<form  method=get name=form>");
              client.println("<button name=b value=01 type=submit>01</button>");
              client.println("<button name=b value=02 type=submit>02</button>");
              client.println("<button name=b value=03 type=submit>03</button>");
              client.println("<button name=b value=04 type=submit>04</button>");            
              client.println("</form>");
              break;
            }
            if (c == '\n') {
              // we're starting a new line
              current_line_is_first = false;
              current_line_is_blank = true;
            } 
            else if (c != '\r') {
              // we've gotten a character on the current line
              current_line_is_blank = false;
            }
            // get the first http request
            if (current_line_is_first && c == '=')
            {
              for (int i = 0; i < 2; i++)
              {
                c = client.read();
                command[i] = c;
              }
              if (!strcmp(command, "01") )
              {
                Serial.println("high pin 2");
                digitalWrite(2, HIGH);
                delay(500);
                digitalWrite(2, LOW);
              }
              if (!strcmp(command, "02") )
              {
                Serial.println("high pin 3");
                digitalWrite(3, HIGH);
                delay(500);
                digitalWrite(3, LOW);
              }
            }
          }
        }
        // give the web browser time to receive the data
        delay(1);
        client.stop();
      }
    }