【问题标题】:Java program not displaying data which is sent on webpage using arduino ethernet shieldJava程序不显示使用arduino ethernet shield在网页上发送的数据
【发布时间】:2019-02-14 10:17:36
【问题描述】:

我希望使用 java 程序在 java 控制台上打印网页数据,并使用 arduino ethernet shield 在网页上发送该数据。我已经提到了IP地址和端口号。在java程序中,但我的数据没有显示在java控制台上。

Java 代码:

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 代码:

#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");
    }
   }

网页输出: UID:$0005497354

java程序的输出: 连接到 /192.168.43.177 已发送请求 HTTP/1.1 200 正常

【问题讨论】:

    标签: java arduino-uno ethernet


    【解决方案1】:

    您在 Arduino 上的 http 响应的开头放置了一个额外的空格。这破坏了 http 协议。

    如果您使用 http,我也会加入 @piet.t,请使用为您处理它的库。或者只是使用http,直接去。这是相当骇人听闻且容易出错的。

    【讨论】:

      【解决方案2】:

      好吧,您只阅读了一行,因为您决定不使用 http-client 而是自己在套接字上执行所有 http-business,所以第一行是 HTTP-statuscode。响应中的某些行将是您要查找的数据。

      因此,将您的 System.out.println(br.readLine()); 包装在一个循环中,当仍有输入时继续读取。

      【讨论】:

      • java 的输出:null
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多