Ethernet and sending to external MySQL web

Hi,

I have been trying to setup the arduino to send some data to a PHP to be stored in Mysql-db.

The arduino opens the PHP-file and the file seems to put date to one cell according to the NOW() function in the PHP.
The problem is that I don`t seems to get data in on variable "watt" from arduino on the Get command.

Hope someone could help me with this.

The arduino code:


#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 2 };
byte gw[] = {192,168,1,1};
byte server[] = { XXX, XX, XXX, XXX }; // My webpage IP
byte subnet[] = { 255, 255, 255, 0 };
int watt = 0;
int tempPin = 2;
int ledPin = 10;

void setup()
{

pinMode(tempPin, INPUT);

}
void loop()
{

delay(3000);
senddata();

}
void senddata()
{

watt = analogRead(tempPin); //Read analogue value
watt = (5.0 * watt * 100.0)/1024.0; //convertera analog data to temperature

Ethernet.begin(mac, ip, gw, subnet);
Client client(server, 80);
Serial.println();
Serial.println("Initiates connection?");
Serial.println("Connecting?");
delay(1000); //This one keeps it from hanging

if (client.connect()) {
Serial.println("Connected!");
client.print("GET http://hjeng.se/PHPFile.php?watt=");
client.print(watt);
client.println(" HTTP/1.1");
client.println("Host: www.hjeng.se");
client.println();
}

else
{
Serial.println("Connection failed");
}
//}
//stop client
client.stop();
while(client.status() != 0)
{
delay(5);
}
}


PHP code:

<?php $forbrukning = $_GET['watt']; //Connect to database $opendb = mysql_connect("hjeng.se.mysql", "hjeng_se", "Password") or mysql_error("Never gonna get here"); mysql_select_db("hjeng_se"); if ($opendb) { mysql_query(" INSERT INTO Mytable (Datum, Forbrukning) VALUES ( NOW() , $forbrukning )"); mysql_close($opendb); } ?>php
client.print("GET http://hjeng.se/PHPFile.php?watt=");
client.print(watt);
client.println(" HTTP/1.1");

GET? Shouldn't you be using PUT? You want to send data TO the server, not read data FROM the server.

I've never used the ethernet shield, so I don't know what functions it supports, but, at a casual glance, GET doesn't feel right.

Hi again,

I found the problem, I had integer in MYsql, but changing to float solved the problem.

Next step would be to make some graphs, don`t ask me how :slight_smile:

GET? Shouldn't you be using PUT? You want to send data TO the server, not read data FROM the server.

I'm not certain about PUT, but, with PHP, there is very little difference between a POST and a GET. The two are essentially interchangeable.

If it were me, six months from now I'd look at the GET and think the code was getting something from the server rather than sending. I'd certainly be confused. From a maintenance perspective, I'd say it makes more sense to use POST.

GET? Shouldn't you be using PUT? You want to send data TO the server, not read data FROM the server.

GET and POST are two methods that HTML uses to send form data TO the webserver. In which case "GET" is a rather confusing name.

"GET" encodes form data into the URL, while "POST" encodes and sends the data separately from the URL. So the code snippet shown would produce a URL like "http://hjeng.se/PHPFile.php?watt=1234". Using POST it would be "http://hjeng.se/PHPFile.php".

GET is easiest to use for this kind of task.

BTW: There is also a PUT method in HTML. It's mostly used for uploading files and it's what you would want to use for this.

No.

GET and POST work exactly the same way. The difference is entirely semantic. GET is guaranteed to be safe - that is, I can GET anything I want without breaking anything at all. Spiders use this - Google et.al will happily GET any normal links on a page.

If you're not careful, and have a GET request deleting a webpage (say, a wiki entry), a spider will come along and happily delete the entire site. And it'd be right.

This happens all the time.

But from a HTTP perspective, you can either GET /file.php?param=val or POST /file.php?param=val. The only difference to the user is the fact that web browsers will complain if the user tries to reload the POST, and the only difference to the application writer is the REQUEST_METHOD env variable (which you can check to explicitly deny, for the aforementioned reasons)

tl;dr Use either GET or POST; there's no technical difference. But be careful, because spiders the world over have been programmed to follow any and all GETs they find. Make sure this won't break anything, or just use POST.

No.

GET and POST work exactly the same way.

So with

<form method="get" action="form.php">

and

<form method="post" action="form.php">

the browser will send exactly the same thing will it?

Andrew

"GET and POST work exactly the same way. The difference is entirely semantic. GET is guaranteed to be safe - that is, I can GET anything I want without breaking anything at all. Spiders use this - Google et.al will happily GET any normal links on a page."

Since this thread is being referenced in other threads... Bzzzzt!!! Totally bad answer. A GET request is attached to a URL and is set as the query_string environmental variable on the server computer by the web server. A POST is sent as a seperate file to the web server and is put in STDIN on the server computer by the web server. Not sure how the ethernet library on the Arduino handles these incomming diffrences.

Hi,

I have a sample in my thread:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1270566281