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