2012-03-06 26 views
0

我幾乎是arduino代碼中的新手,我在遇到以下代碼時遇到了問題。當我用arduino 0022進行編譯時,沒有錯誤。如果我訪問以太網盾,我會看到HTML頁面正常。麻煩來自於我想在代碼中發佈的視頻的鏈接。我能夠看到框架,但鏈接永遠不會出現。在該框架內,我所看到的是已經創建的HTML頁面。是否有可能在代碼或HTML格式中存在錯誤?是否有我需要使用的特定庫?先謝謝您的幫助!如何在Arduino w/Ethernet Shield上的HTML代碼中顯示視頻鏈接?

#include <SPI.h> 
#include <Client.h> 
#include <Ethernet.h> 
#include <Server.h> 
#include <Udp.h> 
#include <Servo.h> 
char Link[]= "http://www.anywebsite.com"; //video link 
Servo tilt; 
Servo pan; 
int panpos = 90; 
int tiltpos = 90; 
byte mac[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; //physical mac address 
byte ip[] = { 192, 128, 13, 169 };   // ip in lan 
byte gateway[] = { 192, 128, 13,1 };   // internet access via router 
byte subnet[] = { 255, 255, 255, 0 };     //subnet mask 
Server server(80);          //server port 
String readString = String(30); //string for fetching data from address 
void setup(){ 
    Ethernet.begin(mac, ip, gateway, subnet); 
    Serial.begin(9600); 
tilt.attach(3); 
pan.attach(9); 
    tilt.write(90); 
    pan.write(90); 
} 
void loop(){ 
// Create a client connection 
Client client = server.available(); 
    if (client) { 
    while (client.connected()) { 
    if (client.available()) { 
    char c = client.read(); 
    //read char by char HTTP request 
    if (readString.length() < 100) 
     { 
     //store characters to string 
     readString += c; 
     } 
     Serial.print(c); 
     if (c == '\n') { 
      if (readString.indexOf("?") <0) 
      { 
      //do nothing 
      } 
      else 
      {   
      if(readString.indexOf("UP=UP") >0) 
       { 
       movetiltupstep(); 
       } 
      else if(readString.indexOf("DN=DN") >0) 
       { 
       movetiltdownstep(); 
       } 
      else if(readString.indexOf("LT=LT") >0) 
       { 
       movepanleftstep(); 
       } 
      else if(readString.indexOf("RT=RT") >0) 
       { 
       movepanrightstep(); 
       } 
      else if(readString.indexOf("CN=CN") >0) 
      { 
      center(); 
      } 
     } 
     // now output HTML data starting with standard header 
     client.println("HTTP/1.1 200 OK"); 
     client.println("Content-Type: text/html"); 
     client.println(); 
     //set background to green 
     client.print("<body style=background-color:blue>"); 
     client.println("<hr />"); 
     client.println("<center>"); 
     client.println("<h1>Camera control</h1>"); 
     client.println("<form method=get name=SERVO>"); 
     client.println("<input type=submit value=UP name=UP style=\"width:100px\"><br>"); 
     client.println("<input type=submit value=LT name=LT style=\"width:100px\"><input type=submit value=CN name=CN style=\"width:100px\"><input type=submit value=RT name=RT style=\"width:100px\"><br>"); 
     client.println("<input type=submit value=DN name=DN style=\"width:100px\">"); 
     client.println("<hr />"); 
     client.println("<iframe width= 640 height= 360 src= Link[]frameborder= 0 allowfullscreen></iframe>"); 
     client.println("</form>"); 
     client.println("</center>"); 
     client.println("</body></html>"); 
     //clearing string for next read 
     readString=""; 
     //stopping client 
     client.stop(); 
     } 
     } 
    } 
    } 
} 
void movetiltupstep(){ 
    tiltpos = tilt.read(); 
    Serial.println(tiltpos); 
    if (tiltpos >= 66) 
    { 
    tilt.write(tiltpos - 2); 
    } 
} 
void movetiltdownstep(){ 
    tiltpos = tilt.read(); 
    Serial.println(tiltpos); 
    if (tiltpos <= 116) 
    { 
    tilt.write(tiltpos + 2); 
    } 
} 
void movepanleftstep(){ 
    panpos = pan.read(); 
    Serial.println(panpos); 
    if (panpos >= 4) 
    { 
    pan.write(panpos - 4); 
    } 
} 
void movepanrightstep(){ 
    panpos = pan.read(); 
    Serial.println(panpos); 
    if (panpos <= 176) 
    { 
    pan.write(panpos + 4); 
    } 
} 
void center(){ 
    panpos = pan.read(); 
    tiltpos = tilt.read(); 
    Serial.println(panpos); 
    if (panpos < 90) 
    { 
    for(int i = panpos; i <= 90; i++) { 
     pan.write(i); 
    } 
    } 
    else if (panpos > 90) 
    { 
    for(int i = panpos; i >= 90; i--) { 
     pan.write(i); 
} 
    } 
    if (tiltpos < 90) 
    { 
    for(int i = tiltpos; i <= 90; i++) { 
     tilt.write(i); 
    } 
    } 
    else if (tiltpos > 90) 
    { 
    for(int i = tiltpos; i >= 90; i--) { 
     tilt.write(i); 
    } 
    } 
} 

回答

0

是的,實際上,您的代碼無法工作。讓我們把這個行的錯誤格式(連接到「frameborder」的這個行的錯誤格式),應該以< html>開頭的頁面,你很幸運,在一個非常特殊的情況下,Content-Length頭不是必需的,等等,讓我們專注於你試圖做的事情。

您試圖做所謂的變量插值:您認爲字符串中的變量名稱「Link []」將被替換爲另一個字符串。這是C或C++中不存在的東西。

有幾種方法可以做你想做的事情(你可以看看自從Arduino-019 http://arduino.cc/en/Reference/StringObject以及所謂的級聯http://arduino.cc/en/Reference/StringConcat以來存在的String類)。

在你的情況,你可以簡單地做(沒有測試過,但是應該說明的伎倆):

client.print("<iframe width=640 height=360 src="); 
client.print(Link); // Link is not between quotes! 
client.println(" frameborder=0 allowfullscreen></iframe>"); 

它比你想要做什麼那麼漂亮,但足夠你需要什麼。

+0

我的工作就像一個魅力。我做的唯一的事情就是:client.print放在我所說的兩行上:client.println ...感謝Vincent的幫助! – 2012-03-06 22:29:41

+0

在你的情況下,它仍然有效。 println()代替print()將強制在每個「print」之間添加新行,而不是拼接字符。在這種情況下,HTML的標準足夠靈活,HTML「源代碼」看起來有點奇怪。 – 2012-03-07 07:30:19