首页 > 解决方案 > 从 Arduino 向 Raspberry Pi 发送 HTTP GET 请求时遇到问题

问题描述

我在 Raspberry Pi3 上运行 Incredible PBX。我有一个向 Raspberry Pi 发送 HTTP GET 请求的 Arduino MEGA。Arduino 发送三个变量 ext、calleeid 和 room 并调用 Raspberry Pi 上的 PHP 文件将值写入 MySql。我可以在 Raspberry Pi 上的 Apache2 访问日志文件中看到 GET 请求,但 PHP 文件没有运行。感谢任何建议。

我以前在 Raspberry Pi 模型 A 上进行过此操作,但现在我使用的是更新版本,我无法弄清楚为什么 PHP 文件不加载并将值写入数据库。

Arduino代码是 -

if (client.connect(server, 80)) {
    Serial.println("-> Connected and sending HTTP Request");
    // Make a HTTP request:
    client.print( "GET /add_data.php?"); //THIS SHOULD BE add_data.php
    client.print("ext=");
    client.print( extn );

    client.print("&&");
    client.print("room=");
    client.print( room );

    client.print("&&");
    client.print("callerid=");
    client.print( callerId2 );

    client.println( " HTTP/1.1");
    client.print( "Host: " );
    client.println(server);
    client.println( "Content-Type: application/x-www-form-urlencoded" );     
    client.println( "Connection: close" );
    client.println();
    client.println();
    client.stop();
    //Serial.print ("Room = " + room);

    Serial.println ("HTTP Request proceesed and sent!");
    Serial.println ("Database queries in process");
   }
   else {
    // you didn't get a connection to the server:
    Serial.println("--> connection failed/n");
   }

PHP代码是 -

`

// Prepare the SQL statement
$SQL = "INSERT INTO test.newdata (ext ,callerid ,room) VALUES ('".$_GET["ext"]."','".$_GET ["callerid"]."','".$_GET["room"]."')";   
$SQL1 = "INSERT INTO test.room (room) VALUES ('".$_GET["room"]."')";              

// Execute SQL statement
mysql_query($SQL);
mysql_query($SQL1);
include("select_room.php");


// Go to the review_data.php (optional)
header("Location: Site_2/review_data.php");
?>` 

Apache2 访问日志文件显示 GET 请求 -

xxxx - - [13/Apr/2019:22:50:29 -0400] "GET /Site_2/review_data.php HTTP/1.1" 200 1538 " http://xxxx/Site_2/review_data.php " "Mozilla/5.0 ( Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134" xxxx - - [13/Apr/2019:22:50:52 -0400] "GET / add_data.php?ext=5000&&room=110&&callerid=110+Config+Test" 400 0 "-" "-" 10.1.11.200 - - [13/Apr/2019:22:51:14 -0400] "GET /Site_2/review_data .php HTTP/1.1" 200 1538 " http://192.168.50.123/Site_2/review_data.php " "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 边缘/17.17134"

标签: phparduinoraspberry-pi3

解决方案


首先,您以错误的方式使用 REST 架构。实际上,您使用 GET 插入新数据而不是 POST 类型的请求。

但是,我没有尝试过您的代码,但问题可能是双 && 而不是单 &。

此外,您必须使用 error_log($variable_name) 调试您的 PHP 代码,以查看您是否正确接收到来自 GET 的值。

提示:当您使用 REST 调用时,请尝试使用 POSTMAN 软件来模拟 REST CALL 并查看响应


推荐阅读