wiki:wade/embedded/arduino/example/example_7_1_ethernet_web_admin

Version 7 (modified by wade, 15 years ago) (diff)

--

回到 Arduino

Demo video

Source code

/*
 * Web Admin 透過 web service 提供網頁監控功能。透過 GET 送命令取回 http 資料。
 * 接法:
 * digital pin2:DC SWITCH
 *         pin10:W5100
 *         pin11:W5100
 *         pin12:W5100
 *         pin13:W5100(SPI) 
 * analog pin1:CT sersor
 * GET 命令:
 *         b=00:第一行傳現在裝置狀態
 *         b=01:DC on
 *         b=02:DC off
 *         b=03:AC on
 *         b=04:AC off
 */

#include <Ethernet.h>
#include <NewSoftSerial.h> 
//#include <string.h>

// W5100 configuration
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 140, 110, 138, 240 };
Server server(80);

// set pin 9 as RX 
uint8_t ssRX = 6;
// set pin 10 as TX
uint8_t ssTX = 7;
// enable soft serial nss
NewSoftSerial nss(ssRX, ssTX);

// initial 
#define id 1           // device ID
#define DC_read 0
#define DC_control 2
#define AC_read 1
#define AC_control 3
int analog_val;        // analog value read from UART

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);
  nss.begin(9600);
  pinMode(DC_control, OUTPUT);
  pinMode(AC_control, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, 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("DC VAL = ");
          //  client.print(i);
          //  client.print(" is ");
          client.print(analogRead(DC_read));
          client.println("<br />");
          client.print("AC VAL = ");
          client.print(analogRead(AC_read));
          client.println("<br />");
          //}
          client.println("<form  method=get name=form>");
          client.println("<button name=b value=01 type=submit>DC On</button>");
          client.println("<button name=b value=02 type=submit>DC Off</button><br />");
          client.println("<button name=b value=03 type=submit>AC On</button>");
          client.println("<button name=b value=04 type=submit>AC Off</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;
          }
          // send all status
          if (!strcmp(command, "00") )
          {
            client.print("ID="); client.print(id); client.print(",");
            client.print("DC_val="); client.print(analogRead(DC_read)); client.print(";");
            client.print("DC_control="); client.print(digitalRead(DC_control)); client.print(";");
            client.print("AC_val="); client.print(analogRead(AC_read)); client.print(";");
            client.print("AC_control="); client.print(digitalRead(AC_control)); client.println(";");
          }
          // DC control
          if (!strcmp(command, "01") )
          {
            //Serial.println("high pin 2");
            digitalWrite(DC_control, HIGH);
            //nss.print("A");
            //nss.print(byte(0x00));
          }
          if (!strcmp(command, "02") )
          {
            //Serial.println("low pin 3");
            digitalWrite(DC_control, LOW);
            //nss.print("A");
            //nss.print(byte(0x02));
          }
          // AC control
          if (!strcmp(command, "03") )
          {
            //Serial.println("high pin 2");
            digitalWrite(AC_control, HIGH);
            //nss.print("A");
            //nss.print(byte(0x00));
          }
          if (!strcmp(command, "04") )
          {
            //Serial.println("low pin 3");
            digitalWrite(AC_control, LOW);
            //nss.print("A");
            //nss.print(byte(0x02));
          }
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
    
    // read from XBee
    /*
    if (nss.available())
    {
      analog_val = nss.read();
      Serial.print("I read analog_val ");
      Serial.println(analog_val);
    }
    */
  }
}