这是我在stackoverflow中的第一个问题帖我正在做一个项目将传感器数据存储到microsoft sql server但是在我尝试了很多次之后我仍然无法将我的arduino数据记录到服务器。我已经在php文件中手动输入值,它适用于数据库的输入。谁能帮我?谢谢 这是我完成的arduino代码。
byte mac[] = {0x78, 0x4B, 0x87, 0xA4, 0xA1, 0xB0 };
IPAddress ip(192,168,1,21);
EthernetClient client;
int DO=10;
int pH=5;
String data;
void setup()
{
data="";
Serial.begin(9600);
Ethernet.begin(mac, ip);
if (client.connect("192.168.1.19", 80))
{
Serial.println("connected");
client.print("POST /psm/test.php?");
client.print("DO");
client.print(DO);
client.print("&pH=");
client.print(pH);
client.println("");
client.println(" HTTP/1.1");
client.stop();
}
else {
Serial.println("--> connection failed\n");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
`
这是我用过的php代码
<?php
#region connection setup
$serverName = "USER-PC\TZEYEOW";
$connectionInfo = array( "Database"=>"tasik_B1", "UID"=>"tzeyeow", "PWD"=>"12345678");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
echo"connected.<br/>";
#regionend
#region getting data from arduino
$DO=(isset($_GET['DO']) ? $_GET['DO'] : null);
$pH=(isset($_GET['pH']) ? $_GET['pH'] : null);
date_default_timezone_set("Asia/Kuala_Lumpur");
$dT = date("Y-m-d h:i:sa");
$query = "INSERT INTO tasik_B1 (DO,pH,logDateTime)
VALUES ('5','".$pH."','".$dT."')";
//VALUES ('".$OREV."','".$OLENGTH."','".$dT."')";
$params =array(1, "value");
$stmt = sqlsrv_query( $conn, $query);
// Close the connection
sqlsrv_close($conn);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
// Close the connection
sqlsrv_close($conn);
}
?>