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