java - Java程序不显示使用arduino以太网屏蔽在网页上发送的数据
问题描述
我希望使用 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程序的输出:Connected to /192.168.43.177 Request sent HTTP/1.1 200 OK
解决方案
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.
推荐阅读
- c# - 我有这个问题不是所有的代码路径都返回一个值
- if-statement - 多个 IF if 语句闪亮
- python - 你如何测试一个 python 类型的协议是另一个协议的子类?
- sql-server - ASP.NET Core Web API 服务无法连接到同一网络上的 Docker SQL Server
- javascript - 将项目添加到 JavaScript 中的对象数组中
- c# - 在 C# 中播放音频时出现白噪声
- java - 如何配置 GraphQL SPQR 以使用 Gson 而不是 Jackson
- xamarin - 框架内的 Xamarin 表单标签未显示
- r - 如何反转该图的 x 轴?
- phpmailer - 此脚本当前仅支持 Google、Microsoft 和 Yahoo OAuth2 提供程序。- php邮件程序