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
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 Wifi * | getInstance () |
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] |
enum SecurityType |
Constructor & Destructor Documentation
~Wifi | ( | ) |
Member Function Documentation
bool bind | ( | unsigned int | port ) | [virtual] |
bool close | ( | ) | [virtual] |
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.
void disconnect | ( | ) | [virtual] |
std::string getDeviceIP | ( | ) |
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.
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.
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:
-
io the buffered io interface that is attached to the wifi radio module.
- Returns:
- true if the init was successful, otherwise false.
bool isConnected | ( | ) | [virtual] |
bool isOpen | ( | ) | [virtual] |
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:
-
address is 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. port the remote port you want to connect to. mode an 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" ) |
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:
-
data a pointer to the data buffer that will be filled with the read data. max the maximum number of bytes to attempt to read, typically the same as the size of the passed in data buffer. timeout the 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.
unsigned int readable | ( | ) | [virtual] |
void reset | ( | ) | [virtual] |
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:
-
command the command to send to the WiFi module without the escape character. timeoutMillis the time in millis to wait for a response before returning. esc escape 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:
-
command the command to send to the WiFi module without the escape character. timeoutMillis the time in millis to wait for a response before returning. response the text string to look for and to return immediately after finding. The default is to look for no specific response. esc escape 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:
-
on if true sets to command mode, otherwise to data mode.
- Returns:
- true if the change was successful, otherwise false.
Code setDeviceIP | ( | std::string | address = "DHCP" ) |
Code setDNS | ( | const std::string & | dnsName ) |
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:
-
ssid the SSID for the network you want to attached to. type the type of security used on the network. The default is NONE. key the security key for the network. The default is no key.
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:
-
data a pointer to the data buffer that will be written to the socket. length the size of the data buffer to be written. timeout the 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.
Generated on Tue Jul 12 2022 21:46:23 by 1.7.2