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