How to get the data from php to arduino uno's serial monitor?

My php file will echo some string in a webpage. The same string i have to print in arduino uno's serial monitor using WiFi shield. Can someone clear me about this ?

This is my part of the php flie...

$resu=mysql_query("SELECT * FROM rfid where RFIDTagID='$RFIDT'");
if($row=mysql_fetch_array($resu))
{
	$w="Welcome";
	$d="Have a Nice Day...!!!";
	$na=$row['EName'];
//echo $row['EName'];
$s=$w."\t".$na."\n".$d;
echo nl2br($s);

This is my part of arduino file...

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
  
    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();
  
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /lowf/Atten/display.php HTTP/1.1");
    client.println("Host: 192.168.0.100");
    client.println("Connection: close");
    client.println();
  }
}

void loop() {
  // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

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

    // do nothing forevermore:
    while(true);
  }
}

Can someone clear me about this ?

You connect to the server. You make a request. The server generates a response that you don't read. Why not?

The server is also responding on the webpage and i have defined the server location and file path too…

The same string i have to get in serial monitor which i have already displayed using php file… Below is the respective code for tht…

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "XXXXXX"; //  your network SSID (name) 
char pass[] = "YYYYYY";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(xxx,xxx,xxx,xxx);  // numeric IP for Google (no DNS)

WiFiClient client;

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    // don't continue:
    while(true);
  } 
  
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
  
    // wait 10 seconds for connection:
    delay(10000);
  } 
  Serial.println("Connected to wifi");
  printWifiStatus();
  
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.println("GET /lowf/Atten/display.php HTTP/1.1");
    client.println("Host: xxx.xxx.xxx.xxx");
    client.println("Connection: close");
    client.println();
  }
}

void loop() {
  // if there are incoming bytes available 
  // from the server, read them and print them:
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

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

    // do nothing forevermore:
    while(true);
  }
}


void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

The first image is, displaying the string on webpage and the second image is what i get on serial monitor using the above code.

11.JPG

Might I suggest that you visit http://snippets-r-us.com, to get help with your snippets? Here, we need to see ALL of your code.

I would guess, though, that there needs to be more to your GET request. Something like ?RFID=xxxxx on the end.

Where is the PHP script supposed to get a value for $RFIDT?

The $RFID is supposed to get a value from the table. I am attaching that snapshot also. This is my server side code.

<?php

$db_username = "root";
$db_password = "";
$db_name="xxxxx";
$db_hostname ="localhost";
$db_handle =mysql_connect($db_hostname, $db_username, $db_password)
or die("unable to connect to MySql");
$selected = mysql_select_db($db_name,$db_handle)
or die("Could not select examples");
?>
<?php
include "db.php";
$x=$_POST['RFIDTagID'];
$y=substr($x,0,11);
echo $x;
$query = mysql_query("SELECT *FROM rfid WHERE RFIDTagID = '$y'");
if($row=mysql_fetch_array($query))
{
	$w="Welcome";
	$na=$row['EName'];
	$s=$w."\t".$na;
	echo $s;
}
?>

Then this is my arduino sketch. so whenever i keep the tag in reader it has to send the string via RFIDTagID to compare wit respective table. As soon as the tag id matches the respective row field has to display in arduino’s serial monitor.

#include <SPI.h>
#include <WiFi.h>
#include <Client.h>
#include<SoftwareSerial.h>
SoftwareSerial RFID(9,10);
int i;
int rfid;
char ssid[] = "Reliance Wi-Pod-2E7";     //  your network SSID (name) 
char pass[] = "A3D2D2E7"; 
int status = WL_IDLE_STATUS;
char newtag[14];
String data;
WiFiClient client;
char location[]="/lowf/Atten/display.php";
char server[]="192.168.0.101";
String serverip="192.168.0.101";
String link="";
char buf[150]="";
char buffer[150]="";
void setup()
{
    Serial.begin(9600);
    RFID.begin(9600);
     while ( status != WL_CONNECTED) 
  { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid,pass);

    // wait 10 seconds for connection:
    delay(5000);
  } 
  Serial.println("Connected to wifi");
 }


void loop()
{
 POST();
 get();
 //delay(10000);
} 


void POST()
{
        if (client.connect("192.168.0.101", 80))
        {
            Serial.print("Connected to Itsme.com:");
            Serial.println();
          while(1)
            {
                if (RFID.available() > 0)
                {
                    Serial.println("Reading RFID Tags");
                    delay(100);
                    for (i=0; i <14; i++)
                    {
                        rfid = RFID.read();
                        newtag[i]=rfid;
                    }
            
                    RFID.flush();
                    Serial.print("RFID Tag no:");
                    Serial.print(newtag);
                    data="RFIDTagID=" + String(newtag);
                    client.print("POST /lowf/Atten/index.php HTTP/1.1\n");
                    client.print("Host: 192.168.0.101\n");
                    client.print("Connection: close\n");
                    client.print("Content-Type: application/x-www-form-urlencoded\n");
                    client.print("Content-Length: ");
                    client.print(data.length());
                    client.print("\n\n");
                    client.print(data);
                    Serial.print(data);
                     
                }
            }
        }
       else
        {
            Serial.println("Cannot connect to Server");
        }
   }

void get()
{
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
  Serial.println("connected to server");
  Serial.println("Host: http://serverip");
  // Make a HTTP request:
  link=String("GET http://"+serverip+location);
  link.toCharArray(buf,150);
  sprintf(buffer,"%s",buf,"HTTP/1.1");
  client.println(buffer);
  client.println();
  delay(20000);
  }

  
  // if there are incoming bytes available 
  // from the server, read them and print them:
  Serial.println("Server echo:");
  while (client.available()) {
  char c = client.read();
  Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();
    client.flush();
     }
else
{
  Serial.println("Connection Error:");
}
Serial.println();
}

14.JPG

                if (RFID.available() > 0)
                {
                    Serial.println("Reading RFID Tags");
                    delay(100);
                    for (i=0; i <14; i++)
                    {

Epic f**king fail!

If there is one or more bytes available to read, it is NOT okay to read all 14 of them!

Reading the tags only after connecting to the server makes NO sensa.

Reading the tag should be what triggers connecting to the server, not the other way around. You don’t answer the phone and then wait for someone to call, do you?

I have corrected the sketch as per your suggestion . What you said is absolutely correct, I have failed to think in that perspective.

Now get() is also working, i.e., server is responding back to arduino but the only thing is, webpage content is not displaying in serial monitor.

#include <SPI.h>
#include <WiFi.h>
#include <Client.h>
#include<SoftwareSerial.h>
SoftwareSerial RFID(9,10);
int i;
int rfid;
char ssid[] = "XXXXX";     //  your network SSID (name) 
char pass[] = "__________"; 
int status = WL_IDLE_STATUS;
char newtag[14];
String data;
WiFiClient client;
char location[]="/lowf/Atten/display.php";
char server[]="XXXXX";
String serverip="XXXXXX";
String link="";
char buf[150]="";
char buffer[150]="";
void setup()
{
    Serial.begin(9600);
    RFID.begin(9600);
     while ( status != WL_CONNECTED) 
  { 
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid,pass);

    // wait 10 seconds for connection:
    delay(5000);
  } 
  Serial.println("Connected to wifi");
 }


void loop()
{
 POST();
 delay(10000);
 Serial.println("HI");
 get();
 delay(5000);
} 


void POST()
{
      if (RFID.available() > 0)
                {
                    Serial.println("Reading RFID Tags");
                    delay(100);
                    for (i=0; i <14; i++)
                    {
                        rfid = RFID.read();
                        newtag[i]=rfid;
                    }
                    RFID.flush();
                    Serial.print("RFID Tag no:");
                    Serial.print(newtag);
                    data="RFIDTagID=" + String(newtag);
                  
                 if (client.connect("XXXXX", 80))
                   {
                    Serial.print("Connected to Itsme.com:");
                    Serial.println();
                    client.print("POST /lowf/Atten/index.php HTTP/1.1\n");
                    client.print("Host: XXXXXXX\n");
                    client.print("Connection: close\n");
                    client.print("Content-Type: application/x-www-form-urlencoded\n");
                    client.print("Content-Length: ");
                    client.print(data.length());
                    client.print("\n\n");
                    client.print(data);
                    Serial.print(data);
              }
        }  
       else
        {
            Serial.println("Cannot connect to Server");
        }
   }

void get()
{
  Serial.println("\nStarting connection to server...");
  delay(15000);
  // if you get a connection, report back via serial:
  //if (client.connect(server, 80)) //{
  Serial.println("connected to server");
  Serial.println("Host: http://serverip");
  // Make a HTTP request:
  link=String("GET http://"+serverip+location);
  link.toCharArray(buf,150);
  sprintf(buffer,"%s",buf,"HTTP/1.1");
  client.println(buffer);
  client.println("");
  //delay(20000);
  //}

  
  // if there are incoming bytes available 
  // from the server, read them and print them:
  Serial.println("Server echo:");
  while (client.available()) {
  char c = client.read();
  Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();
    client.flush();
     }
else
{
  Serial.println("Connection Error:");
}
Serial.println();
}

      if (RFID.available() > 0)
                {
                    Serial.println("Reading RFID Tags");
                    delay(100);
                    for (i=0; i <14; i++)
                    {
                        rfid = RFID.read();

The delay() is an f**king bandaid. Do it PROPERLY. If you think, incorrectly, that there will always be 14 bytes in a RFID packet, don’t do anything if there are less than 14 bytes.

  link=String("GET http://"+serverip+location);

The protocol (“http://”) and server are NOT part of the GET request. The Arduino only does one protocol, and you are are already connected to the server.

Pissing away resources like that is completely unnecessary. sprintf() and the buf array are more than sufficient.

  Serial.println("Host: http://serverip");

Printing the Host: statement to the serial monitor, but not the client, is silly. The host’s name is NOT http://anything.

but the only thing is, webpage content is not displaying in serial monitor.

I have no idea what web page you are referring to, or why you thing the serial monitor should display it.

This is not sufficient:

  while (client.available()) {
  char c = client.read();
  Serial.write(c);
  }

That should be in a while(client.connected()) statement’s body.

Thank you PaulS. I will change the sketch as per your suggestion. is it possible to get a value from localhost? i. e., now for testing purpose I am using localhost so is it possible to get a value from php to arduino.

I have to display it in LCD later no. So first i am trying it to display in serial monitor.

Then i have referred this thread http://forum.arduino.cc/index.php?topic=299567.0 to get a string from php to arduino.

is it possible to get a value from localhost?

On the Arduino? No.

On the other hand, if you understood what localhost means, you would not be asking that question.

It IS possible to get a value from a server.

I have misunderstood the concept then. Can you tell me why it is not possible ? Is it because of cookies ?

localhost is a special name. If means, when a client uses is, to talk to the script on the same machine that the client is running on.

It is NOT the name of a server that is NOT running on the same machine as the client.

When the Arduino makes a GET request, it is NOT asking itself for data. So, you REALLY need to quit using the term localhost, since it is NOT the name of the machine that the Arduino is talking to AND you don't seem to understand what it means.

Have you changed your code to put that snippet I showed in a while loop?

Thank you Paul for explaining this concept. Now i understood the reason behind that.

Here is the while snippet,

 while (client.connected()) 
{
  char c = client.read();
  Serial.write(c);
  }

I have used the get function alone in arduino side to get a data from a database using localhost. Here is the sketch for that.

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "---------------"; //  your network SSID (name) 
char pass[] = "--------------";    // your network password (use for WPA, or use as key for WEP)


int status = WL_IDLE_STATUS;

char location[]="/lowf/Atten/1.php";
char server[]="----------------";
String serverip="-------------";
WiFiClient client;
String link="";
char buf[150]="";
char buffer[150]="";

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600); 
  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) { 
  Serial.print("Attempting to connect to SSID: ");
  Serial.println(ssid);
  // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
  status = WiFi.begin(ssid, pass);
  // wait 10 seconds for connection:
  delay(3000);
  } 
  Serial.println("Connected to wifi");
  }

void loop()
{
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
 
  if (client.connect(server, 80)) 
  {
  Serial.println("connected to server");
  // Make a HTTP request:
  link=String("GET http://"+serverip+location);
  link.toCharArray(buf,150);
  sprintf(buffer,"%s",buf,"HTTP/1.1");
  
  client.println(buffer);
  client.println();
  delay(6000);
  }

  
  // if there are incoming bytes available 
  // from the server, read them and print them:
  Serial.println("Server echo:");
  while (client.available()) {
  char c = client.read();
  Serial.write(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting from server.");
    client.stop();
    client.flush();
     }
else
{
  Serial.println("Connection Error:");
}
Serial.println();
}

This is the scratch on the php side.

<?php
include "db.php";

$y='0006142741';

//$x=$_POST['RFIDTagID'];
//$y=substr($x,0,11);
$query = mysql_query("SELECT * FROM rfid WHERE RFIDTagID = '$y'");
if($row=mysql_fetch_array($query))
{
 $w="Welcome";
 $na=$row['EName'];
 $s=$w."\t".$na;
 echo $s;
}
?>

My Arduino’s serial monitor can display the respective output. I have attached the snapshot of that too.

Now my question, using localhost when get function alone can works properly, Why NOT? when i use both post and get together in arduino using localhost.

1.JPG

Both pictures contain the text "Welcome SNIFF". So, why do you assume that one sketch is working and one is not?

1.jpg shows the echo statement on php side and 2.jpg shows the data which arduino has fetched through php script.

The arduino sketch which i have attached here is, only the get fucntion i.e., what ever the data is echoing in "/lowf/Atten/1.php"; its displaying in serial monitor.So for all instance the same data will display.

Not the thing i want to do is, at one instance when i post a data the same sketch has to run on get function and it has to return the string.

Not the thing i want to do is, at one instance when i post a data the same sketch has to run on get function and it has to return the string.

I’m going to assume that English is not your native language, and that this means that you want to make a GET request when an RFID tag is presented, and get the server response (“Welcome SNIFF” or whoever) as a string.

      if (RFID.available() > 0)
                {
                    Serial.println("Reading RFID Tags");
                    delay(100);
                    for (i=0; i <14; i++)
                    {

needs to be:

      if (RFID.available() >= 14)
                {
                    Serial.println("Reading RFID Tags");
                    for (i=0; i <14; i++)
                    {

Then, this:

 while (client.available())
{
  char c = client.read();
  Serial.write(c);
  }

needs to be:

   Serial.print("Server response:[");
   while(client.connected())
   {
      while (client.available())
      {
         char c = client.read();
         Serial.write(c);
      }
   }
   Serial.println("]");

Then, you need to show us the results, AS TEXT, NOT PICTURES!

Thank you that's my typing mistake. (that's now )

Yes what you said is correct. This is what I am expecting. Thank you PAUL.

I am unable to read the server response on serial monitor after correcting my sketch as you have suggested.

I have configured one of my machine's localhost as a server say for ex:-192.168.1.x where i have saved all my php files.

And then on the other maching's localhost i have connected my arduino board with wifi shield as a client say for ex:-192.168.1.y under same network.

This is the response what i am getting

Attempting to connect to SSID: __________
Connected to wifi
Connected to Itsme.com:
Reading RFID Tags
RFID Tag no:0006110954
00RFIDTagID=0006110954
00HI

Starting connection to server...
connected to server
Server response:[
]

is it possible to do like this ? or else am i in wrong way ?

Thanks for your effort in advance.

I am unable to read the server response on serial monitor

You can't read it there if you don't write it there. Please make it clear WHAT your problem is.

is it possible to do like this ? or else am i in wrong way ?

Yes, it is, and, yes, clearly you are. Post your damned code.