Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Diff: Wifly.cpp
- Revision:
- 18:88a26cd00ba3
- Parent:
- 17:56f3c4da1ea8
- Child:
- 19:cf1a7a5cc8d6
diff -r 56f3c4da1ea8 -r 88a26cd00ba3 Wifly.cpp --- a/Wifly.cpp Mon Jan 30 10:53:56 2012 +0000 +++ b/Wifly.cpp Mon Feb 20 16:08:42 2012 +0000 @@ -62,8 +62,13 @@ bool Wifly::send(char * str, char * ACK, char * res) { char read; size_t found = string::npos; + string checking; Timer tmr; +#ifdef DEBUG + printf("will send: %s\r\n",str); +#endif + attach_rx(false); if (!strcmp(ACK, "NO")) { @@ -75,10 +80,9 @@ tmr.start(); wifi.printf("%s", str); - string checking; while (1) { - if (tmr.read() > 2) { + if (tmr.read() > 3) { //We flush the buffer while (wifi.readable()) wifi.getc(); @@ -123,8 +127,11 @@ } else { if (tmr.read_ms() > 500) { res[i] = 0; +#ifdef DEBUG + printf("user str: %s\r\n", res); +#endif attach_rx(true); - return false; + return true; } if (wifi.readable()) { tmr.start(); @@ -141,7 +148,7 @@ } bool Wifly::join() { - char cmd[60]; + char cmd[150]; if (!adhoc) { //enter in command mode @@ -156,8 +163,45 @@ return false; } + if (!send("get w\r", "NO", cmd)) { +#ifdef DEBUG + printf("join: cannot get wlan settings\r\n"); +#endif + exit(); + return false; + } + + 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); +#ifdef DEBUG + printf("join: state of conn: %s\r\n", cmd); +#endif + if ((cmd[2] - '0') & 0x01) { + exit(); + return true; + } + wait(1); + } + } + + printf("getw: %s\r\n", cmd); + + // ssid + sprintf(cmd, "set w s %s\r", ssid); + if (!send(cmd, "AOK")) { +#ifdef DEBUG + printf("join: cannot set ssid\r\n"); +#endif + exit(); + return false; + } + //auth - sprintf(cmd, "set w a %d\r\n", (wpa) ? 3 : 1); + sprintf(cmd, "set w a %d\r", (wpa) ? 3 : 1); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("join: cannot set auth\r\n"); @@ -167,7 +211,7 @@ } //dhcp - sprintf(cmd, "set i d %d\r\n", (dhcp) ? 1 : 0); + sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("join: cannot set dhcp\r\n"); @@ -177,7 +221,7 @@ } //no echo - if (!send("set u m 1\r\n", "AOK")) { + if (!send("set u m 1\r", "AOK")) { #ifdef DEBUG printf("join: cannot set no echo\r\n"); #endif @@ -188,7 +232,7 @@ // if no dhcp, set ip and netmask if (!dhcp) { #ifdef DEBUG - printf("not dhcp\r\n"); + printf("not dhcp\r"); #endif sprintf(cmd, "set i a %s\r\n", ip); if (!send(cmd, "AOK")) { @@ -199,7 +243,7 @@ return false; } - sprintf(cmd, "set i n %s\r\n", netmask); + sprintf(cmd, "set i n %s\r", netmask); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("Wifly::join: cannot set netmask\r\n"); @@ -212,9 +256,9 @@ //key step if (wpa) - sprintf(cmd, "set w p %s\r\n", phrase); + sprintf(cmd, "set w p %s\r", phrase); else - sprintf(cmd, "set w k %s\r\n", phrase); + sprintf(cmd, "set w k %s\r", phrase); if (!send(cmd, "AOK")) { #ifdef DEBUG @@ -224,7 +268,8 @@ return false; } - if (!send("set w j 1\r\n", "AOK")) { + // auto join + if (!send("set w j 1\r", "AOK")) { #ifdef DEBUG printf("Wifly::join: cannot set join 1\r\n"); #endif @@ -232,7 +277,8 @@ return false; } - if (!send("save\r\n", "Stor")) { + // save + if (!send("save\r", "Stor")) { #ifdef DEBUG printf("Wifly::join: cannot save\r\n"); #endif @@ -241,9 +287,9 @@ } //join the network - sprintf(cmd, "join %s\r\n", ssid); + sprintf(cmd, "join %s\r", ssid); - if (!send(cmd, "IP=")) { + if (!send(cmd, "Associated!")) { #ifdef DEBUG printf("join: cannot join %s\r\n", ssid); #endif @@ -265,8 +311,6 @@ #endif return false; } - - } @@ -283,7 +327,7 @@ return false; } - if (!send("set w j 4\r\n", "AOK")) { + if (!send("set w j 4\r", "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set join 4\r\n"); #endif @@ -292,7 +336,7 @@ } //no echo - if (!send("set u m 1\r\n", "AOK")) { + if (!send("set u m 1\r", "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set no echo\r\n"); #endif @@ -301,7 +345,7 @@ } //ssid - sprintf(cmd, "set w s %s\r\n", ssid); + sprintf(cmd, "set w s %s\r", ssid); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set ssid\r\n"); @@ -310,7 +354,7 @@ return false; } - sprintf(cmd, "set w c %d\r\n", channel); + sprintf(cmd, "set w c %d\r", channel); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set channel\r\n"); @@ -319,7 +363,7 @@ return false; } - sprintf(cmd, "set i a %s\r\n", ip); + sprintf(cmd, "set i a %s\r", ip); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set ip address\r\n"); @@ -328,7 +372,7 @@ return false; } - sprintf(cmd, "set i n %s\r\n", netmask); + sprintf(cmd, "set i n %s\r", netmask); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set netmask\r\n"); @@ -337,7 +381,7 @@ return false; } - if (!send("set i d 0\r\n", "AOK")) { + if (!send("set i d 0\r", "AOK")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot set dhcp off\r\n"); #endif @@ -345,7 +389,7 @@ return false; } - if (!send("save\r\n", "Stor")) { + if (!send("save\r", "Stor")) { #ifdef DEBUG printf("Wifly::CreateAdhocNetwork: cannot save\r\n"); #endif @@ -355,12 +399,12 @@ flush(); - send("reboot\r\n", "NO"); + send("reboot\r", "NO"); #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.2); + wait(0.1); flush(); return true; } else { @@ -387,8 +431,6 @@ } - - void Wifly::reset() { reset_pin = 0; wait(0.2); @@ -399,9 +441,8 @@ - - void Wifly::putc(char c) { + while (!wifi.writeable()); wifi.putc(c); } @@ -441,7 +482,7 @@ return false; } - sprintf(cmd, "set u b %d\r\n", baudrate); + sprintf(cmd, "set u b %d\r", baudrate); if (!send(cmd, "AOK")) { #ifdef DEBUG printf("Wifly::changeBaudrate: cannot set new baudrate\r\n"); @@ -450,7 +491,7 @@ return false; } - if (!send("save\r\n", "Stor")) { + if (!send("save\r", "Stor")) { #ifdef DEBUG printf("Wifly::changeBaudrate: cannot save\r\n"); #endif