I have been trying to get a WiServer sketch working, based on the example sketch, and those of others on this forum. The purpose of the sketch is to control a small robot which has two motors. It should display a web page with the following links layout.
FORWARD
LEFT STOP RIGHT
BACK
The STOP link should stop all motors instantly, and the other links increment or decrement the motor speed to achieve the result. ie. FORWARD LEFT RIGHT or BACK
The code I have been trying is as follows:-
- Code: Select all
/*
* A simple sketch that uses WiServer to serve a web page
*/
#include <WiServer.h> // uses pins 2, 10,11,12,13
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
/*
int leftPin = 3;
int rightPin = 4;
int forwardPin = 7;
int backPin = 6;
*/
int count = 0;
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = { 10,1,1,9}; // IP address of WiShield
unsigned char gateway_ip[] = { 10,1,1,6}; // router or gateway IP address
unsigned char subnet_mask[] = { 255,0,0,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = { "DLINK"}; // 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
// sample HEX 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 ----------------------------------------
// ------------------------------------------------------------------------------
#define SPEEDINCR 13 // Speed increment
int leftSpeed = 0; // Speed of the left wheel +255/-255 Full forward/ reverse
int rightSpeed = 0; // Speed of the right wheel +255/-255 Full forward/ reverse
// ------------------------------------------------------------------------------
void left_speed_changed() {
if (leftSpeed == 0 )
{ // stop
analogWrite(4, 0); //left back
delay(10);
analogWrite(5, 0); //left forward
}
else if (leftSpeed > 0)
{ // Forward speed
analogWrite(4, 0); //left back
delay(10);
analogWrite(5, abs(leftSpeed)); //left forward
}
else if (leftSpeed < 0)
{ // Backward
analogWrite(5, 0); //left forward
delay(10);
analogWrite(4, abs(leftSpeed)); //left back
}
}
void right_speed_changed() {
if (rightSpeed == 0 )
{ // stop
analogWrite(7, 0); //right back
delay(10);
analogWrite(6, 0); //right forward
}
else if (rightSpeed > 0)
{ // Forward speed
analogWrite(7, 0); //right back
delay(10);
analogWrite(6, abs(rightSpeed)); //right forward
}
else if (rightSpeed < 0)
{ // Backward
analogWrite(6, 0); //right forward
delay(10);
analogWrite(7, abs(rightSpeed)); //right back
}
}
// ------------------------------------------------------------------------------
// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {
// URL is a string containing the request part of the URL.
// ie. everything including and after the first '/'.
Serial.println("SendMyPage_URL=");
Serial.println(URL);
Serial.println("SendMyPage_URL");
// Check if the requested URL matches "/" Default case.
if (strcmp(URL, "/") == 0) {
Serial.println("!!!!! Default request. !!!!!!");
links ();
return true; // URL was recognized. Default case.
} else if (strcmp(URL, "/left") == 0) {
Serial.println("Left");
if ( ((leftSpeed - SPEEDINCR) < -255) )
{ // Left speed full, Increase right speed.
rightSpeed += SPEEDINCR;
right_speed_changed();
}
else
{ // Decrease left speed.
leftSpeed -= SPEEDINCR;
left_speed_changed();
}
links ();
return true; // URL was recognized
} else if (strcmp(URL, "/right") == 0) {
Serial.println("right");
if ( ((rightSpeed - SPEEDINCR) < -255) )
{
leftSpeed += SPEEDINCR;
left_speed_changed();
}
else
{
rightSpeed -= SPEEDINCR;
right_speed_changed();
}
links ();
return true; // URL was recognized
} else if (strcmp(URL, "/forward") == 0) { // Go forward.
Serial.println("forward");
// Increment both right and left if not already full speed backward. -255
if ( ((leftSpeed + SPEEDINCR) > 255) || ((rightSpeed + SPEEDINCR) > 255) )
{ // Full speed. Do nothing.
Serial.println("Full speed Forward");
}
else
{
leftSpeed += SPEEDINCR;
rightSpeed += SPEEDINCR;
left_speed_changed();
right_speed_changed();
}
links ();
return true; // URL was recognized
} else if (strcmp(URL, "/back") == 0) { // Go backward.
Serial.println("back");
// Decrement both left and right, if not already full speed forward. -255.
if ( ((leftSpeed - SPEEDINCR) < -255) || ((rightSpeed - SPEEDINCR) < -255) )
{ // Full speed. Do nothing.
Serial.println("Full speed Backward");
}
else
{
leftSpeed -= SPEEDINCR;
rightSpeed -= SPEEDINCR;
left_speed_changed();
right_speed_changed();
}
links ();
return true; // URL was recognized
} else if (strcmp(URL, "/stop") == 0) {
Serial.println("stop");
leftSpeed = 0;
rightSpeed = 0;
left_speed_changed();
right_speed_changed();
links ();
return true; // URL was recognized
}
return false; // URL not found.. !!!!!!!!!!!!
}
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
// <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-STORE">
// <META HTTP-EQUIV="EXPIRES" CONTENT="Mon, 22 Jul 2002 11:12:01 GMT">
// <META HTTP-EQUIV="EXPIRES" CONTENT=-1>
// <META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
//PRAGMA:NO-CACHE and CACHE-CONTROL:NO-CACHE
//Cache-Control: no-store
//Pragma: no-cache
//Expires: Thu, 01 Dec 1994 16:00:00 G
//<html><center>[ <a href="/forward">FORWARD</a> ]<br />[ <a href="/left">LEFT</a> ] [ <a href="/straight">STRAIGHT</a> ] [ <a href="/right">RIGHT</a> ]<br />[ <a href="/stop">STOP</a> ]<br/>[ <a href="/back">BACK</a> ]<br/>[ <a href="/releasecontrol">RELEASE CONTROL</a> ]<br/><center/></html>
void links () {
WiServer.print("<head>");
WiServer.print("<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\">");
WiServer.print("<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-STORE\">");
WiServer.print("<META HTTP-EQUIV=\"EXPIRES\" CONTENT=\"Mon, 22 Jul 2002 11:12:01 GMT\">");
WiServer.print("<META HTTP-EQUIV=\"EXPIRES\" CONTENT=-1>");
WiServer.print("<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">");
WiServer.print("</head>");
WiServer.print("<html><center>");
WiServer.print("[ <a href=\"/forward\">FORWARD</a> ]<br />");
WiServer.print("[ <a href=\"/left\">LEFT</a> ] ");
WiServer.print("[ <a href=\"/stop\">STOP</a> ] ");
WiServer.print("[ <a href=\"/right\">RIGHT</a> ]<br />");
WiServer.print("[ <a href=\"/back\">BACK</a> ]");
WiServer.print("</center><br />");
WiServer.print("<br />");
WiServer.print(" left speed ");
WiServer.print(leftSpeed);
WiServer.print("<br />");
WiServer.print(" right speed ");
WiServer.print(rightSpeed);
WiServer.print("<br />");
WiServer.print("<html><center>");
WiServer.print("[ <a href=\"/forward\">FORWARD</a> ]<br />");
WiServer.print("[ <a href=\"/left\">LEFT</a> ] ");
WiServer.print("[ <a href=\"/stop\">STOP</a> ] ");
WiServer.print("[ <a href=\"/right\">RIGHT</a> ]<br />");
WiServer.print("[ <a href=\"/back\">BACK</a> ]");
WiServer.print("</center><br />");
WiServer.print("<br />");
WiServer.print("<br />");
WiServer.print("</html>");
}
// ------------------------------------------------------------------------------
void setup() {
// Initialize WiServer and have it use the sendMyPage function to serve pages
WiServer.init(sendMyPage);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
// Enable Serial output and ask WiServer to generate log messages (optional)
Serial.begin(57600);
WiServer.enableVerboseMode(true);
}
// ------------------------------------------------------------------------------
void loop(){
// Run WiServer
WiServer.server_task();
}
// ------------------------------------------------------------------------------
This code, so far, has proven to be very unreliable. After uploading or power on, the red connection light comes on after about 15 seconds, indicating that it has found and connected to my D-Link DSL-G604T wireless router. (This router has been flashed with the latest firmware. It was very unreliable before that, but has not missed a beet since then.)
When I type in the routers ip address (10.1.1.9) I get the web page I expect, with the links for FORWARD LEFT STOP RIGHT and BACK. I have also included 'left speed' and 'right speed' to help debugging. A number of serial print statment have been included for debuging also.
The problems start when I click on the links. Some times it does the update and some times I have to click the link two or three times before anything changes on the screen. It is almost like something is cacheing the input, and only outputing after the buffer if full enough.
I have a previous version of this project working using a wired internet connection and the standard ethernet library. Is there a version of this library for the WiShield. If not I would like to suggest it as a good idea.
I have tried every thing I can think of without success. I need help.
Regards
Robert Walker