【问题标题】:ESP32/ESP8266 connect to localhost server using WiFiESP32/ESP8266 使用 WiFi 连接到本地主机服务器
【发布时间】:2017-12-05 18:04:14
【问题描述】:

我制作了一个简单的 Node.js 本地服务器来接收来自 ESP32 的 POST 请求并将其放入数据库中。服务器工作正常,因为我使用邮递员对其进行了测试。服务器正在监听端口 127.0.0.1:3000。我的问题是 client.connect(host, port) 总是返回 false。我无法连接到客户端以发出 POST 请求。

#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() {
}

【问题讨论】:

  • 您是否验证了服务器可以在本地主机之外访问?如果端口打开,也许可以从另一台 PC 上检查。某些防火墙可能会阻止请求。尝试将服务器绑定到0.0.0.0

标签: arduino esp8266 arduino-ide arduino-esp8266 esp32


【解决方案1】:

解决方案是让服务器监听 0.0.0.0,其中包括服务器机器上的所有 IPv4 地址,而不是环回 IP 地址 127.0.0.1

【讨论】:

    猜你喜欢
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 2011-06-05
    相关资源
    最近更新 更多