Hi, i'm italian and that's my first post on the international forum.
I'm trying to use Arduino Ethernet Shield + PHP + MySQL but i have some problems.
That's my Arduino code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//indirizzo server web (locale)
IPAddress server(192, 168, 0, 4);
//indirizzo ip dell'Arduino
IPAddress ip(192, 168, 0, 10);
String strURL = "";
float temp = 0;
char c;
EthernetClient client;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac,ip);
Serial.println("Ethernet Shield Arduino : Temperatura");
Serial.println(Ethernet.localIP());
}
void loop()
{
if(client.connect(server,80)){
temp = analogRead(A5);
temp = temp * 0.48828125;
//creo l'url utilizzanso una stringa
strURL = "GET /Arduino/indice.php?valore=";
strURL += (int)temp;
strURL += "&localita=Studio HTTP/1.1";
//invio la richiesta al server
client.println(strURL);
client.print("Host:");
client.println(server);
client.println("Connection: close");
client.println();
}
while(client.available())
{
char c = client.read(); Serial.print(c);
}
That's my PHP code:
<?php
//controllo se sono presenti i parametri valore e localita
if(isset($_GET['valore']) && isset($_GET['localita']))
{
//Recupero il valore del parametro "valore"
$valore = $_GET['valore'];
//Recupero il valore del parametro "localita"
$localita = $_GET['localita'];
//eseguo la connessione al database sul server locale
//inserendo nome utente e password
$link = mysql_connect('localhost', '...', '...');
//gestione degli errori
if (!$link) {die('Impossibile connettersi: ' . mysql_error());}
//seleziono il databse di nome arduino
mysql_select_db("Arduino") or die( "Impossibile selezionare il database.");
//creo una stringa sql di inserimento con i valori
//recuperati dall'url
$sql = "INSERT INTO Temperature (Valore,Localita)";
$sql .= "VALUES ('" . $valore . "', '" . $localita . "')";
//eseguo la query
$retval = mysql_query( $sql, $link );
//gestione degli errori
if(! $retval ){die('Impossibile eseguire la query: ' . mysql_error());}
//chiudo la connessione al db
mysql_close($link);
}
?>
The PHP code work because if i write in the url bar http://localhost/Arduino/indice.php?valore=20&localita=Home i can found these data in my DB. But if i pulg in my arduino , i can't access to it due to its IP. I tried to do anything i can and i hope someone could help me! Thanks!