0

I am currently having difficulties sending data from an Arduino using an ethernet shield to a PHP database in MySQL. My problem is that it appears that the server is receiving the data as there is a time stamp when I send it from the Arduino however, the other values it is receiving are incorrect (see images below). The server says the values of "data", and "serial_number" are 0 but they are supposed to be 10 and 24. Along with that the Arduino is saying that the data has been sent successfully. My code for both the server and the Arduino is listed below. Any ideas on how to fix this or what is going on are much appreciated.

enter image description here

Arduino Code:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x00, 0xAB, 0xBA, 0xBC, 0xDD, 0x02 };
EthernetClient client;
String data;
int t;
int h;

void setup() {
  Serial.begin(9600);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
  }

  Serial.println("still working here");


}


void loop() {
t = 10;
h = 24;

data = "temp1=";

data.concat(t);

data.concat("&hum1=");

data.concat(h);


  if (client.connect("www.smartwater.x10host.com",80)) {
    Serial.println(data);
    client.print("POST /add.php HTTP/1.1\n");
    client.print("Host: www.smartwater.x10host.com\n");
    client.print("Connection: close\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(data.length());
    client.print("\n\n");
    client.print(data);
    Serial.println("Successfull");


  }

  if (client.connected()) {
    client.stop();
  }

  delay(20000);
}

add.php (Server Code)

<?php
    include("connect.php");

    $link=Connection();

    $temp1=$_POST["data"];
    $hum1=$_POST["serial_number"];

    $query = "INSERT INTO `Main` (`data`, `serial_number`) 
        VALUES ('".$temp1."','".$hum1."')"; 

    mysql_query($query,$link);
    mysql_close($link);

    header("Location: index.php");
?>

connect.php (Server Code)

<?php

    function Connection(){
        $server="localhost";
        $user="smartwat_arduino";
        $pass="*****";
        $db="smartwat_Main";

        $connection = mysql_connect($server, $user, $pass);

        if (!$connection) {
            die('MySQL ERROR: ' . mysql_error());
        }

        mysql_select_db($db) or die( 'MySQL ERROR: '. mysql_error() );

        return $connection;
    }
?>

index.php (Server Code)

<?php

    include("connect.php");     

    $link=Connection();

    $result=mysql_query("SELECT * FROM `Main` ORDER BY `timeStamp` DESC",$link);
?>

<html>
   <head>
      <title>Data</title>
   </head>
<body>
   <h1>Stuff</h1>

   <table border="1" cellspacing="1" cellpadding="1">
        <tr>
            <td>&nbsp;Timestamp&nbsp;</td>
            <td>&nbsp;Data&nbsp;</td>
            <td>&nbsp;Serial Number&nbsp;</td>

        </tr>

      <?php 
          if($result!==FALSE){
             while($row = mysql_fetch_array($result)) {
                printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>", 
                   $row["timeStamp"], $row["data"], $row["serial_number"]);
             }
             mysql_free_result($result);
             mysql_close();
          }
      ?>

   </table>
</body>
</html>
1

In your Arduino code, the function call that send the real data within the HTTP-POST call, is this:

[...]
client.print(data);
[...]

When executed, the content of the data variable is sent to the server.

Supposing data is a String object then data.concat() is this function, allowing "... String, string, char, byte, int, unsigned int, long, unsigned long, float, double..." as argument.

So, after the execution, data should contain:

temp1=10&hum1=24

and such content is exactly what is passed, via POST to the /add.php script.

Basically, you're passing two variables, called "temp1" and "hum1". Please keep note of those variable names!

Now let's switch the the PHP script, where you have:

[...]
$temp1=$_POST["data"];
$hum1=$_POST["serial_number"];
[...]

Above two statements, asks PHP to retrieve, via the $_POST function, the content of the two variables data and serial_number.

As such variables are undefined (the variables you passed were temp1 and hum1), PHP consider them like 0 and, as such, assign 0 to $temp1 and $hum1.

I bet that to solve your problem you only need to change above PHP with:

[...]
$temp1=$_POST["temp1"];
$hum1=$_POST["hum1"];
[...]

BTW: please note that what is inside the $_POST call is totally unrelated with the names of the variables you have on the left of the = sign: they are really different things!

0

Data = 10 is not the characters making 1 and 0 but an ascii character with value 10, your php script doesn't understand that. Try using itoa function to make character array 1 and 0 for your php to understand.

0

You get a timestamp from running the php file. That doest mean that there is a number in $data or $serialnmber. What happens if you fill in a value in your php file instead of the variables $data and serialnumber. If the dbase gets filled, you know that than your variables were empty. If they arent filled something is wrong in your php coding

Not the answer you're looking for? Browse other questions tagged or ask your own question.