简体   繁体   English

使用PHP更新MySQL的Arduino变量失败

[英]Arduino variables updating MySQL using PHP fails

I'm using a Serial to WiFi module which are able to send HTTP. 我正在使用能够发送HTTP的Serial to WiFi模块。 I have made a sketch to update a Table in MySQL with a PHP script. 我已经绘制了一个草图,用PHP脚本更新MySQL中的表。 My problem is, I'm not able to update the Table when using variables. 我的问题是,使用变量时我无法更新表。 It works fine with static values. 它可以与静态值一起正常工作。 I want to know if there is a problem in the Arduino sketch or the way to use the HTTP command. 我想知道Arduino草图或使用HTTP命令的方式是否有问题。

Look at lines below from loop() and PHP script also: 再看下面来自loop()和PHP脚本的代码行:

 float tm = 21.8;

 Serial.write("AT+HTTPPH=/update1x.php?tmp=tm\n");  // Parse values to PHP script

If I insert the value 21.8 instead of variable tm, it works. 如果我插入值21.8而不是变量tm,则它可以工作。

<?php

  $aa = (isset($_GET['tmp']) ? $_GET['tmp'] : null);

  mysql_connect('my.dk.mysql','my_dk','my_pw') or die("Can't connect that way!");

  @mysql_select_db('my_dk') or die("Unable to select a database called 'My'");

  date_default_timezone_set("Europe/Copenhagen");

  $dat = date("Y-m-d");

  $tim = date("H:i:s");

  $qry = "INSERT INTO temp1(temp, date, time) VALUES('$aa','$dat','$tim')";

  mysql_query($qry);

  mysql_close();

  exit('OK');

?>

because C does not "scan" the string and sobstitute "tm" with the value (think about what should happen all the time you used a variable called "i" or like...) C is a lot "lower" level, you have to concatenate by hand. 因为C不会“扫描”字符串并用值来吸收“ tm”(想想在使用变量“ i”之类的所有时间都会发生什么……)C的级别很多,“较低”,您必须用手连接。 actually a easy one may be: 实际上一个简单的可能是:

Serial.print("AT+HTTPPH=/update1x.php?tmp=");
Serial.print(tm);
Serial.print("\n");

using String class is possible, but you will use RAM dinamically, witch is not advised on a microcontroller: 可以使用String类,但是您将动态使用RAM,不建议在微控制器上使用witch:

String s = "AT+HTTPPH=/update1x.php?tmp=";
s += tm;
s += "\n";
Serial.print(s);

note that i concatenate one string for line; 请注意,我将一个字符串连接成一行; that is because on the right we don't have object String, but C string: array of char. 那是因为在右边我们没有对象String,但是C字符串:char数组。 That is not valid for tm 那对tm无效

also real C concatenation (what is appening inside String class can be found here: How do I concatenate const/literal strings in C? 也可以是真正的C串联(可以在这里找到String类中的内容: 如何在C中串联const / literal字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM