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