0

rfid ベースの arduino を使用して電子料金徴収システムを構築しています。タグの「一意の ID」(arduino によって読み取られる) を php スクリプト (ローカルの apache サーバーのルートフォルダーに保存されます) に送信したいです。間違いをなくし、イーサネット設定がプログラムで正しいかどうかも確認してください..

#include <SPI.h>
#include <Ethernet.h>
EthernetServer server(80);
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
IPAddress ip(192,168,1,4);
EthernetClient client;
int  val = 0; 
char code[10]; 
int bytesread = 0; 
void setup() 
{ 
Ethernet.begin(mac, ip, gateway, subnet);
Serial.begin(9600); 
pinMode(2,OUTPUT);   
digitalWrite(2, HIGH);                 
}  

 void loop() <br>
{ 
 if(Serial.available() > 0) {          
    if((val = Serial.read()) == 10) {   
      bytesread = 0; 
      while(bytesread<10) {             
        if( Serial.available() > 0) { 
          val = Serial.read();
          if((val == 10)||(val == 13)) {  
            break;                       
          } 
          code[bytesread] = val;               
          bytesread++;                   
        }
      } 
      if(bytesread == 10) {             
       client.print("GET try.php?code=");
      client.print(code);

client.println(" HTTP/1.1");
client.println("Host: localhost");  
client.println();        
      } 
      bytesread = 0; <br>
      digitalWrite(2, LOW);                  
           delay(1500);                       
           digitalWrite(2, HIGH);                  // Activate the RFID reader
    } 
  } 
}
the php script:

<?php   
$variable = $_GET['code']
echo  "code is  $variable ";
?>
4

2 に答える 2

1

EthernetClient をサーバーに接続するのを忘れていました! Arduinoのドキュメントを見てください。

if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
} else {
    Serial.println("connection failed");
}

あなたの例では、次のように書く必要があります。

Serial.println("connected");
client.print("GET /try?code=");
client.print(code);
client.print(" HTTP/1.0");
client.println();

編集: これは完全な例です:

交換

client.print("GET try.php?code=");
client.print(code);
client.println(" HTTP/1.1");
client.println("Host: localhost");
client.println();

if (client.connect(serverIP, 80)) {
    Serial.println("connected");
    client.print("GET /try?code=");
    client.print(code);
    client.print(" HTTP/1.0");
    client.println();
} else {
    Serial.println("connection failed");
}

これを宣言に追加します

byte serverIP[] = { 127, 0, 0, 1 }; //That's localhost. Change it to whatever you need!
于 2013-02-01T21:27:57.867 に答える
0

HTTPリクエストを送信する前にPHPサーバーに接続するための「client.connect(...)」が欠落しているようです。

http://arduino.cc/en/Reference/EthernetClient

于 2013-02-01T21:18:46.087 に答える