Library to use a wifly module: RN 131 C/G
Dependents: RN-XV_simple_server
Wifly.h@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 | /** |
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 | 0:2f38aaabc810 | 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 | 0:2f38aaabc810 | 39 | |
samux | 0:2f38aaabc810 | 40 | /** Wifly Class |
samux | 0:2f38aaabc810 | 41 | * |
samux | 0:2f38aaabc810 | 42 | * Example: |
samux | 0:2f38aaabc810 | 43 | * @code |
samux | 0:2f38aaabc810 | 44 | * #include "mbed.h" |
samux | 0:2f38aaabc810 | 45 | * #include "Wifly.h" |
samux | 0:2f38aaabc810 | 46 | * |
samux | 0:2f38aaabc810 | 47 | * Wifly * wifly; |
samux | 0:2f38aaabc810 | 48 | * Serial pc(USBTX, USBRX); |
samux | 0:2f38aaabc810 | 49 | * |
samux | 0:2f38aaabc810 | 50 | * int main() |
samux | 0:2f38aaabc810 | 51 | * { |
samux | 0:2f38aaabc810 | 52 | * wifly = new Wifly(p9, p10, p20, "network", "password", true); |
samux | 0:2f38aaabc810 | 53 | * |
samux | 0:2f38aaabc810 | 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 | 0:2f38aaabc810 | 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 | 0:2f38aaabc810 | 64 | public: |
samux | 0:2f38aaabc810 | 65 | /** |
samux | 3:e6f1c618da66 | 66 | * Constructor for joining open, wep or wpa secured networks |
samux | 0:2f38aaabc810 | 67 | * |
samux | 0:2f38aaabc810 | 68 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 0:2f38aaabc810 | 69 | * @param rx mbed pin to use for rx line of Serial interface |
samux | 0:2f38aaabc810 | 70 | * @param reset reset pin of the wifi module |
samux | 0:2f38aaabc810 | 71 | * @param ssid ssid of the network |
samux | 0:2f38aaabc810 | 72 | * @param phrase WEP or WPA key |
samux | 0:2f38aaabc810 | 73 | * @param wpa true if wpa security false otherwise |
samux | 6:f2d3f1528280 | 74 | * @param ip ip of the wifi module if dhcp = false (default: NULL) |
samux | 6:f2d3f1528280 | 75 | * @param netmask netmask if dhcp = false (default: NULL) |
samux | 6:f2d3f1528280 | 76 | * @param dhcp enable or disable dhcp (default: true) |
samux | 0:2f38aaabc810 | 77 | * @param baudrate speed of the communication (default: 9600) |
samux | 0:2f38aaabc810 | 78 | */ |
samux | 0:2f38aaabc810 | 79 | Wifly( PinName tx, PinName rx, PinName reset, char * ssid, char * phrase, bool wpa, |
samux | 5:9890eb81f730 | 80 | char * ip = NULL, char * netmask = NULL, bool dhcp = true, int baudrate = 9600); |
samux | 0:2f38aaabc810 | 81 | |
samux | 0:2f38aaabc810 | 82 | |
samux | 0:2f38aaabc810 | 83 | |
samux | 0:2f38aaabc810 | 84 | /** |
samux | 0:2f38aaabc810 | 85 | * Constructor to create an adhoc network |
samux | 0:2f38aaabc810 | 86 | * |
samux | 0:2f38aaabc810 | 87 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 0:2f38aaabc810 | 88 | * @param rx mbed pin to use for rx line of Serial interface |
samux | 0:2f38aaabc810 | 89 | * @param ssid ssid of the adhoc network which will be created |
samux | 0:2f38aaabc810 | 90 | * @param ip ip of the wifi module (default: "169.254.1.1") |
samux | 0:2f38aaabc810 | 91 | * @param netmask netmask (default: "255.255.0.0") |
samux | 6:f2d3f1528280 | 92 | * @param channel channel (default: "1") |
samux | 0:2f38aaabc810 | 93 | * @param baudrate speed of the communication (default: 9600) |
samux | 0:2f38aaabc810 | 94 | */ |
samux | 0:2f38aaabc810 | 95 | Wifly( PinName tx, PinName rx, PinName reset, char * ssid, char * ip = "169.254.1.1", |
samux | 5:9890eb81f730 | 96 | char * netmask = "255.255.0.0", int channel = 1, int baudrate = 9600); |
samux | 0:2f38aaabc810 | 97 | |
samux | 0:2f38aaabc810 | 98 | /** |
samux | 0:2f38aaabc810 | 99 | * Send a string to the wifi module by serial port |
samux | 0:2f38aaabc810 | 100 | * |
samux | 1:e2f18a3d5215 | 101 | * @param str string to be sent |
samux | 1:e2f18a3d5215 | 102 | * @param ACK string which must be acknowledge by the wifi module |
samux | 10:d3e3e925f62f | 103 | * @param res pointeur where will be stored the response from the wifi module. If res == NULL or ACK != "NO", no response is returned. (by default, res = NULL) |
samux | 0:2f38aaabc810 | 104 | * |
samux | 1:e2f18a3d5215 | 105 | * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 3s. |
samux | 0:2f38aaabc810 | 106 | */ |
samux | 10:d3e3e925f62f | 107 | bool Send(char * str, char * ACK, char * res = NULL); |
samux | 0:2f38aaabc810 | 108 | |
samux | 0:2f38aaabc810 | 109 | /** |
samux | 0:2f38aaabc810 | 110 | * Connect the wifi module to the network. Return true if set correctly, false if not. |
samux | 0:2f38aaabc810 | 111 | * |
samux | 1:e2f18a3d5215 | 112 | * @return true if connected, false otherwise |
samux | 0:2f38aaabc810 | 113 | */ |
samux | 0:2f38aaabc810 | 114 | bool Join(); |
samux | 0:2f38aaabc810 | 115 | |
samux | 0:2f38aaabc810 | 116 | /** |
samux | 0:2f38aaabc810 | 117 | * Create an adhoc network with the ssid contained in the constructor |
samux | 0:2f38aaabc810 | 118 | * |
samux | 1:e2f18a3d5215 | 119 | * @return true if the network is well created, false otherwise |
samux | 0:2f38aaabc810 | 120 | */ |
samux | 0:2f38aaabc810 | 121 | bool CreateAdhocNetwork(); |
samux | 0:2f38aaabc810 | 122 | |
samux | 0:2f38aaabc810 | 123 | /** |
samux | 9:984c1436dd42 | 124 | * Receive a string to the wifi module by serial port |
samux | 0:2f38aaabc810 | 125 | * |
samux | 1:e2f18a3d5215 | 126 | *@param str string to be read |
samux | 0:2f38aaabc810 | 127 | */ |
samux | 11:6eb90004d92a | 128 | bool read(char * str); |
samux | 0:2f38aaabc810 | 129 | |
samux | 0:2f38aaabc810 | 130 | /** |
samux | 0:2f38aaabc810 | 131 | * To enter in command mode (we can configure the module) |
samux | 0:2f38aaabc810 | 132 | * |
samux | 1:e2f18a3d5215 | 133 | * @return true if successful, false otherwise |
samux | 0:2f38aaabc810 | 134 | */ |
samux | 0:2f38aaabc810 | 135 | bool CmdMode(); |
samux | 0:2f38aaabc810 | 136 | |
samux | 0:2f38aaabc810 | 137 | /** |
samux | 0:2f38aaabc810 | 138 | * To exit the command mode |
samux | 0:2f38aaabc810 | 139 | * |
samux | 1:e2f18a3d5215 | 140 | * @return true if successful, false otherwise |
samux | 0:2f38aaabc810 | 141 | */ |
samux | 0:2f38aaabc810 | 142 | bool exit(); |
samux | 0:2f38aaabc810 | 143 | |
samux | 0:2f38aaabc810 | 144 | /** |
samux | 0:2f38aaabc810 | 145 | * Reset the wifi module |
samux | 0:2f38aaabc810 | 146 | */ |
samux | 0:2f38aaabc810 | 147 | void reset(); |
samux | 0:2f38aaabc810 | 148 | |
samux | 0:2f38aaabc810 | 149 | /** |
samux | 0:2f38aaabc810 | 150 | * To check if a character is available |
samux | 0:2f38aaabc810 | 151 | * |
samux | 1:e2f18a3d5215 | 152 | * @return true if a character is available, false otherwise |
samux | 0:2f38aaabc810 | 153 | */ |
samux | 0:2f38aaabc810 | 154 | bool readable(); |
samux | 0:2f38aaabc810 | 155 | |
samux | 0:2f38aaabc810 | 156 | /** |
samux | 0:2f38aaabc810 | 157 | * Read a character |
samux | 0:2f38aaabc810 | 158 | * |
samux | 1:e2f18a3d5215 | 159 | * @return the character read |
samux | 0:2f38aaabc810 | 160 | */ |
samux | 0:2f38aaabc810 | 161 | char getc(); |
samux | 0:2f38aaabc810 | 162 | |
samux | 0:2f38aaabc810 | 163 | /** |
samux | 0:2f38aaabc810 | 164 | * Write a character |
samux | 0:2f38aaabc810 | 165 | * |
samux | 1:e2f18a3d5215 | 166 | * @param the character which will be written |
samux | 0:2f38aaabc810 | 167 | */ |
samux | 0:2f38aaabc810 | 168 | void putc(char c); |
samux | 0:2f38aaabc810 | 169 | |
samux | 4:06ca04e2e279 | 170 | /** |
samux | 4:06ca04e2e279 | 171 | * Change the baudrate of the wifi module. The modification will be effective after a reboot. |
samux | 4:06ca04e2e279 | 172 | * After a baudrate modification, you have to use the correct parameters in the Wifly constructor. |
samux | 4:06ca04e2e279 | 173 | * |
samux | 4:06ca04e2e279 | 174 | * @param baudrate new baudrate |
samux | 4:06ca04e2e279 | 175 | * @return true if the baudrate has been changed, false otherwise |
samux | 4:06ca04e2e279 | 176 | */ |
samux | 4:06ca04e2e279 | 177 | bool changeBaudrate(int baudrate); |
samux | 4:06ca04e2e279 | 178 | |
samux | 0:2f38aaabc810 | 179 | |
samux | 10:d3e3e925f62f | 180 | |
samux | 0:2f38aaabc810 | 181 | |
samux | 0:2f38aaabc810 | 182 | private: |
samux | 0:2f38aaabc810 | 183 | Serial wifi; |
samux | 0:2f38aaabc810 | 184 | DigitalOut reset_pin; |
samux | 0:2f38aaabc810 | 185 | bool wpa; |
samux | 0:2f38aaabc810 | 186 | bool adhoc; |
samux | 0:2f38aaabc810 | 187 | bool dhcp; |
samux | 11:6eb90004d92a | 188 | char phrase[30]; |
samux | 11:6eb90004d92a | 189 | char ssid[30]; |
samux | 11:6eb90004d92a | 190 | char ip[20]; |
samux | 11:6eb90004d92a | 191 | char netmask[20]; |
samux | 0:2f38aaabc810 | 192 | int channel; |
samux | 10:d3e3e925f62f | 193 | |
samux | 11:6eb90004d92a | 194 | char tmp_buf_rx[200]; |
samux | 11:6eb90004d92a | 195 | char buf_rx[200]; |
samux | 10:d3e3e925f62f | 196 | volatile int length; |
samux | 10:d3e3e925f62f | 197 | volatile bool new_msg; |
samux | 10:d3e3e925f62f | 198 | |
samux | 10:d3e3e925f62f | 199 | void attach_rx(bool null); |
samux | 10:d3e3e925f62f | 200 | void handler_rx(void); |
samux | 0:2f38aaabc810 | 201 | }; |
samux | 0:2f38aaabc810 | 202 | |
samux | 0:2f38aaabc810 | 203 | #endif |