A library for talking to Multi-Tech's Cellular SocketModem Devices.

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Committer:
jengbrecht
Date:
Sat Jan 04 00:55:16 2014 +0000
Revision:
138:a233b9775f93
Parent:
131:da8f0e1c264a
Child:
139:73a7d1cd2e9c
Added the ability on open call within Wifi to except a URL in addition to an IP address.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jengbrecht 69:f3e696bbb0d5 1 #ifndef WIFI_H
jengbrecht 69:f3e696bbb0d5 2 #define WIFI_H
jengbrecht 69:f3e696bbb0d5 3
jengbrecht 69:f3e696bbb0d5 4 #include "IPStack.h"
jengbrecht 69:f3e696bbb0d5 5 #include "MTSBufferedIO.h"
jengbrecht 69:f3e696bbb0d5 6 #include "mbed.h"
jengbrecht 69:f3e696bbb0d5 7 #include <string>
jengbrecht 138:a233b9775f93 8 #include <vector>
jengbrecht 69:f3e696bbb0d5 9
jengbrecht 69:f3e696bbb0d5 10 using namespace mts;
jengbrecht 69:f3e696bbb0d5 11
jengbrecht 108:554585370b4a 12 /** This is a class for communicating with a Roving Networks RN-171 Wifi module. This
jengbrecht 108:554585370b4a 13 * module comes in a variety of form factors including the Multi-Tech SocketShield.
jengbrecht 108:554585370b4a 14 * This class supports two main types of WiFi module interactions including:
jengbrecht 108:554585370b4a 15 * configuration and status command processing and TCP/UDP Socket
jengbrecht 108:554585370b4a 16 * data connections. It should be noted that while a data connection is open the module
jengbrecht 108:554585370b4a 17 * must be put in command mode before commands can be sent. This is handled within the class
jengbrecht 108:554585370b4a 18 * automatically for all native commands. This class also inherits from IPStack
jengbrecht 108:554585370b4a 19 * providing a common set of commands for communication devices that have an onboard
jengbrecht 108:554585370b4a 20 * IP Stack. It is also integrated with the standard mbed Sockets package and can therefore
jengbrecht 108:554585370b4a 21 * be used seamlessly with clients and services built on top of this interface already within
jengbrecht 108:554585370b4a 22 * the mbed library.
jengbrecht 108:554585370b4a 23 *
jengbrecht 108:554585370b4a 24 * All of the following examples use the Pin Names for the Freedom KL46Z board coupled with
jengbrecht 108:554585370b4a 25 * the SocketModem Shield Arduino compatible board. Please chage Pin Names accordingly to
jengbrecht 108:554585370b4a 26 * match your hardware configuration. The default baud rate for the WiFi module is 9600 bps.
jengbrecht 108:554585370b4a 27 *
jengbrecht 120:3051dd49fa3a 28 * The following example shows how to connect to a WiFi netork and perform a basic ping test:
jengbrecht 120:3051dd49fa3a 29 * @code
jengbrecht 120:3051dd49fa3a 30 * #include "mbed.h"
jengbrecht 120:3051dd49fa3a 31 * #include "MTSSerial.h"
jengbrecht 120:3051dd49fa3a 32 * #include "Wifi.h"
jengbrecht 120:3051dd49fa3a 33 * using namespace mts;
jengbrecht 120:3051dd49fa3a 34 *
jengbrecht 120:3051dd49fa3a 35 * int main()
jengbrecht 120:3051dd49fa3a 36 * {
jengbrecht 120:3051dd49fa3a 37 * std::string ssid = "Your SSID goes here";
jengbrecht 120:3051dd49fa3a 38 * std::string securityKey = "Your secuirty key goes here";
jengbrecht 120:3051dd49fa3a 39 * Wifi::SecurityType securityType = Wifi::WPA2;
jengbrecht 138:a233b9775f93 40 *
jengbrecht 120:3051dd49fa3a 41 * //Wait for wifi module to boot up
jengbrecht 120:3051dd49fa3a 42 * for (int i = 10; i >= 0; i = i - 2) {
jengbrecht 120:3051dd49fa3a 43 * wait(2);
jengbrecht 120:3051dd49fa3a 44 * printf("Waiting %d seconds...\n\r", i);
jengbrecht 138:a233b9775f93 45 * }
jengbrecht 120:3051dd49fa3a 46 *
jengbrecht 120:3051dd49fa3a 47 * //Setup serial interface to WiFi module
jengbrecht 120:3051dd49fa3a 48 * MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
jengbrecht 120:3051dd49fa3a 49 * serial->baud(9600);
jengbrecht 138:a233b9775f93 50 *
jengbrecht 120:3051dd49fa3a 51 * //Setup Wifi class
jengbrecht 120:3051dd49fa3a 52 * Wifi* wifi = Wifi::getInstance();
jengbrecht 120:3051dd49fa3a 53 * printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");
jengbrecht 138:a233b9775f93 54 *
jengbrecht 120:3051dd49fa3a 55 * //Setup and check connection
jengbrecht 120:3051dd49fa3a 56 * printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
jengbrecht 120:3051dd49fa3a 57 * printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
jengbrecht 120:3051dd49fa3a 58 * printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure");
jengbrecht 120:3051dd49fa3a 59 * printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
jengbrecht 120:3051dd49fa3a 60 * printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");
jengbrecht 138:a233b9775f93 61 *
jengbrecht 120:3051dd49fa3a 62 * //Disconnect from network
jengbrecht 120:3051dd49fa3a 63 * printf("Disconnecting...\n\r");
jengbrecht 120:3051dd49fa3a 64 * wifi->disconnect();
jengbrecht 120:3051dd49fa3a 65 * printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
jengbrecht 138:a233b9775f93 66 *
jengbrecht 120:3051dd49fa3a 67 * printf("End Program\n\r");
jengbrecht 120:3051dd49fa3a 68 * }
jengbrecht 120:3051dd49fa3a 69 * @endcode
jengbrecht 108:554585370b4a 70 */
jengbrecht 69:f3e696bbb0d5 71 class Wifi : public IPStack
jengbrecht 69:f3e696bbb0d5 72 {
jengbrecht 69:f3e696bbb0d5 73 public:
jengbrecht 103:da58d27c15d7 74 ///An enumeration for all the supported WiFi security types.
jengbrecht 69:f3e696bbb0d5 75 enum SecurityType {
jengbrecht 69:f3e696bbb0d5 76 NONE, WEP64, WEP128, WPA, WPA2
jengbrecht 69:f3e696bbb0d5 77 };
jengbrecht 69:f3e696bbb0d5 78
jengbrecht 103:da58d27c15d7 79 /** Destructs a Wifi object and frees all related resources.
jengbrecht 103:da58d27c15d7 80 */
jengbrecht 69:f3e696bbb0d5 81 ~Wifi();
jengbrecht 69:f3e696bbb0d5 82
jengbrecht 103:da58d27c15d7 83 /** This static function is used to create or get a reference to a
jengbrecht 103:da58d27c15d7 84 * Wifi object. Wifi uses the singleton pattern, which means
jengbrecht 103:da58d27c15d7 85 * that you can only have one existing at a time. The first time you
jengbrecht 103:da58d27c15d7 86 * call getInstance this method creates a new uninitialized Wifi
jengbrecht 103:da58d27c15d7 87 * object and returns it. All future calls to this method will return
jengbrecht 103:da58d27c15d7 88 * a reference to the instance created during the first call. Note that
jengbrecht 103:da58d27c15d7 89 * you must call init on the returned instance before mnaking any other
jengbrecht 103:da58d27c15d7 90 * calls. If using this class's bindings to any of the Socket package
jengbrecht 103:da58d27c15d7 91 * classes like TCPSocketConnection, you must call this method and the
jengbrecht 103:da58d27c15d7 92 * init method on the returned object first, before even creating the
jengbrecht 103:da58d27c15d7 93 * other objects.
jengbrecht 103:da58d27c15d7 94 *
jengbrecht 103:da58d27c15d7 95 * @returns a reference to the single Wifi obect that has been created.
jengbrecht 103:da58d27c15d7 96 */
jengbrecht 69:f3e696bbb0d5 97 static Wifi* getInstance();
jengbrecht 103:da58d27c15d7 98
jengbrecht 103:da58d27c15d7 99 /** This method initializes the object with the underlying Wifi module
jengbrecht 103:da58d27c15d7 100 * interface to use. Note that this function MUST be called before
jengbrecht 103:da58d27c15d7 101 * any other calls will function correctly on a Wifi object. Also
jengbrecht 103:da58d27c15d7 102 * note that MTSBufferedIO is abstract, so you must use one of
jengbrecht 103:da58d27c15d7 103 * its inherited classes like MTSSerial or MTSSerialFlowControl.
jengbrecht 103:da58d27c15d7 104 *
jengbrecht 103:da58d27c15d7 105 * @param io the buffered io interface that is attached to the wifi
jengbrecht 103:da58d27c15d7 106 * radio module.
jengbrecht 103:da58d27c15d7 107 * @returns true if the init was successful, otherwise false.
jengbrecht 103:da58d27c15d7 108 */
jengbrecht 69:f3e696bbb0d5 109 bool init(MTSBufferedIO* io);
jengbrecht 69:f3e696bbb0d5 110
jengbrecht 103:da58d27c15d7 111 /** This method establishes a network connection on the Wif radio module.
jengbrecht 103:da58d27c15d7 112 * Note that before calling you NEED to first set the network information
jengbrecht 103:da58d27c15d7 113 * including WiFi SSID and optional security key using the setNetwork
jengbrecht 103:da58d27c15d7 114 * method.
jengbrecht 103:da58d27c15d7 115 *
jengbrecht 103:da58d27c15d7 116 * @returns true if the connection was successfully established, otherwise
jengbrecht 103:da58d27c15d7 117 * false on an error.
jengbrecht 103:da58d27c15d7 118 */
jengbrecht 69:f3e696bbb0d5 119 virtual bool connect();
jengbrecht 103:da58d27c15d7 120
jengbrecht 103:da58d27c15d7 121 /** This method is used to stop a previously established Wifi network connection.
jengbrecht 103:da58d27c15d7 122 */
jengbrecht 69:f3e696bbb0d5 123 virtual void disconnect();
jengbrecht 103:da58d27c15d7 124
jengbrecht 103:da58d27c15d7 125 /** This method is used to check if the radio currently has a Wifi network
jengbrecht 103:da58d27c15d7 126 * connection established.
jengbrecht 103:da58d27c15d7 127 *
jengbrecht 103:da58d27c15d7 128 * @returns true if a network connection exists, otherwise false.
jengbrecht 103:da58d27c15d7 129 */
jengbrecht 69:f3e696bbb0d5 130 virtual bool isConnected();
jengbrecht 69:f3e696bbb0d5 131
jengbrecht 69:f3e696bbb0d5 132 // TCP and UDP Socket related commands
jengbrecht 69:f3e696bbb0d5 133 // For behavior of the following methods refer to IPStack.h documentation
jengbrecht 69:f3e696bbb0d5 134 virtual bool bind(unsigned int port);
jengbrecht 69:f3e696bbb0d5 135 virtual bool open(const std::string& address, unsigned int port, Mode mode);
jengbrecht 69:f3e696bbb0d5 136 virtual bool isOpen();
jengbrecht 69:f3e696bbb0d5 137 virtual bool close();
jengbrecht 69:f3e696bbb0d5 138 virtual int read(char* data, int max, int timeout = -1);
jengbrecht 69:f3e696bbb0d5 139 virtual int write(const char* data, int length, int timeout = -1);
jengbrecht 69:f3e696bbb0d5 140 virtual unsigned int readable();
jengbrecht 69:f3e696bbb0d5 141 virtual unsigned int writeable();
jengbrecht 69:f3e696bbb0d5 142
jengbrecht 121:5a7fba896c98 143 /** This method performs a soft reboot of the device by issuing the
jengbrecht 121:5a7fba896c98 144 * reboot command. If the module is not able to respond to commands
jengbrecht 121:5a7fba896c98 145 * this will not work. This method also waits 10 seconds for the
jengbrecht 121:5a7fba896c98 146 * module to perform the reset and return.
jengbrecht 121:5a7fba896c98 147 */
jengbrecht 69:f3e696bbb0d5 148 virtual void reset();
jengbrecht 69:f3e696bbb0d5 149
jengbrecht 106:358972176b89 150 /** A method for sending a generic text command to the radio. Note that you cannot
jengbrecht 106:358972176b89 151 * send commands and have a socket connection at the same time, unless you first
jengbrecht 106:358972176b89 152 * switch to command mode.
jengbrecht 106:358972176b89 153 *
jengbrecht 106:358972176b89 154 * @param command the command to send to the WiFi module without the escape character.
jengbrecht 106:358972176b89 155 * @param timeoutMillis the time in millis to wait for a response before returning.
jengbrecht 106:358972176b89 156 * @param response the text string to look for and to return immediately after finding.
jengbrecht 106:358972176b89 157 * The default is to look for no specific response.
jengbrecht 106:358972176b89 158 * @param esc escape character to add at the end of the command, defaults to
jengbrecht 106:358972176b89 159 * carriage return (CR). Does not append any character if esc == 0.
jengbrecht 106:358972176b89 160 * @returns all data received from the radio after the command as a string.
jengbrecht 106:358972176b89 161 */
jengbrecht 93:aa7a48e65974 162 std::string sendCommand(std::string command, int timeoutMillis, std::string response = "", char esc = CR);
jengbrecht 106:358972176b89 163
jengbrecht 106:358972176b89 164 /** A method for sending a basic command to the radio. A basic text command is
jengbrecht 106:358972176b89 165 * one that simply has a response of either AOK or ERR without any other information.
jengbrecht 106:358972176b89 166 * Note that you cannot send commands and have a tcp connection at the same time
jengbrecht 106:358972176b89 167 * unless you first switch to command mode.
jengbrecht 106:358972176b89 168 *
jengbrecht 106:358972176b89 169 * @param command the command to send to the WiFi module without the escape character.
jengbrecht 106:358972176b89 170 * @param timeoutMillis the time in millis to wait for a response before returning.
jengbrecht 106:358972176b89 171 * @param esc escape character to add at the end of the command, defaults to
jengbrecht 106:358972176b89 172 * carriage return (CR).
jengbrecht 106:358972176b89 173 * @returns the standard Code enumeration.
jengbrecht 106:358972176b89 174 */
sgodinez 73:bb5bbca971ae 175 Code sendBasicCommand(std::string command, int timeoutMillis, char esc = CR);
jengbrecht 69:f3e696bbb0d5 176
jengbrecht 103:da58d27c15d7 177 /** This method is used to set the network information details. This method must be
jengbrecht 103:da58d27c15d7 178 * called before connect, which establishes the WiFi network connection.
jengbrecht 103:da58d27c15d7 179 *
jengbrecht 103:da58d27c15d7 180 * @param ssid the SSID for the network you want to attached to.
jengbrecht 103:da58d27c15d7 181 * @param type the type of security used on the network. The default is NONE.
jengbrecht 103:da58d27c15d7 182 * @param key the security key for the network. The default is no key.
jengbrecht 103:da58d27c15d7 183 */
jengbrecht 103:da58d27c15d7 184 Code setNetwork(const std::string& ssid, SecurityType type = NONE, const std::string& key = "");
jengbrecht 138:a233b9775f93 185
jengbrecht 106:358972176b89 186 /** This method is used to set the IP address or puts the module in DHCP mode.
jengbrecht 106:358972176b89 187 *
jengbrecht 106:358972176b89 188 * @param address the IP address you want to use in the form of xxx.xxx.xxx.xxx or DHCP
jengbrecht 106:358972176b89 189 * if you want to use DHCP. The default is DHCP.
jengbrecht 106:358972176b89 190 * @returns the standard Code enumeration.
jengbrecht 106:358972176b89 191 */
jengbrecht 106:358972176b89 192 Code setDeviceIP(std::string address = "DHCP");
jengbrecht 138:a233b9775f93 193
jengbrecht 106:358972176b89 194 /** This method is used to get the IP address of the device, which can be
jengbrecht 106:358972176b89 195 * set either statically or via DHCP after connecting to a network.
jengbrecht 106:358972176b89 196 *
jengbrecht 106:358972176b89 197 * @returns the devices IP address.
jengbrecht 106:358972176b89 198 */
jengbrecht 106:358972176b89 199 std::string getDeviceIP();
jengbrecht 103:da58d27c15d7 200
jengbrecht 103:da58d27c15d7 201 /** This method is used to set the DNS which enables the use of URLs instead
jengbrecht 103:da58d27c15d7 202 * of IP addresses when making a socket connection.
jengbrecht 103:da58d27c15d7 203 *
jengbrecht 103:da58d27c15d7 204 * @param the DNS server address as a string in form xxx.xxx.xxx.xxx.
jengbrecht 103:da58d27c15d7 205 * @returns the standard AT Code enumeration.
jengbrecht 103:da58d27c15d7 206 */
jengbrecht 94:1baa587e89ae 207 Code setDNS(const std::string& dnsName);
jengbrecht 103:da58d27c15d7 208
jengbrecht 103:da58d27c15d7 209 /** A method for getting the signal strength of the Wifi module. This method allows
jengbrecht 103:da58d27c15d7 210 * you to get the signal strength in dBm. If you get a result of 99 the signal strength
jengbrecht 103:da58d27c15d7 211 * is not known or there was an error in reading it. Note that you cannot read the signal
jengbrecht 103:da58d27c15d7 212 * strength unless you are already attached to a Wifi network.
jengbrecht 103:da58d27c15d7 213 *
jengbrecht 103:da58d27c15d7 214 * @returns an integer representing the signal strength in dBm.
jengbrecht 103:da58d27c15d7 215 */
jengbrecht 69:f3e696bbb0d5 216 int getSignalStrength();
jengbrecht 103:da58d27c15d7 217
jengbrecht 103:da58d27c15d7 218 /** This method is used test network connectivity by pinging a server.
jengbrecht 103:da58d27c15d7 219 *
jengbrecht 103:da58d27c15d7 220 * @param address the address of the server in format xxx.xxx.xxx.xxx.
jengbrecht 103:da58d27c15d7 221 * @returns true if the ping was successful, otherwise false.
jengbrecht 103:da58d27c15d7 222 */
jengbrecht 95:4fdf968b5b37 223 bool ping(const std::string& address = "8.8.8.8");
jengbrecht 103:da58d27c15d7 224
jengbrecht 103:da58d27c15d7 225 /** This method is used to set whether the device is in command mode or data mode.
jengbrecht 103:da58d27c15d7 226 * In command mode you are able to send configuration and status commands while
jengbrecht 103:da58d27c15d7 227 * data mode is used for sending data when you have an open socket connection.
jengbrecht 103:da58d27c15d7 228 * Note that for all other methods in this class the change is handled automatically.
jengbrecht 103:da58d27c15d7 229 * Only use this methodif you want to send your own commands that are not already
jengbrecht 103:da58d27c15d7 230 * supported and need to make sure that you are in command mode.
jengbrecht 103:da58d27c15d7 231 *
jengbrecht 103:da58d27c15d7 232 * @param on if true sets to command mode, otherwise to data mode.
jengbrecht 103:da58d27c15d7 233 * @returns true if the change was successful, otherwise false.
jengbrecht 103:da58d27c15d7 234 */
jengbrecht 79:f356009dbc12 235 bool setCmdMode(bool on);
jengbrecht 69:f3e696bbb0d5 236
jengbrecht 69:f3e696bbb0d5 237 private:
jengbrecht 69:f3e696bbb0d5 238 static Wifi* instance; //Static pointer to the single Cellular object.
jengbrecht 69:f3e696bbb0d5 239
jengbrecht 69:f3e696bbb0d5 240 MTSBufferedIO* io; //IO interface obect that the radio is accessed through.
jengbrecht 69:f3e696bbb0d5 241
jengbrecht 69:f3e696bbb0d5 242 bool wifiConnected; //Specifies if a Wifi network session is currently connected.
jengbrecht 69:f3e696bbb0d5 243 std::string _ssid; //A string that holds the SSID for the Wifi module.
jengbrecht 69:f3e696bbb0d5 244
jengbrecht 69:f3e696bbb0d5 245 Mode mode; //The current socket Mode.
jengbrecht 69:f3e696bbb0d5 246 bool socketOpened; //Specifies if a Socket is presently opened.
jengbrecht 69:f3e696bbb0d5 247 bool socketCloseable; //Specifies is a Socket can be closed.
jengbrecht 69:f3e696bbb0d5 248 unsigned int local_port; //Holds the local port for socket connections.
jengbrecht 69:f3e696bbb0d5 249 std::string local_address; //Holds the local address for socket connections.
jengbrecht 69:f3e696bbb0d5 250 unsigned int host_port; //Holds the remote port for socket connections.
jengbrecht 69:f3e696bbb0d5 251 std::string host_address; //Holds the remote address for socket connections.
jengbrecht 103:da58d27c15d7 252 bool cmdOn; //Determines whether the device is in command mode or not
jengbrecht 69:f3e696bbb0d5 253
jengbrecht 69:f3e696bbb0d5 254 Wifi(); //Private constructor, use the getInstance() method.
jengbrecht 69:f3e696bbb0d5 255 Wifi(MTSBufferedIO* io); //Private constructor, use the getInstance() method.
sam_grove 131:da8f0e1c264a 256 bool sortInterfaceMode(void); // module gets in wierd state without IO reset
jengbrecht 138:a233b9775f93 257 std::string getHostByName(std::string url); // Gets the IP address for a URL
jengbrecht 69:f3e696bbb0d5 258 };
jengbrecht 69:f3e696bbb0d5 259
jengbrecht 69:f3e696bbb0d5 260 #endif /* WIFI_H */