Can Kocak / Mbed 2 deprecated FRDM-KL25Z_WiFly_Access_Point_Mode_Zumo_RC

Dependencies:   mbed

Committer:
can90
Date:
Sat Jun 22 22:00:43 2013 +0000
Revision:
0:414ffb85772d
FRDM-KL25Z WiFly Access Point iPhone Android Controlled Zumo RC Tracked Vehicle mbed Code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
can90 0:414ffb85772d 1 #include "mbed.h"
can90 0:414ffb85772d 2 #include "Wifly.h"
can90 0:414ffb85772d 3
can90 0:414ffb85772d 4 //wifly instance is created with no security, without ssid and password
can90 0:414ffb85772d 5 Wifly wifly(PTD3, PTD2, PTC8, PTC7, NULL, NULL, NONE);
can90 0:414ffb85772d 6
can90 0:414ffb85772d 7 Serial pc(USBTX,USBRX);
can90 0:414ffb85772d 8
can90 0:414ffb85772d 9 //motor control pins
can90 0:414ffb85772d 10 DigitalOut dirRight(PTC9);
can90 0:414ffb85772d 11 DigitalOut dirLeft(PTA13);
can90 0:414ffb85772d 12 PwmOut pwmRight(PTD5);
can90 0:414ffb85772d 13 PwmOut pwmLeft(PTD0);
can90 0:414ffb85772d 14
can90 0:414ffb85772d 15 //LED control pin. LED is located on Zumo Shield
can90 0:414ffb85772d 16 DigitalOut led(PTD1);
can90 0:414ffb85772d 17
can90 0:414ffb85772d 18 //append function appends a char to
can90 0:414ffb85772d 19 void append(char* s, char c);
can90 0:414ffb85772d 20
can90 0:414ffb85772d 21 //send function sends the given char* to wifly
can90 0:414ffb85772d 22 void sendString(char* str);
can90 0:414ffb85772d 23
can90 0:414ffb85772d 24 int main() {
can90 0:414ffb85772d 25 //factory reset Wifly
can90 0:414ffb85772d 26 wifly.reset();
can90 0:414ffb85772d 27 pc.printf("\nFactory Reset\n");
can90 0:414ffb85772d 28 //reboot wifly
can90 0:414ffb85772d 29 bool success = wifly.reboot();
can90 0:414ffb85772d 30 pc.printf("Reboot: %d\n", success); //success = 1 -> successful process
can90 0:414ffb85772d 31 //set TCP Listen port that will be used to 2000. You can set this to your preferred port number.
can90 0:414ffb85772d 32 success = wifly.sendCommand("set ip local 2000\r", "AOK");
can90 0:414ffb85772d 33 pc.printf("Local = 2000: %d\n", success); //success = 1 -> successful process
can90 0:414ffb85772d 34 //make WiFly Module work as an Access Point
can90 0:414ffb85772d 35 success = wifly.sendCommand("apmode\r", "AP mode");
can90 0:414ffb85772d 36 pc.printf("AP Mode: %d\n", success); //success = 1 -> successful process
can90 0:414ffb85772d 37
can90 0:414ffb85772d 38 char str[15] = "";
can90 0:414ffb85772d 39 int count = 0; //count will be used to count characters after finding 'L' for "Listen"
can90 0:414ffb85772d 40 bool foundL = false; //will be made true when wifly sends 'L' character to mbed from its serial port
can90 0:414ffb85772d 41 bool foundO = false; //will be made true when wifly sends 'O' character to mbed from its serial port
can90 0:414ffb85772d 42
can90 0:414ffb85772d 43 /*
can90 0:414ffb85772d 44 After this state, the code will monitor input characters from WiFly and executes some code
can90 0:414ffb85772d 45 after finding "Listen" or "OPEN" keywords
can90 0:414ffb85772d 46 */
can90 0:414ffb85772d 47
can90 0:414ffb85772d 48 while(true) {
can90 0:414ffb85772d 49 //get current character from wifly's serial port
can90 0:414ffb85772d 50 char ch = wifly.getc();
can90 0:414ffb85772d 51
can90 0:414ffb85772d 52 //----Find "Listen" to find the port number----
can90 0:414ffb85772d 53 if(!foundL) {
can90 0:414ffb85772d 54 if(ch == 'L') {
can90 0:414ffb85772d 55 foundL = true;
can90 0:414ffb85772d 56 append(str, ch);
can90 0:414ffb85772d 57 continue;
can90 0:414ffb85772d 58 }
can90 0:414ffb85772d 59 }
can90 0:414ffb85772d 60 else {
can90 0:414ffb85772d 61 if(count <= 4) {
can90 0:414ffb85772d 62 append(str, ch);
can90 0:414ffb85772d 63 count++;
can90 0:414ffb85772d 64 continue;
can90 0:414ffb85772d 65 }
can90 0:414ffb85772d 66 else {
can90 0:414ffb85772d 67 if(!strcmp(str, "Listen")) {
can90 0:414ffb85772d 68 char portString[5] = "";
can90 0:414ffb85772d 69 for(int i = 0; i < 7; i++) {
can90 0:414ffb85772d 70 char portChar = wifly.getc();
can90 0:414ffb85772d 71 if(i >= 3) {
can90 0:414ffb85772d 72 append(portString, portChar);
can90 0:414ffb85772d 73 }
can90 0:414ffb85772d 74 }
can90 0:414ffb85772d 75 //print the port number that WiFly Module listens
can90 0:414ffb85772d 76 pc.printf("\nPort: %s", portString);
can90 0:414ffb85772d 77 }
can90 0:414ffb85772d 78 foundL = false;
can90 0:414ffb85772d 79 count = 0;
can90 0:414ffb85772d 80 str[0] = 0; //clear char* array
can90 0:414ffb85772d 81 continue;
can90 0:414ffb85772d 82 }
can90 0:414ffb85772d 83 }
can90 0:414ffb85772d 84 //----End of Find "Listen" to find the port number----
can90 0:414ffb85772d 85
can90 0:414ffb85772d 86 //----Find "OPEN" to find out if a TCP Connection is established----
can90 0:414ffb85772d 87 if(!foundO) {
can90 0:414ffb85772d 88 if(ch == 'O') {
can90 0:414ffb85772d 89 foundO = true;
can90 0:414ffb85772d 90 append(str, ch);
can90 0:414ffb85772d 91 continue;
can90 0:414ffb85772d 92 }
can90 0:414ffb85772d 93 }
can90 0:414ffb85772d 94 else {
can90 0:414ffb85772d 95 if(count <= 2) {
can90 0:414ffb85772d 96 append(str, ch);
can90 0:414ffb85772d 97 count++;
can90 0:414ffb85772d 98 continue;
can90 0:414ffb85772d 99 }
can90 0:414ffb85772d 100 else {
can90 0:414ffb85772d 101 if(!strcmp(str, "OPEN")) { //"OPEN" is found, start the demo program
can90 0:414ffb85772d 102 pc.printf("\nConnection Established!");
can90 0:414ffb85772d 103 //commandChar will be used to store current character came from TCP
can90 0:414ffb85772d 104 char commandChar = 0;
can90 0:414ffb85772d 105 //when "CLOS" is received, connectionEnded will be made true and the while loop
can90 0:414ffb85772d 106 //contains demo program will be ended
can90 0:414ffb85772d 107 bool connectionEnded = false;
can90 0:414ffb85772d 108 const int period = 60;
can90 0:414ffb85772d 109 pwmRight.period_us(period);
can90 0:414ffb85772d 110 pwmLeft.period_us(period);
can90 0:414ffb85772d 111 int speed = 20;
can90 0:414ffb85772d 112 do {
can90 0:414ffb85772d 113 //get current character from TCP (using wifly)
can90 0:414ffb85772d 114 commandChar = wifly.getc();
can90 0:414ffb85772d 115 //When 'C' is received, this could be "CLOS" message,
can90 0:414ffb85772d 116 //check this in else
can90 0:414ffb85772d 117 if(commandChar != 'C') {
can90 0:414ffb85772d 118
can90 0:414ffb85772d 119 //You can send messages to the connected device with:
can90 0:414ffb85772d 120 //wifly.putc(<char>);
can90 0:414ffb85772d 121 //sendString(<char*>);
can90 0:414ffb85772d 122 if(commandChar == '0') { //stop
can90 0:414ffb85772d 123 pwmRight.pulsewidth_us(0);
can90 0:414ffb85772d 124 pwmLeft.pulsewidth_us(0);
can90 0:414ffb85772d 125 }
can90 0:414ffb85772d 126 else if(commandChar == '1') { //forward
can90 0:414ffb85772d 127 dirRight = 0;
can90 0:414ffb85772d 128 dirLeft = 0;
can90 0:414ffb85772d 129 pwmRight.pulsewidth_us(speed);
can90 0:414ffb85772d 130 pwmLeft.pulsewidth_us(speed);
can90 0:414ffb85772d 131 }
can90 0:414ffb85772d 132 else if(commandChar == '2') { //backward
can90 0:414ffb85772d 133 dirRight = 1;
can90 0:414ffb85772d 134 dirLeft = 1;
can90 0:414ffb85772d 135 pwmRight.pulsewidth_us(speed);
can90 0:414ffb85772d 136 pwmLeft.pulsewidth_us(speed);
can90 0:414ffb85772d 137 }
can90 0:414ffb85772d 138 else if(commandChar == '3') { //right
can90 0:414ffb85772d 139 dirRight = 1;
can90 0:414ffb85772d 140 dirLeft = 0;
can90 0:414ffb85772d 141 pwmRight.pulsewidth_us(speed);
can90 0:414ffb85772d 142 pwmLeft.pulsewidth_us(speed);
can90 0:414ffb85772d 143 }
can90 0:414ffb85772d 144 else if(commandChar == '4') { //left
can90 0:414ffb85772d 145 dirRight = 0;
can90 0:414ffb85772d 146 dirLeft = 1;
can90 0:414ffb85772d 147 pwmRight.pulsewidth_us(speed);
can90 0:414ffb85772d 148 pwmLeft.pulsewidth_us(speed);
can90 0:414ffb85772d 149 }
can90 0:414ffb85772d 150 else if(commandChar == '5') { //forward right
can90 0:414ffb85772d 151 dirRight = 0;
can90 0:414ffb85772d 152 dirLeft = 0;
can90 0:414ffb85772d 153 pwmRight.pulsewidth_us(speed/2);
can90 0:414ffb85772d 154 pwmLeft.pulsewidth_us(speed);
can90 0:414ffb85772d 155 }
can90 0:414ffb85772d 156 else if(commandChar == '6') { //forward left
can90 0:414ffb85772d 157 dirRight = 0;
can90 0:414ffb85772d 158 dirLeft = 0;
can90 0:414ffb85772d 159 pwmRight.pulsewidth_us(speed);
can90 0:414ffb85772d 160 pwmLeft.pulsewidth_us(speed/2);
can90 0:414ffb85772d 161 }
can90 0:414ffb85772d 162 else if(commandChar == '7') { //backward right
can90 0:414ffb85772d 163 dirRight = 1;
can90 0:414ffb85772d 164 dirLeft = 1;
can90 0:414ffb85772d 165 pwmRight.pulsewidth_us(speed/2);
can90 0:414ffb85772d 166 pwmLeft.pulsewidth_us(speed);
can90 0:414ffb85772d 167 }
can90 0:414ffb85772d 168 else if(commandChar == '8') { //backward left
can90 0:414ffb85772d 169 dirRight = 1;
can90 0:414ffb85772d 170 dirLeft = 1;
can90 0:414ffb85772d 171 pwmRight.pulsewidth_us(speed);
can90 0:414ffb85772d 172 pwmLeft.pulsewidth_us(speed/2);
can90 0:414ffb85772d 173 }
can90 0:414ffb85772d 174 else if(commandChar == '9') { //speed0
can90 0:414ffb85772d 175 speed = 0;
can90 0:414ffb85772d 176 }
can90 0:414ffb85772d 177 else if(commandChar == 'a') { //speed1
can90 0:414ffb85772d 178 speed = 20;
can90 0:414ffb85772d 179 }
can90 0:414ffb85772d 180 else if(commandChar == 'b') { //speed2
can90 0:414ffb85772d 181 speed = 40;
can90 0:414ffb85772d 182 }
can90 0:414ffb85772d 183 else if(commandChar == 'c') { //speed3
can90 0:414ffb85772d 184 speed = 60;
can90 0:414ffb85772d 185 }
can90 0:414ffb85772d 186 }
can90 0:414ffb85772d 187 else {
can90 0:414ffb85772d 188 //construct 4 letter word from wifly serial connection and check if it is "CLOS" or not.
can90 0:414ffb85772d 189 char closString[5] = "";
can90 0:414ffb85772d 190 append(closString, commandChar);
can90 0:414ffb85772d 191 for(int i = 0; i <= 2; i++) {
can90 0:414ffb85772d 192 char closChar = wifly.getc();
can90 0:414ffb85772d 193 append(closString, closChar);
can90 0:414ffb85772d 194 }
can90 0:414ffb85772d 195 //
can90 0:414ffb85772d 196 if(!strcmp(closString, "CLOS")) {
can90 0:414ffb85772d 197 pc.printf("\nConnection Closed!");
can90 0:414ffb85772d 198 connectionEnded = true;
can90 0:414ffb85772d 199 }
can90 0:414ffb85772d 200 }
can90 0:414ffb85772d 201 } while(!connectionEnded);
can90 0:414ffb85772d 202 }
can90 0:414ffb85772d 203 foundO = false;
can90 0:414ffb85772d 204 count = 0;
can90 0:414ffb85772d 205 str[0] = 0;
can90 0:414ffb85772d 206 continue;
can90 0:414ffb85772d 207 }
can90 0:414ffb85772d 208 }
can90 0:414ffb85772d 209 }
can90 0:414ffb85772d 210 }
can90 0:414ffb85772d 211
can90 0:414ffb85772d 212 void append(char* s, char c) {
can90 0:414ffb85772d 213 int len = strlen(s);
can90 0:414ffb85772d 214 s[len] = c;
can90 0:414ffb85772d 215 s[len+1] = '\0';
can90 0:414ffb85772d 216 }
can90 0:414ffb85772d 217
can90 0:414ffb85772d 218 void sendString(char* str) {
can90 0:414ffb85772d 219 for(int i = 0; i < strlen(str); i++) {
can90 0:414ffb85772d 220 wifly.putc(str[i]);
can90 0:414ffb85772d 221 }
can90 0:414ffb85772d 222 }