Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 10 months ago.
ESP8266 - Respond to a HTTP POST Request from a webpage
Hi guys,
At the moment I have written an application running on an STM32F411RE which uses an ESP8266 to serve a configuration webpage to a browser. Currently the user can enter information into the form and submit it. The data from the form reaches the ESP8266 and STM32F411RE as expected which I then parse and send back a HTTP OK response e.g.
#define ESP8266_HTTP_OK "HTTP/1.1 200 OK\r\n"
This response is reaching the browser (verified with the debugger in Google Chrome) however when I send the HTML page back to the browser nothing seems to happen.
In my serveBuffer() function I have hard coded the connection parameter to the 0th connection which may cause issues. Here is the implementation:
int serveBuffer(const char* bufferToSend, int len){ int ret = ESP8266_FAIL_NO_GOOD_RESPONSE; int offset = 0; char recv[255] = {0}; char serialBuffer[255] = {0}; bool readyToSend = false; esp.printf("AT+CIPSEND=0,%d\r\n", len); readResponse(recv, sizeof(recv), 1); for(int i = 0; ((i < sizeof(recv)) && readyToSend == false); i++){ if(recv[i] == '>'){ readyToSend = true; } } if(readyToSend){ while(len > 0){ strncpy(serialBuffer, &bufferToSend[offset], sizeof(serialBuffer)); esp.printf("%s", serialBuffer); offset += strlen(serialBuffer); len -= (strlen(serialBuffer) + 1); memset(serialBuffer, '\0', sizeof(serialBuffer)); } pc.printf("\n"); ret = readResponse(recv, sizeof(recv), 1); } return ret; }
Here is how I'm currently handling the request:
int handleClientWebRequest(){ char buffer[255] = {0}; char numBuf[4] = {0}; int ret; //... other data vars readResponse(buffer, sizeof(buffer), 1); if(strstr(buffer, "GET")){ ret = ESP8266_RECV_REQUEST; } else if(strstr(buffer, HOUR_KEY)){ //parse fields ret = ESP8266_ALARM_REQUEST; readResponse(buffer, sizeof(buffer), 1); serveBuffer(ESP8266_HTTP_OK, strlen(ESP8266_HTTP_OK)); serveBuffer(CONFIG_PAGE, strlen(CONFIG_PAGE)); closeConnection(); } else{ ret = ESP8266_NO_REQUEST; } return ret; }
Then only way I can get the config page back after a submit is to close down the browser tab and re-request the page. Are these steps correct or should I try to handle it in a different manner?
Any assistance with this would be much appreciated!
Thanks, Russell Bateman