Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Wifly.h@17:56f3c4da1ea8, 2012-01-30 (annotated)
- Committer:
- samux
- Date:
- Mon Jan 30 10:53:56 2012 +0000
- Revision:
- 17:56f3c4da1ea8
- Parent:
- 15:044d10aaf9d0
- Child:
- 19:cf1a7a5cc8d6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:2f38aaabc810 | 1 | /** |
samux | 0:2f38aaabc810 | 2 | * @author Samuel Mokrani |
samux | 0:2f38aaabc810 | 3 | * |
samux | 0:2f38aaabc810 | 4 | * @section LICENSE |
samux | 0:2f38aaabc810 | 5 | * |
samux | 0:2f38aaabc810 | 6 | * Copyright (c) 2011 mbed |
samux | 0:2f38aaabc810 | 7 | * |
samux | 0:2f38aaabc810 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
samux | 0:2f38aaabc810 | 9 | * of this software and associated documentation files (the "Software"), to deal |
samux | 0:2f38aaabc810 | 10 | * in the Software without restriction, including without limitation the rights |
samux | 0:2f38aaabc810 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
samux | 0:2f38aaabc810 | 12 | * copies of the Software, and to permit persons to whom the Software is |
samux | 0:2f38aaabc810 | 13 | * furnished to do so, subject to the following conditions: |
samux | 0:2f38aaabc810 | 14 | * |
samux | 0:2f38aaabc810 | 15 | * The above copyright notice and this permission notice shall be included in |
samux | 0:2f38aaabc810 | 16 | * all copies or substantial portions of the Software. |
samux | 0:2f38aaabc810 | 17 | * |
samux | 0:2f38aaabc810 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
samux | 0:2f38aaabc810 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
samux | 0:2f38aaabc810 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
samux | 0:2f38aaabc810 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
samux | 0:2f38aaabc810 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 0:2f38aaabc810 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
samux | 0:2f38aaabc810 | 24 | * THE SOFTWARE. |
samux | 0:2f38aaabc810 | 25 | * |
samux | 0:2f38aaabc810 | 26 | * @section DESCRIPTION |
samux | 0:2f38aaabc810 | 27 | * |
samux | 0:2f38aaabc810 | 28 | * Wifly RN131-C, wifi module |
samux | 0:2f38aaabc810 | 29 | * |
samux | 0:2f38aaabc810 | 30 | * Datasheet: |
samux | 0:2f38aaabc810 | 31 | * |
samux | 0:2f38aaabc810 | 32 | * http://www.sparkfun.com/datasheets/Wireless/WiFi/WiFlyGSX-um2.pdf |
samux | 17:56f3c4da1ea8 | 33 | */ |
samux | 0:2f38aaabc810 | 34 | |
samux | 0:2f38aaabc810 | 35 | #ifndef WIFLY_H |
samux | 0:2f38aaabc810 | 36 | #define WIFLY_H |
samux | 0:2f38aaabc810 | 37 | |
samux | 0:2f38aaabc810 | 38 | #include "mbed.h" |
samux | 17:56f3c4da1ea8 | 39 | #include "CBuffer.h" |
samux | 0:2f38aaabc810 | 40 | |
samux | 0:2f38aaabc810 | 41 | /** Wifly Class |
samux | 0:2f38aaabc810 | 42 | * |
samux | 0:2f38aaabc810 | 43 | * Example: |
samux | 0:2f38aaabc810 | 44 | * @code |
samux | 0:2f38aaabc810 | 45 | * #include "mbed.h" |
samux | 0:2f38aaabc810 | 46 | * #include "Wifly.h" |
samux | 0:2f38aaabc810 | 47 | * |
samux | 15:044d10aaf9d0 | 48 | * Wifly wifly(p9, p10, p20, "network", "password", true); |
samux | 0:2f38aaabc810 | 49 | * Serial pc(USBTX, USBRX); |
samux | 17:56f3c4da1ea8 | 50 | * |
samux | 0:2f38aaabc810 | 51 | * int main() |
samux | 0:2f38aaabc810 | 52 | * { |
samux | 17:56f3c4da1ea8 | 53 | * |
samux | 15:044d10aaf9d0 | 54 | * if(wifly.join()) |
samux | 0:2f38aaabc810 | 55 | * pc.printf("network joined!\r\n"); |
samux | 0:2f38aaabc810 | 56 | * else |
samux | 0:2f38aaabc810 | 57 | * pc.printf("join failed!\r\n"); |
samux | 17:56f3c4da1ea8 | 58 | * |
samux | 2:dd0911f3af8e | 59 | * } |
samux | 0:2f38aaabc810 | 60 | * @endcode |
samux | 0:2f38aaabc810 | 61 | */ |
samux | 0:2f38aaabc810 | 62 | class Wifly { |
samux | 0:2f38aaabc810 | 63 | |
samux | 17:56f3c4da1ea8 | 64 | public: |
samux | 17:56f3c4da1ea8 | 65 | /** |
samux | 17:56f3c4da1ea8 | 66 | * Constructor for joining open, wep or wpa secured networks |
samux | 17:56f3c4da1ea8 | 67 | * |
samux | 17:56f3c4da1ea8 | 68 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 17:56f3c4da1ea8 | 69 | * @param rx mbed pin to use for rx line of Serial interface |
samux | 17:56f3c4da1ea8 | 70 | * @param reset reset pin of the wifi module |
samux | 17:56f3c4da1ea8 | 71 | * @param ssid ssid of the network |
samux | 17:56f3c4da1ea8 | 72 | * @param phrase WEP or WPA key |
samux | 17:56f3c4da1ea8 | 73 | * @param wpa true if wpa security false otherwise |
samux | 17:56f3c4da1ea8 | 74 | * @param ip ip of the wifi module if dhcp = false (default: NULL) |
samux | 17:56f3c4da1ea8 | 75 | * @param netmask netmask if dhcp = false (default: NULL) |
samux | 17:56f3c4da1ea8 | 76 | * @param dhcp enable or disable dhcp (default: true) |
samux | 17:56f3c4da1ea8 | 77 | * @param baudrate speed of the communication (default: 9600) |
samux | 17:56f3c4da1ea8 | 78 | */ |
samux | 17:56f3c4da1ea8 | 79 | Wifly( PinName tx, PinName rx, PinName reset, char * ssid, char * phrase, bool wpa, |
samux | 17:56f3c4da1ea8 | 80 | char * ip = NULL, char * netmask = NULL, bool dhcp = true, int baudrate = 9600); |
samux | 17:56f3c4da1ea8 | 81 | |
samux | 17:56f3c4da1ea8 | 82 | |
samux | 17:56f3c4da1ea8 | 83 | |
samux | 17:56f3c4da1ea8 | 84 | /** |
samux | 17:56f3c4da1ea8 | 85 | * Constructor to create an adhoc network |
samux | 17:56f3c4da1ea8 | 86 | * |
samux | 17:56f3c4da1ea8 | 87 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 17:56f3c4da1ea8 | 88 | * @param rx mbed pin to use for rx line of Serial interface |
samux | 17:56f3c4da1ea8 | 89 | * @param ssid ssid of the adhoc network which will be created |
samux | 17:56f3c4da1ea8 | 90 | * @param ip ip of the wifi module (default: "169.254.1.1") |
samux | 17:56f3c4da1ea8 | 91 | * @param netmask netmask (default: "255.255.0.0") |
samux | 17:56f3c4da1ea8 | 92 | * @param channel channel (default: "1") |
samux | 17:56f3c4da1ea8 | 93 | * @param baudrate speed of the communication (default: 9600) |
samux | 17:56f3c4da1ea8 | 94 | */ |
samux | 17:56f3c4da1ea8 | 95 | Wifly( PinName tx, PinName rx, PinName reset, char * ssid, char * ip = "169.254.1.1", |
samux | 17:56f3c4da1ea8 | 96 | char * netmask = "255.255.0.0", int channel = 1, int baudrate = 9600); |
samux | 17:56f3c4da1ea8 | 97 | |
samux | 17:56f3c4da1ea8 | 98 | /** |
samux | 17:56f3c4da1ea8 | 99 | * Send a string to the wifi module by serial port. This function desactivates the user interrupt handler when a character is received to analyze the response from the wifi module. |
samux | 17:56f3c4da1ea8 | 100 | * Useful to send a command to the module and wait a response. |
samux | 17:56f3c4da1ea8 | 101 | * |
samux | 17:56f3c4da1ea8 | 102 | * |
samux | 17:56f3c4da1ea8 | 103 | * @param str string to be sent |
samux | 17:56f3c4da1ea8 | 104 | * @param ACK string which must be acknowledge by the wifi module. If "ACK" == "NO", no string has to be acknoledged. (default: "NO") |
samux | 17:56f3c4da1ea8 | 105 | * @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) |
samux | 17:56f3c4da1ea8 | 106 | * |
samux | 17:56f3c4da1ea8 | 107 | * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s. |
samux | 17:56f3c4da1ea8 | 108 | */ |
samux | 17:56f3c4da1ea8 | 109 | bool send(char * str, char * ACK = "NO", char * res = NULL); |
samux | 17:56f3c4da1ea8 | 110 | |
samux | 17:56f3c4da1ea8 | 111 | /** |
samux | 17:56f3c4da1ea8 | 112 | * Connect the wifi module to the ssid contained in the constructor. |
samux | 17:56f3c4da1ea8 | 113 | * |
samux | 17:56f3c4da1ea8 | 114 | * @return true if connected, false otherwise |
samux | 17:56f3c4da1ea8 | 115 | */ |
samux | 17:56f3c4da1ea8 | 116 | bool join(); |
samux | 17:56f3c4da1ea8 | 117 | |
samux | 17:56f3c4da1ea8 | 118 | /** |
samux | 17:56f3c4da1ea8 | 119 | * Create an adhoc network with the ssid contained in the constructor |
samux | 17:56f3c4da1ea8 | 120 | * |
samux | 17:56f3c4da1ea8 | 121 | * @return true if the network is well created, false otherwise |
samux | 17:56f3c4da1ea8 | 122 | */ |
samux | 17:56f3c4da1ea8 | 123 | bool createAdhocNetwork(); |
samux | 17:56f3c4da1ea8 | 124 | |
samux | 17:56f3c4da1ea8 | 125 | /** |
samux | 17:56f3c4da1ea8 | 126 | * Read a string if available |
samux | 17:56f3c4da1ea8 | 127 | * |
samux | 17:56f3c4da1ea8 | 128 | *@param str pointer where will be stored the string read |
samux | 17:56f3c4da1ea8 | 129 | */ |
samux | 17:56f3c4da1ea8 | 130 | bool read(char * str); |
samux | 17:56f3c4da1ea8 | 131 | |
samux | 17:56f3c4da1ea8 | 132 | |
samux | 17:56f3c4da1ea8 | 133 | /** |
samux | 17:56f3c4da1ea8 | 134 | * Reset the wifi module |
samux | 17:56f3c4da1ea8 | 135 | */ |
samux | 17:56f3c4da1ea8 | 136 | void reset(); |
samux | 17:56f3c4da1ea8 | 137 | |
samux | 17:56f3c4da1ea8 | 138 | /** |
samux | 17:56f3c4da1ea8 | 139 | * Check if characters are available |
samux | 17:56f3c4da1ea8 | 140 | * |
samux | 17:56f3c4da1ea8 | 141 | * @return number of available characters |
samux | 17:56f3c4da1ea8 | 142 | */ |
samux | 17:56f3c4da1ea8 | 143 | int readable(); |
samux | 17:56f3c4da1ea8 | 144 | |
samux | 17:56f3c4da1ea8 | 145 | /** |
samux | 17:56f3c4da1ea8 | 146 | * Read a character |
samux | 17:56f3c4da1ea8 | 147 | * |
samux | 17:56f3c4da1ea8 | 148 | * @return the character read |
samux | 17:56f3c4da1ea8 | 149 | */ |
samux | 17:56f3c4da1ea8 | 150 | char getc(); |
samux | 17:56f3c4da1ea8 | 151 | |
samux | 17:56f3c4da1ea8 | 152 | /** |
samux | 17:56f3c4da1ea8 | 153 | * Flush the buffer |
samux | 17:56f3c4da1ea8 | 154 | */ |
samux | 17:56f3c4da1ea8 | 155 | void flush(); |
samux | 17:56f3c4da1ea8 | 156 | |
samux | 17:56f3c4da1ea8 | 157 | /** |
samux | 17:56f3c4da1ea8 | 158 | * Write a character |
samux | 17:56f3c4da1ea8 | 159 | * |
samux | 17:56f3c4da1ea8 | 160 | * @param the character which will be written |
samux | 17:56f3c4da1ea8 | 161 | */ |
samux | 17:56f3c4da1ea8 | 162 | void putc(char c); |
samux | 17:56f3c4da1ea8 | 163 | |
samux | 17:56f3c4da1ea8 | 164 | |
samux | 17:56f3c4da1ea8 | 165 | /** |
samux | 17:56f3c4da1ea8 | 166 | * To enter in command mode (we can configure the module) |
samux | 17:56f3c4da1ea8 | 167 | * |
samux | 17:56f3c4da1ea8 | 168 | * @return true if successful, false otherwise |
samux | 17:56f3c4da1ea8 | 169 | */ |
samux | 17:56f3c4da1ea8 | 170 | bool cmdMode(); |
samux | 17:56f3c4da1ea8 | 171 | |
samux | 17:56f3c4da1ea8 | 172 | |
samux | 17:56f3c4da1ea8 | 173 | /** |
samux | 17:56f3c4da1ea8 | 174 | * To exit the command mode |
samux | 17:56f3c4da1ea8 | 175 | * |
samux | 17:56f3c4da1ea8 | 176 | * @return true if successful, false otherwise |
samux | 17:56f3c4da1ea8 | 177 | */ |
samux | 17:56f3c4da1ea8 | 178 | bool exit(); |
samux | 17:56f3c4da1ea8 | 179 | |
samux | 17:56f3c4da1ea8 | 180 | /** |
samux | 17:56f3c4da1ea8 | 181 | * Change the baudrate of the wifi module. The modification will be effective after a reboot. |
samux | 17:56f3c4da1ea8 | 182 | * After a baudrate modification, you have to use the correct parameters in the Wifly constructor. |
samux | 17:56f3c4da1ea8 | 183 | * |
samux | 17:56f3c4da1ea8 | 184 | * @param baudrate new baudrate |
samux | 17:56f3c4da1ea8 | 185 | * @return true if the baudrate has been changed, false otherwise |
samux | 17:56f3c4da1ea8 | 186 | */ |
samux | 17:56f3c4da1ea8 | 187 | bool changeBaudrate(int baudrate); |
samux | 17:56f3c4da1ea8 | 188 | |
samux | 17:56f3c4da1ea8 | 189 | /** |
samux | 17:56f3c4da1ea8 | 190 | * Attach a member function to call when a character is received. |
samux | 17:56f3c4da1ea8 | 191 | * |
samux | 17:56f3c4da1ea8 | 192 | * @param tptr pointer to the object to call the member function on |
samux | 17:56f3c4da1ea8 | 193 | * @param mptr pointer to the member function to be called |
samux | 17:56f3c4da1ea8 | 194 | */ |
samux | 17:56f3c4da1ea8 | 195 | template<typename T> |
samux | 17:56f3c4da1ea8 | 196 | void attach(T* tptr, void (T::*mptr)(void)) { |
samux | 17:56f3c4da1ea8 | 197 | if ((mptr != NULL) && (tptr != NULL)) { |
samux | 17:56f3c4da1ea8 | 198 | rx.attach(tptr, mptr); |
samux | 17:56f3c4da1ea8 | 199 | } |
samux | 17:56f3c4da1ea8 | 200 | } |
samux | 17:56f3c4da1ea8 | 201 | |
samux | 17:56f3c4da1ea8 | 202 | |
samux | 17:56f3c4da1ea8 | 203 | /** |
samux | 17:56f3c4da1ea8 | 204 | * Attach a callback for when a character is received |
samux | 17:56f3c4da1ea8 | 205 | * |
samux | 17:56f3c4da1ea8 | 206 | * @param fptr function pointer |
samux | 17:56f3c4da1ea8 | 207 | */ |
samux | 17:56f3c4da1ea8 | 208 | void attach(void (*fn)(void)) { |
samux | 17:56f3c4da1ea8 | 209 | if (fn != NULL) { |
samux | 17:56f3c4da1ea8 | 210 | rx.attach(fn); |
samux | 17:56f3c4da1ea8 | 211 | } |
samux | 17:56f3c4da1ea8 | 212 | } |
samux | 17:56f3c4da1ea8 | 213 | |
samux | 17:56f3c4da1ea8 | 214 | private: |
samux | 17:56f3c4da1ea8 | 215 | Serial wifi; |
samux | 17:56f3c4da1ea8 | 216 | DigitalOut reset_pin; |
samux | 17:56f3c4da1ea8 | 217 | bool wpa; |
samux | 17:56f3c4da1ea8 | 218 | bool adhoc; |
samux | 17:56f3c4da1ea8 | 219 | bool dhcp; |
samux | 17:56f3c4da1ea8 | 220 | char phrase[30]; |
samux | 17:56f3c4da1ea8 | 221 | char ssid[30]; |
samux | 17:56f3c4da1ea8 | 222 | char ip[20]; |
samux | 17:56f3c4da1ea8 | 223 | char netmask[20]; |
samux | 17:56f3c4da1ea8 | 224 | int channel; |
samux | 17:56f3c4da1ea8 | 225 | CBuffer buf_wifly; |
samux | 17:56f3c4da1ea8 | 226 | |
samux | 17:56f3c4da1ea8 | 227 | void attach_rx(bool null); |
samux | 17:56f3c4da1ea8 | 228 | void handler_rx(void); |
samux | 17:56f3c4da1ea8 | 229 | |
samux | 17:56f3c4da1ea8 | 230 | FunctionPointer rx; |
samux | 17:56f3c4da1ea8 | 231 | |
samux | 17:56f3c4da1ea8 | 232 | |
samux | 0:2f38aaabc810 | 233 | }; |
samux | 0:2f38aaabc810 | 234 | |
samux | 0:2f38aaabc810 | 235 | #endif |