wifly/socket interface for wifly modules
Dependents: WiFi neurGAI_WIFI thingspeak thingspeak2
Wifly/Wifly.h@0:6ffb0aeb3972, 2012-08-17 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 17 08:28:04 2012 +0000
- Revision:
- 0:6ffb0aeb3972
- Child:
- 1:8f04181f9ad8
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:6ffb0aeb3972 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 0:6ffb0aeb3972 | 2 | * |
samux | 0:6ffb0aeb3972 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 0:6ffb0aeb3972 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 0:6ffb0aeb3972 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 0:6ffb0aeb3972 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 0:6ffb0aeb3972 | 7 | * furnished to do so, subject to the following conditions: |
samux | 0:6ffb0aeb3972 | 8 | * |
samux | 0:6ffb0aeb3972 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 0:6ffb0aeb3972 | 10 | * substantial portions of the Software. |
samux | 0:6ffb0aeb3972 | 11 | * |
samux | 0:6ffb0aeb3972 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 0:6ffb0aeb3972 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 0:6ffb0aeb3972 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 0:6ffb0aeb3972 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 0:6ffb0aeb3972 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 0:6ffb0aeb3972 | 17 | * |
samux | 0:6ffb0aeb3972 | 18 | * @section DESCRIPTION |
samux | 0:6ffb0aeb3972 | 19 | * |
samux | 0:6ffb0aeb3972 | 20 | * Wifly RN131-C, wifi module |
samux | 0:6ffb0aeb3972 | 21 | * |
samux | 0:6ffb0aeb3972 | 22 | * Datasheet: |
samux | 0:6ffb0aeb3972 | 23 | * |
samux | 0:6ffb0aeb3972 | 24 | * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf |
samux | 0:6ffb0aeb3972 | 25 | */ |
samux | 0:6ffb0aeb3972 | 26 | |
samux | 0:6ffb0aeb3972 | 27 | #ifndef WIFLY_H |
samux | 0:6ffb0aeb3972 | 28 | #define WIFLY_H |
samux | 0:6ffb0aeb3972 | 29 | |
samux | 0:6ffb0aeb3972 | 30 | #include "mbed.h" |
samux | 0:6ffb0aeb3972 | 31 | #include "CBuffer.h" |
samux | 0:6ffb0aeb3972 | 32 | |
samux | 0:6ffb0aeb3972 | 33 | class Wifly { |
samux | 0:6ffb0aeb3972 | 34 | |
samux | 0:6ffb0aeb3972 | 35 | public: |
samux | 0:6ffb0aeb3972 | 36 | /* |
samux | 0:6ffb0aeb3972 | 37 | * Constructor for joining open, wep or wpa secured networks |
samux | 0:6ffb0aeb3972 | 38 | * |
samux | 0:6ffb0aeb3972 | 39 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 0:6ffb0aeb3972 | 40 | * @param rx mbed pin to use for rx line of Serial interface |
samux | 0:6ffb0aeb3972 | 41 | * @param reset reset pin of the wifi module () |
samux | 0:6ffb0aeb3972 | 42 | * @param tcp_status connection status pin of the wifi module (GPIO 6) |
samux | 0:6ffb0aeb3972 | 43 | * @param ssid ssid of the network |
samux | 0:6ffb0aeb3972 | 44 | * @param phrase WEP or WPA key |
samux | 0:6ffb0aeb3972 | 45 | * @param wpa true if wpa security false otherwise |
samux | 0:6ffb0aeb3972 | 46 | */ |
samux | 0:6ffb0aeb3972 | 47 | Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, bool wpa); |
samux | 0:6ffb0aeb3972 | 48 | |
samux | 0:6ffb0aeb3972 | 49 | /* |
samux | 0:6ffb0aeb3972 | 50 | * 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 | 0:6ffb0aeb3972 | 51 | * Useful to send a command to the module and wait a response. |
samux | 0:6ffb0aeb3972 | 52 | * |
samux | 0:6ffb0aeb3972 | 53 | * |
samux | 0:6ffb0aeb3972 | 54 | * @param str string to be sent |
samux | 0:6ffb0aeb3972 | 55 | * @param len string length |
samux | 0:6ffb0aeb3972 | 56 | * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") |
samux | 0:6ffb0aeb3972 | 57 | * @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 | 0:6ffb0aeb3972 | 58 | * |
samux | 0:6ffb0aeb3972 | 59 | * @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 | 0:6ffb0aeb3972 | 60 | */ |
samux | 0:6ffb0aeb3972 | 61 | int send(const char * str, int len, const char * ACK = NULL, char * res = NULL); |
samux | 0:6ffb0aeb3972 | 62 | |
samux | 0:6ffb0aeb3972 | 63 | /* |
samux | 0:6ffb0aeb3972 | 64 | * Connect the wifi module to the ssid contained in the constructor. |
samux | 0:6ffb0aeb3972 | 65 | * |
samux | 0:6ffb0aeb3972 | 66 | * @return true if connected, false otherwise |
samux | 0:6ffb0aeb3972 | 67 | */ |
samux | 0:6ffb0aeb3972 | 68 | bool join(); |
samux | 0:6ffb0aeb3972 | 69 | |
samux | 0:6ffb0aeb3972 | 70 | /* |
samux | 0:6ffb0aeb3972 | 71 | * Close a connection with the access point |
samux | 0:6ffb0aeb3972 | 72 | * |
samux | 0:6ffb0aeb3972 | 73 | * @ returns true if successful |
samux | 0:6ffb0aeb3972 | 74 | */ |
samux | 0:6ffb0aeb3972 | 75 | bool leave(); |
samux | 0:6ffb0aeb3972 | 76 | |
samux | 0:6ffb0aeb3972 | 77 | /* |
samux | 0:6ffb0aeb3972 | 78 | * Read a string if available |
samux | 0:6ffb0aeb3972 | 79 | * |
samux | 0:6ffb0aeb3972 | 80 | *@param str pointer where will be stored the string read |
samux | 0:6ffb0aeb3972 | 81 | */ |
samux | 0:6ffb0aeb3972 | 82 | bool read(char * str); |
samux | 0:6ffb0aeb3972 | 83 | |
samux | 0:6ffb0aeb3972 | 84 | /* |
samux | 0:6ffb0aeb3972 | 85 | * Reset the wifi module |
samux | 0:6ffb0aeb3972 | 86 | */ |
samux | 0:6ffb0aeb3972 | 87 | void reset(); |
samux | 0:6ffb0aeb3972 | 88 | |
samux | 0:6ffb0aeb3972 | 89 | /* |
samux | 0:6ffb0aeb3972 | 90 | * Check if characters are available |
samux | 0:6ffb0aeb3972 | 91 | * |
samux | 0:6ffb0aeb3972 | 92 | * @return number of available characters |
samux | 0:6ffb0aeb3972 | 93 | */ |
samux | 0:6ffb0aeb3972 | 94 | int readable(); |
samux | 0:6ffb0aeb3972 | 95 | |
samux | 0:6ffb0aeb3972 | 96 | /* |
samux | 0:6ffb0aeb3972 | 97 | * Check if characters are available |
samux | 0:6ffb0aeb3972 | 98 | * |
samux | 0:6ffb0aeb3972 | 99 | * @return number of available characters |
samux | 0:6ffb0aeb3972 | 100 | */ |
samux | 0:6ffb0aeb3972 | 101 | int writeable(); |
samux | 0:6ffb0aeb3972 | 102 | |
samux | 0:6ffb0aeb3972 | 103 | /* |
samux | 0:6ffb0aeb3972 | 104 | * Check if a tcp link is active |
samux | 0:6ffb0aeb3972 | 105 | * |
samux | 0:6ffb0aeb3972 | 106 | * @returns true if successful |
samux | 0:6ffb0aeb3972 | 107 | */ |
samux | 0:6ffb0aeb3972 | 108 | bool is_connected(); |
samux | 0:6ffb0aeb3972 | 109 | |
samux | 0:6ffb0aeb3972 | 110 | /* |
samux | 0:6ffb0aeb3972 | 111 | * Read a character |
samux | 0:6ffb0aeb3972 | 112 | * |
samux | 0:6ffb0aeb3972 | 113 | * @return the character read |
samux | 0:6ffb0aeb3972 | 114 | */ |
samux | 0:6ffb0aeb3972 | 115 | char getc(); |
samux | 0:6ffb0aeb3972 | 116 | |
samux | 0:6ffb0aeb3972 | 117 | /* |
samux | 0:6ffb0aeb3972 | 118 | * Flush the buffer |
samux | 0:6ffb0aeb3972 | 119 | */ |
samux | 0:6ffb0aeb3972 | 120 | void flush(); |
samux | 0:6ffb0aeb3972 | 121 | |
samux | 0:6ffb0aeb3972 | 122 | /* |
samux | 0:6ffb0aeb3972 | 123 | * Write a character |
samux | 0:6ffb0aeb3972 | 124 | * |
samux | 0:6ffb0aeb3972 | 125 | * @param the character which will be written |
samux | 0:6ffb0aeb3972 | 126 | */ |
samux | 0:6ffb0aeb3972 | 127 | int putc(char c); |
samux | 0:6ffb0aeb3972 | 128 | |
samux | 0:6ffb0aeb3972 | 129 | |
samux | 0:6ffb0aeb3972 | 130 | /* |
samux | 0:6ffb0aeb3972 | 131 | * To enter in command mode (we can configure the module) |
samux | 0:6ffb0aeb3972 | 132 | * |
samux | 0:6ffb0aeb3972 | 133 | * @return true if successful, false otherwise |
samux | 0:6ffb0aeb3972 | 134 | */ |
samux | 0:6ffb0aeb3972 | 135 | bool cmdMode(); |
samux | 0:6ffb0aeb3972 | 136 | |
samux | 0:6ffb0aeb3972 | 137 | /* |
samux | 0:6ffb0aeb3972 | 138 | * To exit the command mode |
samux | 0:6ffb0aeb3972 | 139 | * |
samux | 0:6ffb0aeb3972 | 140 | * @return true if successful, false otherwise |
samux | 0:6ffb0aeb3972 | 141 | */ |
samux | 0:6ffb0aeb3972 | 142 | bool exit(); |
samux | 0:6ffb0aeb3972 | 143 | |
samux | 0:6ffb0aeb3972 | 144 | /* |
samux | 0:6ffb0aeb3972 | 145 | * Close a tcp connection |
samux | 0:6ffb0aeb3972 | 146 | * |
samux | 0:6ffb0aeb3972 | 147 | * @ returns true if successful |
samux | 0:6ffb0aeb3972 | 148 | */ |
samux | 0:6ffb0aeb3972 | 149 | bool close(); |
samux | 0:6ffb0aeb3972 | 150 | |
samux | 0:6ffb0aeb3972 | 151 | /* |
samux | 0:6ffb0aeb3972 | 152 | * Reboot the module |
samux | 0:6ffb0aeb3972 | 153 | * |
samux | 0:6ffb0aeb3972 | 154 | * @returns true if successful |
samux | 0:6ffb0aeb3972 | 155 | */ |
samux | 0:6ffb0aeb3972 | 156 | bool reboot(); |
samux | 0:6ffb0aeb3972 | 157 | |
samux | 0:6ffb0aeb3972 | 158 | /* |
samux | 0:6ffb0aeb3972 | 159 | * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode |
samux | 0:6ffb0aeb3972 | 160 | * |
samux | 0:6ffb0aeb3972 | 161 | * @param str string to be sent |
samux | 0:6ffb0aeb3972 | 162 | * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") |
samux | 0:6ffb0aeb3972 | 163 | * @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 | 0:6ffb0aeb3972 | 164 | * |
samux | 0:6ffb0aeb3972 | 165 | * @returns true if successful |
samux | 0:6ffb0aeb3972 | 166 | */ |
samux | 0:6ffb0aeb3972 | 167 | bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL); |
samux | 0:6ffb0aeb3972 | 168 | |
samux | 0:6ffb0aeb3972 | 169 | static Wifly * getInstance() {return inst;}; |
samux | 0:6ffb0aeb3972 | 170 | |
samux | 0:6ffb0aeb3972 | 171 | protected: |
samux | 0:6ffb0aeb3972 | 172 | Serial wifi; |
samux | 0:6ffb0aeb3972 | 173 | DigitalOut reset_pin; |
samux | 0:6ffb0aeb3972 | 174 | DigitalIn tcp_status; |
samux | 0:6ffb0aeb3972 | 175 | bool wpa; |
samux | 0:6ffb0aeb3972 | 176 | bool dhcp; |
samux | 0:6ffb0aeb3972 | 177 | const char * phrase; |
samux | 0:6ffb0aeb3972 | 178 | const char * ssid; |
samux | 0:6ffb0aeb3972 | 179 | const char * ip; |
samux | 0:6ffb0aeb3972 | 180 | const char * netmask; |
samux | 0:6ffb0aeb3972 | 181 | const char * gateway; |
samux | 0:6ffb0aeb3972 | 182 | int channel; |
samux | 0:6ffb0aeb3972 | 183 | CircBuffer<char> buf_wifly; |
samux | 0:6ffb0aeb3972 | 184 | |
samux | 0:6ffb0aeb3972 | 185 | bool cmd_mode; |
samux | 0:6ffb0aeb3972 | 186 | |
samux | 0:6ffb0aeb3972 | 187 | static Wifly * inst; |
samux | 0:6ffb0aeb3972 | 188 | |
samux | 0:6ffb0aeb3972 | 189 | void attach_rx(bool null); |
samux | 0:6ffb0aeb3972 | 190 | void handler_rx(void); |
samux | 0:6ffb0aeb3972 | 191 | }; |
samux | 0:6ffb0aeb3972 | 192 | |
samux | 0:6ffb0aeb3972 | 193 | #endif |