Я хочу, чтобы данные веб-страницы печатались на 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 OK