|
| 1 | +// Includes |
| 2 | +#include <WiServer.h> |
| 3 | +#include <string.h> |
| 4 | +#include <Servo.h> |
| 5 | + |
| 6 | +// Wireless configuration parameters |
| 7 | +unsigned char local_ip[] = { 192,168,0,10 }; // IP address of WiShield |
| 8 | +unsigned char gateway_ip[] = { 192.168,0,1 }; // router or gateway IP address |
| 9 | +unsigned char subnet_mask[] = { 255,255,255,0 }; // subnet mask for the local network |
| 10 | +char ssid[] = {"Robot"}; // max 32 bytes |
| 11 | +unsigned char security_type = 0; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 |
| 12 | + |
| 13 | +// WPA/WPA2 passphrase (UNUSED) |
| 14 | +const char PROGMEM security_passphrase[] = { "12345678"}; // max 64 characters |
| 15 | + |
| 16 | +// WEP 128-bit keys (UNUSED) |
| 17 | +//const unsigned char PROGMEM wep_keys[] = { |
| 18 | +// 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0 |
| 19 | +// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1 |
| 20 | +// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2 |
| 21 | +// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3 |
| 22 | +//}; |
| 23 | +const unsigned char PROGMEM wep_keys[] = {}; |
| 24 | + |
| 25 | +// Wireless mode |
| 26 | +#define WIRELESS_MODE_INFRA 1 |
| 27 | +#define WIRELESS_MODE_ADHOC 2 |
| 28 | +unsigned char wireless_mode = WIRELESS_MODE_INFRA; |
| 29 | + |
| 30 | +// Required for the WiShiled library |
| 31 | +unsigned char ssid_len; |
| 32 | +unsigned char security_passphrase_len; |
| 33 | + |
| 34 | +//-- Constants for the right motor |
| 35 | +const int MOTOR_RIGHT = 5; |
| 36 | +const int MOTOR_RIGHT_FW = 180; //-- Move the right motor forward |
| 37 | +const int MOTOR_RIGHT_BW = 0; //-- Move the right motor backward |
| 38 | +const int MOTOR_RIGHT_STOP = 90; //-- Stop the right motor |
| 39 | + |
| 40 | +//-- Constants for the left motor |
| 41 | +const int MOTOR_LEFT = 6; |
| 42 | +const int MOTOR_LEFT_FW = 0; //-- Move the left motor forward |
| 43 | +const int MOTOR_LEFT_BW = 180; //-- Move the left motor backward |
| 44 | +const int MOTOR_LEFT_STOP = 90; //-- Stop the left motor |
| 45 | + |
| 46 | +bool strobe = 0; |
| 47 | +bool strobe_state = 0; |
| 48 | +unsigned char previous_movement=5; |
| 49 | + |
| 50 | +//-- The two motors are servos that can turn freely |
| 51 | +Servo rm; //-- Right motor |
| 52 | +Servo lm; //-- Left motor |
| 53 | + |
| 54 | +void setup() |
| 55 | +{ |
| 56 | + WiServer.init(sendPage); |
| 57 | + rm.attach(MOTOR_RIGHT); |
| 58 | + lm.attach(MOTOR_LEFT); |
| 59 | + pinMode(4, OUTPUT); |
| 60 | + pinMode(7, OUTPUT); |
| 61 | + pinMode(8, OUTPUT); |
| 62 | +} |
| 63 | + |
| 64 | +void loop() |
| 65 | +{ |
| 66 | + |
| 67 | + // Run WiServer |
| 68 | + if(strobe) |
| 69 | + { |
| 70 | + if(strobe_state) |
| 71 | + { |
| 72 | + digitalWrite(4, LOW); |
| 73 | + strobe_state=0; |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + digitalWrite(4, HIGH); |
| 78 | + strobe_state=1; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + for(unsigned char i=0; i<10 ; i++) |
| 83 | + { |
| 84 | + WiServer.server_task(); |
| 85 | + delay(5); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +} |
| 91 | + |
| 92 | +void robot_action(unsigned char movement) |
| 93 | +{ |
| 94 | + //movement -> 1 = Front |
| 95 | + // 2 = Back |
| 96 | + // 3 = Right |
| 97 | + // 4 = Left |
| 98 | + // 5 = Stop |
| 99 | + // 6 = Light on |
| 100 | + // 7 = Light off |
| 101 | + // 8 = Light Strobe |
| 102 | + // 9 = Eyes on |
| 103 | + // 10 = Eyes off |
| 104 | + |
| 105 | + switch(movement) |
| 106 | + { |
| 107 | + case 1: |
| 108 | + rm.write(MOTOR_RIGHT_FW); |
| 109 | + lm.write(MOTOR_LEFT_FW); |
| 110 | + break; |
| 111 | + case 2: |
| 112 | + rm.write(MOTOR_RIGHT_BW); |
| 113 | + lm.write(MOTOR_LEFT_BW); |
| 114 | + break; |
| 115 | + case 3: |
| 116 | + lm.write(MOTOR_LEFT_FW); |
| 117 | + rm.write(MOTOR_RIGHT_BW); |
| 118 | + break; |
| 119 | + case 4: |
| 120 | + lm.write(MOTOR_LEFT_BW); |
| 121 | + rm.write(MOTOR_RIGHT_FW); |
| 122 | + break; |
| 123 | + case 5: |
| 124 | + rm.write(MOTOR_RIGHT_STOP); |
| 125 | + lm.write(MOTOR_LEFT_STOP); |
| 126 | + break; |
| 127 | + case 6: |
| 128 | + digitalWrite(4, HIGH); |
| 129 | + break; |
| 130 | + case 7: |
| 131 | + digitalWrite(4, LOW); |
| 132 | + strobe = 0; |
| 133 | + break; |
| 134 | + case 8: |
| 135 | + strobe = 1; |
| 136 | + break; |
| 137 | + case 9: |
| 138 | + digitalWrite(7, HIGH); |
| 139 | + digitalWrite(8, HIGH); |
| 140 | + break; |
| 141 | + case 10: |
| 142 | + digitalWrite(7, LOW); |
| 143 | + digitalWrite(8, LOW); |
| 144 | + break; |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +void OurPage(void) |
| 152 | +{ |
| 153 | + WiServer.print("<html><head><title>Robot</title></head>"); |
| 154 | + |
| 155 | + WiServer.print("<body><center>Control !<center>\n<center>"); |
| 156 | + |
| 157 | + WiServer.print("<a href=?Front>Front</a></br>"); |
| 158 | + WiServer.print("<a href=?Back>Back</a></br>"); |
| 159 | + WiServer.print("<a href=?Right>Right</a></br>"); |
| 160 | + WiServer.print("<a href=?Left>Left</a></br>"); |
| 161 | + WiServer.print("<a href=?Stop>Stop</a></br>"); |
| 162 | + WiServer.print("<a href=?LightOn>Light On</a></br>"); |
| 163 | + WiServer.print("<a href=?LightOff>Light Off</a></br>"); |
| 164 | + WiServer.print("<a href=?EyesOn>Eyes On</a></br>"); |
| 165 | + WiServer.print("<a href=?EyesOff>Eyes Off</a></br>"); |
| 166 | + WiServer.print("<a href=?LightStrobe>Light Strobe</a></br>"); |
| 167 | + |
| 168 | + WiServer.print("</html> "); |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +// This is our page serving function that generates web pages |
| 173 | +boolean sendPage(char* URL_Received) |
| 174 | +{ |
| 175 | + String URL_String = URL_Received; // Just convert the chat* receivedto a String object |
| 176 | + |
| 177 | + if (URL_String == "/?Front") |
| 178 | + { |
| 179 | + robot_action(1); |
| 180 | + previous_movement=1; |
| 181 | + } |
| 182 | + else if(URL_String == "/?Back") |
| 183 | + { |
| 184 | + robot_action(2); |
| 185 | + previous_movement=2; |
| 186 | + } |
| 187 | + else if(URL_String == "/?Right") |
| 188 | + { |
| 189 | + robot_action(3); |
| 190 | + delay(100); |
| 191 | + robot_action(previous_movement); |
| 192 | + } |
| 193 | + else if(URL_String == "/?Left") |
| 194 | + { |
| 195 | + robot_action(4); |
| 196 | + delay(100); |
| 197 | + robot_action(previous_movement); |
| 198 | + } |
| 199 | + else if(URL_String == "/?Stop") |
| 200 | + { |
| 201 | + robot_action(5); |
| 202 | + previous_movement=5; |
| 203 | + } |
| 204 | + else if(URL_String == "/?LightOn") |
| 205 | + { |
| 206 | + robot_action(6); |
| 207 | + } |
| 208 | + else if(URL_String == "/?LightOff") |
| 209 | + { |
| 210 | + robot_action(7); |
| 211 | + } |
| 212 | + else if(URL_String == "/?LightStrobe") |
| 213 | + { |
| 214 | + robot_action(8); |
| 215 | + } |
| 216 | + else if(URL_String == "/?EyesOn") |
| 217 | + { |
| 218 | + robot_action(9); |
| 219 | + } |
| 220 | + else if(URL_String == "/?EyesOff") |
| 221 | + { |
| 222 | + robot_action(10); |
| 223 | + } |
| 224 | + else if(URL_String.substring(0,8) == "/?Degree=") |
| 225 | + { |
| 226 | + |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + // unknown URL |
| 231 | + } |
| 232 | + |
| 233 | + OurPage(); |
| 234 | + |
| 235 | + return true; |
| 236 | +} |
0 commit comments