Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I am learning to work with the client - server communication. I am able to communicate with my server and able to store values in the table. But I want to receive one of the specific data from the database.

Here I want to get the 'abc' value from the 'Current' column to store in a variable in my arduino.

Below are the codes that I have done till now. I would be really happy, If you could solve the problem.

Here is my database structure

--------------------------------------------------- 
| Device |  Previous |  Next |  Distance |  Current| 
---------------------------------------------------
|katup123| xyz       | abc   | 2.600     | abc     |    
---------------------------------------------------

Here is my php code

                               locator.php

<?php
$con=mysqli_connect("your_domain.com","peter","abc123","locate");


// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT Current FROM locate
WHERE Device='katup123'");

while($row = mysqli_fetch_array($result)) {
echo $row['Current'];
echo "<br>"; 
}
?> 

Here is my arduino code :

// Include the GSM library
#include <GSM.h>
#define PINNUMBER ""


// APN data

#define GPRS_APN       "GPRS_APN" // replace your GPRS APNgpsll

#define GPRS_LOGIN     "login"    // replace with your GPRS login

#define GPRS_PASSWORD  "password" // replace with your GPRS password



// initialize the library instance

GSM gsmAccess;

GSMClient client;

GPRS gprs;



// URL, path & port (for example: arduino.cc)

char server[] = "your_domain.com";

char path[] = "/locater.php";

int port = 80; // port 80 is the default for HTTP




// Setup function


void setup()

{

// initialize serial communications and wait for port to open:

Serial.begin(9600);

Serial.println("Starting Arduino web client.");


// connection state

boolean notConnected = true;


// Start GSM shield

// If your SIM has PIN, pass it as a parameter of begin() in quotes

while(notConnected)

{

  if((gsmAccess.begin(PINNUMBER)==GSM_READY) &

  (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))

  notConnected = false;

else

{

  Serial.println("Not connected");

  delay(1000);

}

}

Serial.println("GSM initialized");

Serial.println("connecting...");



}


// Loop Function


void loop()

{

  char result [20];


//***************************************************



Serial.print(" Connecting to server Database ");



if (client.connect(server, port))

{


client.print("GET /locator.php?");

Serial.print("GET /locator.php?");

client.println(" HTTP/1.1");

Serial.println(" HTTP/1.1");

client.println("Host: www.your_domain.com");

Serial.println("Host: www.your_domain.com");

client.println("User-Agent: Arduino");

Serial.println("User-Agent: Arduino");

client.println("Accept: text/html");

Serial.println("Accept: text/html");

client.println("Connection: close");

Serial.println("Connection: close");

client.println();

Serial.println();


Serial.println("\nCOMPLETE!\n");

client.stop();


}


else

{


Serial.println("connection failed");

 Serial.println("\n FAILED!\n");


}


delay(5000);

}  

Here I want to store the 'abc' value to my 'result' variable. I don't know how to do it ?

share|improve this question
    
To get data from a webserver to the arduino, just make a hph or html page with the value written on the page. That way you can get the value from the webserver by requesting the page and then use the value in your code. Since you already have the submit done, just make another page that writes out the value from the DB. –  Sourcery May 8 '14 at 11:01

1 Answer 1

If you have your Arduino on tcp/ip your answer can be found in O'Reilly's Arduino Cookbook: Recipe 15-4. As Sourcery mentioned you need to make a new page on the PHP side that spits out just the data you are looking for and then use a web client on the Arduino side to download that data.

If you don't have your Arduino on the network then you have to stick the data over the serial line. One way would be to bake it into your source and recompile/reburn the Arduino. A smaller hammer would be to build-in something to your Arduino sketch that can deal with getting the data via USB serial and have a shim on the computer side to pull the data from PHP and send it to the Arduino. Serial communication with Arduino and Processing shows something similar where he is running Processing on the computer which is passing data to the Arduino.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.