ESP8266 D1 HTML Textbox

Hello,

I have a problem with a program that I am using for the Wemos ESP8266 D1 mini. I am using someone's his code which includes a website and a textbox which you can use to enable a pin for ex. Here is the code :

#include <ESP8266WiFi.h>

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

int Pin0 = D0;
int Pin1 = D1;
int Pin2 = D2;
int Pin3 = D3;
int Pin4 = D4;
int ledIntern = LED_BUILTIN;
WiFiServer server(80);

String readString; 

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(Pin0, OUTPUT);
  pinMode(Pin1, OUTPUT);
  pinMode(Pin2, OUTPUT);
  pinMode(Pin3, OUTPUT);
  pinMode(Pin4, OUTPUT);
  pinMode(ledIntern, OUTPUT);
  digitalWrite(Pin0, LOW);
  digitalWrite(Pin1, LOW);
  digitalWrite(Pin2, LOW);
  digitalWrite(Pin3, LOW);
  digitalWrite(Pin4, LOW);
  digitalWrite(ledIntern, LOW);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.softAP(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Pin 5 'on5' or 'off5': <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Change Pin 5!'>");

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

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if(readString.indexOf("4") >0)//checks for on
          {
            digitalWrite(Pin4, HIGH);    // set pin 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("x") >0)//checks for off
          {
            digitalWrite(Pin0, LOW);    // set pin 5 low
            digitalWrite(Pin1, LOW);    // set pin 5 low
            digitalWrite(Pin2, LOW);    // set pin 5 low
            digitalWrite(Pin3, LOW);    // set pin 5 low
            digitalWrite(Pin4, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

However, when I replace

if(readString.indexOf("4") >0)//checks for on

to

if(readString.indexOf("four") >0)//checks for on

It works. Typing anything else than "four" or leaving the box empty will not enable D4(Pin4). Else, it will enable output.

However, when I replace "four" with "4" in my program, it doesn't seem to work 100%. When I leave the textbox empty, and press the submit button, it will enable D4. Any other character will enable the assigned pin. Does anyone have any idea how I can solve this? Perhaps I need something like a string to int converter but I am not very experienced.

Does anyone have any idea how I can solve this?

Without seeing the results of this:

          Serial.println(readString); //see what was captured

? No.

After some restarts, testing, switching programs etc. It somehow works when I just took a break and went sleeping. The same code works now somehow.

So, I went to the next step:

This is a program which includes a 74hc595. It's original program supports Serial input. By typing number 1 in the Serial Monitor, it will turn on Led 1. Typing number 0, led 0 etc. I am trying to convert from this:

/*
Adafruit Arduino - Lesson 5. Serial Monitor
*/
 
int dataPin = 9;              // The Serial Data Pin to the Shift Register
int latchPin = 10;             // The Latch Pin to the Shift Register
int clockPin = 11;             // The Clock Pin to the Shift Register
 
byte leds = 0;
 
void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}
 
void loop() 
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '7')
    {
      int led = 0 - '0';
      bitSet(leds, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
      leds = 0;
      updateShiftRegister();
    }
    if (ch == 'x')
    {
      leds = 0;
      updateShiftRegister();
      Serial.println("Cleared");
    }
  }
}
 
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}

To this.

#include <ESP8266WiFi.h>

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

int dataPin = D0;              // The Serial Data Pin to the Shift Register
int latchPin = D1;             // The Latch Pin to the Shift Register
int clockPin = D2;             // The Clock Pin to the Shift Register
int Pin4 = D4;

byte leds = 0;
WiFiServer server(80);

String readString;

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(Pin4, OUTPUT);
  digitalWrite(Pin4, LOW);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.softAP(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Type 1-7 to turn led on or x to clear: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Submit'>");

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

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if (readString.indexOf("hallo") > 0)
          {
            Serial.print("whut");
            int led = c - '0';
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
          }
          if (c == 'x')
          {
            leds = 0;
            updateShiftRegister();
            Serial.println("Cleared");

          }
   readString="";
        }
      }
    }
  }
}

So I have added the wifi component, which works well. Now, instead of Serial.Read for char ch, I am trying to read via Client or string for char c.

This is what I get from the Serial monitor when I type other than "hallo":

GET /?LED=123&submit=Submit HTTP/1.1

GET /favicon.ico HTTP/1.1

This is what I get when I type "hallo":

GET /?LED=hallo&submit=Submit HTTP/1.1

whutTurned on LED -38
GET /favicon.ico HTTP/1.1

However, I want to replace "hallo" with something like char c. So, when I type number "3" the following code will proceed with number 3 and replace char c with "3"

            Serial.print("whut");
            int led = c - '0';
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);

Serial monitor tells me that LED -38 is turned on, it's because it doesn't read the textbox when I press submit?

How can I translate from

if (readString.indexOf("hallo") > 0)
          {
            Serial.print("whut");
            int led = c - '0';
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);

To something like

 if (ch >= '0' && ch <= '7')
    {
      int led = 0 - '0';
      bitSet(leds, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
      leds = 0;
      updateShiftRegister();
      int led = 0 - '0';

That's supposed to be

int led = ch - '0';
            int led = c - '0';

Having sent "hallo" in the text box, why are you trying to light up a pin based on the last letter received? Which LED, exactly, is the '\n' - '0' LED?

PaulS:

      int led = 0 - '0';

That's supposed to be

int led = ch - '0';
            int led = c - '0';

Having sent "hallo" in the text box, why are you trying to light up a pin based on the last letter received? Which LED, exactly, is the '\n' - '0' LED?

I've replaced it with "hallo", because I don't know how to use anything else than read.String.indexOf("hallo"). It should be a number. The number should replace the char c and it will normally turn on the led. It's not a specific led given in the program. If I type 3 in the textbox, it should turn on led 3. In my serial monitor, when typing "hallo" since it is required to run the program of the shiftregister part, it says led -38 turned on. Led -38? No wonder it doesn't turn on. However, the hallo must be replaced with something like char c. But when I use char c, it doesn't read the number from the textbox.

Serial.println(readString);

The following code works perfectly with the serial monitor, but instead reading the monitor, I want to read the html textbox value. The number you'll put in the serial is ch. How can I do this for the html version?

void loop() 
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '7')
    {
      int led = ch - '0';
      bitSet(leds, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
      leds = 0;
      updateShiftRegister();
    }
    if (ch == 'x')
    {
      leds = 0;
      updateShiftRegister();
      Serial.println("Cleared");
    }
  }
}
 
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}

Which LED, exactly, is the '\n' - '0' LED?

The original program reads the value of the serial monitor, which is actually the char "ch". The led turned on is the number you'll give.

I hope that you understand me PaulS. I have a working program now.

#include <ESP8266WiFi.h>

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

int dataPin = D0;              // The Serial Data Pin to the Shift Register
int latchPin = D1;             // The Latch Pin to the Shift Register
int clockPin = D2;             // The Clock Pin to the Shift Register
int Pin4 = D4;

byte leds = 0;

WiFiServer server(80);

String readString;

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(Pin4, OUTPUT);
  digitalWrite(Pin4, LOW);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.softAP(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char ch = client.read();

        //read char by char HTTP request
        if (readString.length() < 50) {

          //store characters to string
          readString += ch;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (ch == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Type 1-7 to turn led on or x to clear: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Submit'>");

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

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          /////////////////////
          if (readString.indexOf("Q0") > 0)
          {
            int led = 0 + 0;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q1") > 0)
          {
            int led = 0 + 1;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q2") > 0)
          {
            int led = 0 + 2;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q3") > 0)
          {
            int led = 0 + 3;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q4") > 0)
          {
            int led = 0 + 4;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q5") > 0)
          {
            int led = 0 + 5;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q6") > 0)
          {
            int led = 0 + 6;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if (readString.indexOf("Q7") > 0)
          {
            Serial.println("Led 0 turned on");
            int led = 0 + 7;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println(led);
            }
            if(readString.indexOf("x") >0)
            {
              leds = 0;
              updateShiftRegister();
              Serial.println("Cleared");

            }
            readString = "";
          }
        }
      }
    }
  }

As you can see, when I type Q1 in the textbox, it turns on Q1 etc. This is what I get in the serial monitor.

GET /?LED=Q7&submit=Submit HTTP/1.1

Turned on LED 7
GET /favicon.ico HTTP/1.1

GET /?LED=Q5&submit=Submit HTTP/1.1

Turned on LED 5
GET /favicon.ico HTTP/1.1

Okay, the reason why I use Q1 - Q7 instead of numbers is because I'll get the about the same problem again since this post began. When I remove all the "Q", this is what I get from my monitor:

Turned on LED 1
GET /favicon.ico HTTP/1.1

Turned on LED 1
GET /?LED=0&submit=Submit HTTP/1.1

Turned on LED 0
Turned on LED 1
GET /favicon.ico HTTP/1.1

Turned on LED 1
GET /?LED=3&submit=Submit HTTP/1.1

Turned on LED 1
Turned on LED 3
GET /favicon.ico HTTP/1.1

Turned on LED 1
GET /?LED=4&submit=Submit HTTP/1.1

Turned on LED 1
Turned on LED 4
GET /favicon.ico HTTP/1.1

Turned on LED 1

Every command, it turns LED 1 on. Like twice. It also displays it twice so. Here is the code where I removed the "Q":

          /////////////////////
          if (readString.indexOf("0") > 0)
          {
            int led = 0 + 0;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("0");
            }
            if (readString.indexOf("1") > 0)
          {
            int led = 0 + 1;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("1");
            }
            if (readString.indexOf("2") > 0)
          {
            int led = 0 + 2;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("2");
            }
            if (readString.indexOf("3") > 0)
          {
            int led = 0 + 3;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("3");
            }
            if (readString.indexOf("4") > 0)
          {
            int led = 0 + 4;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("4");
            }
            if (readString.indexOf("5") > 0)
          {
            int led = 0 + 5;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("5");
            }
            if (readString.indexOf("6") > 0)
          {
            int led = 0 + 6;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("6");
            }
            if (readString.indexOf("7") > 0)
          {
            int led = 0 + 7;
            bitSet(leds, led);
            updateShiftRegister();
            Serial.print("Turned on LED ");
            Serial.println("7");
            }
            if(readString.indexOf("x") >0)
            {
              leds = 0;
              updateShiftRegister();
              Serial.println("Cleared");

            }
            readString = "";
          }
        }
      }
    }
  }

Do you have an idea to solve this problem?

Every command, it turns LED 1 on.

Sure, because you are looking for ANY 1 anywhere in the String.

GET /favicon.ico HTTP/1.1

has 2 ones, even though it isn't supposed to turn an LED on at all.

GET /?LED=0&submit=Submit HTTP/1.1

also has 2 ones, even though it is not supposed to deal with LED 1 at all.

You need to seriously rethink your parsing technique.

There is a ? that marks where the info of interest starts. There is a & that marks where it ends. Create a substring from the data between those two locations. LED=n will be the result. Or, an empty string will.

Parse the non-empty LED=n string to get n.

PaulS:
Sure, because you are looking for ANY 1 anywhere in the String.
has 2 ones, even though it isn't supposed to turn an LED on at all.

GET /?LED=0&submit=Submit HTTP/1.1

also has 2 ones, even though it is not supposed to deal with LED 1 at all.

You need to seriously rethink your parsing technique.

There is a ? that marks where the info of interest starts. There is a & that marks where it ends. Create a substring from the data between those two locations. LED=n will be the result. Or, an empty string will.

Parse the non-empty LED=n string to get n.

Thank you! You're a genius, I understand it now why Led 1 keeps on. Also why it parse an empty string! Thanks for the explanation. However, Serial.println(readString); prints the GET /?LED=0&submit=Submit HTTP/1.1. I can't seem how I can stop printing out the "1.1". I will take a look tomorrow on how I can advance my program again by like you said, creating a substring so I don't have to copy paste the shift-register part 7 times. Thanks again!

I can't seem how I can stop printing out the "1.1".

You can't. It was part of the GET request. You can ignore that part of the request, but you can't stop it from being part of the request.

You could use indexOf() to find the &, and replace the char at that position with a NULL, so that the readString value becomes "GET /?LED=0", and then use indexOf() to see if the String contains "0", or "1", or "7".

Icannot find the code for my program where is it kept

Icannot find the code for my program where is it kept

In a shoe box under the bed.

PaulS:
You can't. It was part of the GET request. You can ignore that part of the request, but you can't stop it from being part of the request.

You could use indexOf() to find the &, and replace the char at that position with a NULL, so that the readString value becomes "GET /?LED=0", and then use indexOf() to see if the String contains "0", or "1", or "7".

I am trying to learn the parsing technique so I can understand your intentions more clearly. Now I have this as my code:

#include <ESP8266WiFi.h>

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

int dataPin = D0;              // The Serial Data Pin to the Shift Register
int latchPin = D1;             // The Latch Pin to the Shift Register
int clockPin = D2;             // The Clock Pin to the Shift Register
int Pin4 = D4;

byte leds = 0;

WiFiServer server(80);

String readString;

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(Pin4, OUTPUT);
  digitalWrite(Pin4, LOW);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.softAP(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char ch = client.read();

        //read char by char HTTP request
        if (readString.length() < 50) {

          //store characters to string
          readString += ch;
          //Serial.print(c);
        }

        //if HTTP request has ended
        if (ch == '\n') {

          ///////////////
          Serial.println(readString); //see what was captured

          //now output HTML data header

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>HTML form GET example</H1>");

          client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

          client.println("Type 1-7 to turn led on or x to clear: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

          client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Submit'>");

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

          client.println("
");

          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          //stopping client
          client.stop();

          // substring(index) looks for the substring from the index position to the end:
          if (readString.substring(6, 10) == "LED=") {
            {
              Serial.println("momma told me");
              int led = ch - '0';
              bitSet(leds, led);
              updateShiftRegister();
              Serial.print("Turned on LED ");
              Serial.println(led);
            }
            readString = "";
          }
        }
      }
    }
  }
}

This is what I get from my serial monitor:

Connecting to DKBS
......
WiFi connected
Server started
Use this URL : http://192.168.1.89/
GET /?LED=Jfjdj&submit=Submit HTTP/1.1

momma told me
Turned on LED -38
GET /favicon.ico HTTP/1.1

GET /favicon.ico HTTP/1.1
GET /?LED=&submit=Submi
GET /favicon.ico HTTP/1.1
GET /?LED=&submit=Submi
GET /favicon.ico HTTP/1.1
GET /?LED=&submit=Submi
GET /favicon.ico HTTP/1.1
GET /?LED=&submit=Submi
GET /favicon.ico HTTP/1.1
GET /?LED=&submit=Submi
GET /favicon.ico HTTP/1.1
GET /?LED=&submit=Submi

I just entered a random value of text and press submit, the substring

if (readString.substring(6, 10) == "LED=") {

reads the value between position 6 and ten, so proceeds to

Serial.println("momma told me");

Ok. Progress I guess. However, the next thing I fill in the textbox won't get retrieved and viewed as an empty string. Only the first attempt works as you can see. Also, the

GET /?LED=&submit=Submi

has been cut off and isn't complete, compared to the first attempt.

Also, is there an easier way to use if (readString.indexOf("0") > 0) to search between values? Applying the

if (a >= 10 && a <= 20){}   // true if a is between 10 and 20

method doesn't seem to work. Since LED='\n' can't be retrieved yet. Still working on that part.

reads the value between position 6 and ten

No. It creates a substring with the characters in positions 6, 7, 8, and 9. The same moron wrote that function that wrote the random() function.

          if (readString.substring(6, 10) == "LED=") {
            {
              Serial.println("momma told me");

Why do you have 2 curly braces?

When, exactly, do you reset readString?

When, exactly, are you going to wise up and quit using Strings?

Also, is there an easier way to use
Code: [Select]

if (readString.indexOf("0") > 0)

to search between values?

I don't understand the question. You REALLY should stop using indexOf() that way. Store the returned value in a variable, and use the variable in the if statement.

The indexOf() value is NOT "in a range".

Applying the
Code: [Select]

if (a >= 10 && a <= 20){} // true if a is between 10 and 20

method doesn't seem to work.

It most certainly does work. That it doesn't do what you expect means nothing more than that your expectations are wrong.

It most certainly does work. That it doesn't do what you expect means nothing more than that your expectations are wrong.

I know that function works, what I actually meant is applying the method by changing the read function and add up the function of the between value. However, I've managed to use another method of reading which only search up a particular area.

#include <ESP8266WiFi.h>

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

int dataPin = D0;              // The Serial Data Pin to the Shift Register
int latchPin = D1;             // The Latch Pin to the Shift Register
int clockPin = D2;             // The Clock Pin to the Shift Register
int Pin4 = D4;

byte leds = 0;

WiFiServer server(80);

String readString;

void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(Pin4, OUTPUT);
  digitalWrite(Pin4, LOW);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.softAP(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Match the request

  if (request.indexOf("/?LED=1") != -1) {
    int led = 0 + 0;
    bitSet(leds, 0);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("1");
  }
  if (request.indexOf("/?LED=2") != -1) {
    int led = 0 + 1;
    bitSet(leds, 1);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("2");
  }
  if (request.indexOf("/?LED=3") != -1) {
    int led = 0 + 2;
    bitSet(leds, 0);
    bitSet(leds, 1);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("3");
  }
  if (request.indexOf("/?LED=4") != -1) {
    int led = 0 + 3;
    bitSet(leds, 2);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("4");
  }
  if (request.indexOf("/?LED=5") != -1) {
    int led = 0 + 4;
    bitSet(leds, 0);
    bitSet(leds, 2);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("5");
  }
  if (request.indexOf("/?LED=6") != -1) {
    int led = 0 + 5;
    bitSet(leds, 1);
    bitSet(leds, 2);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("6");
  }
  if (request.indexOf("/?LED=7") != -1) {
    int led = 0 + 6;
    bitSet(leds, 0);
    bitSet(leds, 1);
    bitSet(leds, 2);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("7");
  }
  if (request.indexOf("/?LED=8") != -1) {
    int led = 0 + 7;
    bitSet(leds, 3);
    updateShiftRegister();
    Serial.print("Output on ");
    Serial.println("8");
  }
  if (request.indexOf("/?LED=x") != -1) {
    leds = 0;
    updateShiftRegister();
    Serial.println("Cleared");
  }
  //------------------------------------------
  ///////////////
  Serial.println(readString); //see what was captured

  //now output HTML data header

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();

  client.println("<HTML>");
  client.println("<HEAD>");
  client.println("<TITLE>74HC595 Monitor</TITLE>");
  client.println("</HEAD>");
  client.println("<BODY>");

  client.println("<H1>HTML form GET example</H1>");

  client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

  client.println("Type 1-7 to turn led on or x to clear: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

  client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Submit'>");
  client.println("

");

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

  client.println("
");
  client.println("</BODY>");
  client.println("</HTML>");

  delay(1);
  //stopping client
  client.stop();

  /////////////////////
}

It can read number 1 and exclude the "HTTP 1.1".

The only thing to make this program ideal is to store the "/?LED=Number&" into a variable. How can I store the value between "/?LED=" "Value" "&" into a variable? With that, I can use the numbers and use the "int leds" to manipulate in BCD and many more. That, or manually adding 128 times the same program with different commands since 8 bits has so many possibilities.

Anyway, the int led = 0 + 3; can be removed from this current situation. As I am going to use "leds" instead.

I've tried something like var = (request.indexOf("&") - request.indexOf("=") - 1); then print it in monitor and displays it as 1. When I use 2 numbers "10" till "99" it print as "2" in the monitor. At 100 till 999 print as 3 etc. However, I just want to get the value, not the amount of chars.

However, I just want to get the value, not the amount of chars.

You can't get the value until you know the position in the String where the data of interest starts and where is ends.

Now that you know where the data of interest starts and where is ends, create a substring of the data.

   int ampPos = request.indexOf("&");
   int eqPos = request.indexOf("=");

   String heyThisIsInteresting = request.substring(ampPos+1, eqPos);
   int interestingValue = heyThisIsInteresting.toInt();

Since /?LED=0(anynumber)& is the string, isn't it supposed to be

   int ampPos = request.indexOf("&");
   int eqPos = request.indexOf("=");

   String heyThisIsInteresting = request.substring(eqPos+1, ampPos);
   int interestingValue = heyThisIsInteresting.toInt();

I will post the results soon.

isn't it supposed to be

Well, yeah, that would probably work better.

This is what I get from my serial monitor:

´HAÑtñCÀdØ@x:ÙðÿEnter LED Number 0 to 7 or 'x' to clear
Connecting to DKBS
......
WiFi connected
Server started
Use this URL : http://192.168.1.81/
new client
GET /?LED=3737&submit=Submit HTTP/1.1
3737
new client
GET /favicon.ico HTTP/1.1
0
new client
GET /?LED=3&submit=Submit HTTP/1.1
3
new client
GET /favicon.ico HTTP/1.1
0
new client
GET /?LED=7&submit=Submit HTTP/1.1
7
new client
GET /favicon.ico HTTP/1.1
0
new client
GET /favicon.ico HTTP/1.1
0
new client
GET /?LED=x&submit=Submit HTTP/1.1
0
new client
GET /favicon.ico HTTP/1.1
0
new client

The value is stored. However, retrieving information from client happens twice when I hit the submit button. So, I get the actual value for ex. "7" and a "0" after I hit the submit button once.

Summary of code:

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  Serial.println("new client");
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();

  // Match the request
  int value0 = LOW;
  int value1 = LOW;
  int value2 = LOW;
  int value3 = LOW;
  int value4 = LOW;
  int value5 = LOW;
  int value6 = LOW;
  int value7 = LOW;
  int ampPos = request.indexOf("&");
  int eqPos = request.indexOf("=");

  String VConvert = request.substring(eqPos+1, ampPos);
  int ch = VConvert.toInt();
  Serial.print(ch);

    if (ch >= '0' && ch <= '7')
    {
      int led = ch - '0';
      bitSet(leds, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
    }
    if (ch == 'x')
    {
      leds = 0;
      updateShiftRegister();
      Serial.println("Cleared");
    }
  //--------------------------------------------------//
  Serial.println(readString); //see what was captured

  //now output HTML data header

  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();

  client.println("<HTML>");
  client.println("<HEAD>");
  client.println("<TITLE>74HC595 Monitor</TITLE>");
  client.println("</HEAD>");
  client.println("<BODY>");

  client.println("<H1>HTML form GET example</H1>");

  client.println("<FORM ACTION='/' method=get >"); //uses IP/port of web page

  client.println("Type 1-7 to turn led on or x to clear: <INPUT TYPE=TEXT NAME='LED' VALUE='' SIZE='25' MAXLENGTH='50'>
");

  client.println("<INPUT TYPE=SUBMIT NAME='submit' VALUE='Submit'>");
  client.println("

");

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

  client.println("
");
  client.print("Last succesful command for 4s =");
  if (value0 == HIGH) {
    client.print("1");
  } else if (value1 == HIGH) {
    client.print("2");
  } else if (value2 == HIGH) {
    client.print("3");
  } else if (value3 == HIGH) {
    client.print("4");
  } else if (value4 == HIGH) {
    client.print("5");
  } else if (value5 == HIGH) {
    client.print("6");
  } else if (value6 == HIGH) {
    client.print("7");
  } else if (value7 == HIGH) {
    client.print("8");
  }
  client.println("</BODY>");
  client.println("</HTML>");

  delay(1);
  //stopping client
  client.stop();
}
  /////////////////////

  void updateShiftRegister()
{
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, leds);
  digitalWrite(latchPin, HIGH);
}

Shouldn't:

    if (ch >= '0' && ch <= '7')

be initiated now since the number I've just put in is between 0 and 7?