简体   繁体   中英

How do I translate the following POST request into ESP8266 AT-command format?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM