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
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);
?>
Two problems here, which may or may not be your bug.
Don't send blanks in the query string. Remove the blank after temperature=
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.
@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.
<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.
Thank you for the immediate replies! I was having lunch
@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:
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.
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?
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!
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.
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!
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
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) ""}