我有一个安装了Node Red的Google Cloud Debian VM。我在httpin节点上创建了一个称为json的流。当我http://xx.xx.xx.xx:1880/json从chrome,节点红在VM响应一个测试json字符串我在那里键入。
我的意图是从ESP32发布json,并从节点红得到响应。但是我甚至无法从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");
}
无论我做什么,我总是得到"->Fail")。
援助的欢迎。
草图被划分为几个具有特定功能的选项卡。这是我用来连接另一个在线服务的相同草图,它曾经工作过。现在我正在使用ordered开发自己的服务。当这个功能被调用时,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
实际上是一个以太网/wifi客户端,因为你是在你提供的函数中手工构建HTTP请求。
您需要将http://
从URLWebServer
的开头去掉,它应该是主机名/ip地址而不是URL,因为您将端口作为单独的参数传递。
您需要在HTTP主机头中包含http://
(以及端口号)
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host