【问题标题】:Temperature Sensing with Arduino, PHP and jQuery使用 Arduino、PHP 和 jQuery 进行温度传感
【发布时间】:2013-11-18 19:14:33
【问题描述】:

我试图从这个家伙网站复制部分代码,我正在和他做同样的事情。但在我的数据库中,我无法获得价值。这是链接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] ]
?>

【问题讨论】:

  • 我在你的代码中看不到任何带有插入语句的sql查询!?
  • 我可以看看 php 吗?此外,请使用 GET 或 POST,不要同时使用。
  • @735Tesla 是的,我贴出来了
  • @user2196728 我发布了。
  • 你能加“print_r($_GET);”吗在您的 php 文件的“mysql_connect”行之前并将输出发送给我们?

标签: php jquery arduino


【解决方案1】:

您的 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

【讨论】:

    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多