ESP32 - 无法连接到 Google Cloud VM 运行 Node-Red http in node with post/get request

ESP32 - unable to connect to Google Coud VM running Node-Red httpin node with post/get request

我有一个安装了 Node Red 的 Google Cloud Debian VM。 我在 httpin 节点上创建了一个称为 json 的流。当我从 chrome http://xx.xx.xx.xx:1880/json 时,VM 的红色节点响应我在那里输入的测试 json 字符串。

我的意图是 post 来自 ESP32 的 json 并得到 Node Red 的响应。 但我什至无法从 ESP32 连接到上述 URL。

在草图的顶部我有:

String URLWebServer          = "http://222.222.111.128";
int    PortWebServer         = 1880;
String uploadScript          = "/json"; 

稍后在草图中我有如下网络连接:

void postWebServer(){
  Serial.print("Connect to " + URLWebServer);
  if (webClient.connect(URLWebServer.c_str(), PortWebServer)){
    Serial.println(" -> OK");
...
...
...
  }else{
    Serial.println(" -> Fail");
  } 

无论我做什么,我总是得到“-> 失败”)。

欢迎协助。


草图分为几个具有特定功能的选项卡。 这是我用来连接另一个在线服务的草图,它曾经工作过。现在我正在使用 nodered 开发自己的服务。 调用此函数时 WiFi 已连接。 做web的函数的完整代码post:

void postWebServer(){
  Serial.print("Connect to " + URLWebServer); 
  if (webClient.connect(URLWebServer.c_str(), PortWebServer)){
    Serial.println(" -> OK");
    String startBoundary = "--";
    String postTail = startBoundary + boundary + "--" + newLine;

    String strBody;  // = newLine;
    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"filename\"" + newLine + newLine;
    strBody += ftpFileName + newLine;
    
    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"devid\"" + newLine + newLine;
    strBody += devid + newLine;

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"latitude\"" + newLine + newLine;
    strBody += "123" + newLine;    

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"longitude\"" + newLine + newLine;
    strBody += "345" + newLine;    

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"camera_id\"" + newLine + newLine;
    strBody += camera_id + newLine;

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"maxSpeed\"" + newLine + newLine;
    strBody += "40" + newLine;

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"radarSpeed\"" + newLine + newLine;
    strBody += "65" + newLine;    

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"plate\"" + newLine + newLine;
    strBody += plate + newLine;

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"type\"" + newLine + newLine;
    strBody += type + newLine;

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"make\"" + newLine + newLine;
    strBody += make + newLine;

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"model\"" + newLine + newLine;
    strBody += model + newLine;        

    strBody += startBoundary + boundary + newLine;
    strBody += "Content-Disposition: form-data; name=\"color\"" + newLine + newLine;
    strBody += color + newLine;
    strBody += postTail;  
    
    Serial.println("Connection to " + URLWebServer + " - OK"); 
    String header = "POST " + uploadScript + " HTTP/1.1" + newLine;    
    header += "Host: " + URLWebServer + newLine;      
    header += "Content-Type: multipart/form-data; boundary=" + boundary + newLine;    
    header += "Content-Length: " + String(strBody.length()) + newLine;
    header += newLine;

    webClient.print(header);
    pDBGln(header);                
    webClient.print(strBody);  
    pDBGln(strBody + newLine);
        
    Serial.println("Data sent to " + URLWebServer + "...");

  }else{
    Serial.println(" -> Fail");
  }  
}

最好的猜测,因为您没有真正为 ESP32 代码提供足够的上下文。

但看起来 webClient 实际上是一个 ethernet/wifi 客户端,因为您是在您提供的函数中手动构建 HTTP 请求。

您需要将 http://URLWebServer 的开头去掉,它应该是 hostname/ip 地址而不是 URL,因为您将端口传递为一个单独的参数。

您需要在 HTTP 主机 header 中包含 http://(连同端口号)

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host