Parsing the GET function

I am working on a web server. I need to use PWM, instead of Switching the led. As of now When hit the browser http://192.168.4.1/OFF ,it turns of the LED. Instead of this I will be sending random values to for PWM. For example http://192.168.4.1/255 and will be writing the value "255" to analogWrite(5, val);.

How to do that ??

Here is the code for switching the led :

#include <ESP8266WiFi.h>

WiFiServer server(80); //Initialize the server on Port 80

void setup() {

  WiFi.mode(WIFI_AP); //Our ESP8266-12E is an AccessPoint
  WiFi.softAP("Hello_IoT", "12345678"); // Provide the (SSID, password); .
  server.begin(); // Start the HTTP Server

  pinMode(5, OUTPUT); //GPIO16 is an OUTPUT pin;
  digitalWrite(5, LOW); //Initial state is ON


  Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window
  IPAddress HTTPS_ServerIP = WiFi.softAPIP(); // Obtain the IP of the Server
  Serial.print("Server IP is: "); // Print the IP to the monitor window
  Serial.println(HTTPS_ServerIP);

}c

void loop()
{
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  //Looking under the hood
  Serial.println("Somebody has connected :)");


  //Read what the browser has sent into a String class and print the request to the monitor
  String request = client.readStringUntil('\r');;
  //Looking under the hood
  Serial.println(request);

  if (request.indexOf("/OFF") != -1) {
    digitalWrite(5, LOW);
    //analogWrite(5, brightness);
  }

}

It looks like you have already made an attempt to get it to work:

if (request.indexOf("/OFF") != -1) {
    digitalWrite(5, LOW);
    //analogWrite(5, brightness);
    }

If that did something when you removed the comment, you could try adding:

if (request.indexOf("/255") != -1) {
    analogWrite(5, 255);
    }

You can't use simple numbers for this. The risk is that '255' in this case may appear somewhere else in the string returned by GET so you may have to invent a more complex protocol to signal a pwm activity e.g.:
http://192.168.4.1/PWM$255 and look for what follows PWM$.

But the values will vary from 0-255. I can't compare 256 values. All i want is to copy the value(0-255), whichever it is and write in analogWrite(5, val)

The string I will be sending will be like http://192.168.4.1/R123 or http://192.168.4.1/R203...etc

You can try something like this to get you started.
There is no check for a failed match.

 Serial.begin(9600) ;
  int myValue ;
  char myTestUrl[] = "http://192.168.13.27/R123" ;
  char buff[4] = "" ;   // include null terminator
  char *pch ;
  pch = strstr(myTestUrl, "/R") ;
  strncpy ( buff , pch + 2  , 3 ) ;
  myValue = atoi( buff ) ;
  Serial.println( myValue ) ;

The same a little shorter, with mismatch test.

void setup() {
  Serial.begin(115200);
  char myTestUrl[] = "http://192.168.13.27/R123";
  char *pch = strstr(myTestUrl, "/R");
  if (pch) {
    int myValue  = atoi(pch + 2);
    Serial.println(myValue);
  }
}
void loop() {}

Pretty good that. I've never earned any money with C/C++ and the languages I use have regular expressions for handling that stuff so I tend to be cautious about seeing that the null terminator is explicitly handled etc.

The atoi skips whitspace and stops on nonumeric values, so it is rarely neccessary to copy or inject '\0's.

void setup() {
  Serial.begin(115200);
  char myTestUrl[] = "http://192.168.13.27/R   123 234";
  char *pch = strstr(myTestUrl, "/R");
  if (pch) {
    int myValue  = atoi(pch + 2);
    Serial.println(myValue);
  }
}
void loop() {}

If you want to parse more than one value, strtol maybe better.

void setup() {
  Serial.begin(115200);
  char myTestUrl[] = "http://192.168.13.27/R123,25,234";
  char *pch = strstr(myTestUrl, "/R");
  char *behind;
  byte val[3];
  if (pch) {
    val[0]  = strtol(pch + 2, &behind, 0);
    if (*behind++ == ',') {
      val[1]  = strtol(behind, &behind, 0);
      if (*behind++ == ',') {
        val[2]  = strtol(behind, &behind, 0);
        Serial.print(F("values "));
        for (byte i = 0; i < 3; i++) {
          Serial.print(val[i]);
          if (i != 2) {
            Serial.print(F(", "));
          }
        }
        Serial.println();
      }
    }
  }
}
void loop() {}
values 123, 25, 234

Thanks Everyone. The answers were really very helpful. :smiley: :smiley: