2013年2月16日 星期六

Arduino_Ethernet(4)

這次主題是利用Arduino Ethernet shield去控制LED的Blink

1. 幾個比較需要注意的Function:
a. Server
   |- begin()
   |- available()

b. Client
   |- available()
   |- readBytesUntil
   |- find
   |- findUntil
2. Server:

a.begin():
依照官網的敘述:
Tells the server to begin listening for incoming connections.
可以知道begin()是用來告知Server listen是否有連線的request

b. available():
依照官網的敘述:
Gets a client that is connected to the server and has data available for reading. The connection persists when the returned client object goes out of scope; you can close it by calling client.stop().
表示available()會回傳一個client的object -> 此client object為連上Server的client,可以使用client.stop()來中斷連線
3. Client:

a.available():
依照官網的敘述:
Returns the number of bytes available for reading (that is, the amount of data that has been written to the client by the server it is connected to).
在Client和Server的available不同,從官網上得知,Server.available()會回傳一個和Server連線的Client object,而Client.available()會回傳多少Bytes從Server傳送給Client

b.readBytesUntil():
依照官網的敘述:
readBytesUntil() read characters from a stream into a buffer. The function terminates if the terminator character is detected, the determined length has been read, or it times out
此function繼承自Stream,把讀取到的data放到一個buffer直到設定的character被找到為止

c.find():
依照官網的敘述:
find() reads data from the stream until the target string of given length is found The function returns true if target string is found, false if timed out.
這個function是用來讀取data直到想要的string被讀取到了之後會回傳true,並且會把指標停在下一個要開始read data的位址

d.findUntil():
依照官網的敘述:
findUntil() reads data from the stream until the target string of given length or terminator string is found.
使用的方式和find()差不多,但多惹終止搜尋的關鍵string,當搜尋到terminator string會停止搜尋

4. Sample Code:
這次的Sample Code是把Arduino當作Server,類似於Apache Server,並且有CGI(利用分辨POST和GET來處理)在處理Client端的request
#include 
#include 

byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
byte ip[] = {192,168,1,50};

const int MAX_PAGENAME_LEN = 8;
char buffer[MAX_PAGENAME_LEN+1];

EthernetServer server = 80;

void sendHeader(EthernetClient client,char *title){
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println();
    client.print("<html><head>$lt;title>");
    client.print(title);
    client.println("</title><body>");
  
}

void setup(){
    Serial.begin(9600);
    Ethernet.begin(mac,ip);
    server.begin();
    delay(2000);
  
}

void loop(){
  
    EthernetClient client = server.available();
    if(client){
        
        //當Client連上Server候需要保持連線 -> 使用While(client.connected())
        while(client.connected()){
            //判斷Client是否有資料的request
            if(client.available()){
            //清空Buffer    
            memset(buffer,0,sizeof(buffer));
            //HTTP request的樣子 -> GET /URL HTTP/1.1
            //所以先用找/判斷是什麼HTTP request method
            if(client.readBytesUntil('/',buffer,sizeof(buffer))){
            
                buffer[strlen(buffer)-1]='\0';
                //如果是POST的方法 -> 執行digitalWrite
                if(strcmp(buffer,"POST")==0){
            
                    client.find("\n\r");
                    //找出POST的content
                    while(client.findUntil("pinD","\n\r")){
                      int pin = client.parseInt();
                      int val = client.parseInt();
                      Serial.print("pin = ");
                      Serial.print(pin);
                      Serial.print(" , value = ");
                      Serial.println(val);
                      pinMode(pin,OUTPUT);
                      digitalWrite(pin,val);
                    }
              }
              //送出HTML的網頁
              sendHeader(client,"Post example");
              client.println("<h2>Click buttons to turn pin 8 on or off</h2>");
              client.print(
              "<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
              client.println(" value='0'><input type='submit' value='off'/></form>");
              client.print(
              "<form action='/' method='POST'><p><input type='hidden' name='pinD8'");
              client.print(" value='1'><input type='submit' value='On'/></form>");
              client.println("</body></html>");
              client.stop();
              }
            break;
          }
        }
        delay(1);
        client.stop();
      }
}

run的結果:
上面的程式碼是用HTTP的方式來連接,所以打開瀏覽器可以看到結果

沒有留言:

張貼留言