使用Arduino,PHP和jQuery进行温度传感

时间:2013-11-18 19:14:33

标签: php jquery arduino

我试图从这个家伙网站复制部分代码,我正在做与他相同的事情。但在我的数据库中,我无法获得价值。这是链接temperature sensing。在我的arduino中,我没有问题在串行监视器中收到这样的值:GET /getData/temp.php?t=-23.00

但是当我去我的服务器检查数据库时,没有记录数据。这是我在arduino中的一些代码。

void getData() {

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 8000; // 10 minutes (10*60*1000) 600000
  char strURL[70];

   EthernetClient client;
  // If there's a successful connection, send the HTTP POST request
   currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;

  if (client.connect(server)) {
    Serial.println("get data connecting...");


    sprintf(strURL,"GET /collecting/temp.php?t=%d",ambientTemperature);

    // EDIT: 'Host' to match your domain
    client.println("Host:192.100.1.45 ");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    //Serial.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.print(strURL);
    client.println();
   // client.print(strURL);
    Serial.print(strURL);



  } 
  else {
    // If you couldn't make a connection:
    Serial.println("Connection failed");
    Serial.println("Disconnecting.");
    client.stop();
  }
}
}

这是php代码插入。

<?php
// connect to MySQL
mysql_connect('localhost','temperature','123') or die("Can't connect that way!");
@mysql_select_db('temperature') or die("Unable to select a database called 'temperature'");
if(ISSET($_GET['t']) && (is_numeric($_GET['t'])) ){
  // message from the Arduino
  $temp = $_GET['t'];
  $qry = "INSERT INTO tem(timing, temp) VALUES(".time().",'$temp')";
  echo $qry;
  mysql_query($qry);
  mysql_close();
  exit('200');
}
// no temp reading has been passed, lets show the chart.

$daysec = 60*60*24; //86,400
$daynow = time();
if(!$_GET['d'] || !is_numeric($_GET['d'])){
  $dayoffset = 1;
} else {
  $dayoffset = $_GET['d'];
}
$dlimit = $daynow-($daysec*$dayoffset);

$qryd = "SELECT id, timing, temp FROM tem WHERE timing>='$dlimit' ORDER BY id ASC LIMIT 1008";
// 1008 is a weeks worth of data,
// assuming 10 min intervals
$r = mysql_query($qryd);
$count = mysql_num_rows($r);
$i=0;
$maxtemp = 60; // moderate weather now, but this can be
$mintemp = 0; // adjusted if needed. Just sample values for the moment.
$return;
$r = mysql_query($qryd);
$count = mysql_num_rows($r);
while($row=mysql_fetch_array($r, MYSQL_ASSOC)) {
  $tid=$row['id'];
  $dt = ($row['timing']+36000)*1000;
  $te = $row['temp'];
  $te = (($te+252-500) / 10); // easier to do adjustments here
  if($te>$maxtemp) $maxtemp=$te; // so the graph doesnt run along the top
  if($te<$mintemp) $mintemp=$te; // or bottom of the axis
  $return .= "[$dt, $te]";
  $i++;
  if($i<$count) $return .= ", ";// if there is more data, add a ','
  $latest = "$dt|$te"; // this will get filled up with each one
}
mysql_close();
// convert $latest to actual date - easier to do it here than in javascript (which I loathe)
$latest = explode('|',$latest);
$latest[0] = date('g:ia, j.m.Y',(($latest[0])/1000)-36000);
// End main PHP block. Data looks like this: [ [123456789, 20.9],[1234654321, 22.1] ]
?>

1 个答案:

答案 0 :(得分:0)

您的HTTP标头错误。

get请求应该看起来像

GET /collecting/temp.php?t=123 HTTP/1.0

或http1.1

GET /collecting/temp.php?t=123 HTTP/1.1
Host: 192.100.1.45

HTTP get请求必须从GET行开始 并以空行结束;

如果要为HTTP请求添加额外选项, 你必须在GET之后添加它们,而不是之前。

arduino代码应该看起来像

sprintf(strURL,"GET /collecting/temp.php?t=%d HTTP/1.1",ambientTemperature);
client.println(strURL);
client.println("Host: 192.100.1.45");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.println();
是的,你有一个公共IP地址192.100.1.45
或者是否有拼写错误,应该是私人地址192.168.1.45