Search code examples
arduinoesp8266arduino-idearduino-esp8266esp32

ESP32/ESP8266 connect to localhost server using WiFi


I have made a simple Node.js local server to receive POST requests from ESP32 and put it in a database. The server is working fine as I tested it using postman. The server is listening to port 127.0.0.1:3000. My problem is that client.connect(host, port) always returns false. I cannot connect to the client in order to make POST requests.

#include "Arduino.h"
#include "Arduino.h"
#include "WiFi.h"

WiFiClient client;
const IPAddress server(192,168,1,10);
const int httpPort = 3000;
const char* ssid = "******";
const char* password =  "********";

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println("Booted");
  Serial.println("Connecting to Wi-Fi");
  WiFi.begin (ssid, password);
  WiFi.mode(WIFI_STA);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
    yield();
  }
  Serial.println("WiFi connected");
  if (client.connect(server,httpPort )) {
    Serial.println("Client Connected");
  } else {
    Serial.println("No Connection");
  }

void loop() {
}

Solution

  • The solution was to make the server listen to 0.0.0.0, which includes all IPv4 addresses on the server machine, instead of the loopback IP address 127.0.0.1