Go Down

Topic: Attempt to Get Data from webclient to serial monitor using EthernetShield v1 (Read 95 times) previous topic - next topic

peeknot

I have trouble in getting the data from the webclient using GET api , so the process would be if the UID is exist in the database the webpage add.php will reply either in a get method or post but i dont know how to fetch that data from webpage to the serial monitor. Below is the code I get from this thread zoomkats reply https://forum.arduino.cc/index.php?topic=160616.0

Or is there a way that I can print the echo message from the web page to serial monitor in arduino? If it is possible can someone lend me some guide on how to achieved it?

Code: [Select]

    if (client.connect(server,80)){ // attempt to get a value from the add.php
      // using GET , this is confirmation wheither your UID exist in the database
      Serial.println("Attempting to Connect for the 2nd time");
    // get the reply value in php
      client.print("GET /add.php");
      client.print(" HTTP/1.1");
      client.println();
      client.println("Host: localhost");
      client.println("Connection: close");
      client.println();
      Serial.println("ARDUINO : HTTP REQUEST SENT");
      delay(1000);
      if(client.available()){
         while(client.connected()){ // zoomkats example code
          char c = client.read();
          if (readString.length() < 100) {

          //store characters to string
          readString += c;
          }
          //Serial.print(c);
          if (c == '\n') {
            Serial.println(readString);
            client.stop();
          if(readString.indexOf("on") >0)//checks for on
          {
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            Serial.println("Led Off");
          }
          readString="";
          }
        }
       
      }
      else{
        Serial.println("2ND ATTEMPT FAILED");
      }
     


Below is my php code , Note: this is just test code , maybe theres some kind of errors exists in here I badly need help
Code: [Select]

<?php
$servername 
"localhost";
$username "root";
$password "";
$dbname "micro";

// Create connection
$conn = new mysqli($servername$username$password$dbname);

// Check connection
if ($conn->connect_error) {
    die(
"Connection failed: " $conn->connect_error);
}

$temp1=$_GET['value'];
echo "$temp1";
$que "INSERT INTO students(id_number, student_name) 
VALUES ('
$temp1' , '$temp1')";
   

   
if($conn->query($que) == TRUE){
echo "nice!";
}
else{
echo $link->error();
}

$test "on";
header("location:add.php?reply=".$test);



?>


PaulS

Code: [Select]
      client.println("Host: localhost");
The server is NOT localhost. That name is used ONLY when the client and the server are the SAME machine. The Arduino is NOT the server.

Quote
Or is there a way that I can print the echo message from the web page to serial monitor in arduino?
Of course there is. If you get any reply from the server, client.read() will read it one character at a time. Obviously, you can Serial.print() that character.
The art of getting good answers lies in asking good questions.


Go Up