我几乎是arduino代码的新手,我遇到以下代码的问题。当我用arduino 0022编译它时没有错误。如果我访问以太网屏蔽,我会看到HTML页面正常。麻烦来自于我想在代码中发布的视频链接。我能够看到框架,但链接永远不会出现。在该框架内,我看到的是已创建的HTML页面。代码或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 :(得分:0)
是的,实际上,您的代码无法正常工作。让我们把这个行的错误格式化(“Link []”连接到“frameborder”),这个页面应该以&lt; html&gt;开头,你很幸运能够在一个非常特殊的情况下使用Content-长度标题不是必需的,依此类推,让我们关注您尝试做的事情。
您尝试进行所谓的变量插值:您假设字符串中的变量名“Link []”将被另一个字符串替换。这是C或C ++中不存在的东西。
有几种方法可以做你想要的(你可以看看自Arduino-019 http://arduino.cc/en/Reference/StringObject以来所存在的String类以及所谓的连接http://arduino.cc/en/Reference/StringConcat)。
在你的情况下,你可以简单地做到这一点(没有经过测试,但应该解释诀窍):
client.print("<iframe width=640 height=360 src=");
client.print(Link); // Link is not between quotes!
client.println(" frameborder=0 allowfullscreen></iframe>");
它不如你想做的那么漂亮,但它足以满足你的需要。