HINT!
To all those who try it: Do not drive it into a dark room, it is dark in there, and there are a pretty good chance that you can't find the way out again.
My WiFi tank is now complete enough to be driven around by visitors, but there are still improvements to come. Here is how far I am with it.
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
//PWM output to each motor
int lM = 120;
int rM = 120;
//H-Bridge pins
int EA = 3;
int EB = 4;
int I1 = 41;
int I2 = 40;
int I3 = 39;
int I4 = 38;
//Proximity sensor pins
int pFl = 37; //Front left
int pFr = 36; //Front right
//Battery info
int bV = 15; //Battery voltage
int bA = 13; //Battery voltage
//Relays
int kS = 35; //Kill switch
//Input pins
int iPins[] = {
pFr, pFl};
//Output pins
int oPins[] = {
kS, I1, I2, I3, I4};
//Used to make the tank deny all movements
boolean online = true;
//Used for the proximity sensors, to be able to move away from the obstacle
int lastMove = 0; //1 = forwards - 2 = backwards
//Millis to calculate when to call the stopS() function
unsigned long stopMove = 0;
//Milliseconds it should move
int stopTime = 0;
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {
192,168,1,15}; // IP address of WiShield
unsigned char gateway_ip[] = {
192,168,1,1}; // router or gateway IP address
unsigned char subnet_mask[] = {
255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {
"Captain Slow"}; // max 32 bytes
unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {
"xxxxxxxxxxxxxx"}; // 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 ----------------------------------------
// This is our page serving function that generates web pages and handles requests
boolean sendMyPage(char* URL)
{
if (strcmp(URL, "/F1") == 0)
{
stopTime = 750;
stopMove = millis();
moveF();
}
if (strcmp(URL, "/F2") == 0)
{
stopTime = 375;
stopMove = millis();
moveF();
}
else if (strcmp(URL, "/B1") == 0)
{
stopTime = 750;
stopMove = millis();
moveB();
}
else if (strcmp(URL, "/B2") == 0)
{
stopTime = 375;
stopMove = millis();
moveB();
}
else if (strcmp(URL, "/L3") == 0)
{
stopTime = 300;
stopMove = millis();
moveL();
}
else if (strcmp(URL, "/L2") == 0)
{
stopTime = 200;
stopMove = millis();
moveL();
}
else if (strcmp(URL, "/L1") == 0)
{
stopTime = 100;
stopMove = millis();
moveL();
}
else if (strcmp(URL, "/R3") == 0)
{
stopTime = 300;
stopMove = millis();
moveR();
}
else if (strcmp(URL, "/R2") == 0)
{
stopTime = 200;
stopMove = millis();
moveR();
}
else if (strcmp(URL, "/R1") == 0)
{
stopTime = 100;
stopMove = millis();
moveR();
}
double volts = map(analogRead(bV), 0, 1023, 0, 1933) / 100.0;
double amps = map(analogRead(bA), 512, 1023, 0, 500) / 100.0;
if (amps < 0.01)
{
amps = 0.01;
}
// Use WiServer's print and println functions to write out the page content
WiServer.print(volts);
WiServer.print("|");
WiServer.print(amps);
return true;
}
void moveF() //Forwards
{
if (digitalRead(pFl) == HIGH && digitalRead(pFr) == HIGH)
{
lastMove = 1;
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
}
void moveB() //Backwards
{
lastMove = 2;
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
void moveL() //Left
{
if (digitalRead(pFl) == HIGH)
{
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, HIGH);
digitalWrite(I2, LOW);
digitalWrite(I3, HIGH);
digitalWrite(I4, LOW);
}
}
void moveR() //Right
{
if (digitalRead(pFr) == HIGH)
{
analogWrite(EA, lM);
analogWrite(EB, rM);
digitalWrite(I1, LOW);
digitalWrite(I2, HIGH);
digitalWrite(I3, LOW);
digitalWrite(I4, HIGH);
}
}
void moveS() //Stop
{
lastMove = 0;
stopMove = 0;
analogWrite(EA, 0);
analogWrite(EB, 0);
digitalWrite(hI1, HIGH);
digitalWrite(hI2, HIGH);
digitalWrite(hI3, HIGH);
digitalWrite(hI4, HIGH);
}
void moveES() //Emergency Stop
{
moveB();
delay(50);
moveF();
delay(50);
moveS();
}
void cutPower() //Pull the latching relay to turn everything off
{
digitalWrite(kS, HIGH);
delay(100);
digitalWrite(kS, LOW);
}
//IP of my NAS
uint8 ip[] = {
192,168,1,245};
// Function to print response from the web server
void printData(char* data, int len)
{
}
//Define the request
GETrequest getServer(ip, 80, "192.168.1.245", "");
void setup()
{
pinMode(41, OUTPUT);
//Set all output pins
for(int i=0; i < sizeof(oPins) / sizeof(oPins[0]); i++)
{
pinMode(oPins[i], OUTPUT);
}
//Set all input pins
for(int i=0; i < sizeof(iPins) / sizeof(iPins[0]); i++)
{
pinMode(iPins[i], INPUT);
}
//Reset H-Bridge
moveS();
//Prepare WiShield
getServer.setReturnFunc(printData);
WiServer.init(sendMyPage);
}
void loop()
{
WiServer.server_task();
double volts = map(analogRead(bV), 0, 1023, 0, 1933) / 100.0;
if (lastMove == 1 && (digitalRead(pFl) == LOW || digitalRead(pFr) == LOW))
{
moveES();
}
if ((millis() - stopMove >= stopTime || volts <= 7) && stopMove != 0)
{
moveS();
}
//If not online, pull the latching relay to disconnect from battery
if (!online)
{
delay(250);
cutPower();
delay(250);
return;
}
//If voltage is too low, call the web server to flag the tank as offline
if (volts <= 7)
{
getServer.setURL("/arduino/control/call.php?status=nopower");
getServer.submit();
//Set to offline, to no more commands will be possible
online = false;
return;
}
}
Try it here: http://bld.is-a-geek.com/wifitank/ (only when I am home so far)
Latest picture from it: