我一直试图成功地将我的Arduino连接到我的phpMyAdmin服务器,但我没有能够。Arduino连接了一些超声波传感器(用于测量距离),由于某种原因,并非所有的值都被发送到服务器。我不应该这么复杂。
因此每个传感器都有自己的id,测量距离和传感器所在的水平。
id是用来识别传感器的,但由于某种原因,我得到的这个值是0,我认为这是因为它只读取第一个传感器的值(id为0)。
让我给你看我的代码,这是重复的方法,我认为我的错误是不能多次发送信息,像从每个传感器分离信息,它只发送前4个值,这就是它在数据库上显示的。
void loop(){
int distance;
//=======
for (uint8_t i = 0; i < SONAR_NUM; i++) {
if( cm[i] > 10 || cm[i]==0){
distance = cm [i];//delete if no
}
else{
distance = cm[i];
//Serial.println(distance);
}
}
for (uint8_t i = 0; i < SONAR_NUM; i++) {
if (millis() >= pingTimer[i]) {
pingTimer[i] += PING_INTERVAL * SONAR_NUM;
if (i == 0 && currentSensor == SONAR_NUM - 1)
sonar[currentSensor].timer_stop();
currentSensor = i;
cm[currentSensor] = 0;
sonar[currentSensor].ping_timer(echoCheck);
}
//client.stop();
}
//===================================================
if(client.connect(serverName, 80) )
{
//Serial.println("Connected");
if (client.connected()){
delay(100);
//Serial.println("Client is available, CONNECTED!");
//client.print( "GET /add.php?distance=");
//////////////
int lvl = 0;
for (uint8_t i = 0; i < SONAR_NUM; i++) {
if(i<2)
lvl = 1;
else if (i>1 && i<3)
lvl =2;
else
lvl = 3;
Serial.print("SPOT: ");
client.print( "GET /sendfromarduino.php?sensorID=");
Serial.print(i);
client.print (i);
Serial.print(" DISTANCE: ");
Serial.print(cm[i]);
distance = cm[i];
client.print( "&value=");
client.print(distance);
Serial.print("cm ");
Serial.print(" LEVEL: ");
client.print( "&level=");
Serial.print (lvl);
client.print (lvl);
}
Serial.println();
//////////////////////////////////////
// Serial.print("Distance: ");
// client.print(distance);
// Serial.println(distance);
client.println( "HTTP/1.1");
client.println( "Host: test.testserver.com" );
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println();
client.println();
client.stop();
}
else
{
//Serial.println(distance);
Serial.println("Connection Failed");
}
}
delay(100);
}
,然后这里是PHP文件的代码,应该是完美的,根据我的知识和过去的经验:
<?php
$db = new PDO('mysql:host=localhost;dbname=test_alpha;charset=UTF-8', 'test_alpha', 'testtest');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sensorID = intval($_GET['i']);
//$time = CURRENT_TIMESTAMP;
$value = intval($_GET['distance']);
$level = intval($_GET['lvl']);
$sql = "INSERT INTO `pspace`(`sensorID`, `time`, `value`, `level`) VALUES ('$sensorID',CURRENT_TIMESTAMP,'$value','$level')";
$stmt = $db->query($sql);
//echo json_encode(array('spaces'=>$stmt->fetchAll(PDO::FETCH_ASSOC)));
?>
任何帮助都非常感谢!
上面写着if (client.connected())
应该是while (client.connected())
问题解决了!感谢所有花时间回答我问题的人!