user8524: I'am new to Arduino and c++
i get his code from the INTERNET
void setup() {
    Serial.begin(57600);  
    pinMode(13, OUTPUT);
}
int f = 0, pos;
void loop() {
    boolean has_request = false;
    String in = "";
    if (Serial.available()) {
        in = "";
        while (true) {  // should add time out here
            while (Serial.available() == false) {}
            in += (char)(Serial.read());
            if (in.endsWith("\r\n\r\n")) {
                has_request = true;  break;
            } 
        }
    }
    if (has_request) {
        int i1 = in.indexOf("GET /blink?f="), i2;
        if (i1 != -1) {
            i2 = in.indexOf(" ", i1+13);
            f = in.substring(i1+13, i2).toInt();
        }
        Serial.println("HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close");
        String sr = "<!DOCTYPE HTML>\n";
        sr += "<html>\n";
        sr += "LED frequency: ";
        sr += f;
        sr += "Hz.</html>";
        Serial.print("Content-Length: ");
        Serial.print(sr.length());
        Serial.print("\r\n\r\n");
        Serial.print(sr);
        has_request = false;
    }
    if (f>0) {
        static unsigned long t = millis();
        if (millis() > t + 1000/f) {
            digitalWrite(13, 1-digitalRead(13));
            t = millis();
        }
    }
}

see this link if you cant see the code formated here in the site
this code is used for the Arduino uno wifi shield
it is working perfectly except it is too complicated for me 
while loop inside a while loop 
and i don't want to do frequency i only need to send turn on or off
without any complexity 
anyone could help me ? 

user7508: Here is my take at this. I realize that I did not really make the
program simpler, but hopefully better in some respects, as it avoids
blocking reads and heap memory allocation. Be warned however that I did
not test it. It compiles fine though.
Preamble, hopefully simple enough that it requires no comments:
const uint8_t led_pin = 13;

void setup() {
    Serial.begin(57600);  
    pinMode(led_pin, OUTPUT);
}

Next, here is a function to read the HTTP request. A few things are
worth noting in this function:

It is non-blocking: if there is not data in the serial buffer, it will
return NULL immediately. This is to prevent the whole program from
hanging if the client does not terminate its request.
It only saves the first 31 bytes of the request. This is because
browsers tend to add lots of useless headers, which can be unfriendly
to the memory of a small Arduino. And the only interesting piece of
data is at the very beginning anyway.
The request terminating sequence (CR LF CR LF) is detected without
having to store it in the buffer, just by counting how many
consecutive bytes of that sequence we have already received.
Declaring the function as static makes it possible for the compiler
to inline it, which it does as it is only called from one place. This
makes for a smaller executable.

/*
 * Non-blocking read. Returns the first bytes of the request in a static
 * buffer, or NULL if we do not have a complete request yet.
 */
static char * read_request() {
    static char buffer[32];
    static uint8_t pos;
    static uint8_t crlf_bytes;  // bytes of "\r\n\r\n" received

    while (Serial.available()) {
        char c = Serial.read();

        // Store the received byte if there is enough room.
        if (pos < sizeof buffer - 1)
            buffer[pos++] = c;

        // Update the count of consecutive CRLF bytes received.
        if ((c == '\r' && crlf_bytes % 2 == 0)
         || (c == '\n' && crlf_bytes % 2 == 1))
            crlf_bytes++;
        else
            crlf_bytes = 0;

        // Return the buffer if we have a complete request.
        if (crlf_bytes == 4) {
            buffer[pos] = '\0';  // terminate the string
            pos = 0;
            crlf_bytes = 0;
            return buffer;
        }
    }
    return NULL;
}

Parsing the request is done by a different function. Here, I do not
search for the "GET /blink?f=" string, because it must be at the very
beginning for the request to be valid.
/*
 * Returns the requested frequency, or 0 if nothing valid is found.
 */
static int parse_request(char *request) {

    /* The "GET" should be at the very beginning. */
    if (strncmp(request, "GET /blink?f=", 13) != 0)
        return 0;

    return atoi(request + 13);
}

Now we can send the response to the client, telling it how we parsed the
frequency. I avoid using String, as it involves heap allocation, and I
put the bulk of the response in PROGMEM (F() macro) to save RAM. I
also added a <title> to make it valid HTML.
/*
 * Tell the client how we understood the requested frequency.
 */
static void send_response(int freq) {
    char freq_str[12];
    itoa(freq, freq_str, 10);
    Serial.print(F("HTTP/1.1 200 OK\r\n"
                   "Content-Type: text/html\r\n"
                   "Content-Length: "));
    Serial.print(84 + strlen(freq_str));
    Serial.print(F("\r\nConnection: close\r\n\r\n"
                   "<!DOCTYPE html>\n"
                   "<html><head><title>Blink</title></head>\n"
                   "LED frequency: "));
    Serial.print(freq_str);
    Serial.print(F(" Hz.\n</html>\n"));
}

The next function blinks the LED in a rollover-safe fashion. Storing the
current LED state in a static variable avoids a call to digitalRead().
static void blink_led(unsigned long toggle_time) {
    static uint8_t led_state;
    static unsigned long last_toggle;
    unsigned long now = millis();
    if (now - last_toggle > toggle_time) {
        led_state = !led_state;
        digitalWrite(led_pin, led_state);
        last_toggle = now;
    }
}

Putting it all together, the main loop() ends up being quite straight
forward:
void loop() {
    static unsigned long period = 1000;
    char * request = read_request();
    if (request) {
        int freq = parse_request(request);
        if (freq > 0) period = 1000 / freq;
        send_response(freq);
    }
    blink_led(period);
}

PS: My version is significantly larger than the original at the
source level (66 v.s. 44 source lines of code), but the compiled program
is smaller in both flash and RAM usage:
text    data     bss     version
5646     168     197     original
3316      40     218     my version


user4143: There is only one "while loop inside a while loop" and that is hardly complex:
while (Serial.available() == false) {}

All that means is "Wait for something to become available on the serial".  That is ALL it does.
There is nothing even remotely complex there.  True, it may not be very well written (as is the case with lots of code on t'interwebs).  It's time to start learning how the code works so you can write your own.

8524: <p>I'am new to Arduino and c++</p>

<p>i get his code from the INTERNET</p>

<pre><code>void setup() {
    Serial.begin(57600);  
    pinMode(13, OUTPUT);
}
int f = 0, pos;
void loop() {
    boolean has_request = false;
    String in = "";
    if (Serial.available()) {
        in = "";
        while (true) {  // should add time out here
            while (Serial.available() == false) {}
            in += (char)(Serial.read());
            if (in.endsWith("\r\n\r\n")) {
                has_request = true;  break;
            } 
        }
    }
    if (has_request) {
        int i1 = in.indexOf("GET /blink?f="), i2;
        if (i1 != -1) {
            i2 = in.indexOf(" ", i1+13);
            f = in.substring(i1+13, i2).toInt();
        }
        Serial.println("HTTP/1.1 200 OK\nContent-Type: text/html\nConnection: close");
        String sr = "&lt;!DOCTYPE HTML&gt;\n";
        sr += "&lt;html&gt;\n";
        sr += "LED frequency: ";
        sr += f;
        sr += "Hz.&lt;/html&gt;";
        Serial.print("Content-Length: ");
        Serial.print(sr.length());
        Serial.print("\r\n\r\n");
        Serial.print(sr);
        has_request = false;
    }
    if (f&gt;0) {
        static unsigned long t = millis();
        if (millis() &gt; t + 1000/f) {
            digitalWrite(13, 1-digitalRead(13));
            t = millis();
        }
    }
}
</code></pre>

<p><a href="http://pastebin.com/mNQQamUg" rel="nofollow">see this link if you cant see the code formated here in the site</a></p>

<p>this code is used for the Arduino uno wifi shield
it is working perfectly except it is too complicated for me 
while loop inside a while loop </p>

<p>and i don't want to do frequency i only need to send turn on or off
without any complexity </p>

<p>anyone could help me ? </p>
 7508: <p>Here is my take at this. I realize that I did not really make the
program simpler, but hopefully better in some respects, as it avoids
blocking reads and heap memory allocation. Be warned however that I did
not test it. It compiles fine though.</p>

<p>Preamble, hopefully simple enough that it requires no comments:</p>



<pre class="lang-c++ prettyprint-override"><code>const uint8_t led_pin = 13;

void setup() {
    Serial.begin(57600);  
    pinMode(led_pin, OUTPUT);
}
</code></pre>

<p>Next, here is a function to read the HTTP request. A few things are
worth noting in this function:</p>

<ul>
<li>It is non-blocking: if there is not data in the serial buffer, it will
return <code>NULL</code> immediately. This is to prevent the whole program from
hanging if the client does not terminate its request.</li>
<li>It only saves the first 31 bytes of the request. This is because
browsers tend to add lots of useless headers, which can be unfriendly
to the memory of a small Arduino. And the only interesting piece of
data is at the very beginning anyway.</li>
<li>The request terminating sequence (CR LF CR LF) is detected without
having to store it in the buffer, just by counting how many
consecutive bytes of that sequence we have already received.</li>
<li>Declaring the function as <code>static</code> makes it possible for the compiler
to inline it, which it does as it is only called from one place. This
makes for a smaller executable.</li>
</ul>



<pre class="lang-c++ prettyprint-override"><code>/*
 * Non-blocking read. Returns the first bytes of the request in a static
 * buffer, or NULL if we do not have a complete request yet.
 */
static char * read_request() {
    static char buffer[32];
    static uint8_t pos;
    static uint8_t crlf_bytes;  // bytes of "\r\n\r\n" received

    while (Serial.available()) {
        char c = Serial.read();

        // Store the received byte if there is enough room.
        if (pos &lt; sizeof buffer - 1)
            buffer[pos++] = c;

        // Update the count of consecutive CRLF bytes received.
        if ((c == '\r' &amp;&amp; crlf_bytes % 2 == 0)
         || (c == '\n' &amp;&amp; crlf_bytes % 2 == 1))
            crlf_bytes++;
        else
            crlf_bytes = 0;

        // Return the buffer if we have a complete request.
        if (crlf_bytes == 4) {
            buffer[pos] = '\0';  // terminate the string
            pos = 0;
            crlf_bytes = 0;
            return buffer;
        }
    }
    return NULL;
}
</code></pre>

<p>Parsing the request is done by a different function. Here, I do not
search for the "GET /blink?f=" string, because it <em>must</em> be at the very
beginning for the request to be valid.</p>

<pre class="lang-c++ prettyprint-override"><code>/*
 * Returns the requested frequency, or 0 if nothing valid is found.
 */
static int parse_request(char *request) {

    /* The "GET" should be at the very beginning. */
    if (strncmp(request, "GET /blink?f=", 13) != 0)
        return 0;

    return atoi(request + 13);
}
</code></pre>

<p>Now we can send the response to the client, telling it how we parsed the
frequency. I avoid using <code>String</code>, as it involves heap allocation, and I
put the bulk of the response in PROGMEM (<code>F()</code> macro) to save RAM. I
also added a <code>&lt;title&gt;</code> to make it valid HTML.</p>

<pre class="lang-c++ prettyprint-override"><code>/*
 * Tell the client how we understood the requested frequency.
 */
static void send_response(int freq) {
    char freq_str[12];
    itoa(freq, freq_str, 10);
    Serial.print(F("HTTP/1.1 200 OK\r\n"
                   "Content-Type: text/html\r\n"
                   "Content-Length: "));
    Serial.print(84 + strlen(freq_str));
    Serial.print(F("\r\nConnection: close\r\n\r\n"
                   "&lt;!DOCTYPE html&gt;\n"
                   "&lt;html&gt;&lt;head&gt;&lt;title&gt;Blink&lt;/title&gt;&lt;/head&gt;\n"
                   "LED frequency: "));
    Serial.print(freq_str);
    Serial.print(F(" Hz.\n&lt;/html&gt;\n"));
}
</code></pre>

<p>The next function blinks the LED in a rollover-safe fashion. Storing the
current LED state in a static variable avoids a call to <code>digitalRead()</code>.</p>

<pre class="lang-c++ prettyprint-override"><code>static void blink_led(unsigned long toggle_time) {
    static uint8_t led_state;
    static unsigned long last_toggle;
    unsigned long now = millis();
    if (now - last_toggle &gt; toggle_time) {
        led_state = !led_state;
        digitalWrite(led_pin, led_state);
        last_toggle = now;
    }
}
</code></pre>

<p>Putting it all together, the main <code>loop()</code> ends up being quite straight
forward:</p>

<pre class="lang-c++ prettyprint-override"><code>void loop() {
    static unsigned long period = 1000;
    char * request = read_request();
    if (request) {
        int freq = parse_request(request);
        if (freq &gt; 0) period = 1000 / freq;
        send_response(freq);
    }
    blink_led(period);
}
</code></pre>

<p><strong>PS</strong>: My version is significantly larger than the original at the
source level (66 v.s. 44 source lines of code), but the compiled program
is smaller in both flash and RAM usage:</p>

<pre class="lang-c++ prettyprint-override"><code>text    data     bss     version
5646     168     197     original
3316      40     218     my version
</code></pre>
 4143: <p>There is only one "while loop inside a while loop" and that is hardly complex:</p>

<pre><code>while (Serial.available() == false) {}
</code></pre>

<p>All that means is "Wait for something to become available on the serial".  That is ALL it does.</p>

<p>There is nothing even remotely complex there.  True, it may not be very well written (as is the case with lots of code on t'interwebs).  It's time to start learning how the code works so you can write your own.</p>
