Webserver, HTML serial print problem

Hello, I have a simple webserver that I want to print the number entered in the webpage on a serial monitor with a message.

Webpage control code that is asking for the input

            client.println("<form action='/' methode=post>");
            client.println("<label for='A'>Enter number: </label>");
            client.println("<input type='number' id='N' name='N'>");
            client.println("<input type='submit' value='Enter'>");
            client.println("</form>");

The part that checks for the input and prints the information to the serial monitor

        if (currentLine[6] == 'N') {
          String myString = currentLine;
          myString.remove(0, 8);
          Serial.print("Number enterd is:" + myString + "/n" );
        }

The results that I'm getting in the serial monitor as shown below:

You enterd:

You enterd:

You enterd:3

You enterd:3

You enterd:3 H

You enterd:3 HT

You enterd:3 HTT

You enterd:3 HTTP

You enterd:3 HTTP/

You enterd:3 HTTP/1

You enterd:3 HTTP/1.

You enterd:3 HTTP/1.1

You enterd:3 HTTP/1.1

The results I want the serial monitor to display:
You enterd:3

Any help would be appreciated.

The results you show are clearly the consequence of a part of the code that you haven't shown us. Where is 'currentline' assigned it's value ?
For debugging purposes, it is practical to change the method to 'get', so you actually see that the form is returning the correct URL (i think it sort of is, but just to be sure...)if (currentLine[6] == 'N')This is a rough method, you are better of using indexOf()

I changed it to the indexOF, thanks!

Here is the entire code:

#include <WiFi.h>

const char *ssid = "";
const char *password = "";

WiFiServer server(80);

void setup() {
  Serial.begin(57600);
  delay(10);

  WiFi.begin(ssid, password);
  server.begin();

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

}


void loop() {

  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    String currentLine = "";
    // an http request ends with a blank line
    //boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if gotten to the end of the line (received a newline character) and the line is blank, the http request has ended, so you can send a reply
        if (c == '\n') {                    // if the byte is a newline character
          // send a standard http response header
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<h1>  Webpage Controller </h1>");
            client.println("
");

            client.println("<form action='/' methode=get>");
            client.println("<label for='N'>Enter number: </label>");
            client.println("<input type='number' id='N' name='N'>");
            client.println("<input type='submit' value='Enter'>");
            client.println("</form>");

            client.println("</html>");

            // The HTTP response ends with another blank line:
            client.println();

            break;    // break out of the while loop:

          }

          else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }

        } else if (c != '\r') {  // if you got anything else but a carriage return character,

          currentLine += c;      // add it to the end of the currentLine
        }

        // Check the client request
       if (currentLine.indexOf('N', 0)== 6) {
          String myString = currentLine;
          myString.remove(0, 8);
          Serial.print("You enterd:" + myString + "\n");
        }
      }
    }

    // close the connection:
    client.stop();

  }
}

Hello, I have a simple webserver that I want to print the number entered in the webpage on a serial monitor with a message.

The code I'm using

#include <WiFi.h>

const char *ssid = "";
const char *password = "";



WiFiServer server(80);

void setup() {
  Serial.begin(57600);
  delay(10);

  WiFi.begin(ssid, password);
  server.begin();

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

}


void loop() {

  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    String currentLine = "";
    // an http request ends with a blank line
    //boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if gotten to the end of the line (received a newline character) and the line is blank, the http request has ended, so you can send a reply
        if (c == '\n') {                    // if the byte is a newline character
          // send a standard http response header
          if (currentLine.length() == 0) {
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            // the content of the HTTP response follows the header:
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
            client.println("<h1>  Webpage Controller </h1>");
            client.println("
");

            client.println("<form action='/' methode=get>");
            client.println("<label for='N'>Enter number: </label>");
            client.println("<input type='number' id='N' name='N'>");
            client.println("<input type='submit' value='Enter'>");
            client.println("</form>");

            client.println("</html>");

            // The HTTP response ends with another blank line:
            client.println();

            break;    // break out of the while loop:

          }

          else {    // if you got a newline, then clear currentLine:
            currentLine = "";
          }

        } else if (c != '\r') {  // if you got anything else but a carriage return character,

          currentLine += c;      // add it to the end of the currentLine
        }

        // Check the client request
        if (currentLine.indexOf('N') == 6) {
          String myString = currentLine;
          myString.remove(0, 8);
          Serial.print("You enterd:" +  myString + "\n");
        }
      }
    }

    // close the connection:
    client.stop();

  }
}

The results that I'm getting in the serial monitor as shown below:

You enterd:

You enterd:

You enterd:3

You enterd:3

You enterd:3 H

You enterd:3 HT

You enterd:3 HTT

You enterd:3 HTTP

You enterd:3 HTTP/

You enterd:3 HTTP/1

You enterd:3 HTTP/1.

You enterd:3 HTTP/1.1

You enterd:3 HTTP/1.1

The results I want the serial monitor to display:
You enterd:3

Any help would be appreciated.

Your code that prints to the serial monitor gets executed each time through this loop

    while (client.connected()) {
      if (client.available()) {

So the code reads in 1 char, processes it and then executes your print statement. You really only want to do that after you have read in the entire line, not after every char. Possibly as an else{} clause to the if(client.available()){}

Duplicate topics merged

Why did you start a second one after a week ?

Something like this is what I think you need (compiles but not tested)

#include <WiFi.h>
const char *ssid = "";
const char *password = "";

WiFiServer server(80);

void setup() {
  Serial.begin(57600);
  delay(10);

  WiFi.begin(ssid, password);
  server.begin();

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

// read client until until_c char found, returns true when found else false
// non-blocking, until_c is returned as last char in String, updates input String with chars read
bool readStringUntil(String& input, char until_c, WiFiClient &client) {
  while (client.available()) {
    char c = client.read();
    input += c;
    if (c == until_c) {
      return true;
    }
  }
  return false;
}

void loop() {

  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {
    String currentLine = "";
    // an http request ends with a blank line
    //boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (readStringUntil(currentLine, '\n', client)) {
        // got line process it
        // note current line has '\r''\n' if in the stream
        currentLine.trim(); // remove \r \n
        // Check the client request
        if (currentLine.indexOf('N') == 6) {
          String myString = currentLine;
          myString.remove(0, 8);
          Serial.print("You enterd:" +  myString + "\n");
        } else if (currentLine.length() == 0) {
          // send response .... your response code here
        }
        currentLine = ""; // finished with this line
      }
    }
    // close the connection:
    client.stop();
  }
}

see Arduino Software Solutions for various examples of how to read in text

Thank you so much! This fixed the issue. I'm getting this response now:
You enterd:23 HTTP/1.1

Do you know how do I get rid of the HTTP/1.1?

drmpf:
Something like this is what I think you need (compiles but not tested)

#include <WiFi.h>

const char *ssid = "";
const char *password = "";

WiFiServer server(80);

void setup() {
 Serial.begin(57600);
 delay(10);

WiFi.begin(ssid, password);
 server.begin();

while (WiFi.status() != WL_CONNECTED) {
   delay(500);
 }
}

// read client until until_c char found, returns true when found else false
// non-blocking, until_c is returned as last char in String, updates input String with chars read
bool readStringUntil(String& input, char until_c, WiFiClient &client) {
 while (client.available()) {
   char c = client.read();
   input += c;
   if (c == until_c) {
     return true;
   }
 }
 return false;
}

void loop() {

// listen for incoming clients
 WiFiClient client = server.available();

if (client) {
   String currentLine = "";
   // an http request ends with a blank line
   //boolean currentLineIsBlank = true;
   while (client.connected()) {
     if (readStringUntil(currentLine, '\n', client)) {
       // got line process it
       // note current line has '\r''\n' if in the stream
       currentLine.trim(); // remove \r \n
       // Check the client request
       if (currentLine.indexOf('N') == 6) {
         String myString = currentLine;
         myString.remove(0, 8);
         Serial.print("You enterd:" +  myString + "\n");
       } else if (currentLine.length() == 0) {
         // send response .... your response code here
       }
       currentLine = ""; // finished with this line
     }
   }
   // close the connection:
   client.stop();
 }
}




see [Arduino Software Solutions](https://www.forward.com.au/pfod/ArduinoProgramming/SoftwareSolutions/index.html) for various examples of how to read in text

blh64:
Your code that prints to the serial monitor gets executed each time through this loop

    while (client.connected()) {

if (client.available()) {



So the code reads in 1 char, processes it and then executes your print statement. You really only want to do that after you have read in the entire line, not after every char. Possibly as an else{} clause to the if(client.available()){}

Thank you, this helped me narrow down the issue, but I used the code posted by drmpf and it fixed it.

UKHeliBob:
Duplicate topics merged

Why did you start a second one after a week ?

I wasn't getting any help, and I couldn't figure it out on my own

try

int spaceIdx = currentLine.indexOf(' ');
currentLine.remove(spaceIdx); // remove the rest

OR

currentLine.replace("  HTTP/1.1","");

int spaceIdx = currentLine.indexOf(' ');
currentLine.remove(spaceIdx); // remove the rest

This works. Thank you!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.