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:
sam_grove
Date:
Fri Jan 03 05:00:54 2014 +0000
Revision:
131:da8f0e1c264a
Parent:
121:5a7fba896c98
Child:
138:a233b9775f93
Added DBG to wifi.cpp, implement reset in init() so hardware boots the same way each time as if reset IO pin was present. Loop until echo is off by response. All these changes seem to fix the rouge data after disconnect caused by module ERR: Net IF

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