1

I've got a working local website that takes in HTML form data.

The fields are:

Temperature Humidity

The server successfully receives the data and spits out a graph updated with the new entries.

Using a browser tool, I was able to capture the actual POST request as follows:

http://127.0.0.1:5000/add_data

Temperature=25.4&Humidity=52.2

Content-Length:30

Now, I want to migrate from using the human interface browser with manual entries to an ESP01 device using AT commands.

According to the ESP AT-commands documentation, a POST request is performed using the following command:

AT+HTTPCPOST=

Find the link below for the full description of the command.

I cannot seem to get this POST request working. The ESP01 device immediately returns an "ERROR" message without any delay, as though it did not even try to send the request, that the syntax might be wrong.

Among many variations, the following is my best attempt:

AT+HTTPCPOST="http://MYIPADDR:5000/add_data",30,2,"Temperature: 25.4","Humidity: 52.2"

With MYIPADDR above replaced with my IP address.

How do I translate a post request into ESP01 AT command format, and are there any prerequisites needed to be in place to perform such a request?

I did connect the ESP01 device to the WiFi network.

Here's the link to the POST AT command description:

https://docs.espressif.com/projects/esp-at/en/release-v2.2.0.0_esp8266/AT_Command_Set/HTTP_AT_Commands.html#cmd-httpcpost

Bosterrier
  • 11
  • 1

1 Answers1

1

The documentation says:

AT+HTTPCPOST=url,length[,<http_req_header_cnt>][,<http_req_header>..<http_req_header>] Response:

OK

The symbol > indicates that AT is ready for receiving serial data, and you can enter the data now. When the requirement of message length determined by the parameter is met, the transmission starts. ...

Parameters

: HTTP URL. : HTTP data length to POST. The maximum length is equal to the system allocable heap size. <http_req_header_cnt>: the number of <http_req_header> parameters. [<http_req_header>]: you can send more than one request header to the server.

You're sending:

AT+HTTPCPOST="http://MYIPADDR:5000/add_data",30,2,"Temperature: 25.4","Humidity: 52.2"

The length is 30. The problem is that everything after the length is HTTP header fields; you need to send the variables in the body. So the command is:

AT+HTTPCPOST="http://MYIPADDR:5000/add_data",30

followed on the next line by after the ESP-01 send the > character:

Temperature=25.4&Humidity=52.2

Because you passed 30 as the body length, the ESP-01 will read exactly 30 characters after the end of the AT command and send that data as the post body. If the size of that data changes (for instance, maybe the temperature is 2.2, so one digit less), you'll need to send the new length rather than 30.

romkey
  • 6,218
  • 3
  • 16
  • 12
  • 1
    The `>` character is resembling the `"\r\n> "` character sequence for [AT+CMGS](https://stackoverflow.com/a/46288907/23118), so reading up on how that command works might help. Looking at the documentation for `AT+HTTPCPOST` it returns a final result code **before** waiting for additional data which is horrible, horrible, horrible bad AT command implementation. But it seem that that is maybe specific to only that command, at least the basic AT command section looks good. – hlovdal Jul 24 '22 at 23:36
  • Oh that's bizarre! It can't possibly have the result before getting the data. Definitely horrible design if it's doing that. – romkey Jul 25 '22 at 00:45
  • I double checked the documentation and that's not my reading of it. You should get the ">" before any result. You'll get "SEND OK" or "SEND FAIL" and can retrieve the HTTP error code with another command (`AT+HTTPCLIENT`). The "\r\n" is just a newline. – romkey Jul 25 '22 at 03:24
  • Thanks, now I have a better idea how to send the POST request. I did try it though and the module still does not seem to recognize the command. As Juraj mentioned I might have to upgrade the firmware to version 2.0. I saw a tutorial somewhere how to do that. – Bosterrier Jul 27 '22 at 18:27