0
votes

I want the data of a webpage to be printed on java console using java program and this data is sent on webpage using arduino ethernet shield. I have mentioned the IP address and port no. in java program but my data is not displayed on java console.

Java Code :

package IP_Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class IP1 {

public static void main(String []args) {
    try {
        // "192.168.1.177" is a IP of Server
        Socket s = new Socket("192.168.43.177", 80); 
        InetAddress add = s.getInetAddress();
        System.out.println("Connected to " + add);

        PrintWriter pw = new PrintWriter(s.getOutputStream());
        // "?butonon" is a content which you send to server
        pw.println("GET /?buttonon HTTP/1.1");
        pw.println("");
        pw.println("");
        pw.flush();
        System.out.println("Request sent");

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println(br.readLine());
    }
    catch (IOException e) {
        e.printStackTrace();
    }

}

}

Arduino Code:

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

SoftwareSerial rfid(0,1);             // rfid pins connected to arduino 
 (rx_pin, tx_pin)
 String rcard;
 char c;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  };
 IPAddress ip(192, 168, 43, 177);

 // Initialize the Ethernet server library
 // with the IP address and port you want to use
 // (port 80 is default for HTTP):
 EthernetServer server(80);

   void setup() {
     // Open serial communications and wait for port to open:
     rfid.begin(9600); 
     Serial.begin(9600);
       while (!Serial) {
       ; // wait for serial port to connect. Needed for native USB port 
        only
      }


     // start the Ethernet connection and the server:
     Ethernet.begin(mac, ip);
     server.begin();
     Serial.print("server is at ");
     Serial.println(Ethernet.localIP());
    }


    void loop() {

       // listen for incoming clients
       EthernetClient client = server.available();
       if (client) {
        Serial.println("new client");
        // an http request ends with a blank line
       boolean currentLineIsBlank = true;
       while (client.connected()) {
          if (client.available()) {
            c = client.read();

    Serial.write(c);

    // if you've gotten to the end of the line (received a newline
    // character) and the line is blank, the http request has ended,
    // so you can send a reply
    if (c == '\n' && currentLineIsBlank) {
      // send a standard http response header

      client.println(" HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("Connection: close");  // the connection will be closed after completion of the response
     // client.println("Refresh: 4");  // refresh the page automatically every 5 sec
      client.println();
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      // output the value of each analog input pin
      rcard="";
  delay(5);
  while ( Serial.available()>0 )
   {
    delay(20);
    c = Serial.read();
     rcard+=c;
     rcard =rcard.substring(0,11);     
  }
      if(rcard.length()==11)
      {
                   client.print("UID: ");
               client.print(rcard);
       }  

      client.println("</html>");
      break;
    }
    if (c == '\n') {
      // you're starting a new line
      currentLineIsBlank = true;
    } else if (c != '\r') {
      // you've gotten a character on the current line
      currentLineIsBlank = false;
    }
  }
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
    }
   }

Output of Webpage: UID: $0005497354

Output of java program: Connected to /192.168.43.177 Request sent HTTP/1.1 200 OK

2

2 Answers

0
votes

You put an extra space at the begin of the http response on Arduino. That breaks the http protocol.

I'd also join @piet.t in that if you are using http, use a library that handles it for you. Or just drop using http, go direct. This is quite hackish and error-prone.

0
votes

Well, you are reading just one line and since you decided not to use a http-client but rather do all http-business on the socket yourself that first line is the HTTP-statuscode. Some lines down the response will be the data you are looking for.

So wrap your System.out.println(br.readLine()); in a loop that continues reading while there is still input left.