尝试将C程序连接到phpMyAdmin

时间:2013-03-14 02:10:36

标签: php mysql c phpmyadmin arduino

我一直在努力将我的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)));

    ?>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

它所说的if (client.connected())

应为while (client.connected())

问题解决了!感谢所有为我的问题投入时间的人!