0

I'm using a Serial to WiFi module which are able to send HTTP. I have made a sketch to update a Table in MySQL with a PHP script. 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.

Look at lines below from loop() and PHP script also:

 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.

<?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');

?>
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • Two unrelated things: Use PDO/MySQLi, mysql_ is deprecated, and mysql has a "datetime" type, you could save yourself some work by using that instead of separating it. – John V. Apr 03 '14 at 14:00

1 Answers1

0

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. 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 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. That is not valid for tm

also real C concatenation (what is appening inside String class can be found here: How do I concatenate const/literal strings in C?

Community
  • 1
  • 1
Lesto
  • 2,260
  • 2
  • 19
  • 26
  • Thanks,It solves the problem by split the serial commands into more lines. – user3493816 Apr 03 '14 at 15:06
  • also note the usage of print vs write: write will send RAW byte (for example, for TM will send 4 always byte), while print will convert that raw form into a humand readable (and URL compliant) string (witch size in byte is one for every letter, plus one for dot) – Lesto Apr 03 '14 at 16:14
  • edited the answer to add link to REAL string concatination in C, and String concatenation using String class. – Lesto Apr 03 '14 at 16:17