[SOLVED] Arduino Ethernet write to MySQL $_GET issue PHP WAMP

After much search -very much- i am very dissapointed and would be grateful if any in here can help in some way.
I am writing a basic script for the Arduino to connect to WAMP server etc and write to MySQL.
I have tested the php and it writes to the database fine, so it aint a php-server thing.
Also the arduino code seems quite fine for my purposes and according to many examples around the net.
What seems to be the problem is my $_GET which is emty despite sending the data needed from the Arduino GET.
Also, when i have straight $_GET and not @$_GET it shows Notice: Undefined index: temperature in C:\wamp\www\phpNoobTest.php.
One last thing: When i try the php with a straight number in the query and not what should have been "GET" it enters it fine in the database.
HOWEVER, two times in a hundred tries, it managed somehow to enter value in the database(the correct expected temperature from my LM35 sensor), as if out of magic! I cant understand whats going on. Seems evil.

Thank you in advance for any answers, tries :slight_smile:

I ll post the codes and results so i am clear:

PHP:

<?php
var_dump($_GET);
//exit;
$val = @$_GET['temperature'];  
if(empty($_GET))
    echo "No GET variables";
else
    print_r($_GET); 
//@ for Notice: Undefined index: temperature in C:\wamp\www\phpNoobTest.php
//$val = htmlspecialchars($_GET["temperature"],ENT_QUOTES);
//Connect to database 
$con = mysql_connect("xxxxxxxxx", "xxxxx", "xxxxx");
if(!$con)
      die('Could not connect: ' .mysql_error());
mysql_select_db("test", $con);

$result = mysql_query("INSERT INTO tempinputs(idinput) VALUES ($val)");
mysql_close($con);
?>

ARDUINO CODE:

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { thecorrectIP };
byte serverName[] = { thecorrectIP }; 

EthernetClient client; 
float temperature;

void setup()
{
    Ethernet.begin(mac, ip);
    
    delay(1000);
    Serial.begin(9600);
}

void loop()
{
  
       temperature = (analogRead(0) * 500.00)/1024.00;

        Serial.print("\nARDUINO: temperature: ");
        Serial.println(temperature);
        Serial.print("ARDUINO: attempting to connect... ");

        if(client.connect(serverName, 80))
        {
            Serial.println("connected...");
            Serial.println("ARDUINO: forming HTTP request message");
            client.print("GET /phpNoobTest.php?temperature= ");
            client.print(temperature);
            client.println();
            client.println(" HTTP/1.1");
            client.println("Host: localhost");
            client.println();

            Serial.println("ARDUINO: HTTP message sent");
            delay(30000);

            client.stop();
            //client.flush(); 
        }
        else
        {
            Serial.println("connection failure");
        }
    
}

RESULTS FROM CODES:

-When i run the script phpNoobtest.php:

array (size=0)
empty

No GET variables

-When i run the arduino script, everything seems fine and in serial screen it shows many of those:

ARDUINO: temperature: 22.95
ARDUINO: attempting to connect... connected...
ARDUINO: forming HTTP request message
ARDUINO: HTTP message sent

       if(client.connect(serverName, 80))
        {
            Serial.println("connected...");
            Serial.println("ARDUINO: forming HTTP request message");
            client.print("GET /phpNoobTest.php?temperature= ");
            client.print(temperature);
            client.println();
            client.println(" HTTP/1.1");
            client.println("Host: localhost");
            client.println();

Two problems here, which may or may not be your bug.

  1. Don't send blanks in the query string. Remove the blank after temperature=

  2. The client.println() after sending the temperature is prematurely terminating your GET request before sending HTTP 1.1. Remove it.

(Important debugging tip: you can see this sort of bug in the server logs - examine access.log and error.log on the server and you will probably see requests without the HTTP1.1 part.)

An optimization you may wish to consider in the future: you don't need floating point arithmetic for what you're doing. You may as well use integer arithmetic for the temperature calculation instead and save a bunch of code space in the sketch.

Good luck with your project.

-br

@billroy: It is a problem. I was typing this when you responded.

There is a url/sql query error here.

// remove the space after '=' from this part. Spaces are an illegal character
            client.print("GET /phpNoobTest.php?temperature= ");
            client.print(temperature);
// no println here. The HTTP/1.1 must be on the same line
//            client.println();
            client.println(" HTTP/1.1");
            client.println("Host: localhost");
// HTTP/1.1 wants this Connection: close
            client.println("Connection: close");
            client.println();

Well, thank you both billroy and SurferTim!!!
Those were exactly perfect tips, and as a matter of fact, they made a little impact:
Once -and only once- more, a value was written to the database! Like the magic thing. I guess the other inputs were inserted when i was making test changes to the code and by luck your changes were there. Then again i was testing and ...
But the issue is still there, it wrote once, php and arduino code output are the same, and i still cant write anything more to the database.
Any other ideas?

Try something simple. The only problem is you must be able to read and display the response from the server. Otherwise, you are trying to troubleshoot this with your eyes closed. After your request send, use this to display the response and close the connection.

int loopCount = 0;

while(client.connected()) {
  while(client.available()) {
    loopCount = 0;
    Serial.write(client.read());
  }

  loopCount++;

  // if no packet received for 10 seconds, close the connection
  if(loopCount > 10000) {
    client.stop();
    Serial.println(F("\r\nTimeout"));
  }

  // 1ms delay for the timeout
  delay(1);
}

client.stop();
Serial.println(F("disconnected"));

Then try this for your php code. You will be able to see now. :slight_smile:

<html><body>
TEST

<?php
  $val = $_GET['temperature'];  
  echo('temperature is ' . $val . ' ');
?>
</body></html>

Once you get it displaying the temperature, then put it in the database.

edit: I forgot the delay(1) in the Arduino code. Corrected now.

What do the access and error logs say on the second attempt, compared to the first attempt?

-br

Thank you for the immediate replies! I was having lunch :slight_smile:

@SurferTim The code is there and working fine for the Arduino serial output. But the PHP still ain't working (without the "@" it shows the Notice message as before, i kept it to highlight it in here, as i am wondering if thats the step before the root of problems). Results:

ARDUINO: temperature: 21.97
ARDUINO: attempting to connect... connected...
ARDUINO: forming HTTP request message
ARDUINO: HTTP message sent
HTTP/1.1 200 OK
Date: Sun, 21 Apr 2013 13:26:29 GMT
Server: Apache/2.2.22 (Win64) PHP/5.3.13
X-Powered-By: PHP/5.3.13
Content-Length: 59
Content-Type: text/html

TEST

temperature is 21.97

TEST

( ! ) Notice: Undefined index: temperature in C:\wamp\www\phpNoobTest2.php on line 4
Call Stack

Time Memory Function Location

1 0.0005 668408 {main}( ) ..\phpNoobTest2.php:0
temperature is

======================================================================

@billyroy :
Apache Error log entries from my last time with the extra code and different php:

[Sun Apr 21 16:27:17 2013] [error] [client 127.0.0.1] PHP Notice: Undefined index: temperature in C:\wamp\www\phpNoobTest2.php on line 4
[Sun Apr 21 16:27:17 2013] [error] [client 127.0.0.1] PHP Stack trace:
[Sun Apr 21 16:27:17 2013] [error] [client 127.0.0.1] PHP 1. {main}() C:\wamp\www\phpNoobTest2.php:0

before, i was getting all those:

[Sun Apr 21 15:30:20 2013] [error] [client 192.168.2.8] request failed: error reading the headers
[Sun Apr 21 15:30:51 2013] [error] [client 192.168.2.8] request failed: error reading the headers
[Sun Apr 21 15:31:21 2013] [error] [client 192.168.2.8] request failed: error reading the headers

Apache access log entries before (phpNoobTest) and after with only SurferTim's code:

127.0.0.1 - - [21/Apr/2013:16:24:16 +0300] "GET /phpNoobTest.php HTTP/1.1" 200 1150
192.168.2.8 - - [21/Apr/2013:16:24:37 +0300] "GET /phpNoobTest.php?temperature=21.97 HTTP/1.1" 200 307
192.168.2.8 - - [21/Apr/2013:16:25:07 +0300] "GET /phpNoobTest.php?temperature=21.48 HTTP/1.1" 200 240
192.168.2.8 - - [21/Apr/2013:16:25:37 +0300] "GET /phpNoobTest.php?temperature=21.48 HTTP/1.1" 200 240
127.0.0.1 - - [21/Apr/2013:16:26:06 +0300] "GET /phpNoobTest.php HTTP/1.1" 200 135
192.168.2.8 - - [21/Apr/2013:16:26:10 +0300] "GET /phpNoobTest2.php?temperature=21.97 HTTP/1.1" 200 59
127.0.0.1 - - [21/Apr/2013:16:26:13 +0300] "GET /phpNoobTest2.php HTTP/1.1" 200 1009
127.0.0.1 - - [21/Apr/2013:16:26:23 +0300] "GET /phpNoobTest2.php HTTP/1.1" 200 1009
192.168.2.8 - - [21/Apr/2013:16:26:29 +0300] "GET /phpNoobTest2.php?temperature=21.97 HTTP/1.1" 200 59
127.0.0.1 - - [21/Apr/2013:16:26:41 +0300] "GET /phpNoobTest2.php HTTP/1.1" 200 1009
127.0.0.1 - - [21/Apr/2013:16:26:42 +0300] "GET /phpNoobTest2.php HTTP/1.1" 200 1009
127.0.0.1 - - [21/Apr/2013:16:26:57 +0300] "GET /phpNoobTest2.php HTTP/1.1" 200 1009

Those are log entries for two different pages. Are you requesting both those?

192.168.2.8 - - [21/Apr/2013:16:24:37 +0300] "GET /phpNoobTest.php?temperature=21.97 HTTP/1.1" 200 307

192.168.2.8 - - [21/Apr/2013:16:26:10 +0300] "GET /phpNoobTest2.php?temperature=21.97 HTTP/1.1" 200 59

And some are localhost (web browser on the server?)

127.0.0.1 - - [21/Apr/2013:16:26:06 +0300] "GET /phpNoobTest.php HTTP/1.1" 200 35

edit: This is an error created from localhost (127.0.0.1). That is a web browser on the server. Probably because you did not include the temperature with the request.

[Sun Apr 21 16:27:17 2013] [error] [client 127.0.0.1] PHP Notice: Undefined index: temperature in C:\wamp\www\phpNoobTest2.php on line 4
[Sun Apr 21 16:27:17 2013] [error] [client 127.0.0.1] PHP Stack trace:
[Sun Apr 21 16:27:17 2013] [error] [client 127.0.0.1] PHP 1. {main}() C:\wamp\www\phpNoobTest2.php:0

phpNoobTest is the first php code i posted.
phpNoobTest2 is the php code i used afterwards for testing purposes from you, SurferTim, in the previous post.
Server: yes, localhost, from WAMP specifically, on Mozilla Firefox 20.0.1

edit after SurferTim edit:
Can you explain this please! "Probably because you did not include the temperature with the request."
How do i fix that? Isnt in the Arduino code the thing GET does?

That was an error generated by your web browser on the server. If you enter this in the address bar
http://127.0.0.1/phpNoobTest2.php
and not
http://127.0.0.1/phpNoobTest2.php?temperature=21.97
then you might get an error in the php code ONLY from localhost (127.0.0.1)

The only log entries that matter here are the ones involving 192.168.2.8 (your Arduino).

Not so sure i got what you mean, but let me try again. (i am new to all this) Also not sure if it helps more. If not excuse me and ask away anything that might be needed.
I restarted WAMP, so i can get a cleaner view of the logs. And i run the codes as given by a mix of mine and SurferTim's for arduino, and the PHP given by SurferTim .
[edit]
Once more it doesn't work and php doesnt print the value temperature.
However as expected, http://127.0.0.1/phpNoobTest2.php?temperature=21.97 shows what is obvious: a temperature 21.97 .
Thanks for the progress till now!

Apache ERROR Log:

[Sun Apr 21 17:13:03 2013] [error] [client 127.0.0.1] PHP Notice: Undefined index: temperature in C:\wamp\www\phpNoobTest2.php on line 4
[Sun Apr 21 17:13:03 2013] [error] [client 127.0.0.1] PHP Stack trace:
[Sun Apr 21 17:13:03 2013] [error] [client 127.0.0.1] PHP 1. {main}() C:\wamp\www\phpNoobTest2.php:0

Apache ACCESS Log:

192.168.2.8 - - [21/Apr/2013:17:13:32 +0300] "GET /phpNoobTest2.php?temperature=22.95 HTTP/1.1" 200 59
192.168.2.8 - - [21/Apr/2013:17:14:07 +0300] "GET /phpNoobTest2.php?temperature=22.95 HTTP/1.1" 200 59
192.168.2.8 - - [21/Apr/2013:17:14:43 +0300] "GET /phpNoobTest2.php?temperature=22.95 HTTP/1.1" 200 59
192.168.2.8 - - [21/Apr/2013:17:15:18 +0300] "GET /phpNoobTest2.php?temperature=22.95 HTTP/1.1" 200 59

192.168.2.8 - - [21/Apr/2013:17:15:54 +0300] "GET /phpNoobTest2.php?temperature=22.46 HTTP/1.1" 200 59

For the Arduino, that is a perfect Apache Access log. You could not do any better. "200 59" is the target, and you hit it every time, at what looks like about a 35 to 40 second interval? And the temperature is 22.95.

I can't say the same for your web browser stuff on the server, but that is not the Arduino's problem. That sounds like a skinware error. :slight_smile:

I can't say the same for your web browser stuff on the server, but that is not the Arduino's problem. That sounds like a skinware error. smiley

Damn! i was thinking similar from the beginning. At least till now, unless someone disagrees with you, SurferTim, we have concluded somewhat at the point of no coding or logic error.
So the issue has to be somewhere else?
That's why i specified i saw the issue at $_GET which seems not to GET the value...
Is there anything else we can do?
I mean, it aint Arduino's problem, but what does "skinware error" means? Maybe something is wrong with Mozilla, the server or something more inside?

Grr.. Days and nights, hours and hours, trying to set up a MySQL/Apache/PHP server part by part, failed multiple times, installed WAMP and it plays alright, and now this!
[edit]
and this thing, once in a while to write a damn entry in the database!?! out of the blue!
Help is truly needed here, thank you all for the assistance till now, even for any thoughts passing by

That error was caused by skinware (you) because you did not send the temperature with that url. You can avoid an error situation in the php code by changing that "perfect world" test code to "real world" code. You must check the "temperature" variable to insure it both exists and is the correct format. But that is php.

Now you know the data sent by the Arduino and arriving at your server is valid. It it there in the access log.

Ok , now you are truly speaking in riddles onii-chan 8)
If i had any idea how to insure it both exists (i do know it gets the temperature and sends it to the server from the code right?) and is the correct format(that i dont know what it means and how to check it) i would be a happier person.
If its just php issue, maybe this ain't the right place for discussion, and i have already been helped, but my main issue remains, and i would very much like this thread to stay here at the forum some more so if anyone has encountered the same problem, or knows probable solutions and is kind enough, maybe i can move on.
Thanx again for all!

Here is a start on the "exists" part:
http://php.net/manual/en/function.isset.php

SOLVED!!!

It was a matter of php as said by SurferTim, who, along with billroy of course, i thank a lot for the guidance an' all !!!

The matter was that $_GET in php is one of the Register_globals, which for security reasons in later versions like the one i am using, are disabled.
Google search will show to anyone interested the full subject.

What i did was simple:
In php.ini, i changed Register_globals = off to Register_globals = on ,
but its for a personal use and nothing else. An experiment in home. So i wouldn't recommend doing so to anyone that will make any public use. Security reasons are not to be playing with without deep knowledge. And for anyone wanting to ask, i don't have it :smiley:

Be strong everyone

I got the same problem. my problem when the ardunio send the transcation, it create the record, but the temp value is zero, when I run browser same php file ? temp=22, the record is created and data is stored. so it is no problem in my php or ardunio script. I think..
I check with my access log in my server, it does receive the transaction with temp=22.3. lead to me there are some global variable security setting, I already disable my firewall, also I change php.ini set register_global to On, get the fatel err, saying php is no longer support register_global..

anyone can give me some idea to try

I also capture $GET from my php page use var_dump and store to my DB field value = "array(1) { ["temperature"]=> string(0) ""}

uncledj