E
E
Egor Lepikhin2019-12-09 12:00:32
Arduino
Egor Lepikhin, 2019-12-09 12:00:32

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

arduino code

#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

3 answer(s)
K
Konstantin Zaitsev, 2019-12-09
@LepikhinEgor

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.

A
Alexander, 2019-12-09
@NeiroNx

Instead of localhost, specify (on the server) your ip address on the local network or 0.0.0.0 to run on all addresses.

K
kalapanga, 2019-12-09
@kalapanga

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

Here is where you need to clarify. Requests are entered in the browser on which computer? If on the same place as the server, then it's not interesting. First, get the server to respond to browser requests from another computer on your network.
Next, you can move on to Arduino. It will essentially act as this "other computer". If something does not work, then you will deal with Arduino and its sketch.
And by the way, how did you connect the Arduino to the network?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question