.
用 arduino 架站比用其他 Apache 等軟體還要簡單許多,基本上也不需要做什麼設(shè)定
不過在上面只能運行比較簡單的網(wǎng)頁,較複雜的儲存空間則會不夠,因此建議用arduino
架站主要用來進(jìn)行I/O控制會比較適當(dāng),運算等功能可以移到PC上作業(yè)
要使用arduino的網(wǎng)路功能是前則需要購買擴(kuò)充模組
Arduino Ethernet Shield W5100 (乙太網(wǎng)路擴(kuò)充板) 如下圖所示
不過買的時候要注意來源,我先前要測買了另外一片從淘寶弄來的,價格是便宜很多
不過一買到全新的就一根Pin角歪掉,然後用那測了一天,結(jié)果怎麼樣都連不上
一直懷疑是Code的問題 結(jié)果到最後發(fā)現(xiàn)是版子問題,因為我又訂過一片,突然恍然大悟
以下是有問題的原兇圖片
以下是比較圖 左 淘寶買的 右 沒屋頂買的
看背面更明顯 先上淘寶買的 根本粗製濫造 焊接點那邊都看得出工很差 該有的都沒有
山寨中的山寨 空空如也
接著放沒屋頂買的 跟上圖比起來至少好很多 標(biāo)示也至少都有出現(xiàn) 雖然也可能是山寨的
跟原本的 arduino 結(jié)合之後長這樣
硬體就介紹到這邊
以下先放Code
- #include <SPI.h>
- #include <Ethernet.h>
-
- byte mac[] = {
- 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 //MAC位置自定義,不能與現(xiàn)有MAC重複
- };
- EthernetServer server(80);
- String readString="";
- int Light=2;
- int Sensor=A0;
- int select = 999;
- void setup() {
- Serial.begin(9600);
- while (!Serial) {
- ;
- }
- Ethernet.begin(mac); //指定MAC IP自動取得
- server.begin();
- pinMode(Light,OUTPUT);
- Serial.print("server is at ");
- Serial.println(Ethernet.localIP());
- }
-
- void loop() {
- EthernetClient client = server.available();
- if (client) {
- Serial.println("new client");
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- readString += c;
- if (c == '\n' && currentLineIsBlank) {
- //------
- Serial.println(readString);
- //檢查收到的訊息中是否有 on ,有 -> 開燈
- if(readString.indexOf("?on") >0) {
- digitalWrite(Light, HIGH);
- Serial.println("Led On");
- }
- //檢查收到的訊息中是否有 off ,有 -> 關(guān)燈
- if(readString.indexOf("?off") >0) {
- digitalWrite(Light, LOW);
- Serial.println("Led Off");
- }
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close");
- client.println("Refresh: 5");
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- client.println("<head>
- <style>table, th, td {border: 1px solid black;}
- </style>"); //定義表格邊線
- //定義背景圖片CSS
- client.println("<style type=\"text/css\"> #T2{ background-
- image:url('http://desk.fd.zol-
- img.com.cn/t_s960x600c5/g5/M00/0F/09/ChMkJ
- lauze2IPKICABzBh_ueXY0AAH9JAMQ2qUAHMGf334.jpg'); background-
- repeat:no-repeat; width:1920px; height:1080px;
- border:1px #ccc solid; margin-
- top:10px; background-size:cover;}</style>");
- //定義JavaScript顯示當(dāng)前時間
- client.println("<script language=\"JavaScript\">
- function ShowTime()
- { document.getElementById('showbox').innerHTML =
- new Date(); setTimeout('ShowTime()',1000); }");
- client.println("function send2arduino()
- {var xmlhttp;if (window.XMLHttpRequest)xmlhttp
- =new XMLHttpRequest();else xmlhttp=new ActiveXObject
- (\"Microsoft.XMLHTTP\");element=document.getElementById
- (\"light\");if (element.innerHTML.match(\"LED on\"))
- {element.innerHTML=\"LED off\"; xmlhttp.open(\"GET\",\"?
- on\",true);}else{ element.innerHTML=\"LED on\";xmlhttp.open(\"GET\",\"?
- off\",true); }xmlhttp.send();}");
- client.println("function getBrightness()
- {var xmlhttp;if (window.XMLHttpRequest)xmlhttp
- =new XMLHttpRequest();else xmlhttp=new ActiveXObject
- (\"Microsoft.XMLHTTP\");xmlhttp.onreadystatechange=function()
- {if (xmlhttp.readyState==4 && xmlhttp.status==200)
- document.getElementById(\"brightness\").innerHTML=
- xmlhttp.responseText;};xmlhttp.open(\"GET\",\"?
- getBrightness\",true); xmlhttp.send();}window.
- setInterval(getBrightness,1000);");
- client.println("var select = 0; ");
- client.println("function funcSelect1(){ select = 1;");
- client.print("alert(");
- client.print("select");
- client.println(")}");
- client.println("</script>");
- client.println("</head>");
- client.println("<body onload=\"ShowTime()\">");
- client.println("<div id=\"T2\">");
- //定義跑馬燈
- client.println("
- <marquee direction=\"right\" height=\"30\"
- scrollamount=\"8\" behavior=\"alternate\" >Web
- Sensoer Monitor</marquee>");
- client.println("<div id=\"showbox\"></div>");
- //---------
- client.println("<script>");
- client.println("if(true){");
- client.println("String("") + \"<p>\" + \"<p>\"");
- client.println("document.write(\"<h1>Refresh Sensor</hl>\")");
- //client.println("document.write(\"</a>\")");
- client.println("}</script>");
- client.println("
- <input type=\"button\" onclick=\"funcSelect1()\"
- value=\"Show Sensor Value\">");
- //---------
- client.print("<table align=\"center\">");
- //用表格方式呈現(xiàn)當(dāng)前Sensor數(shù)值
- client.print("<th>Input NO</th><th>Value</th>");
- for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
- int sensorReading = analogRead(analogChannel);
- client.print("<tr><td ALIGN=CENTER>analog input");
- client.print(analogChannel);
- client.print(" is </td><td ALIGN=CENTER>");
- client.print(String("") + sensorReading + " "+analogChannel);
- client.println("</td></tr>");
- }
- client.print("</table>");
- client.println("
- <button id=\"light\" type=\"button\"
- onclick=\"send2arduino()\">LED on</button>
- <button type=\"button\" onclick=\"alert('OpenJumper Web
- Server')\">About</button>");
- client.println("</div>");
- client.println("</body>");
- client.println("</html>");
- break;
- }
- if (c == '\n') {
- currentLineIsBlank = true;
- } else if (c != '\r') {
- currentLineIsBlank = false;
- }
- }
- }
- delay(10);
- client.stop();
- Serial.println("client disconnected");
- readString="";
- }
- }
此程式主要的功能就是會每5秒取得 Pin0 ~ Pin5 的值顯示在網(wǎng)頁上
網(wǎng)頁的介面是用 Client.print的方式執(zhí)行Html和Javascript語法
然後點擊按鈕LED ON 會開啟 arduino 上所連接的 LED
執(zhí)行結(jié)果
硬體呈現(xiàn)結(jié)果 當(dāng)壓下LED ON時燈會亮
當(dāng)壓下 LED OFF時燈熄滅