Control a robot over the internet using UDP and a Wifly module (WiFi).

Dependencies:   Motor TextLCD WiflyInterface mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wifly.h Source File

Wifly.h

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  *
00018  * @section DESCRIPTION
00019  *
00020  * Wifly RN131-C, wifi module
00021  *
00022  * Datasheet:
00023  *
00024  * http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Wireless/WiFi/WiFly-RN-UM.pdf
00025  */
00026 
00027 #ifndef WIFLY_H
00028 #define WIFLY_H
00029 
00030 #include "mbed.h"
00031 #include "CBuffer.h"
00032 
00033 #define DEFAULT_WAIT_RESP_TIMEOUT 500
00034 
00035 enum Security {
00036     NONE = 0,
00037     WEP_128 = 1,
00038     WPA = 3
00039 };
00040 
00041 enum Protocol {
00042     UDP = (1 << 0),
00043     TCP = (1 << 1)
00044 };
00045 
00046 class Wifly
00047 {
00048 
00049 public:
00050     /*
00051     * Constructor
00052     *
00053     * @param tx mbed pin to use for tx line of Serial interface
00054     * @param rx mbed pin to use for rx line of Serial interface
00055     * @param reset reset pin of the wifi module ()
00056     * @param tcp_status connection status pin of the wifi module (GPIO 6)
00057     * @param ssid ssid of the network
00058     * @param phrase WEP or WPA key
00059     * @param sec Security type (NONE, WEP_128 or WPA)
00060     */
00061     Wifly(  PinName tx, PinName rx, PinName reset, PinName tcp_status, const char * ssid, const char * phrase, Security sec);
00062 
00063     /*
00064     * Connect the wifi module to the ssid contained in the constructor.
00065     *
00066     * @return true if connected, false otherwise
00067     */
00068     bool join();
00069 
00070     /*
00071     * Disconnect the wifly module from the access point
00072     *
00073     * @ returns true if successful
00074     */
00075     bool disconnect();
00076 
00077     /*
00078     * Open a tcp connection with the specified host on the specified port
00079     *
00080     * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
00081     * @param port port
00082     * @ returns true if successful
00083     */
00084     bool connect(const char * host, int port);
00085 
00086 
00087     /*
00088     * Set the protocol (UDP or TCP)
00089     *
00090     * @param p protocol
00091     * @ returns true if successful
00092     */
00093     bool setProtocol(Protocol p);
00094 
00095     /*
00096     * Reset the wifi module
00097     */
00098     void reset();
00099     
00100     /*
00101     * Reboot the wifi module
00102     */
00103     bool reboot();
00104 
00105     /*
00106     * Check if characters are available
00107     *
00108     * @return number of available characters
00109     */
00110     int readable();
00111 
00112     /*
00113     * Check if characters are available
00114     *
00115     * @return number of available characters
00116     */
00117     int writeable();
00118 
00119     /*
00120     * Check if a tcp link is active
00121     *
00122     * @returns true if successful
00123     */
00124     bool is_connected();
00125 
00126     /*
00127     * Read a character
00128     *
00129     * @return the character read
00130     */
00131     char getc();
00132 
00133     /*
00134     * Flush the buffer
00135     */
00136     void flush();
00137 
00138     /*
00139     * Write a character
00140     *
00141     * @param the character which will be written
00142     */
00143     int putc(char c);
00144 
00145 
00146     /*
00147     * To enter in command mode (we can configure the module)
00148     *
00149     * @return true if successful, false otherwise
00150     */
00151     bool cmdMode();
00152 
00153     /*
00154     * To exit the command mode
00155     *
00156     * @return true if successful, false otherwise
00157     */
00158     bool exit();
00159 
00160     /*
00161     * Close a tcp connection
00162     *
00163     * @ returns true if successful
00164     */
00165     bool close();
00166 
00167     /*
00168     * 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.
00169     * Useful to send a command to the module and wait a response.
00170     *
00171     *
00172     * @param str string to be sent
00173     * @param len string length
00174     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00175     * @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)
00176     *
00177     * @return true if ACK has been found in the response from the wifi module. False otherwise or if there is no response in 5s.
00178     */
00179     int send(const char * str, int len, const char * ACK = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00180 
00181     /*
00182     * Send a command to the wify module. Check if the module is in command mode. If not enter in command mode
00183     *
00184     * @param str string to be sent
00185     * @param ACK string which must be acknowledge by the wifi module. If ACK == NULL, no string has to be acknoledged. (default: "NO")
00186     * @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)
00187     *
00188     * @returns true if successful
00189     */
00190     bool sendCommand(const char * cmd, const char * ack = NULL, char * res = NULL, int timeout = DEFAULT_WAIT_RESP_TIMEOUT);
00191     
00192     /*
00193     * Return true if the module is using dhcp
00194     *
00195     * @returns true if the module is using dhcp
00196     */
00197     bool isDHCP() {
00198         return state.dhcp;
00199     }
00200 
00201     bool gethostbyname(const char * host, char * ip);
00202 
00203     static Wifly * getInstance() {
00204         return inst;
00205     };
00206 
00207 protected:
00208     Serial wifi;
00209     DigitalOut reset_pin;
00210     DigitalIn tcp_status;
00211     char phrase[30];
00212     char ssid[30];
00213     const char * ip;
00214     const char * netmask;
00215     const char * gateway;
00216     int channel;
00217     CircBuffer<char> buf_wifly;
00218 
00219     static Wifly * inst;
00220 
00221     void attach_rx(bool null);
00222     void handler_rx(void);
00223 
00224 
00225     typedef struct STATE {
00226         bool associated;
00227         bool tcp;
00228         bool dhcp;
00229         Security sec;
00230         Protocol proto;
00231         bool cmd_mode;
00232     } State;
00233 
00234     State state;
00235     char * getStringSecurity();
00236 };
00237 
00238 #endif