【问题标题】:Arduino Ethernet + PHP RequestArduino 以太网 + PHP 请求
【发布时间】:2017-08-13 21:33:18
【问题描述】:

我正在为 Arduino + ethernet shield 编写代码,以使用 $_GET 方法 (PHP) 将数据发送到 Mysql 数据库。

我已经在浏览器上测试了 PHP 代码,它运行良好,但在 Arduino 上它没有运行。我不知道问题出在哪里。我一直在寻找其他论坛,但找不到任何答案。以下是我的代码,如果有人可以帮助解决崩溃,我将不胜感激

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = { 192, 168, 1, 60 }; //IP (WEB CLIENT)
byte server[] = { 192, 168, 1, 8 }; //IP (SERVER) 
EthernetClient client;

void setup() { 
  Serial.begin(9600);  
  Ethernet.begin(mac, ip);
  if (client.connect(server, 8095)) {        
    Serial.println("CONECTED");
    client.print("GET http://192.168.1.8:8095/prs/Query.php?a=");
    client.print(2);
    client.print(" HTTP/1.1\r\n");
    Serial.println("Information sent successfully!"); 
    delay(1000);
  } else {
    Serial.println("Conection fail");
  }
}

void loop() { 
}

【问题讨论】:

  • 欢迎来到 Stackoverflow。不幸的是,您的问题很不清楚,有点像“这是我的代码,请为我更正!”这里不欢迎。
  • 对不起,如果我被误解了,但我只是请在这方面更有经验的人帮助我找到我的代码的问题,因为我已经尝试了一切,但我无法让它工作。
  • 串口监视器有什么输出?

标签: php mysql arduino ethernet


【解决方案1】:
client.print("GET http://192.168.1.8:8095/prs/Query.php?a=");
client.print(2);
client.print(" HTTP/1.1\r\n");

这不是格式良好的 HTTP 1.1 请求:

  1. 它在请求行中包含协议、主机名和端口。这在 HTTP 代理请求之外是不合适的。

  2. 它不包含Host 标头。

  3. 请求后不包含两个换行符。

正确的请求可能类似于:

"GET /prs/Query.php?a=2\r\nHost: 192.168.1.8\r\n\r\n"

【讨论】:

    【解决方案2】:

    我注意到以太网客户端变量/对象在其声明的地方称为 cliente(末尾带有 e),但在此下方,对象的名称是 client(末尾没有 e),如果您更正此差异名字有用吗?

    EthernetClient cliente;
    
    client.connect(server, 8095)
    

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多