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

Embed: (wiki syntax)

« Back to documentation index

Wifi Class Reference

This is a class for communicating with a Roving Networks RN-171 Wifi module. More...

#include <Wifi.h>

Inherits mts::IPStack.

Public Types

enum  SecurityType
 

An enumeration for all the supported WiFi security types.

More...
enum  Mode
 

An enumeration for selecting the Socket Mode of TCP or UDP.

More...

Public Member Functions

 ~Wifi ()
 Destructs a Wifi object and frees all related resources.
bool init (MTSBufferedIO *io)
 This method initializes the object with the underlying Wifi module interface to use.
virtual bool connect ()
 This method establishes a network connection on the Wif radio module.
virtual void disconnect ()
 This method is used to stop a previously established Wifi network connection.
virtual bool isConnected ()
 This method is used to check if the radio currently has a Wifi network connection established.
virtual bool bind (unsigned int port)
 This method is used to set the local port for the UDP or TCP socket connection.
virtual bool isOpen ()
 This method is used to determine if a socket connection is currently open.
virtual bool close ()
 This method is used to close a socket connection that is currently open.
virtual int read (char *data, int max, int timeout=-1)
 This method is used to read data off of a socket, assuming a valid socket connection is already open.
virtual int write (const char *data, int length, int timeout=-1)
 This method is used to write data to a socket, assuming a valid socket connection is already open.
virtual unsigned int readable ()
 This method is used to get the number of bytes available to read off the socket.
virtual unsigned int writeable ()
 This method is used to get the space available to write bytes to the socket.
virtual void reset ()
 This method performs a soft reboot of the device by issuing the reboot command.
std::string sendCommand (std::string command, int timeoutMillis, std::string response="", char esc=CR)
 A method for sending a generic text command to the radio.
Code sendBasicCommand (std::string command, int timeoutMillis, char esc=CR)
 A method for sending a basic command to the radio.
Code setNetwork (const std::string &ssid, SecurityType type=NONE, const std::string &key="")
 This method is used to set the network information details.
Code setDeviceIP (std::string address="DHCP")
 This method is used to set the IP address or puts the module in DHCP mode.
std::string getDeviceIP ()
 This method is used to get the IP address of the device, which can be set either statically or via DHCP after connecting to a network.
Code setDNS (const std::string &dnsName)
 This method is used to set the DNS which enables the use of URLs instead of IP addresses when making a socket connection.
int getSignalStrength ()
 A method for getting the signal strength of the Wifi module.
bool ping (const std::string &address="8.8.8.8")
 This method is used test network connectivity by pinging a server.
bool setCmdMode (bool on)
 This method is used to set whether the device is in command mode or data mode.
virtual bool open (const std::string &address, unsigned int port, Mode mode)=0
 This method is used to open a socket connection with the given parameters.

Static Public Member Functions

static WifigetInstance ()
 This static function is used to create or get a reference to a Wifi object.

Detailed Description

This is a class for communicating with a Roving Networks RN-171 Wifi module.

This module comes in a variety of form factors including the Multi-Tech SocketShield. This class supports two main types of WiFi module interactions including: configuration and status command processing and TCP Socket data connections. It should be noted that while a data connection is open the module must be put in command mode before commands can be sent. This is handled within the class automatically for all native commands. This class also inherits from IPStack providing a common set of commands for communication devices that have an onboard IP Stack. It is also integrated with the standard mbed Sockets package and can therefore be used seamlessly with clients and services built on top of this interface already within the mbed library.

All of the following examples use the Pin Names for the Freedom KL46Z board coupled with the SocketModem Shield Arduino compatible board. Please chage Pin Names accordingly to match your hardware configuration. The default baud rate for the WiFi module is 9600 bps.

The following example shows how to connect to a WiFi netork and perform a basic ping test:

 #include "mbed.h"
 #include "MTSSerial.h"
 #include "Wifi.h"
 using namespace mts;

 int main()
 {
   std::string ssid = "Your SSID goes here";
   std::string securityKey = "Your secuirty key goes here";
   Wifi::SecurityType securityType = Wifi::WPA2;

   //Wait for wifi module to boot up
   for (int i = 10; i >= 0; i = i - 2) {
       wait(2);
       printf("Waiting %d seconds...\n\r", i);
   }

   //Setup serial interface to WiFi module
   MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
   serial->baud(9600);

   //Setup Wifi class
   Wifi* wifi = Wifi::getInstance();
   printf("Init: %s\n\r", wifi->init(serial) ? "SUCCESS" : "FAILURE");

   //Setup and check connection
   printf("Set Network: %s\n\r", getCodeNames(wifi->setNetwork(ssid, securityType, securityKey)).c_str());
   printf("Set DHCP: %s\n\r", getCodeNames(wifi->setDeviceIP("DHCP")).c_str());
   printf("Connect: %s\n\r", wifi->connect() ? "Success" : "Failure");
   printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");
   printf("Ping Server: %s\n\r", wifi->ping("8.8.8.8") ? "Success" : "Failed");

   //Disconnect from network
   printf("Disconnecting...\n\r");
   wifi->disconnect();
   printf("Is Connected: %s\n\r", wifi->isConnected() ? "True" : "False");

   printf("End Program\n\r");
 }

Definition at line 87 of file Wifi.h.


Member Enumeration Documentation

enum Mode [inherited]

An enumeration for selecting the Socket Mode of TCP or UDP.

Definition at line 38 of file IPStack.h.

An enumeration for all the supported WiFi security types.

Definition at line 91 of file Wifi.h.


Constructor & Destructor Documentation

~Wifi (  )

Destructs a Wifi object and frees all related resources.

Definition at line 122 of file Wifi.cpp.


Member Function Documentation

bool bind ( unsigned int  port ) [virtual]

This method is used to set the local port for the UDP or TCP socket connection.

The connection can be made using the open method.

Parameters:
portthe local port of the socket as an int.

Implements IPStack.

Definition at line 230 of file Wifi.cpp.

bool close (  ) [virtual]

This method is used to close a socket connection that is currently open.

Returns:
true if successfully closed, otherwise returns false on an error.

Implements IPStack.

Definition at line 386 of file Wifi.cpp.

bool connect (  ) [virtual]

This method establishes a network connection on the Wif radio module.

Note that before calling you NEED to first set the network information including WiFi SSID and optional security key using the setNetwork method.

Returns:
true if the connection was successfully established, otherwise false on an error.

Implements IPStack.

Definition at line 126 of file Wifi.cpp.

void disconnect (  ) [virtual]

This method is used to stop a previously established Wifi network connection.

Implements IPStack.

Definition at line 175 of file Wifi.cpp.

std::string getDeviceIP (  )

This method is used to get the IP address of the device, which can be set either statically or via DHCP after connecting to a network.

Returns:
the devices IP address.

Definition at line 556 of file Wifi.cpp.

Wifi * getInstance (  ) [static]

This static function is used to create or get a reference to a Wifi object.

Wifi uses the singleton pattern, which means that you can only have one existing at a time. The first time you call getInstance this method creates a new uninitialized Wifi object and returns it. All future calls to this method will return a reference to the instance created during the first call. Note that you must call init on the returned instance before mnaking any other calls. If using this class's bindings to any of the Socket package classes like TCPSocketConnection, you must call this method and the init method on the returned object first, before even creating the other objects.

Returns:
a reference to the single Wifi obect that has been created.

Definition at line 30 of file Wifi.cpp.

int getSignalStrength (  )

A method for getting the signal strength of the Wifi module.

This method allows you to get the signal strength in dBm. If you get a result of 99 the signal strength is not known or there was an error in reading it. Note that you cannot read the signal strength unless you are already attached to a Wifi network.

Returns:
an integer representing the signal strength in dBm.

Definition at line 605 of file Wifi.cpp.

bool init ( MTSBufferedIO io )

This method initializes the object with the underlying Wifi module interface to use.

Note that this function MUST be called before any other calls will function correctly on a Wifi object. Also note that MTSBufferedIO is abstract, so you must use one of its inherited classes like MTSSerial or MTSSerialFlowControl.

Parameters:
iothe buffered io interface that is attached to the wifi radio module.
Returns:
true if the init was successful, otherwise false.
bool isConnected (  ) [virtual]

This method is used to check if the radio currently has a Wifi network connection established.

Returns:
true if a network connection exists, otherwise false.

Implements IPStack.

Definition at line 200 of file Wifi.cpp.

bool isOpen (  ) [virtual]

This method is used to determine if a socket connection is currently open.

Returns:
true if the socket is currently open, otherwise false.

Implements IPStack.

Definition at line 355 of file Wifi.cpp.

virtual bool open ( const std::string &  address,
unsigned int  port,
Mode  mode 
) [pure virtual, inherited]

This method is used to open a socket connection with the given parameters.

This socket connection is established using the devices built in IP stack. Currently TCP is the only supported mode.

Parameters:
addressis the address you want to connect to in the form of xxx.xxx.xxx.xxx or a URL. If using a URL make sure the device supports DNS and is properly configured for that mode.
portthe remote port you want to connect to.
modean enum that specifies whether this socket connection is type TCP or UDP. Currently only TCP is supported.
Returns:
true if the connection was successfully opened, otherwise false.

Implemented in Cellular.

bool ping ( const std::string &  address = "8.8.8.8" )

This method is used test network connectivity by pinging a server.

Parameters:
addressthe address of the server in format xxx.xxx.xxx.xxx.
Returns:
true if the ping was successful, otherwise false.

Definition at line 632 of file Wifi.cpp.

int read ( char *  data,
int  max,
int  timeout = -1 
) [virtual]

This method is used to read data off of a socket, assuming a valid socket connection is already open.

Parameters:
dataa pointer to the data buffer that will be filled with the read data.
maxthe maximum number of bytes to attempt to read, typically the same as the size of the passed in data buffer.
timeoutthe amount of time in milliseconds to wait in trying to read the max number of bytes. If set to -1 the call blocks until it receives the max number of bytes or encounters and error.
Returns:
the number of bytes read and stored in the passed in data buffer. Returns -1 if there was an error in reading.

Implements IPStack.

Definition at line 419 of file Wifi.cpp.

unsigned int readable (  ) [virtual]

This method is used to get the number of bytes available to read off the socket.

Returns:
the number of bytes available, 0 if there are no bytes to read.

Implements IPStack.

Definition at line 478 of file Wifi.cpp.

void reset (  ) [virtual]

This method performs a soft reboot of the device by issuing the reboot command.

If the module is not able to respond to commands this will not work. This method also waits 10 seconds for the module to perform the reset and return.

Implements IPStack.

Definition at line 505 of file Wifi.cpp.

Code sendBasicCommand ( std::string  command,
int  timeoutMillis,
char  esc = CR 
)

A method for sending a basic command to the radio.

A basic text command is one that simply has a response of either AOK or ERR without any other information. Note that you cannot send commands and have a tcp connection at the same time unless you first switch to command mode.

Parameters:
commandthe command to send to the WiFi module without the escape character.
timeoutMillisthe time in millis to wait for a response before returning.
escescape character to add at the end of the command, defaults to carriage return (CR).
Returns:
the standard Code enumeration.
std::string sendCommand ( std::string  command,
int  timeoutMillis,
std::string  response = "",
char  esc = CR 
)

A method for sending a generic text command to the radio.

Note that you cannot send commands and have a socket connection at the same time, unless you first switch to command mode.

Parameters:
commandthe command to send to the WiFi module without the escape character.
timeoutMillisthe time in millis to wait for a response before returning.
responsethe text string to look for and to return immediately after finding. The default is to look for no specific response.
escescape character to add at the end of the command, defaults to carriage return (CR). Does not append any character if esc == 0.
Returns:
all data received from the radio after the command as a string.
bool setCmdMode ( bool  on )

This method is used to set whether the device is in command mode or data mode.

In command mode you are able to send configuration and status commands while data mode is used for sending data when you have an open socket connection. Note that for all other methods in this class the change is handled automatically. Only use this methodif you want to send your own commands that are not already supported and need to make sure that you are in command mode.

Parameters:
onif true sets to command mode, otherwise to data mode.
Returns:
true if the change was successful, otherwise false.

Definition at line 650 of file Wifi.cpp.

Code setDeviceIP ( std::string  address = "DHCP" )

This method is used to set the IP address or puts the module in DHCP mode.

Parameters:
addressthe IP address you want to use in the form of xxx.xxx.xxx.xxx or DHCP if you want to use DHCP. The default is DHCP.
Returns:
the standard Code enumeration.

Definition at line 530 of file Wifi.cpp.

Code setDNS ( const std::string &  dnsName )

This method is used to set the DNS which enables the use of URLs instead of IP addresses when making a socket connection.

Parameters:
theDNS server address as a string in form xxx.xxx.xxx.xxx.
Returns:
the standard AT Code enumeration.

Definition at line 595 of file Wifi.cpp.

Code setNetwork ( const std::string &  ssid,
SecurityType  type = NONE,
const std::string &  key = "" 
)

This method is used to set the network information details.

This method must be called before connect, which establishes the WiFi network connection.

Parameters:
ssidthe SSID for the network you want to attached to.
typethe type of security used on the network. The default is NONE.
keythe security key for the network. The default is no key.

Definition at line 561 of file Wifi.cpp.

int write ( const char *  data,
int  length,
int  timeout = -1 
) [virtual]

This method is used to write data to a socket, assuming a valid socket connection is already open.

Parameters:
dataa pointer to the data buffer that will be written to the socket.
lengththe size of the data buffer to be written.
timeoutthe amount of time in milliseconds to wait in trying to write the entire number of bytes. If set to -1 the call blocks until it writes all of the bytes or encounters and error.
Returns:
the number of bytes written to the socket's write buffer. Returns -1 if there was an error in writing.

Implements IPStack.

Definition at line 449 of file Wifi.cpp.

unsigned int writeable (  ) [virtual]

This method is used to get the space available to write bytes to the socket.

Returns:
the number of bytes that can be written, 0 if unable to write.

Implements IPStack.

Definition at line 491 of file Wifi.cpp.