我有一個安裝了 Node Red 的 Google Cloud Debian VM。我在 httpin 節點上創建了一個稱為 json 的流。當我從 chrome 訪問http://xx.xx.xx.xx:1880/json時,VM 上的 Node Red 回應我在那里輸入的測驗 json 字串。
我的目的是從 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 已連接。完成網路帖子的功能的完整代碼:
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");
}
}
uj5u.com熱心網友回復:
最好的猜測,因為您還沒有真正為 ESP32 代碼提供足夠的背景關系。
但它看起來webClient實際上是一個以太網/wifi 客戶端,因為您是在您提供的功能中手動構建 HTTP 請求。
您需要http://取消URLWebServer它應該是主機名/IP 地址而不是 URL的開頭,因為您將埠作為單獨的引數傳遞。
您將需要http://在 HTTP 主機標頭中包含(以及埠號)
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324611.html
