Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Revision 19:cf1a7a5cc8d6, committed 2012-05-09
- Comitter:
- samux
- Date:
- Wed May 09 11:46:39 2012 +0000
- Parent:
- 18:88a26cd00ba3
- Commit message:
Changed in this revision
Wifly.cpp | Show annotated file Show diff for this revision Revisions of this file |
Wifly.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 88a26cd00ba3 -r cf1a7a5cc8d6 Wifly.cpp --- a/Wifly.cpp Mon Feb 20 16:08:42 2012 +0000 +++ b/Wifly.cpp Wed May 09 11:46:39 2012 +0000 @@ -20,10 +20,6 @@ reset(); } - - - - Wifly::Wifly( PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel, int baudrate): wifi(tx, rx), reset_pin(_reset) { wifi.baud(baudrate); @@ -37,7 +33,6 @@ reset(); } - void Wifly::handler_rx(void) { //read the character @@ -50,15 +45,13 @@ rx.call(); } -void Wifly::attach_rx(bool null) { - if (!null) +void Wifly::attach_rx(bool callback) { + if (!callback) wifi.attach(NULL); else wifi.attach(this, &Wifly::handler_rx); } - - bool Wifly::send(char * str, char * ACK, char * res) { char read; size_t found = string::npos; @@ -69,9 +62,8 @@ printf("will send: %s\r\n",str); #endif - attach_rx(false); - if (!strcmp(ACK, "NO")) { + if (!ACK || !strcmp(ACK, "NO")) { wifi.printf("%s", str); } else { //We flush the buffer @@ -115,7 +107,7 @@ return true; } - //the user wants the result from the command (ACK == "NO", res != NULL) + //the user wants the result from the command (ACK == NULL, res != NULL) if ( res != NULL) { int i = 0; Timer timeout; @@ -123,6 +115,7 @@ while (1) { if (timeout.read() > 3) { read = NULL; + attach_rx(true); return false; } else { if (tmr.read_ms() > 500) { @@ -130,18 +123,28 @@ #ifdef DEBUG printf("user str: %s\r\n", res); #endif - attach_rx(true); - return true; + break; } if (wifi.readable()) { tmr.start(); read = wifi.getc(); + + // wa have detected an end of response + if (read == '<') { + res[i] = 0; + break; + } + + // we drop \r and \n if ( read != '\r' && read != '\n') { res[i++] = read; } } } } +#ifdef DEBUG + //printf("user str: %s\r\n", res); +#endif } attach_rx(true); return true; @@ -156,14 +159,17 @@ #ifdef DEBUG printf("join: cannot enter in cmd mode\r\n"); #endif - send("a\r\n"); + // send fake command otherwise exit() will fail as $$$exit\r will be sent + // this is because there is no \ê at the end of $$$ + send("a\r"); wait(0.2); exit(); if (!cmdMode()) return false; } - if (!send("get w\r", "NO", cmd)) { + // to get all wlan parameters + if (!send("get w\r", NULL, cmd)) { #ifdef DEBUG printf("join: cannot get wlan settings\r\n"); #endif @@ -171,12 +177,15 @@ return false; } + // if the wlan parameters are already known, we just connect without resending all commands if ((string(cmd).find(ssid) != string::npos) && (string(cmd).find(phrase) != string::npos) ) { #ifdef DEBUG printf("ssid found && phrase found\r\n"); #endif - while (1) { - send("show c\r", "NO", cmd); + Timer tmr; + tmr.start(); + while (tmr.read() < 2) { + send("show c\r", NULL, cmd); #ifdef DEBUG printf("join: state of conn: %s\r\n", cmd); #endif @@ -184,11 +193,13 @@ exit(); return true; } - wait(1); + wait(0.2); } } +#ifdef DEBUG printf("getw: %s\r\n", cmd); +#endif // ssid sprintf(cmd, "set w s %s\r", ssid); @@ -251,7 +262,6 @@ exit(); return false; } - } //key step @@ -298,9 +308,6 @@ } exit(); - wait(0.1); - flush(); - #ifdef DEBUG printf("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); #endif @@ -397,15 +404,10 @@ return false; } - flush(); - - send("reboot\r", "NO"); + send("reboot\r", NULL); #ifdef DEBUG printf("\r\ncreating an adhoc\r\nnetwork: %s\r\nip: %s\r\nnetmask: %s\r\nchannel: %d\r\n\r\n", ssid, ip, netmask, channel); #endif - - wait(0.1); - flush(); return true; } else { #ifdef DEBUG @@ -463,6 +465,7 @@ bool Wifly::exit() { + flush(); return send("exit\r", "EXIT"); }
diff -r 88a26cd00ba3 -r cf1a7a5cc8d6 Wifly.h --- a/Wifly.h Mon Feb 20 16:08:42 2012 +0000 +++ b/Wifly.h Wed May 09 11:46:39 2012 +0000 @@ -101,12 +101,12 @@ * * * @param str string to be sent - * @param ACK string which must be acknowledge by the wifi module. If "ACK" == "NO", no string has to be acknoledged. (default: "NO") + * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") * @param res this field will contain the response from the wifi module, result of a command sent. This field is available only if ACK = "NO" AND res != NULL (default: NULL) * * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s. */ - bool send(char * str, char * ACK = "NO", char * res = NULL); + bool send(char * str, char * ACK = NULL, char * res = NULL); /** * Connect the wifi module to the ssid contained in the constructor.