我希望使用Java程序在Java控制台上打印网页的数据,并使用arduino以太网屏蔽在网页上发送此数据。我已经提到了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 OK
答案 0 :(得分:0)
好吧,您只读一行,因为您决定不使用http客户端,而是自己在套接字上执行所有http业务,所以第一行是HTTP状态代码。响应下方的某些行将是您要查找的数据。
因此,将System.out.println(br.readLine());
包裹在一个循环中,在仍然有剩余输入时继续读取。
答案 1 :(得分:0)
您在Arduino上的http响应的开头放置了额外的空间。这破坏了http协议。
如果您使用的是http,我也会加入@ piet.t,请使用为您处理它的库。或者直接使用http删除,直接进入。这是非常骇人的错误,容易出错。