首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Arduino没有正确地将数字发送到SQL (通过PHP)

Arduino没有正确地将数字发送到SQL (通过PHP)
EN

Stack Overflow用户
提问于 2017-10-09 16:34:06
回答 1查看 451关注 0票数 2

因此,基本上我尝试从Arduino板中获取一些值,并将其保存到存储在PC上的SQL数据库中。

我已经在网上查看了多个资源,花了大约5-6个小时试图破解这个问题,但现在我在这里请求你的帮助。

因此,以下是似乎有效的方法。

通过键入:[http://localhost/myapp/write_data.php?value1=2&value2=10] to google chrome,我可以直接将值发送到数据库。这为我提供了PHP脚本的预期输出,并更新到数据库中。然而,当我在Arduino上运行它时,它会通过代码,就像它在工作一样,但它不会向SQL Server发送任何数据。

下面是Arduino代码。

代码语言:javascript
运行
复制
#include <SPI.h>
#include <Ethernet.h>

int rand1 = 0;
int rand2 = 0;



//EthServer(80);

//IPAddress server(10,0,0,1);
char server[] =("10.0.0.1");



EthernetClient client;


byte mac[] = {
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x0
};

byte ip[] = { 10, 0, 0, 2 }; 



void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
}

void printIPAddress()
{
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }

  Serial.println();
}

 void loop() {

  delay(1000);
    rand1 = random(1, 3);
    rand2 = random(25);
    Serial.println(rand1);
    Serial.println(rand2);

 // Connect to the server (your computer or web page)  
  if (client.connect(("http://10.0.0.1"), 80)) {
    client.print("GET /myapp/write_data.php?"); // This
    client.print("value1="); // This
    client.print(rand1); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print(";");
    client.print("value2=");
    client.print(rand2);
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 10.0.0.1"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line
    client.stop();    // Closing connection to server
  }
    else {

    // If Arduino can't connect to the server (your computer or web page)
    Serial.println("--> connection failed\n");
    printIPAddress();

  }
  delay(10000);

}

然后在这里我有PHP脚本

代码语言:javascript
运行
复制
    //connect

    $link=new mysqli("localhost", "root", "", "myappdb");

    //check connection

    if ($link==false) {
        die("Connection failed: " . mysqli_connect_error());
        } 


    //insert values

    $sql = "INSERT INTO resistence (phase, max_reading) 
            VALUES (". $_GET['value1'] .",". $_GET['value2'].")";

    //// Check if values have been inserted, confirm with user that values are correct.

    if(mysqli_query($link, $sql)){

        echo "Records inserted successfully.";
        echo "Values inserted are Phase " . $_GET['value1']. " and Max_Reading ". $_GET['value2']. ".";
    }
        else
    {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    }

    /////


//close connection
mysqli_close($link);

?>

任何帮助都是很棒的。

干杯约翰

EN

回答 1

Stack Overflow用户

发布于 2017-10-09 19:58:27

如果你没有得到“连接失败”的响应,那么它应该是连接的。试一试

代码语言:javascript
运行
复制
client.print("&");

相反,

代码语言:javascript
运行
复制
client.print(";");

如果仍然不起作用,请在PHP文件中使用

代码语言:javascript
运行
复制
error_log("text");

并检查服务器error.log以确定PHP脚本是否已启动,并检查是否设置了$_GET变量。

编辑:添加:

你不应该在if语句中使用client.connect。

client.connect返回指示连接状态的int (1,-1,-2,-3,-4

代码语言:javascript
运行
复制
SUCCESS 1
TIMED_OUT -1
INVALID_SERVER -2
TRUNCATED -3
INVALID_RESPONSE -4 

所以打印出返回值,然后继续调试。您提供的IP地址一定有问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46642002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档