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 请求之前,您似乎缺少一个“client.connect(...)”来连接到您的 PHP 服务器:

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

于 2013-02-01T21:18:46.087 回答