Hi,
I have a WeMos R1 board, on which trying to bring up a webserver, to recieve post request from restlet client. The code example has been obtained from the site and logic works (apparently only for the first time). Arduino Webserver Receive POST Data and Parameters
The next time, no client request is being processed by the wemos board.
Also notice the postrequest needs some time for success reception (got couple of empty hits).
So, forced to restart the wemos board every time for logic to work.
Could any one check the code and see whats wrong in it.
Serial monitor logs:
WiFi connected
Server started
Server is ready.
Please connect to 192.168.0.105
[server] client connected
postParameter=
Error: empty post key: 1
postParameter=
Error: empty post key: 2
postParameter=
Error: empty post key: 3
postParameter=
Error: empty post key: 4
postParameter=
Error: empty post key: 5
postParameter=
Error: empty post key: 6
postParameter=
Error: empty post key: 7
postParameter=
Error: empty post key: 8
postParameter=
Error: empty post key: 9
postParameter=
Error: empty post key: 10
POST / HTTP/1.1
method=POST
uri=/
Host: 192.168.0.105
Connection: keep-alive
Content-Length: 7
Accept: */*
User-Agent: Curl/7.31
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
postParameter=
Error: empty post key: 11
LED1=OnpostParameter=LED1=On
LED1 ON/OFF request cnt:1 len:7
LED matchLED1 matchLED1 match opstr:On
LED1 ON done!
Success: post key processed for LED
Code:
/*
Web Server post
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SPI.h>
#define LED_BOARD 14
const char* ssid = "XXXXXXXXXX";
const char* password = "YYYYYYYYYYYYY";
WiFiServer server(80);
bool wifiConnected = false;
WiFiClient wifiClient;
int cnt = 0;
int failcnt=0;
int processed=0;
void setup() {
// Initialize serial port
Serial.begin(115200);
while (!Serial) continue;
pinMode ( LED_BUILTIN , OUTPUT );
pinMode (LED_BOARD, OUTPUT );//LED BOARD CENTRAL
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
wifiConnected = true;
// Start the web server and get ready to handle incoming requests
startWebServer();
Serial.println(F("Server is ready."));
Serial.print(F("Please connect to http://"));
Serial.println(WiFi.localIP());
digitalWrite ( LED_BUILTIN , HIGH );
}
void startWebServer()
{
server.begin();
Serial.println("Server started");
}
void handleRoot()
{
Serial.println( "Already connected" );
delay( 100 );
}
void send404(WiFiClient &client)
{
// Serial.println("[server] response 404 file not found");
client.println("HTTP/1.0 404 Not Found\r\n"
"Content-Type: text/plain\r\n" // simple plain text without html tags
"\r\n"
"File Fot Found");
}
void send400(WiFiClient &client)
{
// Serial.println("[server] response 204 empty content");
client.println("HTTP/1.0 400 empty content\r\n"); // no content
}
void send200(WiFiClient &client)
{
// Serial.println("[server] response 200 no content");
client.println("HTTP/1.0 200 ok\r\n"); // no content
}
void checkForClient()
{
WiFiClient client = server.available();
if (client) {
Serial.println(F("\n[server] client connected"));
uint8_t i = 0; // index / current read position
const uint16_t buffersize = 100; // size of read buffer (reads a complete line) (if larger than 255, modify i also!
const uint16_t smallbuffersize = 30; // a smaller buffer for results
char lineBuffer[buffersize] {'\0'}; // buffer for incomming data
char method[8]; // largest one 7+1. HTTP request methods in RFC7231 + RFC5789: GET HEAD POST PUT DELETE CONNECT OPTONS TRACE PATCH
char uri[smallbuffersize]; // the requestet page, shorter than smallbuffersize - method
char requestParameter[smallbuffersize]; // parameter appended to the URI after a ?
char postParameter[smallbuffersize]; // parameter transmitted in the body / by POST
enum class Status {REQUEST, CONTENT_LENGTH, EMPTY_LINE, BODY};
Status status = Status::REQUEST;
while (client.connected()) {
while (client.available()) {
char c = client.read();
Serial.print(c); // Debug print received characters to Serial monitor
if ( c == '\n' )
{
if (status == Status::REQUEST) // read the first line
{
//Serial.print(F("lineBuffer="));Serial.println(lineBuffer);
// now split the input
char *ptr;
ptr = strtok(lineBuffer, " "); // strtok willdestroy the newRequest
strlcpy(method, ptr, smallbuffersize);
Serial.print(F("method=")); Serial.println(method);
ptr = strtok(NULL, " ");
strlcpy(uri, ptr, smallbuffersize); // enthält noch evtl. parameter
if (strchr(uri, '?') != NULL)
{
ptr = strtok(uri, "?"); // split URI from parameters
strcpy(uri, ptr);
ptr = strtok(NULL, " ");
strcpy(requestParameter, ptr);
Serial.print(F("requestParameter=")); Serial.println(requestParameter);
}
Serial.print(F("uri=")); Serial.println(uri);
status = Status::EMPTY_LINE; // jump to next status
}
else if (status == Status::CONTENT_LENGTH) // MISSING check for Content-Length
{
status = Status::EMPTY_LINE;
}
else if (status > Status::REQUEST && i < 2) // check if we have an empty line
{
status = Status::BODY;
}
else if (status == Status::BODY)
{
strlcpy(postParameter, lineBuffer, smallbuffersize);
break; // we have received one line payload and break out
}
i = 0;
strcpy(lineBuffer, "");
}
else
{
if (i < buffersize)
{
lineBuffer[i] = c;
i++;
lineBuffer[i] = '\0';
}
// MISSING wenn status 3 und content-length --> abbrechen.
}
}
if (status == Status::BODY) // status 3 could end without linefeed, therefore we takeover here also
{
strlcpy(postParameter, lineBuffer, smallbuffersize);
}
Serial.print(F("postParameter=")); Serial.println(postParameter);
Serial.println();
if(strlen(postParameter) > 3)
{
cnt++;
Serial.printf("LED1 ON/OFF request cnt:%d len:%d\n",cnt,strlen(postParameter));
// Simple evaluation of postParameter from body
// post data looks like LED1=On/Off
if ( strncmp( postParameter, "LED", 3) == 0 ) {
Serial.print("LED match");
if ( strncmp( postParameter, "LED1", 4) == 0 ) {
Serial.print("LED1 match");
Serial.printf("LED1 match opstr:%s\n",postParameter+5);
if ( strncmp( postParameter+5, "On", 2) == 0 ) {
digitalWrite ( LED_BUILTIN , LOW );
Serial.printf("LED1 ON done!\n");
}
else if ( strncmp( postParameter+5, "Off", 3) == 0 ) {
digitalWrite ( LED_BUILTIN , HIGH );
Serial.printf("LED1 OFF done!\n");
}
}
else if( strncmp( postParameter, "LED2", 4) == 0 ) {
Serial.print("LED2 match");
Serial.printf("LED2 match opstr:%s\n",postParameter+5);
if ( strncmp( postParameter+5, "On", 2) == 0 ) {
digitalWrite ( LED_BOARD , HIGH );
Serial.printf("LED2 ON done!\n");
}
else if ( strncmp( postParameter+5, "Off", 3) == 0 ) {
digitalWrite ( LED_BOARD , LOW );
Serial.printf("LED2 OFF done!\n");
}
}
Serial.printf("Success: post key processed for LED\n");
// send back a response
send200(client);
delay(1);
client.stop();
// give the web browser time to receive the data
client.flush();
}
else
{
Serial.printf("Error: incorrect post key\n");
send400(client);
delay(1);
client.stop();
// give the web browser time to receive the data
client.flush();
}
}
else
{
failcnt++;
Serial.printf("Error: empty post key: %d\n",failcnt);
}
/*
delay(1);
client.stop();
*/
}
}
}
void loop() {
// listen for incoming clients
checkForClient();
}