Answer the question
In order to leave comments, you need to log in
How to connect to localhost from Arduino?
I am writing a server in Java, deploying it according to the classics on localhost:8080. The server processes requests correctly if they are entered in the browser
. I need to send a request from the Arduino Nano to this server, from the same local network. How can i do this?
I’m not strong in networks, but as I understand it, from another computer on the local network you can’t connect to localhost with the server image
#include <SPI.h>
#include <Ethernet2.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192, 168, 0, 40); // numeric IP for Google (no DNS)
//char server[] = "www.google.com"; // name address for Google (using DNS)
IPAddress ip(192, 168, 0, 229);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.init(4);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET http://localhost:8080/intler_iot_war_exploded/login/connect-device?device_id=1");
client.println("Host: ");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
while (true);
}
}
Answer the question
In order to leave comments, you need to log in
localhost is a conditional reference to itself, so everyone has their own =)
You need to contact the IP address that the server has (static or from a DHCP router, it doesn’t matter). Those. just write the IP address instead of localhost.
Instead of localhost, specify (on the server) your ip address on the local network or 0.0.0.0 to run on all addresses.
I am writing a server in Java, deploying it according to the classics on localhost:8080. The server correctly processes requests if they are entered in the browser
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question