2016-10-21 108 views
0

我開發了一個代碼,用於統計通過紅外傳感器前方的人數。Arduino以太網盾牌php

但我想arduino是一個客戶端,FTP服務器使用這些值。但我不知道PHP,但它會是這樣的。服務器是localhost。

file.ino:

#include <UIPEthernet.h> 

EthernetServer server = EthernetServer(80); 

int sensor1 = 8; 
int sensor2 = 9; 
unsigned long timeS1 = 0, timeS2 = 0; 
unsigned long dif; 
long int nPessoas = 0; 


void setup() 
{ 
    pinMode(sensor1, INPUT); 
    pinMode(sensor2, INPUT); 

    Serial.begin(9600); 

    uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; 
    IPAddress myIP(192,168,0,25); 
    Ethernet.begin(mac,myIP); 
    server.begin(); 
} 

void loop() 
{ 
    if (!digitalRead(sensor1)) 
    { 
     timeS1 = millis(); 
    } 
    if (!digitalRead(sensor2)) 
    { 
     timeS2 = millis(); 
    } 

    dif = timeS2 - timeS1; 

    dif = timeS2 - timeS1; 
    if(dif >= 500 && dif <= 1500) 
    { 
     timeS1 = timeS2 = 0; 
     nPessoas++; 
    } 

    size_t size; 

    if (EthernetClient client = server.available()) 
    { 
     while((size = client.available()) > 0) 
     { 
      uint8_t* msg = (uint8_t*)malloc(size); 
      size = client.read(msg,size); 
      Serial.write(msg,size); 
      free(msg); 
     } 
     if (client.connect("http://192.168.0.7/",80)) { 
     client.println("POST /file.php HTTP/1.1"); 
     client.println("Host: http://192.168.0.7/"); 
     client.println("Content-Type: application/x-www-form-urlencoded"); 
     client.print("Content-Length: "); 
     client.println(); 
     client.print(nPessoas); 
} 
     client.stop(); 
    } 

    delay(10); 

} 

file.php:

<?Php 
$value = $_GET {['nPessoas']}; 
echo $value; 
?> 

回答

0

File.php:

<?php 
$value = $_POST['nPessoas']; 
echo $value; 
?> 

如果它沒有通過編輯PHP文件工作,那麼請在if (client.connect)之前加入以下內容:

data = "nPessoas=" + nPessoas; 

,然後更改client.print(nPessoas);client.print(data);

+0

公告:未定義的索引:在第2行 – mib

+0

nPessoas在/opt/lampp/htdocs/teste.php嘗試'的print_r($ _ POST);' –

+0

這是因爲如果他沒有通過這個職位。 – mib

相關問題