Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Wifly.cpp@19:cf1a7a5cc8d6, 2012-05-09 (annotated)
- Committer:
- samux
- Date:
- Wed May 09 11:46:39 2012 +0000
- Revision:
- 19:cf1a7a5cc8d6
- Parent:
- 18:88a26cd00ba3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:2f38aaabc810 | 1 | #include "mbed.h" |
samux | 0:2f38aaabc810 | 2 | #include "Wifly.h" |
samux | 0:2f38aaabc810 | 3 | #include <string> |
samux | 0:2f38aaabc810 | 4 | |
samux | 17:56f3c4da1ea8 | 5 | //#define DEBUG |
samux | 0:2f38aaabc810 | 6 | |
samux | 10:d3e3e925f62f | 7 | Wifly::Wifly( PinName tx, PinName rx, PinName _reset, char * ssid, char * phrase, |
samux | 10:d3e3e925f62f | 8 | bool wpa, char * ip, char * netmask, bool dhcp, int baudrate): |
samux | 10:d3e3e925f62f | 9 | wifi(tx, rx), reset_pin(_reset) { |
samux | 0:2f38aaabc810 | 10 | wifi.baud(baudrate); |
samux | 5:9890eb81f730 | 11 | wifi.format(8, Serial::None, 1); |
samux | 0:2f38aaabc810 | 12 | this->wpa = wpa; |
samux | 0:2f38aaabc810 | 13 | this->dhcp = dhcp; |
samux | 11:6eb90004d92a | 14 | strcpy(this->phrase, phrase); |
samux | 11:6eb90004d92a | 15 | strcpy(this->ssid, ssid); |
samux | 11:6eb90004d92a | 16 | strcpy(this->ip, ip); |
samux | 11:6eb90004d92a | 17 | strcpy(this->netmask, netmask); |
samux | 0:2f38aaabc810 | 18 | adhoc = false; |
samux | 17:56f3c4da1ea8 | 19 | |
samux | 17:56f3c4da1ea8 | 20 | reset(); |
samux | 0:2f38aaabc810 | 21 | } |
samux | 0:2f38aaabc810 | 22 | |
samux | 17:56f3c4da1ea8 | 23 | Wifly::Wifly( PinName tx, PinName rx, PinName _reset, char * ssid, char * ip, char * netmask, int channel, int baudrate): |
samux | 10:d3e3e925f62f | 24 | wifi(tx, rx), reset_pin(_reset) { |
samux | 0:2f38aaabc810 | 25 | wifi.baud(baudrate); |
samux | 5:9890eb81f730 | 26 | wifi.format(8, Serial::None, 1); |
samux | 0:2f38aaabc810 | 27 | adhoc = true; |
samux | 11:6eb90004d92a | 28 | strcpy(this->ssid, ssid); |
samux | 11:6eb90004d92a | 29 | strcpy(this->ip, ip); |
samux | 11:6eb90004d92a | 30 | strcpy(this->netmask, netmask); |
samux | 0:2f38aaabc810 | 31 | this->channel = channel; |
samux | 17:56f3c4da1ea8 | 32 | |
samux | 17:56f3c4da1ea8 | 33 | reset(); |
samux | 10:d3e3e925f62f | 34 | } |
samux | 10:d3e3e925f62f | 35 | |
samux | 10:d3e3e925f62f | 36 | void Wifly::handler_rx(void) { |
samux | 17:56f3c4da1ea8 | 37 | |
samux | 17:56f3c4da1ea8 | 38 | //read the character |
samux | 10:d3e3e925f62f | 39 | char read = wifi.getc(); |
samux | 17:56f3c4da1ea8 | 40 | |
samux | 17:56f3c4da1ea8 | 41 | //queue it |
samux | 17:56f3c4da1ea8 | 42 | buf_wifly.queue(read); |
samux | 17:56f3c4da1ea8 | 43 | |
samux | 17:56f3c4da1ea8 | 44 | //call user callback |
samux | 17:56f3c4da1ea8 | 45 | rx.call(); |
samux | 10:d3e3e925f62f | 46 | } |
samux | 10:d3e3e925f62f | 47 | |
samux | 19:cf1a7a5cc8d6 | 48 | void Wifly::attach_rx(bool callback) { |
samux | 19:cf1a7a5cc8d6 | 49 | if (!callback) |
samux | 10:d3e3e925f62f | 50 | wifi.attach(NULL); |
samux | 10:d3e3e925f62f | 51 | else |
samux | 10:d3e3e925f62f | 52 | wifi.attach(this, &Wifly::handler_rx); |
samux | 0:2f38aaabc810 | 53 | } |
samux | 0:2f38aaabc810 | 54 | |
samux | 12:fd6874542b28 | 55 | bool Wifly::send(char * str, char * ACK, char * res) { |
samux | 17:56f3c4da1ea8 | 56 | char read; |
samux | 17:56f3c4da1ea8 | 57 | size_t found = string::npos; |
samux | 18:88a26cd00ba3 | 58 | string checking; |
samux | 17:56f3c4da1ea8 | 59 | Timer tmr; |
samux | 0:2f38aaabc810 | 60 | |
samux | 18:88a26cd00ba3 | 61 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 62 | printf("will send: %s\r\n",str); |
samux | 18:88a26cd00ba3 | 63 | #endif |
samux | 18:88a26cd00ba3 | 64 | |
samux | 10:d3e3e925f62f | 65 | attach_rx(false); |
samux | 19:cf1a7a5cc8d6 | 66 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 0:2f38aaabc810 | 67 | wifi.printf("%s", str); |
samux | 17:56f3c4da1ea8 | 68 | } else { |
samux | 10:d3e3e925f62f | 69 | //We flush the buffer |
samux | 10:d3e3e925f62f | 70 | while (wifi.readable()) |
samux | 10:d3e3e925f62f | 71 | wifi.getc(); |
samux | 10:d3e3e925f62f | 72 | |
samux | 0:2f38aaabc810 | 73 | tmr.start(); |
samux | 0:2f38aaabc810 | 74 | wifi.printf("%s", str); |
samux | 10:d3e3e925f62f | 75 | |
samux | 10:d3e3e925f62f | 76 | while (1) { |
samux | 18:88a26cd00ba3 | 77 | if (tmr.read() > 3) { |
samux | 10:d3e3e925f62f | 78 | //We flush the buffer |
samux | 10:d3e3e925f62f | 79 | while (wifi.readable()) |
samux | 10:d3e3e925f62f | 80 | wifi.getc(); |
samux | 17:56f3c4da1ea8 | 81 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 82 | printf("check: %s\r\n", checking.c_str()); |
samux | 17:56f3c4da1ea8 | 83 | #endif |
samux | 10:d3e3e925f62f | 84 | attach_rx(true); |
samux | 0:2f38aaabc810 | 85 | return false; |
samux | 10:d3e3e925f62f | 86 | } else if (wifi.readable()) { |
samux | 0:2f38aaabc810 | 87 | read = wifi.getc(); |
samux | 10:d3e3e925f62f | 88 | if ( read != '\r' && read != '\n') { |
samux | 0:2f38aaabc810 | 89 | checking += read; |
samux | 0:2f38aaabc810 | 90 | found = checking.find(ACK); |
samux | 10:d3e3e925f62f | 91 | if (found != string::npos) { |
samux | 17:56f3c4da1ea8 | 92 | wait(0.01); |
samux | 10:d3e3e925f62f | 93 | |
samux | 10:d3e3e925f62f | 94 | //We flush the buffer |
samux | 10:d3e3e925f62f | 95 | while (wifi.readable()) |
samux | 10:d3e3e925f62f | 96 | wifi.getc(); |
samux | 10:d3e3e925f62f | 97 | |
samux | 0:2f38aaabc810 | 98 | break; |
samux | 0:2f38aaabc810 | 99 | } |
samux | 0:2f38aaabc810 | 100 | } |
samux | 0:2f38aaabc810 | 101 | } |
samux | 0:2f38aaabc810 | 102 | } |
samux | 17:56f3c4da1ea8 | 103 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 104 | printf("check: %s\r\n", checking.c_str()); |
samux | 17:56f3c4da1ea8 | 105 | #endif |
samux | 10:d3e3e925f62f | 106 | attach_rx(true); |
samux | 17:56f3c4da1ea8 | 107 | return true; |
samux | 0:2f38aaabc810 | 108 | } |
samux | 17:56f3c4da1ea8 | 109 | |
samux | 19:cf1a7a5cc8d6 | 110 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 10:d3e3e925f62f | 111 | if ( res != NULL) { |
samux | 10:d3e3e925f62f | 112 | int i = 0; |
samux | 17:56f3c4da1ea8 | 113 | Timer timeout; |
samux | 17:56f3c4da1ea8 | 114 | timeout.start(); |
samux | 17:56f3c4da1ea8 | 115 | while (1) { |
samux | 17:56f3c4da1ea8 | 116 | if (timeout.read() > 3) { |
samux | 17:56f3c4da1ea8 | 117 | read = NULL; |
samux | 19:cf1a7a5cc8d6 | 118 | attach_rx(true); |
samux | 17:56f3c4da1ea8 | 119 | return false; |
samux | 17:56f3c4da1ea8 | 120 | } else { |
samux | 17:56f3c4da1ea8 | 121 | if (tmr.read_ms() > 500) { |
samux | 17:56f3c4da1ea8 | 122 | res[i] = 0; |
samux | 18:88a26cd00ba3 | 123 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 124 | printf("user str: %s\r\n", res); |
samux | 18:88a26cd00ba3 | 125 | #endif |
samux | 19:cf1a7a5cc8d6 | 126 | break; |
samux | 17:56f3c4da1ea8 | 127 | } |
samux | 17:56f3c4da1ea8 | 128 | if (wifi.readable()) { |
samux | 17:56f3c4da1ea8 | 129 | tmr.start(); |
samux | 17:56f3c4da1ea8 | 130 | read = wifi.getc(); |
samux | 19:cf1a7a5cc8d6 | 131 | |
samux | 19:cf1a7a5cc8d6 | 132 | // wa have detected an end of response |
samux | 19:cf1a7a5cc8d6 | 133 | if (read == '<') { |
samux | 19:cf1a7a5cc8d6 | 134 | res[i] = 0; |
samux | 19:cf1a7a5cc8d6 | 135 | break; |
samux | 19:cf1a7a5cc8d6 | 136 | } |
samux | 19:cf1a7a5cc8d6 | 137 | |
samux | 19:cf1a7a5cc8d6 | 138 | // we drop \r and \n |
samux | 17:56f3c4da1ea8 | 139 | if ( read != '\r' && read != '\n') { |
samux | 17:56f3c4da1ea8 | 140 | res[i++] = read; |
samux | 17:56f3c4da1ea8 | 141 | } |
samux | 17:56f3c4da1ea8 | 142 | } |
samux | 10:d3e3e925f62f | 143 | } |
samux | 10:d3e3e925f62f | 144 | } |
samux | 19:cf1a7a5cc8d6 | 145 | #ifdef DEBUG |
samux | 19:cf1a7a5cc8d6 | 146 | //printf("user str: %s\r\n", res); |
samux | 19:cf1a7a5cc8d6 | 147 | #endif |
samux | 10:d3e3e925f62f | 148 | } |
samux | 10:d3e3e925f62f | 149 | attach_rx(true); |
samux | 0:2f38aaabc810 | 150 | return true; |
samux | 0:2f38aaabc810 | 151 | } |
samux | 0:2f38aaabc810 | 152 | |
samux | 12:fd6874542b28 | 153 | bool Wifly::join() { |
samux | 18:88a26cd00ba3 | 154 | char cmd[150]; |
samux | 10:d3e3e925f62f | 155 | |
samux | 17:56f3c4da1ea8 | 156 | if (!adhoc) { |
samux | 17:56f3c4da1ea8 | 157 | //enter in command mode |
samux | 17:56f3c4da1ea8 | 158 | if (!cmdMode()) { |
samux | 11:6eb90004d92a | 159 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 160 | printf("join: cannot enter in cmd mode\r\n"); |
samux | 11:6eb90004d92a | 161 | #endif |
samux | 19:cf1a7a5cc8d6 | 162 | // send fake command otherwise exit() will fail as $$$exit\r will be sent |
samux | 19:cf1a7a5cc8d6 | 163 | // this is because there is no \ê at the end of $$$ |
samux | 19:cf1a7a5cc8d6 | 164 | send("a\r"); |
samux | 17:56f3c4da1ea8 | 165 | wait(0.2); |
samux | 17:56f3c4da1ea8 | 166 | exit(); |
samux | 17:56f3c4da1ea8 | 167 | if (!cmdMode()) |
samux | 17:56f3c4da1ea8 | 168 | return false; |
samux | 17:56f3c4da1ea8 | 169 | } |
samux | 10:d3e3e925f62f | 170 | |
samux | 19:cf1a7a5cc8d6 | 171 | // to get all wlan parameters |
samux | 19:cf1a7a5cc8d6 | 172 | if (!send("get w\r", NULL, cmd)) { |
samux | 18:88a26cd00ba3 | 173 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 174 | printf("join: cannot get wlan settings\r\n"); |
samux | 18:88a26cd00ba3 | 175 | #endif |
samux | 18:88a26cd00ba3 | 176 | exit(); |
samux | 18:88a26cd00ba3 | 177 | return false; |
samux | 18:88a26cd00ba3 | 178 | } |
samux | 18:88a26cd00ba3 | 179 | |
samux | 19:cf1a7a5cc8d6 | 180 | // if the wlan parameters are already known, we just connect without resending all commands |
samux | 18:88a26cd00ba3 | 181 | if ((string(cmd).find(ssid) != string::npos) && (string(cmd).find(phrase) != string::npos) ) { |
samux | 18:88a26cd00ba3 | 182 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 183 | printf("ssid found && phrase found\r\n"); |
samux | 18:88a26cd00ba3 | 184 | #endif |
samux | 19:cf1a7a5cc8d6 | 185 | Timer tmr; |
samux | 19:cf1a7a5cc8d6 | 186 | tmr.start(); |
samux | 19:cf1a7a5cc8d6 | 187 | while (tmr.read() < 2) { |
samux | 19:cf1a7a5cc8d6 | 188 | send("show c\r", NULL, cmd); |
samux | 18:88a26cd00ba3 | 189 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 190 | printf("join: state of conn: %s\r\n", cmd); |
samux | 18:88a26cd00ba3 | 191 | #endif |
samux | 18:88a26cd00ba3 | 192 | if ((cmd[2] - '0') & 0x01) { |
samux | 18:88a26cd00ba3 | 193 | exit(); |
samux | 18:88a26cd00ba3 | 194 | return true; |
samux | 18:88a26cd00ba3 | 195 | } |
samux | 19:cf1a7a5cc8d6 | 196 | wait(0.2); |
samux | 18:88a26cd00ba3 | 197 | } |
samux | 18:88a26cd00ba3 | 198 | } |
samux | 18:88a26cd00ba3 | 199 | |
samux | 19:cf1a7a5cc8d6 | 200 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 201 | printf("getw: %s\r\n", cmd); |
samux | 19:cf1a7a5cc8d6 | 202 | #endif |
samux | 18:88a26cd00ba3 | 203 | |
samux | 18:88a26cd00ba3 | 204 | // ssid |
samux | 18:88a26cd00ba3 | 205 | sprintf(cmd, "set w s %s\r", ssid); |
samux | 18:88a26cd00ba3 | 206 | if (!send(cmd, "AOK")) { |
samux | 18:88a26cd00ba3 | 207 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 208 | printf("join: cannot set ssid\r\n"); |
samux | 18:88a26cd00ba3 | 209 | #endif |
samux | 18:88a26cd00ba3 | 210 | exit(); |
samux | 18:88a26cd00ba3 | 211 | return false; |
samux | 18:88a26cd00ba3 | 212 | } |
samux | 18:88a26cd00ba3 | 213 | |
samux | 17:56f3c4da1ea8 | 214 | //auth |
samux | 18:88a26cd00ba3 | 215 | sprintf(cmd, "set w a %d\r", (wpa) ? 3 : 1); |
samux | 12:fd6874542b28 | 216 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 217 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 218 | printf("join: cannot set auth\r\n"); |
samux | 17:56f3c4da1ea8 | 219 | #endif |
samux | 17:56f3c4da1ea8 | 220 | exit(); |
samux | 17:56f3c4da1ea8 | 221 | return false; |
samux | 17:56f3c4da1ea8 | 222 | } |
samux | 17:56f3c4da1ea8 | 223 | |
samux | 17:56f3c4da1ea8 | 224 | //dhcp |
samux | 18:88a26cd00ba3 | 225 | sprintf(cmd, "set i d %d\r", (dhcp) ? 1 : 0); |
samux | 17:56f3c4da1ea8 | 226 | if (!send(cmd, "AOK")) { |
samux | 17:56f3c4da1ea8 | 227 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 228 | printf("join: cannot set dhcp\r\n"); |
samux | 17:56f3c4da1ea8 | 229 | #endif |
samux | 17:56f3c4da1ea8 | 230 | exit(); |
samux | 17:56f3c4da1ea8 | 231 | return false; |
samux | 17:56f3c4da1ea8 | 232 | } |
samux | 17:56f3c4da1ea8 | 233 | |
samux | 17:56f3c4da1ea8 | 234 | //no echo |
samux | 18:88a26cd00ba3 | 235 | if (!send("set u m 1\r", "AOK")) { |
samux | 17:56f3c4da1ea8 | 236 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 237 | printf("join: cannot set no echo\r\n"); |
samux | 11:6eb90004d92a | 238 | #endif |
samux | 0:2f38aaabc810 | 239 | exit(); |
samux | 0:2f38aaabc810 | 240 | return false; |
samux | 0:2f38aaabc810 | 241 | } |
samux | 10:d3e3e925f62f | 242 | |
samux | 17:56f3c4da1ea8 | 243 | // if no dhcp, set ip and netmask |
samux | 17:56f3c4da1ea8 | 244 | if (!dhcp) { |
samux | 17:56f3c4da1ea8 | 245 | #ifdef DEBUG |
samux | 18:88a26cd00ba3 | 246 | printf("not dhcp\r"); |
samux | 17:56f3c4da1ea8 | 247 | #endif |
samux | 17:56f3c4da1ea8 | 248 | sprintf(cmd, "set i a %s\r\n", ip); |
samux | 17:56f3c4da1ea8 | 249 | if (!send(cmd, "AOK")) { |
samux | 17:56f3c4da1ea8 | 250 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 251 | printf("Wifly::join: cannot set ip address\r\n"); |
samux | 17:56f3c4da1ea8 | 252 | #endif |
samux | 17:56f3c4da1ea8 | 253 | exit(); |
samux | 17:56f3c4da1ea8 | 254 | return false; |
samux | 17:56f3c4da1ea8 | 255 | } |
samux | 17:56f3c4da1ea8 | 256 | |
samux | 18:88a26cd00ba3 | 257 | sprintf(cmd, "set i n %s\r", netmask); |
samux | 17:56f3c4da1ea8 | 258 | if (!send(cmd, "AOK")) { |
samux | 17:56f3c4da1ea8 | 259 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 260 | printf("Wifly::join: cannot set netmask\r\n"); |
samux | 17:56f3c4da1ea8 | 261 | #endif |
samux | 17:56f3c4da1ea8 | 262 | exit(); |
samux | 17:56f3c4da1ea8 | 263 | return false; |
samux | 17:56f3c4da1ea8 | 264 | } |
samux | 17:56f3c4da1ea8 | 265 | } |
samux | 17:56f3c4da1ea8 | 266 | |
samux | 17:56f3c4da1ea8 | 267 | //key step |
samux | 17:56f3c4da1ea8 | 268 | if (wpa) |
samux | 18:88a26cd00ba3 | 269 | sprintf(cmd, "set w p %s\r", phrase); |
samux | 17:56f3c4da1ea8 | 270 | else |
samux | 18:88a26cd00ba3 | 271 | sprintf(cmd, "set w k %s\r", phrase); |
samux | 17:56f3c4da1ea8 | 272 | |
samux | 12:fd6874542b28 | 273 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 274 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 275 | printf("join: cannot set phrase\r\n"); |
samux | 11:6eb90004d92a | 276 | #endif |
samux | 0:2f38aaabc810 | 277 | exit(); |
samux | 0:2f38aaabc810 | 278 | return false; |
samux | 0:2f38aaabc810 | 279 | } |
samux | 10:d3e3e925f62f | 280 | |
samux | 18:88a26cd00ba3 | 281 | // auto join |
samux | 18:88a26cd00ba3 | 282 | if (!send("set w j 1\r", "AOK")) { |
samux | 17:56f3c4da1ea8 | 283 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 284 | printf("Wifly::join: cannot set join 1\r\n"); |
samux | 17:56f3c4da1ea8 | 285 | #endif |
samux | 17:56f3c4da1ea8 | 286 | exit(); |
samux | 17:56f3c4da1ea8 | 287 | return false; |
samux | 17:56f3c4da1ea8 | 288 | } |
samux | 17:56f3c4da1ea8 | 289 | |
samux | 18:88a26cd00ba3 | 290 | // save |
samux | 18:88a26cd00ba3 | 291 | if (!send("save\r", "Stor")) { |
samux | 17:56f3c4da1ea8 | 292 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 293 | printf("Wifly::join: cannot save\r\n"); |
samux | 17:56f3c4da1ea8 | 294 | #endif |
samux | 17:56f3c4da1ea8 | 295 | exit(); |
samux | 17:56f3c4da1ea8 | 296 | return false; |
samux | 17:56f3c4da1ea8 | 297 | } |
samux | 17:56f3c4da1ea8 | 298 | |
samux | 17:56f3c4da1ea8 | 299 | //join the network |
samux | 18:88a26cd00ba3 | 300 | sprintf(cmd, "join %s\r", ssid); |
samux | 10:d3e3e925f62f | 301 | |
samux | 18:88a26cd00ba3 | 302 | if (!send(cmd, "Associated!")) { |
samux | 17:56f3c4da1ea8 | 303 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 304 | printf("join: cannot join %s\r\n", ssid); |
samux | 17:56f3c4da1ea8 | 305 | #endif |
samux | 17:56f3c4da1ea8 | 306 | exit(); |
samux | 17:56f3c4da1ea8 | 307 | return false; |
samux | 17:56f3c4da1ea8 | 308 | } |
samux | 17:56f3c4da1ea8 | 309 | exit(); |
samux | 10:d3e3e925f62f | 310 | |
samux | 11:6eb90004d92a | 311 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 312 | printf("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase, (wpa) ? "WPA" : "WEP"); |
samux | 11:6eb90004d92a | 313 | #endif |
samux | 17:56f3c4da1ea8 | 314 | return true; |
samux | 17:56f3c4da1ea8 | 315 | } else { |
samux | 17:56f3c4da1ea8 | 316 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 317 | printf("Wifly::join: You didn't choose the right constructor to join a network!\r\n"); |
samux | 17:56f3c4da1ea8 | 318 | #endif |
samux | 0:2f38aaabc810 | 319 | return false; |
samux | 0:2f38aaabc810 | 320 | } |
samux | 0:2f38aaabc810 | 321 | } |
samux | 0:2f38aaabc810 | 322 | |
samux | 0:2f38aaabc810 | 323 | |
samux | 10:d3e3e925f62f | 324 | |
samux | 12:fd6874542b28 | 325 | bool Wifly::createAdhocNetwork() { |
samux | 10:d3e3e925f62f | 326 | if (adhoc) { |
samux | 0:2f38aaabc810 | 327 | char cmd[50]; |
samux | 10:d3e3e925f62f | 328 | |
samux | 12:fd6874542b28 | 329 | if (!cmdMode()) { |
samux | 11:6eb90004d92a | 330 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 331 | printf("Wifly::CreateAdhocNetwork: cannot enter in cmd mode\r\n"); |
samux | 11:6eb90004d92a | 332 | #endif |
samux | 0:2f38aaabc810 | 333 | exit(); |
samux | 10:d3e3e925f62f | 334 | return false; |
samux | 0:2f38aaabc810 | 335 | } |
samux | 10:d3e3e925f62f | 336 | |
samux | 18:88a26cd00ba3 | 337 | if (!send("set w j 4\r", "AOK")) { |
samux | 11:6eb90004d92a | 338 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 339 | printf("Wifly::CreateAdhocNetwork: cannot set join 4\r\n"); |
samux | 11:6eb90004d92a | 340 | #endif |
samux | 0:2f38aaabc810 | 341 | exit(); |
samux | 0:2f38aaabc810 | 342 | return false; |
samux | 0:2f38aaabc810 | 343 | } |
samux | 10:d3e3e925f62f | 344 | |
samux | 7:c6cf9ae117b1 | 345 | //no echo |
samux | 18:88a26cd00ba3 | 346 | if (!send("set u m 1\r", "AOK")) { |
samux | 11:6eb90004d92a | 347 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 348 | printf("Wifly::CreateAdhocNetwork: cannot set no echo\r\n"); |
samux | 11:6eb90004d92a | 349 | #endif |
samux | 7:c6cf9ae117b1 | 350 | exit(); |
samux | 7:c6cf9ae117b1 | 351 | return false; |
samux | 7:c6cf9ae117b1 | 352 | } |
samux | 10:d3e3e925f62f | 353 | |
samux | 0:2f38aaabc810 | 354 | //ssid |
samux | 18:88a26cd00ba3 | 355 | sprintf(cmd, "set w s %s\r", ssid); |
samux | 12:fd6874542b28 | 356 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 357 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 358 | printf("Wifly::CreateAdhocNetwork: cannot set ssid\r\n"); |
samux | 11:6eb90004d92a | 359 | #endif |
samux | 0:2f38aaabc810 | 360 | exit(); |
samux | 0:2f38aaabc810 | 361 | return false; |
samux | 0:2f38aaabc810 | 362 | } |
samux | 10:d3e3e925f62f | 363 | |
samux | 18:88a26cd00ba3 | 364 | sprintf(cmd, "set w c %d\r", channel); |
samux | 12:fd6874542b28 | 365 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 366 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 367 | printf("Wifly::CreateAdhocNetwork: cannot set channel\r\n"); |
samux | 11:6eb90004d92a | 368 | #endif |
samux | 0:2f38aaabc810 | 369 | exit(); |
samux | 0:2f38aaabc810 | 370 | return false; |
samux | 0:2f38aaabc810 | 371 | } |
samux | 10:d3e3e925f62f | 372 | |
samux | 18:88a26cd00ba3 | 373 | sprintf(cmd, "set i a %s\r", ip); |
samux | 12:fd6874542b28 | 374 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 375 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 376 | printf("Wifly::CreateAdhocNetwork: cannot set ip address\r\n"); |
samux | 11:6eb90004d92a | 377 | #endif |
samux | 0:2f38aaabc810 | 378 | exit(); |
samux | 0:2f38aaabc810 | 379 | return false; |
samux | 0:2f38aaabc810 | 380 | } |
samux | 10:d3e3e925f62f | 381 | |
samux | 18:88a26cd00ba3 | 382 | sprintf(cmd, "set i n %s\r", netmask); |
samux | 12:fd6874542b28 | 383 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 384 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 385 | printf("Wifly::CreateAdhocNetwork: cannot set netmask\r\n"); |
samux | 11:6eb90004d92a | 386 | #endif |
samux | 0:2f38aaabc810 | 387 | exit(); |
samux | 0:2f38aaabc810 | 388 | return false; |
samux | 0:2f38aaabc810 | 389 | } |
samux | 10:d3e3e925f62f | 390 | |
samux | 18:88a26cd00ba3 | 391 | if (!send("set i d 0\r", "AOK")) { |
samux | 11:6eb90004d92a | 392 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 393 | printf("Wifly::CreateAdhocNetwork: cannot set dhcp off\r\n"); |
samux | 11:6eb90004d92a | 394 | #endif |
samux | 0:2f38aaabc810 | 395 | exit(); |
samux | 0:2f38aaabc810 | 396 | return false; |
samux | 0:2f38aaabc810 | 397 | } |
samux | 10:d3e3e925f62f | 398 | |
samux | 18:88a26cd00ba3 | 399 | if (!send("save\r", "Stor")) { |
samux | 11:6eb90004d92a | 400 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 401 | printf("Wifly::CreateAdhocNetwork: cannot save\r\n"); |
samux | 11:6eb90004d92a | 402 | #endif |
samux | 0:2f38aaabc810 | 403 | exit(); |
samux | 0:2f38aaabc810 | 404 | return false; |
samux | 0:2f38aaabc810 | 405 | } |
samux | 10:d3e3e925f62f | 406 | |
samux | 19:cf1a7a5cc8d6 | 407 | send("reboot\r", NULL); |
samux | 11:6eb90004d92a | 408 | #ifdef DEBUG |
samux | 0:2f38aaabc810 | 409 | 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); |
samux | 11:6eb90004d92a | 410 | #endif |
samux | 0:2f38aaabc810 | 411 | return true; |
samux | 10:d3e3e925f62f | 412 | } else { |
samux | 11:6eb90004d92a | 413 | #ifdef DEBUG |
samux | 17:56f3c4da1ea8 | 414 | printf("Wifly::CreateAdhocNetwork: You didn't choose the right constructor for creating an adhoc mode!\r\n"); |
samux | 11:6eb90004d92a | 415 | #endif |
samux | 0:2f38aaabc810 | 416 | return false; |
samux | 0:2f38aaabc810 | 417 | } |
samux | 0:2f38aaabc810 | 418 | } |
samux | 0:2f38aaabc810 | 419 | |
samux | 17:56f3c4da1ea8 | 420 | void Wifly::flush() { |
samux | 17:56f3c4da1ea8 | 421 | while (readable()) |
samux | 17:56f3c4da1ea8 | 422 | getc(); |
samux | 17:56f3c4da1ea8 | 423 | } |
samux | 17:56f3c4da1ea8 | 424 | |
samux | 12:fd6874542b28 | 425 | bool Wifly::cmdMode() { |
samux | 12:fd6874542b28 | 426 | if (!send("$$$", "CMD")) { |
samux | 11:6eb90004d92a | 427 | #ifdef DEBUG |
samux | 12:fd6874542b28 | 428 | printf("Wifly::cmdMode: cannot enter in cmd mode\r\n"); |
samux | 11:6eb90004d92a | 429 | #endif |
samux | 9:984c1436dd42 | 430 | return false; |
samux | 0:2f38aaabc810 | 431 | } |
samux | 0:2f38aaabc810 | 432 | return true; |
samux | 0:2f38aaabc810 | 433 | } |
samux | 0:2f38aaabc810 | 434 | |
samux | 0:2f38aaabc810 | 435 | |
samux | 10:d3e3e925f62f | 436 | void Wifly::reset() { |
samux | 0:2f38aaabc810 | 437 | reset_pin = 0; |
samux | 0:2f38aaabc810 | 438 | wait(0.2); |
samux | 0:2f38aaabc810 | 439 | reset_pin = 1; |
samux | 0:2f38aaabc810 | 440 | wait(0.2); |
samux | 0:2f38aaabc810 | 441 | } |
samux | 0:2f38aaabc810 | 442 | |
samux | 0:2f38aaabc810 | 443 | |
samux | 0:2f38aaabc810 | 444 | |
samux | 0:2f38aaabc810 | 445 | |
samux | 10:d3e3e925f62f | 446 | void Wifly::putc(char c) { |
samux | 18:88a26cd00ba3 | 447 | while (!wifi.writeable()); |
samux | 0:2f38aaabc810 | 448 | wifi.putc(c); |
samux | 0:2f38aaabc810 | 449 | } |
samux | 0:2f38aaabc810 | 450 | |
samux | 0:2f38aaabc810 | 451 | |
samux | 0:2f38aaabc810 | 452 | |
samux | 0:2f38aaabc810 | 453 | |
samux | 11:6eb90004d92a | 454 | bool Wifly::read(char * str) { |
samux | 17:56f3c4da1ea8 | 455 | int length = buf_wifly.available(); |
samux | 17:56f3c4da1ea8 | 456 | if (length == 0) |
samux | 17:56f3c4da1ea8 | 457 | return false; |
samux | 17:56f3c4da1ea8 | 458 | for (int i = 0; i < length; i++) |
samux | 17:56f3c4da1ea8 | 459 | buf_wifly.dequeue((uint8_t *)&str[i]); |
samux | 17:56f3c4da1ea8 | 460 | str[length] = 0; |
samux | 17:56f3c4da1ea8 | 461 | return true; |
samux | 0:2f38aaabc810 | 462 | } |
samux | 0:2f38aaabc810 | 463 | |
samux | 0:2f38aaabc810 | 464 | |
samux | 0:2f38aaabc810 | 465 | |
samux | 0:2f38aaabc810 | 466 | |
samux | 10:d3e3e925f62f | 467 | bool Wifly::exit() { |
samux | 19:cf1a7a5cc8d6 | 468 | flush(); |
samux | 12:fd6874542b28 | 469 | return send("exit\r", "EXIT"); |
samux | 0:2f38aaabc810 | 470 | } |
samux | 0:2f38aaabc810 | 471 | |
samux | 0:2f38aaabc810 | 472 | |
samux | 0:2f38aaabc810 | 473 | |
samux | 17:56f3c4da1ea8 | 474 | int Wifly::readable() { |
samux | 17:56f3c4da1ea8 | 475 | return buf_wifly.available(); |
samux | 0:2f38aaabc810 | 476 | } |
samux | 0:2f38aaabc810 | 477 | |
samux | 10:d3e3e925f62f | 478 | bool Wifly::changeBaudrate(int baudrate) { |
samux | 4:06ca04e2e279 | 479 | char cmd[20]; |
samux | 4:06ca04e2e279 | 480 | exit(); |
samux | 12:fd6874542b28 | 481 | if (!cmdMode()) { |
samux | 11:6eb90004d92a | 482 | #ifdef DEBUG |
samux | 4:06ca04e2e279 | 483 | printf("Wifly::changeBaudrate: cannot enter in cmd mode\r\n"); |
samux | 11:6eb90004d92a | 484 | #endif |
samux | 4:06ca04e2e279 | 485 | return false; |
samux | 4:06ca04e2e279 | 486 | } |
samux | 10:d3e3e925f62f | 487 | |
samux | 18:88a26cd00ba3 | 488 | sprintf(cmd, "set u b %d\r", baudrate); |
samux | 12:fd6874542b28 | 489 | if (!send(cmd, "AOK")) { |
samux | 11:6eb90004d92a | 490 | #ifdef DEBUG |
samux | 4:06ca04e2e279 | 491 | printf("Wifly::changeBaudrate: cannot set new baudrate\r\n"); |
samux | 11:6eb90004d92a | 492 | #endif |
samux | 4:06ca04e2e279 | 493 | exit(); |
samux | 4:06ca04e2e279 | 494 | return false; |
samux | 4:06ca04e2e279 | 495 | } |
samux | 10:d3e3e925f62f | 496 | |
samux | 18:88a26cd00ba3 | 497 | if (!send("save\r", "Stor")) { |
samux | 11:6eb90004d92a | 498 | #ifdef DEBUG |
samux | 4:06ca04e2e279 | 499 | printf("Wifly::changeBaudrate: cannot save\r\n"); |
samux | 11:6eb90004d92a | 500 | #endif |
samux | 4:06ca04e2e279 | 501 | exit(); |
samux | 4:06ca04e2e279 | 502 | return false; |
samux | 4:06ca04e2e279 | 503 | } |
samux | 10:d3e3e925f62f | 504 | |
samux | 4:06ca04e2e279 | 505 | return true; |
samux | 4:06ca04e2e279 | 506 | } |
samux | 0:2f38aaabc810 | 507 | |
samux | 0:2f38aaabc810 | 508 | |
samux | 10:d3e3e925f62f | 509 | char Wifly::getc() { |
samux | 17:56f3c4da1ea8 | 510 | char c; |
samux | 17:56f3c4da1ea8 | 511 | while (!buf_wifly.available()); |
samux | 17:56f3c4da1ea8 | 512 | buf_wifly.dequeue((uint8_t *)&c); |
samux | 17:56f3c4da1ea8 | 513 | return c; |
samux | 0:2f38aaabc810 | 514 | } |