WiFi server programming

Hi All I’m facing a problem in controlling Remote control car by wifi , I have Arduino blackDimond WiFi , the program that i have can control 3 pins on and off , it works perfectly but when i want to add extra pin , by copy pasting a pin and change the variables , this code create wifi server and gives html code to open it via mobile to control the arduino and the car ,this is the code that i got ,

#include <WiServer.h>

#define WIRELESS_MODE_INFRA    1 
#define WIRELESS_MODE_ADHOC    2

// Wireless configuration parameters —————————————- 
unsigned char local_ip[]    = {192,168,0,100};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[]                 = {"ahmed"};   // max 32 bytes
unsigned char security_type = 0;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};	// max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = { 
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
};

// setup the wireless mode 
// infrastructure – connect to AP 
// adhoc – connect to another WiFi device 
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len; 
unsigned char security_passphrase_len; 
// End of wireless configuration parameters —————————————-

int redState = 0; 
int whiteState = 0; 
int greenState = 0;

int redPin = 5; 
int whitePin = 6; 
int greenPin = 7;

// This is our page serving function that generates web pages 
boolean sendMyPage(char* URL) { 
    if (strcmp(URL, "/red") == 0) redState = !redState; 
    if (strcmp(URL, "/white") == 0) whiteState = !whiteState; 
    if (strcmp(URL, "/green") == 0) greenState = !greenState; 
    digitalWrite(redPin, redState); 
    digitalWrite(whitePin, whiteState); 
    digitalWrite(greenPin, greenState); 
    // Check if the requested URL matches "/" 
   // if (strcmp(URL, "/") == 0) { 
        // Use WiServer’s print and println functions to write out the page content 
        WiServer.print("<html>"); 
        WiServer.print("Light Control

"); 
        printLightStatus("red", redState); 
        printLightStatus("white", whiteState); 
        printLightStatus("green", greenState); 
        WiServer.print("The page you requested was: "); 
        WiServer.print(URL); 
        WiServer.print("
The arduino has been running for: "); 
        WiServer.print(millis()); 
        WiServer.print(" milliseconds
"); 
        WiServer.print("</html>"); 
        // URL was recognized 
        return true; 
    //} 
    // URL not found 
    return false; 
}

void printLightStatus( String lightName, int lightState) { 
  WiServer.print(lightName); 
  WiServer.print(" Light is "); 
        if(lightState ==0) { 
            WiServer.print(" <b>off</b> <a href=/"); 
            WiServer.print(lightName); 
            WiServer.print(">Turn On</a>
"); 
        } else { 
          WiServer.print(" <b>on</b> <a href=/"); 
            WiServer.print(lightName); 
            WiServer.print(">Turn off</a>
"); 
        } 
}

void setup() { 
  // Initialize WiServer and have it use the sendMyPage function to serve pages 
  WiServer.init(sendMyPage); 
  // Enable Serial output and ask WiServer to generate log messages (optional) 
  Serial.begin(57600); 
  WiServer.enableVerboseMode(true); 
  pinMode(redPin, OUTPUT); 
  pinMode(whitePin, OUTPUT); 
  pinMode(greenPin, OUTPUT); 
}

void loop(){

  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}

and this is the code that i modified

/* 
* A simple sketch that uses WiServer to serve a web page 
*/

#include <WiServer.h>

#define WIRELESS_MODE_INFRA    1 
#define WIRELESS_MODE_ADHOC    2

// Wireless configuration parameters —————————————- 
unsigned char local_ip[]    = {192,168,0,100};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[]                 = {"ahmed"};   // max 32 bytes
unsigned char security_type = 0;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};	// max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = { 
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
};

// setup the wireless mode 
// infrastructure – connect to AP 
// adhoc – connect to another WiFi device 
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len; 
unsigned char security_passphrase_len; 
// End of wireless configuration parameters —————————————-

int fowrwardState = 0; 
int backwardState = 0; 
int rightState = 0;
int leftState = 0;

int  fowrwardPin = 4; 
int  backwardPin = 5; 
int rightPin = 6;
int leftPin = 7;
// This is our page serving function that generates web pages 
boolean sendMyPage(char* URL) { 
    if (strcmp(URL, "/fowrward") == 0) fowrwardState = !fowrwardState; 
    if (strcmp(URL, "/backward") == 0) backwardState = !backwardState; 
    if (strcmp(URL, "/right") == 0) rightState = !rightState; 
        if (strcmp(URL, "/left") == 0) leftState = !leftState; 
    digitalWrite(fowrwardPin, fowrwardState); 
    digitalWrite(backwardPin, backwardState); 
    digitalWrite(rightPin, rightState); 
     digitalWrite(leftPin, leftState); 
    // Check if the requested URL matches "/" 
   // if (strcmp(URL, "/") == 0) { 
        // Use WiServer’s print and println functions to write out the page content 
        WiServer.print("<html>"); 
        WiServer.print("WiFi RC 

"); 
        printcarStatus("fowrward", fowrwardState); 
                WiServer.print("------"); 
        printcarStatus("backward", backwardState); 
                        WiServer.print("------"); 
        printcarStatus("right", rightState); 
                        WiServer.print("------"); 
          printcarStatus("left", leftState); 
                          WiServer.print("------"); 
        WiServer.print("The page you requested was: "); 
        WiServer.print(URL); 
                WiServer.print("
Done by :Ahmed Alghasra "); 
        WiServer.print("
The arduino has been running for: "); 
        WiServer.print(millis()); 
        WiServer.print(" milliseconds
"); 
        WiServer.print("</html>"); 
        // URL was recognized 
        return true; 
    //} 
    // URL not found 
    return false; 
}

void printcarStatus( String carName, int carState) { 
  WiServer.print(carName); 
  WiServer.print(" car is "); 
        if(carState ==0) { 
            WiServer.print(" <b>off</b> <a href=/"); 
            WiServer.print(carName); 
            WiServer.print(">Turn On</a>
"); 
        } else { 
          WiServer.print(" <b>on</b> <a href=/"); 
            WiServer.print(carName); 
            WiServer.print(">Turn off</a>
"); 
        } 
}

void setup() { 
  // Initialize WiServer and have it use the sendMyPage function to serve pages 
  WiServer.init(sendMyPage); 
  // Enable Serial output and ask WiServer to generate log messages (optional) 
  Serial.begin(57600); 
  WiServer.enableVerboseMode(true); 
  pinMode(fowrwardPin, OUTPUT); 
  pinMode(backwardPin, OUTPUT); 
  pinMode(rightPin, OUTPUT); 
    pinMode(leftPin, OUTPUT); 
}

void loop(){

  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}

this is the code that i got

and this is the code that i modified

And the problem is?

Its not working propubly it just run the motor for one sec and stop !

Its not working propubly it just run the motor for one sec and stop !

When? When you first request the page, with no parameters, or when you submit the page?

If it happens when you submit the page, what are you setting/selecting on the page before submitting it?

It shouldn't be this difficult to get you to clearly define the problem you are having.

PaulS:

Its not working propubly it just run the motor for one sec and stop !

When? When you first request the page, with no parameters, or when you submit the page?

If it happens when you submit the page, what are you setting/selecting on the page before submitting it?

It shouldn't be this difficult to get you to clearly define the problem you are having.

No submitting the page work fine , click on the ip of the chip and is shows me the links , but if i click on any link "function " of the program , it runs for a sec and stop , it supposed to run until i stop it ,,,

click on the ip of the chip

Say what? If this means that you direct your browser to fetch the page that the Arduino hosts, say that.

and is shows me the link

Say what? If you mean that the Arduino serves up the original page, says so.

but if i click on any link "function " of the program

Describe that the page actually looks like, and describe what you do.

  Serial.begin(57600);

Nary a Serial.print() in sight, though. Why not?

PaulS:

click on the ip of the chip

Say what? If this means that you direct your browser to fetch the page that the Arduino hosts, say that.
yes i do

and is shows me the link

Say what? If you mean that the Arduino serves up the original page, says so.
yes your right

but if i click on any link "function " of the program

Describe that the page actually looks like, and describe what you do.
4 links ,
1- backward on/off
2- forward on/off
3- right on/off
4- left on/off

  Serial.begin(57600);

Nary a Serial.print() in sight, though. Why not?
its optional i dont use it any way

its optional i dont use it any way

It was a clue. Start using it. You can debug your program. Add a statement to setup(), so you can see when that runs. Add one to loop to show what you get from the client.

If you see the setup statement execute more than once, that tells you one thing. If it executes exactly once, that tells you something else.

PaulS:

its optional i dont use it any way

It was a clue. Start using it. You can debug your program. Add a statement to setup(), so you can see when that runs. Add one to loop to show what you get from the client.

If you see the setup statement execute more than once, that tells you one thing. If it executes exactly once, that tells you something else.

i tried what u said and that's the output on the serial this is the first code , which it works 100%

Server connected
Processing request for /red
TX 312 bytes
Server connection closed
Server connected
Processing request for /red
TX 312 bytes
Server connection closed
Server connected
Processing request for /green
TX 314 bytes
Server connection closed
Server connected
Processing request for /green
TX 314 bytes
Server connection closed
Server connected
Processing request for /white
TX 314 bytes
Server connection closed
Server connected
Processing request for /white
TX 314 bytes
Server connection closed
Server connected
Processing request for /red
TX 313 bytes
Server connection closed
Server connected
Processing request for /red
TX 313 bytes
Server connection closed
Server connected
Processing request for /red
TX 313 bytes
Server connection closed
Server connected
Processing request for /red
TX 313 bytes
Server connection closed
Server connected
Processing request for /green
TX 315 bytes
Server connection closed
Server connected
Processing request for /green
TX 315 bytes
Server connection closed

and this what i got with my modified code

0000Server connected
Processing request for /
TX 329 bytes
TX 91 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /right
TX 329 bytes
TX 96 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /fowrward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /backward
TX 329 bytes
TX 99 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed
Server connected
Processing request for /left
TX 329 bytes
TX 95 bytes
Server connection closed

So, you've learned that the Arduino is getting the proper request, and is able to determine what the user wanted done. That you don't then do the right thing ought to be easy enough to correct.

What, exactly, should the Arduino do when it gets a /fowrward request? What is it doing, instead?

PaulS:
So, you've learned that the Arduino is getting the proper request, and is able to determine what the user wanted done. That you don't then do the right thing ought to be easy enough to correct.

What, exactly, should the Arduino do when it gets a /fowrward request? What is it doing, instead?

/fowrward should give high voltage "5V" on Pin 4 , none stop
what it gives now it gives 5V for less than a sec and drop

Do the other commands behave incorrectly, to? That is, does /backward also run for only a second or less?

Does the state on the web page change to reflect the fact that the pin has shut off?

PaulS:
Do the other commands behave incorrectly, to? That is, does /backward also run for only a second or less?

Does the state on the web page change to reflect the fact that the pin has shut off?

no it reflect the command that i gave , so if i click on /backward the state shows on !

The best ratio of answers to questions is 1. Your ratio is something less than 0.5.

PaulS:
The best ratio of answers to questions is 1. Your ratio is something less than 0.5.

what do u mean ?

Do the other commands behave incorrectly, to? That is, does /backward also run for only a second or less?

Does the state on the web page change to reflect the fact that the pin has shut off?

Count them. Two questions.

no it reflect the command that i gave , so if i click on /backward the state shows on !

Count them. One answer.

The ratio of answers to questions is 0.5.

hey i almost solved half of the problem by changing this command " if (strcmp(URL, “/red”) == 0) redState = !redState; " with
if (strcmp(URL, “/red”) == 0) redState = HIGH ;

so now i found that the problem with this command !redState that it let it just work for one sec and close ,

now with this new code i can open the 4 ports but i can’t close any of then coz i removed this command !redState so what to do now

this is my code now

/* 
* A simple sketch that uses WiServer to serve a web page 
*/

#include <WiServer.h>

#define WIRELESS_MODE_INFRA    1 
#define WIRELESS_MODE_ADHOC    2

// Wireless configuration parameters —————————————- 
unsigned char local_ip[]    = {192,168,0,1};   // IP address of WiShield
unsigned char gateway_ip[]  = {192,168,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
char ssid[]                 = {"test"};   // max 32 bytes
unsigned char security_type = 0;               // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"12345678"};	// max 64 characters

// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = { 
	0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
};

// setup the wireless mode 
// infrastructure – connect to AP 
// adhoc – connect to another WiFi device 
unsigned char wireless_mode = WIRELESS_MODE_ADHOC;

unsigned char ssid_len; 
unsigned char security_passphrase_len; 
// End of wireless configuration parameters —————————————-
int wState = LOW  ;
int xState = LOW; 
int yState = LOW; 
int zState = LOW;

int wPin = 4; 
int xPin = 5; 
int yPin = 6; 
int zPin = 7; 

// This is our page serving function that generates web pages 
boolean sendMyPage(char* URL) { 
      if (strcmp(URL, "/w") == LOW) 
    
        if  (strcmp(URL, "/w") == HIG) 
      

      if (strcmp(URL, "/x") == LOW) xState = HIGH ;

      if (strcmp(URL, "/y") == LOW) yState = HIGH; 
           
       if (strcmp(URL, "/z") == LOW) zState = HIGH; 
          

    //if (strcmp(URL, "/green") == 0) greenState = !greenState; 
    digitalWrite(wPin, wState); 
    digitalWrite(xPin, xState); 
        digitalWrite(yPin, yState); 
        digitalWrite(zPin, zState); 

        
   // digitalWrite(greenPin, greenState); 
   
    // Check if the requested URL matches "/" 
   // if (strcmp(URL, "/") == 0) { 
        // Use WiServer’s print and println functions to write out the page content 
        WiServer.print("<html>"); 
        WiServer.print("Light Control

"); 
        printLightStatus("w", wState); 
        printLightStatus("x", xState); 
        printLightStatus("y", yState);
       printLightStatus("z", zState);  
      //  printLightStatus("green", greenState); 
        WiServer.print("The page you requested was: "); 
        WiServer.print(URL); 
        WiServer.print("
The arduino has been running for: "); 
        WiServer.print(millis()); 
        WiServer.print(" milliseconds
"); 
        WiServer.print("</html>"); 
        // URL was recognized 
        return true; 
    //} 
    // URL not found 
    return false; 
}

void printLightStatus( String lightName, int lightState) { 
  WiServer.print(lightName); 
  WiServer.print(" Light is "); 
        if(lightState ==LOW) { 
            WiServer.print(" <b>off</b> <a href=/"); 
            WiServer.print(lightName); 


            WiServer.print(">Turn On</a>
"); 
        } else { 
          WiServer.print(" <b>on</b> <a href=/"); 
            WiServer.print(lightName); 
            WiServer.print(">Turn off</a>
"); 
        } 
}

void setup() { 
  // Initialize WiServer and have it use the sendMyPage function to serve pages 
  WiServer.init(sendMyPage); 
  // Enable Serial output and ask WiServer to generate log messages (optional) 
  Serial.begin(57600); 
  WiServer.enableVerboseMode(true); 
  pinMode(wPin, OUTPUT); 
  pinMode(xPin, OUTPUT); 
    pinMode(yPin, OUTPUT); 
      pinMode(zPin, OUTPUT); 
//  pinMode(greenPin, OUTPUT); 
}

void loop(){

  // Run WiServer 
  WiServer.server_task(); 
  delay(10); 
}