Program disconnects after each button pressed

Hi guys,

I need some help with my current project. I am using the arduino wifi shield and currently the program is not running well. It either disconnects after it exits the 'if' loop or it just repeats the previous command. What should i do make sure it does not disconnect after the "if" loop and that it does not executes the previous command?

A part of my program:

if (currentLine.endsWith("GET /H")) {

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, HIGH);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, HIGH);

delay(5000);

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, LOW);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, LOW);

digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);

delay(500);

command = '\0';
}//move forward

It still executes GET \H even after i press other buttons, is there any command i should add to prevent it from disconnecting? Please reply asap, thanks for your help!

You really need to post your whole program (in code tags) or it will be difficult to give any help. For instance, it is not possible to see which library or libraries you are using and how.

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

#define ENABLE1 3
#define ENABLE2 6
#define DIRB1 4
#define DIRA1 5
#define DIRA2 8
#define DIRB2 7

int c;
int a, b, d, e;
char command = ‘\0’;//additional
char ssid = “dd-wrt”; // your network SSID (name)
char pass = “secretPassword”; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
Serial.begin(9600); // initialize serial communication
pinMode(9, OUTPUT);
pinMode(ENABLE1,OUTPUT); // set the LED pin mode
pinMode(ENABLE2, OUTPUT);
pinMode(DIRA1, OUTPUT); // set the LED pin mode
pinMode(DIRA2,OUTPUT);
pinMode(DIRB1,OUTPUT);
pinMode(DIRB2, 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
}

// 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=”/H">here to move car forward
“);
client.print(“Click <a href=”/B”>here to move car backwards
“);
client.print(“Click <a href=”/L”>here to turn the car left
“);
client.print(“Click <a href=”/R”>here to turn the car right
");

// 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(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, HIGH);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, HIGH);

delay(5000);

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, LOW);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, LOW);

digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);

delay(500);

command = ‘\0’;
}//move forward

if (currentLine.endsWith(“GET /B”)) {
digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, HIGH);
digitalWrite(DIRB1, LOW);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, HIGH);
digitalWrite(DIRB2, LOW);

delay(5000);

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, LOW);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, LOW);

digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);

delay(500);

command = ‘\0’;
}//move car backwards

if (currentLine.endsWith(“GET /L”)) {
digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1,HIGH);
digitalWrite(DIRB1,LOW);
delay(500);
digitalWrite(ENABLE2,HIGH);
digitalWrite(DIRA2,LOW);
digitalWrite(DIRB2,HIGH);

delay(500);

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, LOW);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, LOW);

digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);

delay(500);

command = ‘\0’;
} //turn car left

if (currentLine.endsWith(“GET /R”)) {

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1,LOW);
digitalWrite(DIRB1,HIGH);

delay(500);

digitalWrite(ENABLE2,HIGH);
digitalWrite(DIRA2,HIGH);
digitalWrite(DIRB2,LOW);

delay(500);

digitalWrite(ENABLE1, HIGH);
digitalWrite(DIRA1, LOW);
digitalWrite(DIRB1, LOW);

digitalWrite(ENABLE2, HIGH);
digitalWrite(DIRA2, LOW);
digitalWrite(DIRB2, LOW);

digitalWrite(ENABLE1, LOW);
digitalWrite(ENABLE2, LOW);

delay(500);

command = ‘\0’;
}

}

}

// 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);
}

thanks for the reply, does this help?

It's not in code tags.

How to use this forum

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

#define ENABLE1 3
#define ENABLE2 6
#define DIRB1 4
#define DIRA1 5
#define DIRA2 8
#define DIRB2 7

 
int c;
int a, b, d, e;
char ssid[] = "dd-wrt";      //  your network SSID (name) 
char pass[] = "secretPassword";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);    // initialize serial communication
  pinMode(9, OUTPUT);
  pinMode(ENABLE1,OUTPUT);  // set the LED pin mode
  pinMode(ENABLE2, OUTPUT);
  pinMode(DIRA1, OUTPUT);      // set the LED pin mode
  pinMode(DIRA2,OUTPUT);
  pinMode(DIRB1,OUTPUT);
  pinMode(DIRB2, 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
  } 

  // 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=\"/H\">here</a> to move car forward
");
            client.print("Click <a href=\"/B\">here</a> to move car backwards
");
            client.print("Click <a href=\"/L\">here</a> to turn the car left
");
            client.print("Click <a href=\"/R\">here</a> to turn the car right
");
            
            // 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(ENABLE1, HIGH);
        digitalWrite(DIRA1, LOW);
        digitalWrite(DIRB1, HIGH);
        
        digitalWrite(ENABLE2, HIGH);
        digitalWrite(DIRA2, LOW);
        digitalWrite(DIRB2, HIGH);
       
        delay(5000);
      
       digitalWrite(ENABLE1, HIGH);
        digitalWrite(DIRA1, LOW);
        digitalWrite(DIRB1, LOW);
        
        digitalWrite(ENABLE2, HIGH);
        digitalWrite(DIRA2, LOW);
        digitalWrite(DIRB2, LOW);
        
        
        digitalWrite(ENABLE1, LOW);
        digitalWrite(ENABLE2, LOW);
        
        delay(500);
        
      }
       
      if (currentLine.endsWith("GET /B")) {
        digitalWrite(ENABLE1, HIGH);
        digitalWrite(DIRA1, HIGH);
        digitalWrite(DIRB1, LOW);
        
        digitalWrite(ENABLE2, HIGH);
        digitalWrite(DIRA2, HIGH);
        digitalWrite(DIRB2, LOW);
     
        delay(5000);
        
       digitalWrite(ENABLE1, HIGH);
        digitalWrite(DIRA1, LOW);
        digitalWrite(DIRB1, LOW);
        
        digitalWrite(ENABLE2, HIGH);
        digitalWrite(DIRA2, LOW);
        digitalWrite(DIRB2, LOW);
         
        
        digitalWrite(ENABLE1, LOW);
        digitalWrite(ENABLE2, LOW);
        
        delay(500);
        
        
        }//move car backwards

      if (currentLine.endsWith("GET /L")) {
          digitalWrite(ENABLE1, HIGH);
          digitalWrite(DIRA1,HIGH);
          digitalWrite(DIRB1,LOW); 
          delay(500);
          digitalWrite(ENABLE2,HIGH);
          digitalWrite(DIRA2,LOW);
          digitalWrite(DIRB2,HIGH);
       
         delay(500);
         
         digitalWrite(ENABLE1, HIGH);
        digitalWrite(DIRA1, LOW);
        digitalWrite(DIRB1, LOW);
        
        digitalWrite(ENABLE2, HIGH);
        digitalWrite(DIRA2, LOW);
        digitalWrite(DIRB2, LOW);
        
        
        digitalWrite(ENABLE1, LOW);
        digitalWrite(ENABLE2, LOW);
        
        delay(500);
        } //turn car left

      if (currentLine.endsWith("GET /R")) {
        
        digitalWrite(ENABLE1, HIGH);
          digitalWrite(DIRA1,LOW);
          digitalWrite(DIRB1,HIGH);
         
         delay(500); 
          
          digitalWrite(ENABLE2,HIGH);
          digitalWrite(DIRA2,HIGH);
          digitalWrite(DIRB2,LOW); 
       
          delay(500);
          
        digitalWrite(ENABLE1, HIGH);
        digitalWrite(DIRA1, LOW);
        digitalWrite(DIRB1, LOW);
        
        digitalWrite(ENABLE2, HIGH);
        digitalWrite(DIRA2, LOW);
        digitalWrite(DIRB2, LOW);
        
        
        digitalWrite(ENABLE1, LOW);
        digitalWrite(ENABLE2, LOW);
        
        delay(500);
      }
      
      
      }
       
        
       
             
        }
     
    // 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);
}

Sorry about that, new to the forum. Thanks a lot for your response by the way.

    String currentLine = "";                // make a String to hold incoming data from the client
...

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software