I am trying to send data via wifi from esp32 to a web service created by me that receives a string of data and saves it in a .txt file.
My webservice works correctly and when I write a string manually it saves it in the file as expected.
In the ESP32 code I am trying to access the webservice following the steps given in the HTTP POTS provided on the web page of the web service.
My code is:
#include <WiFi.h>
#include <ArduinoHttpClient.h>
const char* nome_rede = "iPhone de *******";
const char* password = "********";
const char* host = "localhost";
const int httpPort = 80;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
conectar_rede();
}
void loop() {
http();
}
void http() {
String PostData = "recebida=1234567890"; //string para teste
WiFiClient client = server.available();
if (!client.connect(host, httpPort)) { //conectar a um determinado host
Serial.println("conecao ao host falhada");
loop();
}
if (client) {
if (client.connected()) {
Serial.println("Connected to client");
client.println("POST :/WebService.asmx/HelloWorld HTTP/1.1");
client.println("Host: localhost");
client.println("Content-Type: application/x-www-form-urlencoded");
//client.println("User-Agent: Arduino/1.0");
//client.println("Connection: close");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
delay(300);
client.stop();
Serial.println("Client stop");
}
else Serial.println("Cliente not conected");
// close the connection:
}
else Serial.println("Cliente not found");
delay(2000);
}
void conectar_rede() {
WiFi.begin(nome_rede, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print("A conectar á rede");
Serial.println(nome_rede);
}
Serial.println("Conectada");
Serial.print("IP address do ESP32: ");
Serial.println(WiFi.localIP());
server.begin();
}
With this code I can connect to wifi and to the host but my .txt file does not get the value 1234567890 of the postData variable.
Someone can tell me how I can do this better to try to get it working?
Best regards and Sorry about my english.