Go Down

Topic: Sending data from ESP32 to a Web service (Read 298 times) previous topic - next topic

Ricjard


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:

Code: [Select]
#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.

PaulS

Quote
const char* host = "localhost";
You clearly do not understand what the name localhost means. Your Arduino is NOT hosting the service you are trying to "POTS" to, so that name is wrong.

Code: [Select]
      client.println("POST :/WebService.asmx/HelloWorld HTTP/1.1");
Have you EVER seen a path name that contains a colon? You need to perform a colonectomy.
The art of getting good answers lies in asking good questions.

Ricjard

I have no knowledge about HTTP and for this reason I have these kind of errors.

Are you saying that localhost corresponds to the ip address of my pc?

This and the information provided by the web service to make HTTP POST:
Code: [Select]

POST /WebService.asmx/HelloWorld HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

recebida=string


Can you help me configure what to send in HTTP POST request with this information?

PaulS

Quote
Are you saying that localhost corresponds to the ip address of my pc?
No, that is not what I said, and is not true.

When you have a client that wants to make a request to a server RUNNING ON THE SAME MACHINE, you can use the name "localhost" to bypass the need to know the name or IP address of the machine that the server is running on.

Your client and server are NOT running on the same machine, so you can NOT use the name "localhost".
The art of getting good answers lies in asking good questions.

Go Up