Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将arduino读取的变量发送到php脚本(电子收费系统)_Php_Arduino - Fatal编程技术网

将arduino读取的变量发送到php脚本(电子收费系统)

将arduino读取的变量发送到php脚本(电子收费系统),php,arduino,Php,Arduino,我正在使用基于rfid的arduino建立电子收费系统;我想将标签的“唯一id”(由arduino读取)发送到php脚本(存储在本地apache服务器根文件夹中)。我已经编写了代码,请指出错误,并查看以太网设置在程序中是否正确 #include <SPI.h> #include <Ethernet.h> EthernetServer server(80); byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; IPAddr

我正在使用基于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 ";
?>
#包括
#包括
以太网络服务器(80);
字节mac[]={0x00,0x00,0x00,0x00,0x00,0x00};
IP地址网关(192168,1,1);
IP地址子网(255255,0);
ip地址ip(192168,1,4);
以太网络客户端;
int-val=0;
字符码[10];
int字节读取=0;
无效设置()
{ 
以太网开始(mac、ip、网关、子网);
Serial.begin(9600);
pinMode(2,输出);
数字写入(2,高);
}  
void loop()
{ 如果(Serial.available()>0){ 如果((val=Serial.read())==10){ 字节读取=0; 而(字节读取0){ val=Serial.read(); 如果((val==10)|(val==13)){ 打破 } 代码[字节读取]=val; bytesread++; } } 如果(字节读==10){ print(“GET-try.php?code=”); 客户端。打印(代码); client.println(“HTTP/1.1”); client.println(“主机:localhost”); client.println(); } 字节读取=0;
数字写入(2,低); 延迟(1500); digitalWrite(2,高);//激活RFID读卡器 } } } php脚本:
在发送HTTP请求之前,您似乎缺少连接到PHP服务器的“client.connect(…)”:


您忘记将EthernetClient连接到服务器!看看Arduino

在您的示例中,您必须编写:

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();

并将此添加到声明中

byte serverIP[] = { 127, 0, 0, 1 }; //That's localhost. Change it to whatever you need!

??? 当变量名为
code
from
“get try.php?code=“
我的错误…对不起,我编辑了代码,你从哪里得到了
$\u get['temp']
;看看你的代码还是错的
cde
而不是
code
谢谢您的回复,但是当我尝试这样做时,我收到了一个错误,您是否查找了链接?您必须将服务器作为字节数组而不是EthernetServer提供给connect函数。因此,您必须将该变量更改为{127,0,0,1}之类的字节数组或任何您想要的内容,而不要将EthernetServer变量指定给它!请看这里给出的示例:问题已解决..我必须配置我的服务器..感谢HowUpDoHighKnee建议我回头看@arduinodocumentation@Nikhil您的方法正确,必须检查此错误。你能分享一下吗?当我尝试执行你在链接中建议的参考代码时,它会起作用;但是,当我在自己的代码中进行更改时……然后就没有了,我怀疑这是否会在localhost上发生。是的,你应该“连接”到你的HTTP服务器,你有它的主机名或IP吗;我放弃了,现在我正在使用域名希望它works@Nikhil您的arduino的“localhost”就是您的arduino,我确信您的HTTP服务器不在您的arduino上,所以它无法连接到localhost。
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!