Controlling a led using a arduino by ethernet using href

Could i please have help with this code. The LED is in pin GND and 9, but for some reason when i try to turn it on or off, the LED does not turn off or on:

#include <SPI.h>
#include <Ethernet2.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x10, 0x8B, 0xBF
};
IPAddress ip(192, 168, 1, 195);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

String readString;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  pinMode(9, OUTPUT);
}


void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've 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' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          client.println("Refresh: 60");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<a href=\"/?0\"\">Turn Off LED</a>");
          client.println("<a href=\"/?1\"\">Turn On LED</a>
");  
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    if (readString.indexOf("?0") >0){
               digitalWrite(9, LOW);
    }
    if (readString.indexOf("?1") >0){
               digitalWrite(9, HIGH);
    }
  }
}

What does your serial output show?

Does the LED actually work in a blink type sketch?

The usual advice is to avoid the use of String on an Arduino.
Why so much code to check out an LED?
Most LEDs require a resistor. Where is yours?
Do you have the LED the right way around?

PaulS:
What does your serial output show?

Does the LED actually work in a blink type sketch?

Yes it does work and in my serial it shows:

server is at 192.168.1.195
new client
GET / HTTP/1.1
Host: 192.168.1.195
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 OPR/43.0.2442.991
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
DNT: 1
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6

vaj4088:
The usual advice is to avoid the use of String on an Arduino.
Why so much code to check out an LED?
Most LEDs require a resistor. Where is yours?
Do you have the LED the right way around?

I need to use the string fro the read string part at the bottom to turn on the led and also basically i want to turn on a led fro my android in the end and to make my life easier i am using href so i just send the url and it turns on and off.

Yes it does work and in my serial it shows:

What part of that output shows that you get a ? followed by 0 or 1?

Why not do it the standard way and use two buttons (on the form)?

PaulS:
What part of that output shows that you get a ? followed by 0 or 1?

Why not do it the standard way and use two buttons (on the form)?

What do you mean?

What do you mean?

Do you have a clue what that code does? Did you even read it?

A GET request contains the name of a script to run (no name, in your case), followed by a ? and some name = value pairs, separated by &s. If there are no name = value pairs, there is no ?, either.

When the form has input items, of type submit, the browser will generate a GET request that contains the name and value of the button that was pressed.

When the form contains links, unless the link has a ? in it, the GET request will not contain a ?.

Your links have ?s in them, but the serial output does NOT show that one of the links was pressed.

Since the Arduino never got a GET request that contained a ?, it had no incentive to turn the LED on or off.

PaulS:
Do you have a clue what that code does? Did you even read it?

A GET request contains the name of a script to run (no name, in your case), followed by a ? and some name = value pairs, separated by &s. If there are no name = value pairs, there is no ?, either.

When the form has input items, of type submit, the browser will generate a GET request that contains the name and value of the button that was pressed.

When the form contains links, unless the link has a ? in it, the GET request will not contain a ?.

Your links have ?s in them, but the serial output does NOT show that one of the links was pressed.

Since the Arduino never got a GET request that contained a ?, it had no incentive to turn the LED on or off.

I know what it means, but I just didn't understand you. Now I know what to do. So I basically have to get the serial to detect the ?0 or ?1

So I basically have to get the serial to detect the ?0 or ?1

Well, the GET request doesn't come in the serial port, so, no.

You already have code to test for a ?0 or ?1 in the GET request. What you need to do is make the form generate the proper GET request.

Before you click the link in the browser, what is the URL being shown?
When you click the link in the browser, what does the URL in the address field become?

PaulS:
Before you click the link in the browser, what is the URL being shown?
When you click the link in the browser, what does the URL in the address field become?

When i click Turn Off LED, a /?0 shows in the URL and when i click Turn On LED, a /?1 shows in the URL!

    // give the web browser time to receive the data
    delay(1);

Rubbish!

    if (readString.indexOf("?0") >0){
               digitalWrite(9, LOW);
    }

What IS in readString at this point?

PaulS:

    // give the web browser time to receive the data

delay(1);



Rubbish!



if (readString.indexOf("?0") >0){
              digitalWrite(9, LOW);
    }



What IS in readString at this point?

Now i know what is wrong, readstring doesn't know what it is looking for. But how would i declare that?

readstring doesn't know what it is looking for.

readString isn't looking for anything. YOU are looking for something in readString, but apparently you haven't a clue what is in readString. Remedy that, pronto.

PaulS:
readString isn't looking for anything. YOU are looking for something in readString, but apparently you haven't a clue what is in readString. Remedy that, pronto.

So how would i get readString to find out if it is ?0 or ?1

So how would i get readString to find out if it is ?0 or ?1

You already get readString. What you now need to do is find out what is actually in readString.

   Serial.print("The GET request was: [");
   Serial.print(readString);
   Serial.println("]");

Put this after the end of the while(client.connected) body, before where you call indexOf() on the readString instance.

PaulS:

   Serial.print("The GET request was: [");

Serial.print(readString);
  Serial.println("]");




Put this after the end of the while(client.connected) body, before where you call indexOf() on the readString instance.

So i did that, and it just keeps printing 'The GET request was: '. What is wrong and how do it solve it?

I do not not think there is anything in readstring. But how would i do this?

DerpDiamonds:
I do not not think there is anything in readstring. But how would i do this?

Use input buttons on the form, like every one does.

PaulS:
Use input buttons on the form, like every one does.

What do you mean?

Add buttons to the form, instead of links.

Off
On