The code will not turn relay on and off with the buttons

I am trying to make it where they have on and off buttons to turn relays on and off, but it is not turning off and on when i click it. I NEED HELP!! PLEASE
CODE:

#include <SPI.h>
#include <WiFi.h>

char ssid = “2WIRE839”; // your network SSID (name)
char pass = “1112142651”; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

#define RELAY1 7
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(RELAY1, OUTPUT); // set the RELAY mode

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
while (true); // don’t continue
}

String fv = WiFi.firmwareVersion();
if ( fv != “1.1.0” )
Serial.println(“Please upgrade the firmware”);

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you’re connected now, so print out the status
}

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

if (client) { // if you get a client,
Serial.println(“new client”); // print a message out the serial port
String currentLine = “”; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client’s connected
if (client.available()) { // if there’s bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == ‘\n’) { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.
// that’s the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what’s coming, then a blank line:
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.print(“Click <a href=”/L">here turn the Light on RELAY1 on
“);
client.print(“Click <a href=”/H”>here turn the Light on RELAY1 off
");

// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
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 to see if the client request was “GET /H” or “GET /L”:
if (currentLine.endsWith(“GET /H”)) {
digitalWrite(RELAY1, HIGH); // GET /L turns the LED on
}
if (currentLine.endsWith(“GET /L”)) {
digitalWrite(RELAY1, LOW); // GET /H turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println(“client disonnected”);
}
}

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print(“To see this page in action, open a browser to http://”);
Serial.println(ip);
}

7 below:

http://forum.arduino.cc/index.php/topic,148850.0.html

[code]/*
  WiFi Web Server LED Blink

 A simple web server that lets you blink an LED via the web.
 This sketch will print the IP address of your WiFi Shield (once connected)
 to the Serial monitor. From there, you can open that address in a web browser
 to turn on and off the LED on pin 9.

 If the IP address of your shield is yourAddress:
 http://yourAddress/H turns the LED on
 http://yourAddress/L turns it off

 This example is written for a network using WPA encryption. For
 WEP or WPA, change the Wifi.begin() call accordingly.

 Circuit:
 * WiFi shield attached
 * LED attached to pin 9

 created 25 Nov 2012
 by Tom Igoe
 */
#include <SPI.h>
#include <WiFi.h>

char ssid[] = "2WIRE839";      //  your network SSID (name)
char pass[] = "1112142651";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

#define RELAY1 7
#define RELAY2 6
#define RELAY3 5
#define RELAY4 3
void setup() {
  Serial.begin(9600);      // initialize serial communication
  pinMode(RELAY1, OUTPUT);  // set the RELAY mode
  pinMode(RELAY2, OUTPUT);
  pinMode(RELAY3, OUTPUT);
  pinMode(RELAY4, OUTPUT);

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true);       // don't continue
  }

  String fv = WiFi.firmwareVersion();
  if ( fv != "1.1.0" )
    Serial.println("Please upgrade the firmware");

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


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

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            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.print("Click <a href=/?on7 >7 ON</a> turn the Light on RELAY1 on
");
            client.print("Click <a href=\?off7 >7 OFF</a> turn the Light on RELAY1 off
");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          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 to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(RELAY1, HIGH);               // GET /L turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(RELAY1, LOW);                // GET /H turns the LED off
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}

[/code]

You might try the bottom approach as the GET request line might actually end with a carriage return or line feed instead of an H or L.

// Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(RELAY1, HIGH);               // GET /L turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(RELAY1, LOW);                // GET /H turns the LED off
        }
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input

You might also try printing currentLine to see EXACTLY what it does contain.

Serial.print("The dumb client sent: [");
Serial.print(currentLine);
Serial.print("]");

The dumb client sent: [GET /H] will tell you one thing.

The dumb client sent: [GET /L ] will tell you something else.

zoomkat: You might try the bottom approach as the GET request line might actually end with a carriage return or line feed instead of an H or L.

// Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(RELAY1, HIGH);               // GET /L turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(RELAY1, LOW);                // GET /H turns the LED off
        }
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input

I did that but i get these errors. How do you fix it?

/Users/TaylorRichard/Documents/Arduino/SimpleWebServerWiFi/SimpleWebServerWiFi.ino: In function 'void loop()': SimpleWebServerWiFi:109: error: 'readString' was not declared in this scope if(readString.indexOf("on") >=0) ^ SimpleWebServerWiFi:114: error: 'readString' was not declared in this scope if(readString.indexOf("off") >=0) ^ SimpleWebServerWiFi:119: error: 'readString' was not declared in this scope readString=""; //clears variable for new input ^ exit status 1 'readString' was not declared in this scope

"The bottom APPROACH" is not the same as "the exact code in the bottom window".

taylorbryan97:
I am trying to make it where they have on and off buttons to turn relays on and off, but it is not turning off and on when i click it. I NEED HELP!! PLEASE
CODE:

#include <SPI.h>
#include <WiFi.h>

char ssid = “2WIRE839”; // your network SSID (name)
char pass = “1112142651”; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

#define RELAY1 7
void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(RELAY1, OUTPUT); // set the RELAY mode

// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println(“WiFi shield not present”);
while (true); // don’t continue
}

String fv = WiFi.firmwareVersion();
if ( fv != “1.1.0” )
Serial.println(“Please upgrade the firmware”);

// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you’re connected now, so print out the status
}

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

if (client) { // if you get a client,
Serial.println(“new client”); // print a message out the serial port
String currentLine = “”; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client’s connected
if (client.available()) { // if there’s bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
if (c == ‘\n’) { // if the byte is a newline character

// if the current line is blank, you got two newline characters in a row.
// that’s the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what’s coming, then a blank line:
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.print(“Click <a href=”/L">here turn the Light on RELAY1 on
“);
client.print(“Click <a href=”/H”>here turn the Light on RELAY1 off
");

// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
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 to see if the client request was “GET /H” or “GET /L”:
if (currentLine.endsWith(“GET /H”)) {
digitalWrite(RELAY1, HIGH); // GET /L turns the LED on
}
if (currentLine.endsWith(“GET /L”)) {
digitalWrite(RELAY1, LOW); // GET /H turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println(“client disonnected”);
}
}

void printWifiStatus() {
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print(“signal strength (RSSI):”);
Serial.print(rssi);
Serial.println(" dBm");
// print where to go in a browser:
Serial.print(“To see this page in action, open a browser to http://”);
Serial.println(ip);
}

May I make a suggestion?
Instead of currentLine.endsWith use substring method.

You are looking for specific string of characters and you probably do no need to know if they are at the end and / or if the terminating character was actually received.

Vaclav: May I make a suggestion? Instead of currentLine.endsWith use substring method.

You are looking for specific string of characters and you probably do no need to know if they are at the end and / or if the terminating character was actually received.

How would I use the substring method? I don't understand what I need to change in the code to make it using substring method.

Just replace .endsWith with .substring. See Reference. Be aware that substring returns the actual string found , not just true / false. So you will have to make the turn on / off relay by analyzing / comparing the returned string.

in pseudocode

find High = "GET /H" find LOW = "GET /L"

String found = currentLine.substring( find HIGH or find LOW );

not sure if substring can take compound condition , needs to be tested

if( found == find HIGH) set relay else if ( found == find LOW) reset relay else error - did not find either value