使用 DHCP 架設 Web Server
首先,組立 W5100 以太網路模組是非常容易的一件事,如下圖所示,只要將 W5100 以太網路模組堆疊到任何 Arduino 開發板之上就可以了。
▲圖6 將 Arduino 開發板與 W5100 以太網路模組堆疊組立
之後,在將組立好的 W5100 以太網路模組,如下圖所示,只要將 USB 線差到 Arduino 開發板,再將 RJ 45的網路線一端插到 W5100 以太網路模組,另一端插到可以上網的集線器(Switch HUB)的任何一個區域網路接口(Lan Port)就可以了。
▲圖7 接上電源與網路線的 W5100 以太網路模組堆疊卡
我們遵照前幾章所述,將 Arduino 開發板的驅動程式安裝好之後,我們打開 Arduino 開發板的開發工具:Sketch IDE 整合開發軟體,攥寫一段程式,如下表所示之 WebServer 測試程式一,我們就可以讓 W5100 以太網路模組堆疊卡變成一台簡易的網頁伺服器運作了。
表 2 WebServer 測試程式一
WebServer測試程式一(WebServer_dhcp) |
/* 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. // The IP address will be dependent on your local network: byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; IPAddress ip(192, 168, 30, 200);
//IPAddress ip = Ethernet.localIP() ; IPAddress dnServer(168, 95, 1, 1); // the router's gateway address: IPAddress gateway(192, 168, 30, 254); // the subnet: IPAddress subnet(255, 255, 255, 0);
// Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80);
void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); //ip = Ethernet.localIP() ; Serial.println("now program Start") ;
while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only }
// 啟用 Ethernet 連線,預設會以 DHCP 取得 IP 位址 if (Ethernet.begin(mac) == 0) { Serial.println("無法取得 IP 位址"); // 無法取得 IP 位址,不做任何事情 for(;;) ; } // 輸出 IP 位址 Serial.print("IP 位址:"); Serial.println(ip);
// Ethernet.begin(mac, ip, dnServer, gateway, subnet); Ethernet.begin(mac); // use this statement , will request DHCP server for ip
server.begin(); Serial.print("server is at "); Serial.println(Ethernet.localIP()); }
void loop() { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // 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 if (c == '\n' && currentLineIsBlank) { // send a standard http response header 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 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 disconnected"); } } |
程式範例原始碼網址:https://github.com/brucetsao/arduino_RFProgramming
如下圖所示,讀者可以看到本次實驗- WebServer 測試程式一結果畫面。
▲圖8 WebServer 測試程式一結果畫面
最後一頁是作者介紹及後續文章發展唷 !
請注意!留言要自負法律責任,相關案例層出不窮,請慎重發文!