Links

Software

Waspmote libraries

Waspmote WiFi PRO libraries

The files related to the WiFi PRO libraries are:
/WIFI_PRO/WaspWIFI_PRO.h
/WIFI_PRO/WaspWIFI_PRO.cpp
/WIFI_PRO/utility/ati_error_codes.h
/WIFI_PRO/utility/ati_generator.h
It is mandatory to include the WiFi PRO library when using this module. So the following line must be added at the beginning of the code:
#include <WaspWIFI_PRO.h>

Class constructor

To start using the Waspmote WiFi PRO library, an object from the ‘WaspWIFI_PRO’ class must be created. This object, called WIFI_PRO, is already created by default inside Waspmote WIFI_PRO library. It will be used through this guide to show how Waspmote works.
When using the class constructor, all variables are initialized to a default value.

API constants

The API constants used in functions are:
Constant
Description
DEBUG_WIFI
This definition enables/disables the debug mode via USB port:
0: No debug mode enabled
1: debug mode enabled for error output messages
2: debug mode enabled for both error and OK messages
WIFI_PRO_SCANFILE
This constant defines the file name where the scanned APs are stored
WIFI_PRO_LISTFILE
This constant defines the file name where the FTP listed directories and files are stored
OPEN
Security mode: no security enabled
WEP64
Security mode: WEP 64-bit security enabled
WEP128
Security mode: WEP 128-bit security enabled
WPA
Security mode: WPA security enabled
WPA2
Security mode: WPA2 security enabled
PROFILE_0
PROFILE_1
PROFILE_2
PROFILE_3
PROFILE_4
PROFILE_5
PROFILE_6
PROFILE_7
PROFILE_8
PROFILE_9
Profile definition for multiple SSIDs

API variables

The variables used inside functions and Waspmote codes are:
Variable
Description
_buffer
The buffer of memory used for storing the responses from the module
_length
The useful length of the buffer
_def_delay
The time to wait after sending every command until listen for a response
_baudrate
The baud rate to be used when the module is switched on
_uart
The selected UART (regarding the socket used: SOCKET0 or SOCKET1)
_errorCode
It stores the error code returned by the module when calling a function with error response
_rtt
It stores the last round trip time performed by a ping call
_ip
It stores the module's IP address when the proper function is called
_gw
It stores the gateway's IP address when the proper function is called
_netmask
It stores the netmask's IP address when the proper function is called
_dns1
It stores the DNS #1 server's IP address when the proper function is called
_dns2
It stores the DNS #2 server's IP address when the proper function is called
_socket_handle
It stores the handle number for a new TCP/UDP socket
_ftp_handle
It stores the handle number for a new FTP session
_filesize
It stores the FTP server file size when the proper function is called
_essid
It stores the ESSID of the AP where the module is connected to
_bssid
It stores the BSSID of the AP where the module is connected to
_channel
It stores the channel used by the module in the current connection
_rate
It stores the transmission rate used by the module in the current connection
_level
It stores the signal level of the module in the current connection (%RSSI)
_quality
It stores the link quality of the module in the current connection (%SNR)
_snr
It stores the SNR of the module in the current connection (dBm)
_power
It stores the transmission power level of the module (dBm)

API functions

Through this guide there are lots of examples of using functions. In these examples, API functions are called to execute the commands, storing in their related variables the parameter value in each case. The functions are called using the predefined object WIFI_PRO.
All public functions return one of these possible values:
  • 0: OK
  • 1: ERROR. See corresponding error code

Error codes

When a function returns error, the _errorCode variable stores the corresponding error meaning. This error value is described by constants as the table below:
Constant
Value
Error code description
ERROR_CODE_0000
0
Waspmote API timeout error
ERROR_CODE_0010
10
SD not present
ERROR_CODE_0011
11
SD file not created
ERROR_CODE_0012
12
SD error opening file
ERROR_CODE_0013
13
SD error setting file offset
ERROR_CODE_0014
14
SD error writing
ERROR_CODE_0020
20
RX buffer full
ERROR_CODE_0021
21
Error downloading UPGRADE.TXT
ERROR_CODE_0022
22
Filename in UPGRADE.TXT is not a 7-byte name
ERROR_CODE_0023
23
No FILE label is found in UPGRADE.TXT
ERROR_CODE_0024
24
NO_FILE is defined as FILE in UPGRADE.TXT
ERROR_CODE_0025
25
No PATH label is found in UPGRADE.TXT
ERROR_CODE_0026
26
No SIZE label is found in UPGRADE.TXT
ERROR_CODE_0027
27
No VERSION label is found in UPGRADE.TXT
ERROR_CODE_0028
28
Version indicated in UPGRADE.TXT is lower/equal to Waspmote's version
ERROR_CODE_0029
29
File size does not match the indicated in UPGRADE.TXT
ERROR_CODE_0030
30
Error downloading binary file
ERROR_CODE_0031
31
Invalid data length
ERROR_CODE_0041
41
Illegal delimiter
ERROR_CODE_0042
42
Illegal value
ERROR_CODE_0043
43
CR expected
ERROR_CODE_0044
44
Number expected
ERROR_CODE_0045
45
CR or ‘,’ expected
ERROR_CODE_0046
46
DNS expected
ERROR_CODE_0047
47
‘:’ or ‘~’ expected
ERROR_CODE_0048
48
String expected
ERROR_CODE_0049
49
‘:’ or ‘=’ expected
ERROR_CODE_0050
50
Text expected
ERROR_CODE_0051
51
Syntax error
ERROR_CODE_0052
52
‘,’ expected
ERROR_CODE_0053
53
Illegal command code
ERROR_CODE_0054
54
Error when setting parameter
ERROR_CODE_0055
55
Error when getting parameter value
ERROR_CODE_0056
56
User abort
ERROR_CODE_0061
61
Internal memory failure
ERROR_CODE_0062
62
User aborted the system
ERROR_CODE_0063
63
CTSH needs to be LOW to change to hardware flow control
ERROR_CODE_0064
64
User aborted last command using ‘---’
ERROR_CODE_0065
65
iChip unique ID already exists
ERROR_CODE_0066
66
Error when setting the MIF parameter
ERROR_CODE_0067
67
Command ignored as irrelevant
ERROR_CODE_0068
68
iChip serial number already exists
ERROR_CODE_0069
69
Timeout on host communication
ERROR_CODE_0070
70
Modem failed to respond
ERROR_CODE_0071
71
No dial tone response
ERROR_CODE_0072
72
No carrier modem response
ERROR_CODE_0073
73
Dial failed
ERROR_CODE_0074
74
WLAN connection lost
ERROR_CODE_0075
75
Access denied to ISP server
ERROR_CODE_0086
86
Writing to internal non-volatile parameters database failed
ERROR_CODE_0087
87
Web server IP registration failed
ERROR_CODE_0088
88
Socket IP registration failed
ERROR_CODE_0094
94
In Always Online mode, connection was lost and re-established
ERROR_CODE_0096
96
A remote host was disconnected
ERROR_CODE_0100
100
Error restoring default parameters
ERROR_CODE_0101
101
No ISP access numbers defined
ERROR_CODE_0102
102
No USRN defined
ERROR_CODE_0103
103
No PWD entered
ERROR_CODE_0104
104
No DNS defined
ERROR_CODE_0111
111
Serial data overflow
ERROR_CODE_0112
112
Illegal command when modem online
ERROR_CODE_0116
116
Error parsing a new trusted CA certificate
ERROR_CODE_0117
117
Error parsing a new Private Key
ERROR_CODE_0118
118
Protocol specified in the USRV parameter does not exist or is unknown
ERROR_CODE_0119
119
WPA passphrase too short has to be 8-63 chars
ERROR_CODE_0125
125
Invalid WEP key
ERROR_CODE_0126
126
Invalid parameters’ profile number
ERROR_CODE_0128
128
Product ID already exists
ERROR_CODE_0129
129
HW pin can not be changed after Product-ID was set
ERROR_CODE_0200
200
Socket does not exist
ERROR_CODE_0201
201
Socket empty on receive
ERROR_CODE_0202
202
Socket not in use
ERROR_CODE_0203
203
Socket down
ERROR_CODE_0204
204
No available sockets
ERROR_CODE_0206
206
PPP open failed for socket
ERROR_CODE_0207
207
Error creating socket
ERROR_CODE_0208
208
Socket send error
ERROR_CODE_0209
209
Socket receive error
ERROR_CODE_0210
210
PPP down for socket
ERROR_CODE_0212
212
Socket flush error
ERROR_CODE_0215
215
No carrier error on socket operation
ERROR_CODE_0216
216
General exception
ERROR_CODE_0217
217
Out of memory
ERROR_CODE_0218
218
An STCP (Open Socket) command specified a local port number that is already in use
ERROR_CODE_0220
220
SSL initialization/internal CA certificate loading error
ERROR_CODE_0221
221
Illegal SSL socket handle. Must be an open and active TCP socket
ERROR_CODE_0222
222
Trusted CA certificate does not exist
ERROR_CODE_0224
224
Decoding error on incoming SSL data
ERROR_CODE_0225
225
No additional SSL sockets available
ERROR_CODE_0226
226
Maximum SSL packet size (2KB) exceeded
ERROR_CODE_0227
227
Send command failed because size of stream sent exceeded 2048 bytes
ERROR_CODE_0228
228
Send command failed because checksum calculated does not match checksum sent by host
ERROR_CODE_0229
229
SSL parameters are missing
ERROR_CODE_0230
230
Maximum packet size (4 GB) exceeded
ERROR_CODE_0300
300
HTTP server unknown
ERROR_CODE_0301
301
HTTP server timeout
ERROR_CODE_0303
303
No URL specified
ERROR_CODE_0304
304
Illegal HTTP host name
ERROR_CODE_0305
305
Illegal HTTP port number
ERROR_CODE_0306
306
Illegal URL address
ERROR_CODE_0307
307
URL address too long
ERROR_CODE_0400
400
MAC address exists
ERROR_CODE_0401
401
No IP address
ERROR_CODE_0402
402
Wireless LAN power set failed
ERROR_CODE_0403
403
Wireless LAN radio control failed
ERROR_CODE_0404
404
Wireless LAN reset failed
ERROR_CODE_0405
405
Wireless LAN hardware setup failed
ERROR_CODE_0406
406
Command failed because WiFi module is currently busy
ERROR_CODE_0407
407
Illegal WiFi channel
ERROR_CODE_0408
408
Illegal SNR threshold
ERROR_CODE_0409
409
WPA connection process has not yet completed
ERROR_CODE_0410
410
The network connection is offline (modem)
ERROR_CODE_0411
411
Command is illegal when Bridge mode is active
ERROR_CODE_0501
501
Communications platform already active
ERROR_CODE_0505
505
Cannot open additional FTP session – all FTP handles in use
ERROR_CODE_0506
506
Not an FTP session handle
ERROR_CODE_0507
507
FTP server not found
ERROR_CODE_0508
508
Timeout when connecting to FTP server
ERROR_CODE_0509
509
Failed to login to FTP server (bad username or password or account)
ERROR_CODE_0510
510
FTP command could not be completed
ERROR_CODE_0511
511
FTP data socket could not be opened
ERROR_CODE_0512
512
Failed to send data on FTP data socket
ERROR_CODE_0513
513
FTP shutdown by remote server
ERROR_CODE_0570
570
PING destination not found
ERROR_CODE_0571
571
No reply to PING request

Switch on

The ON() function allows to switch on the WiFi PRO module and it opens the MCU's UART for communicating with the module. After this step the module will be able to receive commands to manage it. It is necessary to indicate the socket that it is being used: SOCKET0 or SOCKET1.
Example of use for SOCKET0:
{
WIFI_PRO.ON(SOCKET0);
}

Restore to factory defaults

The resetValues() function allows to restore the module's non-volatile parameter database values to factory defaults. Each one of the module's non-volatile parameters has an associated default value. This function restores all parameters to their factory default values.
Example of use:
{
WIFI_PRO.resetValues();
}

Switch off

The OFF() function allows the user to switch off the WiFi PRO module and close the UART. This function must be called in order to keep battery level when the module is not going to be used. It is necessary to indicate the socket that it is being used: SOCKET0 or SOCKET1.
Example of use for SOCKET0:
{
WIFI_PRO.OFF(SOCKET0);
}

How to configure and join an Access Point

In order to configure the module to join an Access Point (AP), it is mandatory to define the ESSID of the AP, the password of the security enabled in that link and finally perform a software reset so as to apply the changes.
Once these parameters have been set, they are permanently stored in the non-volatile memory of the module. So, it is not necessary to re-configure these parameters anymore, unless the user needs to change the AP settings.

Configure ESSID

The setESSID() function allows the user to configure the ESSID to join.
The getESSID() function allows the user to request the current ESSID setting. The _essid attribute permits to read the settings of the module.
Example of use:
{
WIFI_PRO.setESSID(“libelium_AP”);
WIFI_PRO.getESSID();
}
Related variable:
WIFI_PRO._essid → Stores the current ESSID of the module

Configure the password

The setPassword() function allows the user to configure the password to the module. It takes several seconds to generate the keys. This function needs two inputs:
  • Authentication mode:
    • OPEN: No security
    • WEP64: WEP 64-bit
    • WEP128: WEP 128-bit
    • WPA: WPA-PSK
    • WPA2: WPA2-PSK
  • Password:
    • If Security Mode = WPA/WPA2: This is the pass-phrase to be used in generating the PSK encryption key. The allowed value for pass is an ASCII string containing 8 to 63 characters.
    • If Security Mode = WEP64: This key must be defined by 10 hexadecimal digits. Each byte of the 5-byte key is defined by two ASCII characters in the ranges ['0' to '9'], ['A' to 'F'] or ['a' to 'f'].
    • If Security Mode = WEP128: This key must be defined by 26 hexadecimal digits. Each byte of the 13-byte key is defined by two ASCII characters in the ranges ['0' to '9'], ['A' to 'F'] or ['a' to 'f'].
Example of use:
{
WIFI_PRO.setPassword(WPA2, “password”);
}

Software reset

Once the module has been set to the correct settings they are kept in the non volatile memory of the module. Besides, it is mandatory to restart the module in order to force the module to use the new settings. For that purpose, the softReset() function is used to perform a software reset to the module. After calling this function, the new setting takes effect.

Join the Access Point

Once the module has valid settings in the non volatile memory, it automatically starts searching to join the Access Point. The isConnected() function permits to know if the WiFi PRO module is already connected to the Access Point. This function returns true or false values in order to provide the status information.
Examples of configuring the module and joining the AP:

IP addressing

When joining an AP it is possible to use the DHCP client of the module or configure a static IP address.

DHCP client

By default, the WiFi PRO module uses the DHCP client, so when it joins the AP, an IP address is assigned to the module. The getIP() function permits to request the current IP address of the module.
Example of use:
{
WIFI_PRO.getIP();
}
Related variable:
WIFI_PRO._ip → Stores the current IP address assigned to the module
Example of getting the module's IP address:

Static IP address

It is possible to set up a default IP address for the WiFi PRO module. Besides, it is possible to set other network parameters: DNS address, Gateway address and Netmask. The functions for all these settings are shown below:
The setIP() function allows the user to set the IP address to the module in the network.
The setDNS() function allows the user to set the DNS address to the WiFi PRO module.
The setGateway() function allows the user to set the Gateway address to the WiFi PRO module.
The setNetmask() function allows the user to set the netmask address to the WiFi PRO module.
Remember that a software reset is needed in order to apply all these changes in the WiFi PRO module.
Example of use:
{
WIFI_PRO.setIP(192.168.5.248);
WIFI_PRO.setDNS(8.8.8.8);
WIFI_PRO.setGateway(192.168.1.2);
WIFI_PRO.setNetmask(255.255.128.0);
}
Example of using static IP address:

Ping

The ping() function sends a two-byte ICMP PING request packet to the remote host defined as input argument. The input of the function can be a logical name of the target host or a host IP address. Upon successfully receiving an ICMP PING reply from the host, the round trip time in milliseconds is returned (RTT) and stored in the _rtt attribute.
Example of use:
{
WIFI_PRO.ping(“www.google.com”);
}
Related variable:
WIFI_PRO._rtt → Stores the last round trip time performed by a ping call
Example of performing a ping from the module:

Power level

The setPower() function allows the user to configure the transmission power of the chipset. The getPower() function allows the user to request the transmission power of the chipset which is stored in the _power attribute. After a hardware or software reset, the power level parameter returns to its default value. This parameter is in the range 1 to 14 dBm. The default value is 14 dBm.
Example of use:
{
WIFI_PRO.setPower(14);
WIFI_PRO.getPower();
}
Related variable:
WIFI_PRO._power → Stores the power level setting
Example of setting the transmission power level:

Certificate management for SSL connections

How SSL works

Secure Sockets Layer (SSL) technology provides data encryption, server authentication and message integrity for a TCP/IP connection. The server authenticates the client using the client’s Public Key Certificate (PKC). So, it will be necessary to install the corresponding certificate, created by a CA (Certification Authority), to the module. These CA certificates are usually provided by the browsers.
For more information, refer to the tutorial related to SSL connections:

Set the CA certificate

The setCA() function sets the certificate of the trusted certificate authority. The WiFi PRO module accepts a server’s identity only if its certificate is signed by one of these certificate authorities.
The certificate is a PEM format X509 certificate (DER format, Base-64 encoded with header and footer lines). The certificate is referenced as the trusted certificate authority’s certificate during SSL socket connection establishment (handshake). The WiFi PRO module establishes an SSL socket connection only to servers having a certificate authenticated by this certificate authority. The certificate must be defined by multiple lines separated by <CR>, beginning with: -----BEGIN CERTIFICATE----- and terminating with: -----END CERTIFICATE-----. The certificate should include an RSA encryption public key of 1024 or 2048 bits. The signature algorithm may be MD2, MD5 or SHA1. The maximum size of the certificate is 1500 characters.
Example of valid certificate setting:
{
char TRUSTED_CA[] =\
"-----BEGIN CERTIFICATE-----\r"\
"MIICPDCCAaUCEHC65B0Q2Sk0tjjKewPMur8wDQYJKoZIhvcNAQECBQAwXzELMAkG\r"\
"A1UEBhMCVVMxFzAVBgNVBAoTDlZlcmlTaWduLCBJbmMuMTcwNQYDVQQLEy5DbGFz\r"\
"cyAzIFB1YmxpYyBQcmltYXJ5IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTk2\r"\
"MDEyOTAwMDAwMFoXDTI4MDgwMTIzNTk1OVowXzELMAkGA1UEBhMCVVMxFzAVBgNV\r"\
"BAoTDlZlcmlTaWduLCBJbmMuMTcwNQYDVQQLEy5DbGFzcyAzIFB1YmxpYyBQcmlt\r"\
"YXJ5IENlcnRpZmljYXRpb24gQXV0aG9yaXR5MIGfMA0GCSqGSIb3DQEBAQUAA4GN\r"\
"ADCBiQKBgQDJXFme8huKARS0EN8EQNvjV69qRUCPhAwL0TPZ2RHP7gJYHyX3KqhE\r"\
"BarsAx94f56TuZoAqiN91qyFomNFx3InzPRMxnVx0jnvT0Lwdd8KkMaOIG+YD/is\r"\
"I19wKTakyYbnsZogy1Olhec9vn2a/iRFM9x2Fe0PonFkTGUugWhFpwIDAQABMA0G\r"\
"CSqGSIb3DQEBAgUAA4GBALtMEivPLCYATxQT3ab7/AoRhIzzKBxnki98tsX63/Do\r"\
"lbwdj2wsqFHMc9ikwFPwTtYmwHYBV4GSXiHx0bH/59AhWM1pF+NEHJwZRDmJXNyc\r"\
"AA9WjQKZ7aKQRUzkuxCkPfAyAw7xzvjoyVGM5mKf5p/AfbdynMk2OmufTqj/ZA1k\r"\
"-----END CERTIFICATE-----;
WIFI_PRO.setCA(TRUSTED_CA);
}
Note: Firmware versions ID811d15 and greater use SSL3/TLS1.2 protocol only.

TCP/UDP sockets

TCP client

The setTCPclient() function opens a Transmission Control Protocol (TCP) client socket and attempts to connect to the specified port on a server defined as input. Therefore, this function needs three different inputs:
  • Host: The server name may be any legal Internet server name that can be resolved by module's DNS (Domain Name Server) settings. The server name can also be specified as an absolute IP address given in dot-decimal notation.
  • Remote port: It is assumed that the server system is listening on the specified port.
  • Local port: This is the local port when opening the TCP socket.
Upon successfully opening and connecting the TCP socket to the <Host>:<Remote port>, a socket handle is returned. The socket handle is stored in the _socket_handle attribute. This handle is in the range 0 to 9. This handle is needed to reference the socket in all following socket commands.
Example of use:
{
char HOST[] = "192.168.5.152";
char REMOTE_PORT[] = "2000";
char LOCAL_PORT[] = "3000";
WIFI_PRO.setTCPclient( HOST, REMOTE_PORT, LOCAL_PORT);
}
Related variable:
WIFI_PRO._socket_handle → Stores the TCP socket handle
Example of use for TCP sockets:
Figure: TCP client in Waspmote vs TCP server in laptop

TCP server

The setTCPserver() function opens a TCP listening socket on the local IP address and the specified port. The second input parameter specifies the maximum number of remote concurrent connections allowed through the listening socket. Thus, this function needs 2 different inputs:
  • Local port: The listening port to be used by a remote system when connecting to the WiFi PRO module.
  • Max number of clients: Specifies the maximum number of active connections that may be concurrently established through the listening socket.
Once the listening socket is open, it automatically accepts remote connect requests up to the maximum allowed. When a remote system connects through the listening socket, a new TCP socket is spawned internally ready to send and receive data.
The getAllSocketStatus() function allows the user to update the information of all active sockets connected through a listening socket. The information for each one of the active sockets is stored in a structure called listen_socket_t. There are ten structures defined in the Waspmote libraries for permitting up to ten connections simultaneously.
The definition of the array of sockets structures is:
listen_socket_t socket[10];
The definition of the structure is:
struct listen_socket_t
{
uint16_t handle; // socket handle
int8_t status; // 0: active; -1: non-active
char ip[16]; // xxx.xxx.xxx.xxx
uint16_t port; // remote connection port
int size; // size of pending bytes
};
Example of use for TCP server:

UDP

The setUDP() function opens a UDP (User Datagram Protocol) socket and sets the remote system’s <Host>:<port> address. Therefore, this function needs three different inputs:
  • Host: Logical name of the target server or a host IP address. The remote system’s name may be any legal Internet server name that can be resolved by module's DNS (Domain Name Server) settings. The server name may also be specified as an absolute IP address given in dot-decimal notation. When the <Host> is defined, the resulting UDP socket is created and connected. If <Host>=0.0.0.0, the socket is created but remains unconnected. The first UDP packet to arrive automatically latches the sender’s IP port, in effect connecting the socket. <Host> may be a subnet directed Broadcast address which allows to broadcast packets to the immediate subnet, not crossing gateways. For example, to broadcast to subnet 192.168.x.x on destination port 1234: Host=”192.168.255.255” and Remote port=“1234”. <Host> may be a multicast IP address in the range 224.0.0.0 to 239.255.255.255. IP multicast datagrams will not cross gateways. In this case, data is sent and received on <Remote port>. <Local port> is ignored.
  • Remote port: Specifies the remote system’s port.
  • Local port: Specifies the local port to use.
Upon successfully opening and connecting the UDP socket to <Host>:<Remote port>, a socket handle is returned. The socket handle is stored in the _socket_handle attribute. The socket handle is in the range 0 to 9 and is used to reference the socket in all following socket commands.
Note: The WiFi PRO will only be able to receive UDP packets from the specified <Host> IP address to the specified <Local port>. In the case other connections are needed it is possible to establish new UDP sockets with different hosts.
Example of use:
{
char HOST[] = "192.168.5.152";
char REMOTE_PORT[] = "2000";
char LOCAL_PORT[] = "3000";
WIFI_PRO.setUDP( HOST, REMOTE_PORT, LOCAL_PORT);
}
Related variable:
WIFI_PRO._socket_handle → Stores the UDP socket handle
Examples of use for UDP sockets:

Send data to a TCP/UDP socket

The send() function sends a byte stream to the socket specified by the socket handle input. This function needs two different inputs:
  • Socket handle: A TCP/UDP socket handle of a previously open socket.
  • Data: This is the stream of data to send to the TCP/UDP socket. This stream of data can be defined as a simple string message. Or an array of bytes, specifying a third input for the length of the array of bytes to send.
Example of use for the string message:
{
WIFI_PRO.send( WIFI_PRO._socket_handle,this is a message”);
}