2013-06-12 4 views
1

나는 간단한 Arduino 이더넷 프로그램을 작성 중이다. 이 프로그램은 서버에 HTTP GET 요청을 보낸 다음 서버가 "Hello World"를 에코하여 Arduino 이더넷을 통해 수신하고 Arduino 1.0.4 IDE의 직렬 모니터에 인쇄 할 수 있어야합니다. 다음은 유용한 정보입니다. Windows Server 2003에서 XAMPP 서버를 사용하고 있습니다./xampp/htdocs/xampp에 PHP 파일을 넣었으며 파일 이름은 rec.php입니다. rec.php의 내용은 내가이 메시지가 아두 이노에 프로그램을로드 한 후이는 아두 이노 프로그램PHP 파일에서 Arduino 이더넷 읽기

#include <Ethernet.h> 
#include <SPI.h> 

byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x7E, 0xAE} 
IPAddress server { 192, 168, 1, 223 }; 
IPAddress ipAddress { xxx,xxx,xxx,xxx }; 
IPAddress myDNS {8,8,8,8}; 
IPAddress myGateway{192,168,1,1}; 
IPAddress mySubnet{255,255,255,0}; 

EthernetClient client; 

void setup() 
{ 
Serial.begin(9600); 
Ethernet.begin(mac, ipAddress, myDNS, myGateway, mySubnet); 

delay(1000); 
Serial.println("connecting"); 

if(client.connect(server, 80)) 
{ 
    Serial.println("Connected"); 
    client.println("GET /rec.php HTTP/1.1"); 
} 
else 
    Serial.println("Not Connected"); 

}

void loop() 
{ 
    if(client.available()) 
    { 
     char c = client.read(); 
     Serial.println(c); 
     delay(1000); 
    } 
    else 
    { 
     Serial.println("Not Available"); 
     delay(1000); 
    } 
} 

의 파일 내용이

<?php 
echo "Hello World"; 
?> 

입니다 직렬 모니터 "HTTP/1.1 400 Bad Request"에서 확인하십시오. 그 문제를 해결하는 방법에 대한 제안? 당신의 대답을 간단하게하십시오.

답변

2

필요한 줄 끝을 보내지 않습니다. 프로토콜은 요청 방법의 끝에서 CR-LF를 요구하고 다른 헤더 라인을 포함 할 수있는 전체 요청의 끝에서 다른 CR-LF를 필요로합니다. 참조 :

HTTP requests

이 귀하의 경우에 의미 두 개의 CR-LF의 요청을 종료해야합니다. 기본 println 기능에 의존하지 마십시오. 코드로 인쇄 된 줄 끝을 제어하십시오.

client.print("GET /rec.php HTTP/1.1\r\n\r\n"); 
관련 문제