Control a robot over the internet using UDP and a Wifly module (WiFi).
Dependencies: Motor TextLCD WiflyInterface mbed-rtos mbed
WiflyInterface/Wifly/Wifly.h@0:c0dc3a76f3d4, 2013-10-17 (annotated)
- Committer:
- apatel336
- Date:
- Thu Oct 17 13:27:56 2013 +0000
- Revision:
- 0:c0dc3a76f3d4
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apatel336 | 0:c0dc3a76f3d4 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
apatel336 | 0:c0dc3a76f3d4 | 2 | * |
apatel336 | 0:c0dc3a76f3d4 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
apatel336 | 0:c0dc3a76f3d4 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
apatel336 | 0:c0dc3a76f3d4 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
apatel336 | 0:c0dc3a76f3d4 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
apatel336 | 0:c0dc3a76f3d4 | 7 | * furnished to do so, subject to the following conditions: |
apatel336 | 0:c0dc3a76f3d4 | 8 | * |
apatel336 | 0:c0dc3a76f3d4 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
apatel336 | 0:c0dc3a76f3d4 | 10 | * substantial portions of the Software. |
apatel336 | 0:c0dc3a76f3d4 | 11 | * |
apatel336 | 0:c0dc3a76f3d4 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
apatel336 | 0:c0dc3a76f3d4 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
apatel336 | 0:c0dc3a76f3d4 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
apatel336 | 0:c0dc3a76f3d4 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
apatel336 | 0:c0dc3a76f3d4 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
apatel336 | 0:c0dc3a76f3d4 | 17 | * |
apatel336 | 0:c0dc3a76f3d4 | 18 | * @section DESCRIPTION |
apatel336 | 0:c0dc3a76f3d4 | 19 | * |
apatel336 | 0:c0dc3a76f3d4 | 20 | * Wifly RN131-C, wifi module |
apatel336 | 0:c0dc3a76f3d4 | 21 | * |
apatel336 | 0:c0dc3a76f3d4 | 22 | * Datasheet: |
apatel336 | 0:c0dc3a76f3d4 | 23 | * |
apatel336 | 0:c0dc3a76f3d4 | 24 | * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf |
apatel336 | 0:c0dc3a76f3d4 | 25 | */ |
apatel336 | 0:c0dc3a76f3d4 | 26 | |
apatel336 | 0:c0dc3a76f3d4 | 27 | #ifndef WIFLY_H |
apatel336 | 0:c0dc3a76f3d4 | 28 | #define WIFLY_H |
apatel336 | 0:c0dc3a76f3d4 | 29 | |
apatel336 | 0:c0dc3a76f3d4 | 30 | #include "mbed.h" |
apatel336 | 0:c0dc3a76f3d4 | 31 | #include "CBuffer.h" |
apatel336 | 0:c0dc3a76f3d4 | 32 | |
apatel336 | 0:c0dc3a76f3d4 | 33 | #define DEFAULT_WAIT_RESP_TIMEOUT 500 |
apatel336 | 0:c0dc3a76f3d4 | 34 | |
apatel336 | 0:c0dc3a76f3d4 | 35 | enum Security { |
apatel336 | 0:c0dc3a76f3d4 | 36 | NONE = 0, |
apatel336 | 0:c0dc3a76f3d4 | 37 | WEP_128 = 1, |
apatel336 | 0:c0dc3a76f3d4 | 38 | WPA = 3 |
apatel336 | 0:c0dc3a76f3d4 | 39 | }; |
apatel336 | 0:c0dc3a76f3d4 | 40 | |
apatel336 | 0:c0dc3a76f3d4 | 41 | enum Protocol { |
apatel336 | 0:c0dc3a76f3d4 | 42 | UDP = (1 << 0), |
apatel336 | 0:c0dc3a76f3d4 | 43 | TCP = (1 << 1) |
apatel336 | 0:c0dc3a76f3d4 | 44 | }; |
apatel336 | 0:c0dc3a76f3d4 | 45 | |
apatel336 | 0:c0dc3a76f3d4 | 46 | class Wifly |
apatel336 | 0:c0dc3a76f3d4 | 47 | { |
apatel336 | 0:c0dc3a76f3d4 | 48 | |
apatel336 | 0:c0dc3a76f3d4 | 49 | public: |
apatel336 | 0:c0dc3a76f3d4 | 50 | /* |
apatel336 | 0:c0dc3a76f3d4 | 51 | * Constructor |
apatel336 | 0:c0dc3a76f3d4 | 52 | * |
apatel336 | 0:c0dc3a76f3d4 | 53 | * @param tx mbed pin to use for tx line of Serial interface |
apatel336 | 0:c0dc3a76f3d4 | 54 | * @param rx mbed pin to use for rx line of Serial interface |
apatel336 | 0:c0dc3a76f3d4 | 55 | * @param reset reset pin of the wifi module () |
apatel336 | 0:c0dc3a76f3d4 | 56 | * @param tcp_status connection status pin of the wifi module (GPIO 6) |
apatel336 | 0:c0dc3a76f3d4 | 57 | * @param ssid ssid of the network |
apatel336 | 0:c0dc3a76f3d4 | 58 | * @param phrase WEP or WPA key |
apatel336 | 0:c0dc3a76f3d4 | 59 | * @param sec Security type (NONE, WEP_128 or WPA) |
apatel336 | 0:c0dc3a76f3d4 | 60 | */ |
apatel336 | 0:c0dc3a76f3d4 | 61 | Wifly( PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec); |
apatel336 | 0:c0dc3a76f3d4 | 62 | |
apatel336 | 0:c0dc3a76f3d4 | 63 | /* |
apatel336 | 0:c0dc3a76f3d4 | 64 | * Connect the wifi module to the ssid contained in the constructor. |
apatel336 | 0:c0dc3a76f3d4 | 65 | * |
apatel336 | 0:c0dc3a76f3d4 | 66 | * @return true if connected, false otherwise |
apatel336 | 0:c0dc3a76f3d4 | 67 | */ |
apatel336 | 0:c0dc3a76f3d4 | 68 | bool join(); |
apatel336 | 0:c0dc3a76f3d4 | 69 | |
apatel336 | 0:c0dc3a76f3d4 | 70 | /* |
apatel336 | 0:c0dc3a76f3d4 | 71 | * Disconnect the wifly module from the access point |
apatel336 | 0:c0dc3a76f3d4 | 72 | * |
apatel336 | 0:c0dc3a76f3d4 | 73 | * @ returns true if successful |
apatel336 | 0:c0dc3a76f3d4 | 74 | */ |
apatel336 | 0:c0dc3a76f3d4 | 75 | bool disconnect(); |
apatel336 | 0:c0dc3a76f3d4 | 76 | |
apatel336 | 0:c0dc3a76f3d4 | 77 | /* |
apatel336 | 0:c0dc3a76f3d4 | 78 | * Open a tcp connection with the specified host on the specified port |
apatel336 | 0:c0dc3a76f3d4 | 79 | * |
apatel336 | 0:c0dc3a76f3d4 | 80 | * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) |
apatel336 | 0:c0dc3a76f3d4 | 81 | * @param port port |
apatel336 | 0:c0dc3a76f3d4 | 82 | * @ returns true if successful |
apatel336 | 0:c0dc3a76f3d4 | 83 | */ |
apatel336 | 0:c0dc3a76f3d4 | 84 | bool connect(const char * host, int port); |
apatel336 | 0:c0dc3a76f3d4 | 85 | |
apatel336 | 0:c0dc3a76f3d4 | 86 | |
apatel336 | 0:c0dc3a76f3d4 | 87 | /* |
apatel336 | 0:c0dc3a76f3d4 | 88 | * Set the protocol (UDP or TCP) |
apatel336 | 0:c0dc3a76f3d4 | 89 | * |
apatel336 | 0:c0dc3a76f3d4 | 90 | * @param p protocol |
apatel336 | 0:c0dc3a76f3d4 | 91 | * @ returns true if successful |
apatel336 | 0:c0dc3a76f3d4 | 92 | */ |
apatel336 | 0:c0dc3a76f3d4 | 93 | bool setProtocol(Protocol p); |
apatel336 | 0:c0dc3a76f3d4 | 94 | |
apatel336 | 0:c0dc3a76f3d4 | 95 | /* |
apatel336 | 0:c0dc3a76f3d4 | 96 | * Reset the wifi module |
apatel336 | 0:c0dc3a76f3d4 | 97 | */ |
apatel336 | 0:c0dc3a76f3d4 | 98 | void reset(); |
apatel336 | 0:c0dc3a76f3d4 | 99 | |
apatel336 | 0:c0dc3a76f3d4 | 100 | /* |
apatel336 | 0:c0dc3a76f3d4 | 101 | * Reboot the wifi module |
apatel336 | 0:c0dc3a76f3d4 | 102 | */ |
apatel336 | 0:c0dc3a76f3d4 | 103 | bool reboot(); |
apatel336 | 0:c0dc3a76f3d4 | 104 | |
apatel336 | 0:c0dc3a76f3d4 | 105 | /* |
apatel336 | 0:c0dc3a76f3d4 | 106 | * Check if characters are available |
apatel336 | 0:c0dc3a76f3d4 | 107 | * |
apatel336 | 0:c0dc3a76f3d4 | 108 | * @return number of available characters |
apatel336 | 0:c0dc3a76f3d4 | 109 | */ |
apatel336 | 0:c0dc3a76f3d4 | 110 | int readable(); |
apatel336 | 0:c0dc3a76f3d4 | 111 | |
apatel336 | 0:c0dc3a76f3d4 | 112 | /* |
apatel336 | 0:c0dc3a76f3d4 | 113 | * Check if characters are available |
apatel336 | 0:c0dc3a76f3d4 | 114 | * |
apatel336 | 0:c0dc3a76f3d4 | 115 | * @return number of available characters |
apatel336 | 0:c0dc3a76f3d4 | 116 | */ |
apatel336 | 0:c0dc3a76f3d4 | 117 | int writeable(); |
apatel336 | 0:c0dc3a76f3d4 | 118 | |
apatel336 | 0:c0dc3a76f3d4 | 119 | /* |
apatel336 | 0:c0dc3a76f3d4 | 120 | * Check if a tcp link is active |
apatel336 | 0:c0dc3a76f3d4 | 121 | * |
apatel336 | 0:c0dc3a76f3d4 | 122 | * @returns true if successful |
apatel336 | 0:c0dc3a76f3d4 | 123 | */ |
apatel336 | 0:c0dc3a76f3d4 | 124 | bool is_connected(); |
apatel336 | 0:c0dc3a76f3d4 | 125 | |
apatel336 | 0:c0dc3a76f3d4 | 126 | /* |
apatel336 | 0:c0dc3a76f3d4 | 127 | * Read a character |
apatel336 | 0:c0dc3a76f3d4 | 128 | * |
apatel336 | 0:c0dc3a76f3d4 | 129 | * @return the character read |
apatel336 | 0:c0dc3a76f3d4 | 130 | */ |
apatel336 | 0:c0dc3a76f3d4 | 131 | char getc(); |
apatel336 | 0:c0dc3a76f3d4 | 132 | |
apatel336 | 0:c0dc3a76f3d4 | 133 | /* |
apatel336 | 0:c0dc3a76f3d4 | 134 | * Flush the buffer |
apatel336 | 0:c0dc3a76f3d4 | 135 | */ |
apatel336 | 0:c0dc3a76f3d4 | 136 | void flush(); |
apatel336 | 0:c0dc3a76f3d4 | 137 | |
apatel336 | 0:c0dc3a76f3d4 | 138 | /* |
apatel336 | 0:c0dc3a76f3d4 | 139 | * Write a character |
apatel336 | 0:c0dc3a76f3d4 | 140 | * |
apatel336 | 0:c0dc3a76f3d4 | 141 | * @param the character which will be written |
apatel336 | 0:c0dc3a76f3d4 | 142 | */ |
apatel336 | 0:c0dc3a76f3d4 | 143 | int putc(char c); |
apatel336 | 0:c0dc3a76f3d4 | 144 | |
apatel336 | 0:c0dc3a76f3d4 | 145 | |
apatel336 | 0:c0dc3a76f3d4 | 146 | /* |
apatel336 | 0:c0dc3a76f3d4 | 147 | * To enter in command mode (we can configure the module) |
apatel336 | 0:c0dc3a76f3d4 | 148 | * |
apatel336 | 0:c0dc3a76f3d4 | 149 | * @return true if successful, false otherwise |
apatel336 | 0:c0dc3a76f3d4 | 150 | */ |
apatel336 | 0:c0dc3a76f3d4 | 151 | bool cmdMode(); |
apatel336 | 0:c0dc3a76f3d4 | 152 | |
apatel336 | 0:c0dc3a76f3d4 | 153 | /* |
apatel336 | 0:c0dc3a76f3d4 | 154 | * To exit the command mode |
apatel336 | 0:c0dc3a76f3d4 | 155 | * |
apatel336 | 0:c0dc3a76f3d4 | 156 | * @return true if successful, false otherwise |
apatel336 | 0:c0dc3a76f3d4 | 157 | */ |
apatel336 | 0:c0dc3a76f3d4 | 158 | bool exit(); |
apatel336 | 0:c0dc3a76f3d4 | 159 | |
apatel336 | 0:c0dc3a76f3d4 | 160 | /* |
apatel336 | 0:c0dc3a76f3d4 | 161 | * Close a tcp connection |
apatel336 | 0:c0dc3a76f3d4 | 162 | * |
apatel336 | 0:c0dc3a76f3d4 | 163 | * @ returns true if successful |
apatel336 | 0:c0dc3a76f3d4 | 164 | */ |
apatel336 | 0:c0dc3a76f3d4 | 165 | bool close(); |
apatel336 | 0:c0dc3a76f3d4 | 166 | |
apatel336 | 0:c0dc3a76f3d4 | 167 | /* |
apatel336 | 0:c0dc3a76f3d4 | 168 | * 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. |
apatel336 | 0:c0dc3a76f3d4 | 169 | * Useful to send a command to the module and wait a response. |
apatel336 | 0:c0dc3a76f3d4 | 170 | * |
apatel336 | 0:c0dc3a76f3d4 | 171 | * |
apatel336 | 0:c0dc3a76f3d4 | 172 | * @param str string to be sent |
apatel336 | 0:c0dc3a76f3d4 | 173 | * @param len string length |
apatel336 | 0:c0dc3a76f3d4 | 174 | * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") |
apatel336 | 0:c0dc3a76f3d4 | 175 | * @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) |
apatel336 | 0:c0dc3a76f3d4 | 176 | * |
apatel336 | 0:c0dc3a76f3d4 | 177 | * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s. |
apatel336 | 0:c0dc3a76f3d4 | 178 | */ |
apatel336 | 0:c0dc3a76f3d4 | 179 | int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT); |
apatel336 | 0:c0dc3a76f3d4 | 180 | |
apatel336 | 0:c0dc3a76f3d4 | 181 | /* |
apatel336 | 0:c0dc3a76f3d4 | 182 | * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode |
apatel336 | 0:c0dc3a76f3d4 | 183 | * |
apatel336 | 0:c0dc3a76f3d4 | 184 | * @param str string to be sent |
apatel336 | 0:c0dc3a76f3d4 | 185 | * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO") |
apatel336 | 0:c0dc3a76f3d4 | 186 | * @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) |
apatel336 | 0:c0dc3a76f3d4 | 187 | * |
apatel336 | 0:c0dc3a76f3d4 | 188 | * @returns true if successful |
apatel336 | 0:c0dc3a76f3d4 | 189 | */ |
apatel336 | 0:c0dc3a76f3d4 | 190 | bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT); |
apatel336 | 0:c0dc3a76f3d4 | 191 | |
apatel336 | 0:c0dc3a76f3d4 | 192 | /* |
apatel336 | 0:c0dc3a76f3d4 | 193 | * Return true if the module is using dhcp |
apatel336 | 0:c0dc3a76f3d4 | 194 | * |
apatel336 | 0:c0dc3a76f3d4 | 195 | * @returns true if the module is using dhcp |
apatel336 | 0:c0dc3a76f3d4 | 196 | */ |
apatel336 | 0:c0dc3a76f3d4 | 197 | bool isDHCP() { |
apatel336 | 0:c0dc3a76f3d4 | 198 | return state.dhcp; |
apatel336 | 0:c0dc3a76f3d4 | 199 | } |
apatel336 | 0:c0dc3a76f3d4 | 200 | |
apatel336 | 0:c0dc3a76f3d4 | 201 | bool gethostbyname(const char * host, char * ip); |
apatel336 | 0:c0dc3a76f3d4 | 202 | |
apatel336 | 0:c0dc3a76f3d4 | 203 | static Wifly * getInstance() { |
apatel336 | 0:c0dc3a76f3d4 | 204 | return inst; |
apatel336 | 0:c0dc3a76f3d4 | 205 | }; |
apatel336 | 0:c0dc3a76f3d4 | 206 | |
apatel336 | 0:c0dc3a76f3d4 | 207 | protected: |
apatel336 | 0:c0dc3a76f3d4 | 208 | Serial wifi; |
apatel336 | 0:c0dc3a76f3d4 | 209 | DigitalOut reset_pin; |
apatel336 | 0:c0dc3a76f3d4 | 210 | DigitalIn tcp_status; |
apatel336 | 0:c0dc3a76f3d4 | 211 | char phrase[30]; |
apatel336 | 0:c0dc3a76f3d4 | 212 | char ssid[30]; |
apatel336 | 0:c0dc3a76f3d4 | 213 | const char * ip; |
apatel336 | 0:c0dc3a76f3d4 | 214 | const char * netmask; |
apatel336 | 0:c0dc3a76f3d4 | 215 | const char * gateway; |
apatel336 | 0:c0dc3a76f3d4 | 216 | int channel; |
apatel336 | 0:c0dc3a76f3d4 | 217 | CircBuffer<char> buf_wifly; |
apatel336 | 0:c0dc3a76f3d4 | 218 | |
apatel336 | 0:c0dc3a76f3d4 | 219 | static Wifly * inst; |
apatel336 | 0:c0dc3a76f3d4 | 220 | |
apatel336 | 0:c0dc3a76f3d4 | 221 | void attach_rx(bool null); |
apatel336 | 0:c0dc3a76f3d4 | 222 | void handler_rx(void); |
apatel336 | 0:c0dc3a76f3d4 | 223 | |
apatel336 | 0:c0dc3a76f3d4 | 224 | |
apatel336 | 0:c0dc3a76f3d4 | 225 | typedef struct STATE { |
apatel336 | 0:c0dc3a76f3d4 | 226 | bool associated; |
apatel336 | 0:c0dc3a76f3d4 | 227 | bool tcp; |
apatel336 | 0:c0dc3a76f3d4 | 228 | bool dhcp; |
apatel336 | 0:c0dc3a76f3d4 | 229 | Security sec; |
apatel336 | 0:c0dc3a76f3d4 | 230 | Protocol proto; |
apatel336 | 0:c0dc3a76f3d4 | 231 | bool cmd_mode; |
apatel336 | 0:c0dc3a76f3d4 | 232 | } State; |
apatel336 | 0:c0dc3a76f3d4 | 233 | |
apatel336 | 0:c0dc3a76f3d4 | 234 | State state; |
apatel336 | 0:c0dc3a76f3d4 | 235 | char * getStringSecurity(); |
apatel336 | 0:c0dc3a76f3d4 | 236 | }; |
apatel336 | 0:c0dc3a76f3d4 | 237 | |
apatel336 | 0:c0dc3a76f3d4 | 238 | #endif |