Go Down

Topic: Arduino and PHP (Read 2174 times) previous topic - next topic


May 20, 2012, 08:47 pm Last Edit: May 20, 2012, 11:27 pm by tillsidephil Reason: 1
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---------------------------------------------

$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";


--------my current working html page-------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


<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' />


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.

Rob Tillaart

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



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: [Select]

if (!
'could not connect: ' .mysql_error());

$sql="INSERT INTO Automation (arduino,pin2,pin3,pin4,pin5,pin6)

if (!
'Error: ' mysql_error());
"1 record added";


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

#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:
  // 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:
  // give the Ethernet shield a second to initialize:

void loop()
  //read pin values:
  pin2 = analogRead(0);
  spin2 = String(pin2, DEC);
  if (client.connect(server, 80)) {
    // Make a HTTP request:
    client.println("GET /arduinoInsert.php?arduino=arduino&pin2="+spin2+"&pin3="+spin3+"&pin4="+spin4+"&pin5="+spin5+"&pin6="+spin6+" HTTP/1.0");
  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()) {

    // do nothing forevermore:

Go Up