【问题标题】:Problem with sending data from Arduino to database via PHP script通过 PHP 脚本将数据从 Arduino 发送到数据库的问题
【发布时间】:2021-10-22 20:01:26
【问题描述】:

各位程序员大家好!我正在做一个小项目——它是关于通过 PHP 脚本将数据从 arduino 传感器发送到数据库。我有自己的网站作为主机,我的 php 脚本使用 GET 方法发送数据。我对其进行了多次测试,如果我将原始数字放入 sql 查询(数据库正在获得记录),它会正确发送数据,但是当我想将 arduino 中的变量放入查询时问题就开始了 - 它在表中放入“0” .我看了很多教程并在这里寻找解决方案 - 无济于事。这是我的 PHP 脚本:

<html>
<body>

<?php

require_once "sql_connect.php"; 

$connect = @mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);

if(!$connect){
    echo "Error: " . mysqli_connect_error();
    exit();
}

echo "Connection Success!<br><br>";

$waterlevel = $_GET["waterlevel"];
echo $waterlevel;

$query = "INSERT INTO waterLevelTable (waterlevel) VALUES ('".$waterlevel."')";  //sends 0 
//$query = "INSERT INTO waterLevelTable (waterlevel) VALUES ('20')"; sends 20 to table
$result = mysqli_query($connect,$query);
echo "Insertion Success!<br>";

?>
</body>
</html>

sql_connect.php 是我拥有凭据的地方。

现在进入 Arduino 代码。我从arduino示例中“借用”了代码片段,DHCPAdressPrint连接到互联网,为水传感器编写代码,然后尝试通过GET方法将传感器值发送到数据库。这就是我挣扎的地方......这是代码:

#include <SPI.h>
#include <Ethernet.h>
#define POWER_PIN_WATER_SENS 7
#define SIGNAL_PIN_WATER_SENS A5
#define SOUND_PIN A0
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
int waterLevel = 0;
String waterLevelString;     
EthernetClient client;
void setup() {

  Serial.begin(9600);
  pinMode(POWER_PIN_WATER_SENS,OUTPUT);
  digitalWrite(POWER_PIN_WATER_SENS, LOW);  
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    while (true) {
      delay(1);
    }
  }
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  switch (Ethernet.maintain()) {
    case 1:
      //renewed fail
      Serial.println("Error: renewed fail");
      break;

    case 2:
      //renewed success
      Serial.println("Renewed success");
      //print your local IP address:
      Serial.print("My IP address: ");
      Serial.println(Ethernet.localIP());
      break;

    case 3:
      //rebind fail
      Serial.println("Error: rebind fail");
      break;

    case 4:
      //rebind success
      Serial.println("Rebind success");
      //print your local IP address:
      Serial.print("My IP address: ");
      Serial.println(Ethernet.localIP());
      break;

    default:
      //nothing happened
      break;
  }
  digitalWrite(POWER_PIN_WATER_SENS, HIGH);
  delay(100);
  waterLevel = analogRead(SIGNAL_PIN_WATER_SENS);
  digitalWrite(POWER_PIN_WATER_SENS, LOW);
  Serial.print("Water: ");
  waterLevel = 1; //debug value
  Serial.println(waterLevel);
  waterLevelString = "waterlevel=" + (String)waterLevel;


    if (client.connect("<snip>", 80)) {
    client.print("GET /arduino_connect_to_db.php HTTP/1.1");
    client.println("Host: <snip>");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(waterLevelString.length());
    client.println();
    client.print(waterLevelString);
  }
    if (client.connected()) { 
    client.stop();
  }
    delay(3000);
  }

我认为问题出在 arduino 和 php 脚本之间的连接上 - 我不知道有错误。你能帮我找到它并给出解决方案吗?

【问题讨论】:

  • 我认为您可以将水位字符串添加到GET /arduino_to_db.php 行并删除有关 Content-Type 和 Content-Length 的标题。
  • Content-Type: application/x-www-form-urlencoded 是 POST 请求所必需的,而不是 GET 请求
  • 您的脚本对于 sql 注入来说是易受攻击的,因此请仅使用 带参数的准备好的语句参见stackoverflow.com/questions/60174/…
  • ... 或者只是将您的变量转换为 int。这个脚本对于任何长期解决方案来说都太简单了。

标签: php mysql sql arduino


【解决方案1】:

看起来您构建的 HTTP 请求不正确,试试这个:

if (client.connect("<snip>", 80)) {
    client.print("GET /arduino_connect_to_db.php?");
    client.print(waterLevelString);
    client.println(" HTTP/1.1");
    client.println("Host: <snip>");
    client.println("User-Agent: arduino");
    client.println("Accept: */*");
}

【讨论】:

  • 这似乎不起作用,数据库表中没有新记录。
  • OP,您可能应该将 getallheaders() 和 $_REQUEST 值转储到文件中以进行调试。
猜你喜欢
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 2012-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多