2014-10-06 3 views
4

저는 CC3000 WiFi 방패가있는 Arduino UNO 프로젝트를 진행하고 있습니다.Arduino UNO, C3300 + PHP Webservice challenge

문제점없이 로컬로 호스팅 된 웹 서버 (MAMP 테스트)에 게시 할 수있는 지점에 왔지만 원격 서버에 게시 할 올바른 방법을 찾지 못하는 것 같습니다. 두 가지 다른 접근 방식을 시도했는데 (아래 코드 참조) 웹 서비스 및 데이터베이스에 올바르게 게시 된 것으로 보이지 않습니다.

다른 유형의 연결을 사용해야하는지 또는 GET 요청에 오류가 있는지 궁금합니다.

이것은 Arduino 스케치 코드입니다. PHP 파일 sensor.phpoccupied=VALUE을받은 다음 연결된 MySQL 데이터베이스로 전달합니다. PHP 파일은 경로를 직접로드 할 때 작동하므로 Arduino 문제이며 서버 측 문제는 아닙니다.

미리 감사드립니다.

// Include required libraries 
#include <Adafruit_CC3000.h> 
#include <SPI.h> 
#include <elapsedMillis.h> 

// Define CC3000 chip pins 
#define ADAFRUIT_CC3000_IRQ 3 
#define ADAFRUIT_CC3000_VBAT 5 
#define ADAFRUIT_CC3000_CS 10 

#define WLAN_SSID  "NAME_OF_WIFI"  // cannot be longer than 32 characters! 
#define WLAN_PASS  "XXXXXXXXX" 
#define WLAN_SECURITY WLAN_SEC_WPA2 

//#define WEBSITE "http://www.webservice.com" 
//#define SENSORPATH "/hello/sensor.php?" 

// Create CC3000 & DHT instances 
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT, 
             SPI_CLOCK_DIV2); 

// Local server IP, port, and repository (change with your settings !) 
uint32_t ip = cc3000.IP2U32(XXX,XXX,X,XX); 

int port = 8888; 
String repository = "hello/"; 

int ledPin = 8; // choose the pin for the LED 
int ledPinSecond = 7; 
int inputPin = 2;    // choose the input pin (for PIR sensor) 
int pirState = LOW;    // we start, assuming no motion detected 
int val = 0; // variable for reading the pin status 
String occupied;   

void setup(void) 
{ 
    Serial.begin(115200); 
    Serial.println("Wroom wroom start it up!"); 

    pinMode(ledPin, OUTPUT); // declare LED as output 
    pinMode(ledPinSecond, OUTPUT);  
    pinMode(inputPin, INPUT);  // declare sensor as input 

    // Initialise the CC3000 module 
    if (!cc3000.begin()) 
    { 
    Serial.println(F("Unable to initialise the CC3000! Check your wiring?")); 
    while(1); 
    } 

    // Connect to WiFi network 
    char *ssid = WLAN_SSID;    /* Max 32 chars */ 
    Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid); 
    cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY); 
    Serial.println("Connected to WiFi network!"); 

    if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) { 
    Serial.println(F("Failed to connect to WiFi")); 
    while(1); 
    } 

    // Check DHCP 
    Serial.println(F("Request DHCP")); 
    while (!cc3000.checkDHCP()) 
    { 
    Serial.println(F("DHCP requested - starting the void")); 
    delay(3000); 
    } 

} 
void loop(void) 
{ 
    //Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80); 
// if (www.connected()) { 

    // www.println(F("GET ")); 
    // www.println(SENSORPATH); 
    // www.println(F(" HTTP/1.0\r\n")); 
    // www.println(F("Host: ")); 
    // www.println(WEBSITE); 
    // www.println(F("\n")); 
    // www.println(F("Connection: close\n")); 
    // www.println(F("\n")); 
    // www.println(); 

    // Serial.println(F("Inserted data\n")); 

    //} else { 
    // Serial.println(F("Testing: Connection failed"));  
    // return; 
    //} 

    // Check connection, reset if connection is lost 
    if(!cc3000.checkConnected()){while(1){}} 

    val = digitalRead(inputPin); // read input value 
    Serial.print(F("Starting to detect motion")); 
    //Detect motion 
    if (val == HIGH) {   // check if the input is HIGH 
    digitalWrite(ledPin, LOW); // turn LED ON 
    digitalWrite(ledPinSecond, HIGH); 
    occupied = "Occupied"; 
    delay(150); 

    if (pirState == LOW) { 
     // we have just turned on 
     Serial.println(F("Motion detected!")); 
     // We only want to print on the output change, not state 
     pirState = HIGH; 
     } 
    } else { 
     digitalWrite(ledPin, HIGH); // turn LED OFF 
     digitalWrite(ledPinSecond, LOW); 
     occupied = "Free"; 
     delay(150);  

     if (pirState == HIGH){ 
     // we have just turned off 
     Serial.println(F("Motion ended!")); 
     // We only want to print on the output change, not state 
     pirState = LOW; 
     } 
    } 

    Serial.print(F("Room state: ")); 
    Serial.println(occupied); 
    // Send request 
    String request = "GET www.webservice.com/"+ repository + "sensor.php?occupied=" + occupied + " HTTP/1.0\n"; 
    Serial.println(request); 
    send_request(request); 

    delay(1000); 
    // Update every second 

} 

// Function to send a TCP request and get the result as a string 
void send_request (String request) { 

    // Connect  
    Serial.println("Starting connection to server..."); 
    Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port); 

    // Send request 
    if (client.connected()) { 
     client.println(request);  
     client.println(F("")); 
     Serial.println("Connected & Data sent"); 
    } 
    else { 
     Serial.println(F("Connection failed"));  
    } 

    while (client.connected()) { 
     while (client.available()) { 

     // Read answer 
     char c = client.read(); 
     } 
    } 
    Serial.println("Closing connection"); 
    Serial.println(""); 
    client.close(); 
} 

그리고 여기가

Wroom wroom start it up! 

Attempting to connect to WiFi 
Connected to WiFi network! 
Request DHCP 
DHCP requested - starting the void 
Starting to detect motionRoom state: Free 
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 

Starting to detect motionRoom state: Free 
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 

Starting to detect motionRoom state: Free 
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 

Starting to detect motionRoom state: Free 
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 

Starting to detect motion 
Room state: Free 
GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 

Starting to detect motion 
Motion detected! 
Room state: Occupied 
GET www.webservice.com/hello/sensor.php?occupied=Occupied HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 

Starting to detect motion 
Room state: Occupied 
GET www.webservice.com/hello/sensor.php?occupied=Occupied HTTP/1.0 

Starting connection to server... 
Connected & Data sent 
Closing connection 
+0

Google에서 볼 수있는 로그가 있습니까? – Namphibian

+0

@ am 비앙 (Nphphian). 처음부터 포함시키지 않아서 미안해. 여기에 간다. 'WiFi에 연결하려고 시도했습니다. WiFi 네트워크에 연결되었습니다! 요청 DHCP DHCP가 모션 룸 상태 감지하기 시작 요청 : 무료 GET www.webservice.com/hello/sensor.php?occupied=Free HTTP/서버 연결을 시작 1.0 ... 는 연결 및 데이터 전송 연결 종료' Arduino는 GET이 웹 서비스에 실제로 게시되지는 않지만 스크립트가 수행하는 것처럼 반복합니다. 즉, 결코 도달하지 않습니다. – Thomas

+1

서버의 액세스 로그를 확인하십시오. – Styx

답변

0

아마 서버가 HTTP 1.0을 좋아하지 않는다 로그를합니다. 요청을 보내려하고 서버의 응답에 신경 쓰지 않으려면 연결 헤더 : Closed가있는 HTTP/1.1을 사용해보십시오. 경우 자신을 확인 고려해

// ... 
while (client.connected()) { 
    while (client.available()) { 

    // Read answer 
    char c = client.read(); 
    } 
} 
// ... 

PS가

GET /hello/sensor.php?occupied=Free HTTP/1.1 
Host: www.webservice.com 
Connection: closed 

그렇지 않으면, 당신은 실제로 요청 서버의 응답을 기다리는 그래서 Connection: keep-alive를 사용해야합니다 그리고 당신은 당신의 조각의이 부분을 사용하여 읽을 수있을 것 url에 액세스 할 수 있습니다. Arduino 문제인지 확인하기 만하면됩니다.

+0

이 답변이 왜 효과가 있는지 설명해주십시오. 유효한 응답이 낮은 품질로 표시되는 것을 보는 것을 싫어합니다! – ficuscr