Pages: [1]   Go Down
Author Topic: Arduino and PHP  (Read 1658 times)
0 Members and 1 Guest are viewing this topic.
Norwich-UK
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there, im fairly new to computer programing so excuse my question if its obvious but im trying to get the arduino to connect to a mysql datebase, this is to store the pin output of the arduino for a homeautomation project. I have set up a working PHP please see below and this connects from an HTML page using forms. How can i adapt this to work with the arduino sending the pin output.

I have spent weeks trying to work this out and many hours googling to just find myself getting more and more lost. any help will be much appreciated.

have obviously taken my webserver name, password and username out.

-------my PHP file on my webserver---------------------------------------------

<?php
$con = mysql_connect("my_webhost","username","password");
if (!$con)
   {
   die('could not connect: ' .mysql_error());
   }
   
mysql_select_db("my_database", $con);

$sql="INSERT INTO Automation (arduino,pin2,pin3,pin4,pin5,pin6)
VALUES ('$_POST[arduino]','$_POST[pin2]','$_POST[pin3]','$_POST[pin4]','$_POST[pin5]','$_POST[pin6]')";

if (!mysql_query($sql,$con))
   {
   die('Error: ' . mysql_error());
   }
echo "1 record added";

mysql_close($con);
?>

--------my current working html page-------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>

</head>
<body>
<form action='http://my_webhost/arduinoInsert.php' method='post'>
arduino: <input type="text" name='arduino' /><br />
pin2: <input type='text' name='pin2' /><br />
pin3: <input type='text' name='pin3' /><br />
pin4: <input type='text' name='pin4' /><br />
pin5: <input type='text' name='pin5' /><br />
pin6: <input type='text' name='pin6' /><br />
<input type='submit' />
</form>
</body>
</html>
« Last Edit: May 20, 2012, 04:27:54 pm by tillsidephil » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


find inspiration in this link - http://snipplr.com/view/57138/http-post-from-arduino-wifly-library/ -

but you have to format your own data-string.


Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 22
www.buildinginternetofthings.com
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

are you trying to just store values from the Arduino on MySQL or send values to the Arduino from the html page as well?

If it is only the first care, all you need is the php script that will receive values directly from the Arduino. It is more easy to use GET instead of POST, so here is your php code slightly modified:

Code:
<?php
$con 
mysql_connect("my_webhost","username","password");
if (!
$con)
   {
   die(
'could not connect: ' .mysql_error());
   }
   
mysql_select_db("my_database"$con);

$sql="INSERT INTO Automation (arduino,pin2,pin3,pin4,pin5,pin6)
VALUES ('
$_GET[arduino]','$_GET[pin2]','$_GET[pin3]','$_GET[pin4]','$_GET[pin5]','$_GET[pin6]')";

if (!
mysql_query($sql,$con))
   {
   die(
'Error: ' mysql_error());
   }
echo 
"1 record added";

mysql_close($con);
?>



Here is an Arduino sketch that connects to the php script and sends pin2, pin3, ...variables through POST:
Code:

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,1); // YOUR SERVER'S IP ADDRESS

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;


int pin2, pin3, pin4, pin5, pin6;

//String representations of the pin values
String spin2, spin3, spin4, spin5, spin6;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
 
}

void loop()
{
 
  //read pin values:
  pin2 = analogRead(0);
  //...
 
  spin2 = String(pin2, DEC);
 
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /arduinoInsert.php?arduino=arduino&pin2="+spin2+"&pin3="+spin3+"&pin4="+spin4+"&pin5="+spin5+"&pin6="+spin6+" HTTP/1.0");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
 

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    for(;;)
      ;
  }
}


Logged


Pages: [1]   Go Up
Jump to: