I have a Google Cloud Debian VM with Node Red installed. I created a flow that is refered as json on the httpin node. When I http://xx.xx.xx.xx:1880/json from chrome, Node Red at the VM responds with a test json string I typed there.
My intention is to post a json from ESP32 and get a response from Node Red. But I am just unable to even connect with the above URL from ESP32.
At the top of my sketch I have:
String URLWebServer = "http://222.222.111.128";
int PortWebServer = 1880;
String uploadScript = "/json";
Later in the sketch I have the web connection as below:
void postWebServer(){
Serial.print("Connect to " + URLWebServer);
if (webClient.connect(URLWebServer.c_str(), PortWebServer)){
Serial.println(" -> OK");
...
...
...
}else{
Serial.println(" -> Fail");
}
And no matter what I do I always get "-> Fail").
Assistance welcome.
The sketch is divided in several tabs with specific functions. This is the same sketch I use to connect with another online service and it used to work. Now I am developing my own service using nodered. When this function is called WiFi is already connected. Complete code of the function that does the 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");
}
}
Best guess since you haven't really provided enough context to your ESP32 code.
But it looks like webClient
is actually an ethernet/wifi client since you are building the HTTP request by hand in the function you have provided.
You need to take the http://
off the start of the URLWebServer
it should be a hostname/ip address not a URL, since you are passing the port as a separate argument.
You will need to include the http://
in the HTTP Host header (along with the port number)
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host