Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
//設定 MAC 跟 IP 位置。
// The IP address will be dependent on your local network:
//IP 位置需設定到ip分享器指定的網段
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
// Initialize the Ethernet server library
// 初始化乙太網路
// with the IP address and port you want to use
// 使用80 port 接收網頁連線
// (port 80 is default for HTTP):
// (80 port 是預設的 http port )
EthernetServer server(80);
void setup() {
// Open serial communications and wait for port to open:
// 設定使用9600做為rs232連接埠的鮑率
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
//等待rs232連接成功
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);//設定 server 的 mac/ip 位址
server.begin(); //綁定服務
Serial.print("server is at ");
Serial.println(Ethernet.localIP());//秀出 server 的 ip 位址
}
void loop() {
// listen for incoming clients
// 監聽是否有"網頁要求封包" 到來
EthernetClient client = server.available();
if (client) { // 如果有"網頁要求封包" 到來
Serial.println("new client");
// an http request ends with a blank line
// 網頁要求封包,會以 "blink line 空白行" 做為結尾 編按:"blink line"指的是"\r\n"字串 編編按: "\r"是回車符號;"\n"是換行符號
boolean currentLineIsBlank = true; //設定變數 currentLineIsBlank , 用來記錄現在是否已經收到"blink line"。如果已經收到"blink line",就可以開始處理網頁要求。
while (client.connected()) {//如果client仍然保持連線 **這裡是"資料讀取回圈"的開頭
if (client.available()) {//如果client正常可連絡
char c = client.read(); //從網路讀取一個byte進來
Serial.write(c); // 把讀進來的這個byte 秀到rs232連接埠
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
// 當讀進來的資料是"換行符號",而且已經出現過空白行,代表"網頁要求封包"已經讀取完畢,可以開始準備回傳網頁給Client。
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
// 傳送標準的 http 回應的標頭
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response 指定傳送完這個網頁之後,連線就中斷。
//以下是回傳給瀏覽器的"網頁內容"
client.println("Refresh: 5"); // refresh the page automatically every 5 sec 要求瀏覽器每五秒鐘自動更新一次這個網頁
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin 回傳6個類比輸入腳位的讀取值
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
}
client.println("</html>");
break;傳送完畢,跳離"資料讀取回圈"
}
if (c == '\n') {
// you're starting a new line 收到換行符號,代表接下來會收到新的一行資料。
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line 收到回車符號
currentLineIsBlank = false;
}
}
}**這裡是"資料讀取回圈"的結尾
// give the web browser time to receive the data 等待資料送出
delay(1);
// close the connection: 關閉這一個瀏覽器得連線
client.stop();
Serial.println("client disonnected");
}
}
沒有留言:
張貼留言