Wifi Networking Guide Waspmote
Wifi Networking Guide Waspmote
Networking Guide
Index
INDEX
1. Introduction.......................................................................................................................................... 4
2. Topologies............................................................................................................................................. 5
2.1. Access Point....................................................................................................................................................................................5
2.2. Ad-hoc mode with iPhone/Android.......................................................................................................................................8
2.3. Ad-hoc mode between Waspmotes.......................................................................................................................................8
3. Hardware............................................................................................................................................... 9
3.1. Specifications.................................................................................................................................................................................9
3.2. Power consumption.....................................................................................................................................................................9
3.3. Bandwidth.................................................................................................................................................................................... 10
3.4. Distance ranges ......................................................................................................................................................................... 10
3.5. Connection times....................................................................................................................................................................... 10
4. General considerations...................................................................................................................... 11
4.1. Waspmote library....................................................................................................................................................................... 11
4.1.1. Waspmote Wifi files....................................................................................................................................................11
4.1.2. Constructor....................................................................................................................................................................11
4.2. API functions................................................................................................................................................................................ 12
4.3. Waspmote reboots.................................................................................................................................................................... 16
4.4. Constants predefined............................................................................................................................................................... 16
4.5. Class Variables............................................................................................................................................................................. 17
5. Initialization........................................................................................................................................ 18
5.1. Expansion Radio Board............................................................................................................................................................ 18
5.2. Initialization................................................................................................................................................................................. 19
5.3. Setting ON.................................................................................................................................................................................... 19
5.4. Setting OFF................................................................................................................................................................................... 19
7. Network Configuration...................................................................................................................... 22
7.1. DHCP protocol............................................................................................................................................................................ 22
7.2. IP manual...................................................................................................................................................................................... 22
7.3. Ad-hoc............................................................................................................................................................................................ 22
-2- v0.3
Index
8. Connection Creation.......................................................................................................................... 23
8.1. TCP................................................................................................................................................................................................... 23
8.1.1. TCP Client.......................................................................................................................................................................23
8.1.2. TCP Server......................................................................................................................................................................24
8.2. UDP................................................................................................................................................................................................. 24
8.2.1. UDP Client......................................................................................................................................................................24
8.2.2. UDP Server.....................................................................................................................................................................25
8.3. HTTP................................................................................................................................................................................................ 25
8.4. FTP................................................................................................................................................................................................... 26
8.5. Broadcast-UDP............................................................................................................................................................................ 26
9. Device Options.................................................................................................................................... 27
9.1. Default settings.......................................................................................................................................................................... 27
9.2. Improving performance.......................................................................................................................................................... 27
9.3. Sleeping the radio Wifi............................................................................................................................................................. 28
-3- v0.3
Introduction
1. Introduction
This guide describes all features of the Wifi module for the Waspmote sensor platform, which has been designed to make
a connection between a Waspmote and any other device with a Wifi radio (smart-phones, computers, routers...) or to make a
connection directly to an Internet server.
The Waspmote Wifi module is able to make connections and communicate by using different ways. Connections can be done
by Ad-hoc (point to point) or infrastructure mode (connecting to an available Access Point - AP). In the Infrastructure mode we
can connect to the AP’s by using secure connections such as WEP-128, WPA-PSK (TKIP) and WPA2-PSK (AES).
Furthermore, the Wifi module can manage high level networking applications such as: DHCP client, DNS client, ARP, ICMP pings,
FTP, TELNET, HTTP, TCP, UDP or UDP Broadcast.
This guide will try to describe main parts of the Wifi module, its characteristics like consumption and ranges (between them and
between a Wifi module and a device like a Meshlium or a smart-phone), and how to carry out many types of inquiries, explaining
the API and providing code examples for each case. It also describes the software made to test and connect Wifi module, like
PC software and mobile applications.
-4- v0.3
Topologies
2. Topologies
2.1. Access Point
Sensor nodes may connect to any standard Wifi router which is configured as Access Point (AP) and then send the data to other
devices in the same network such as laptops and smartphones. This is the common case when implementing home sensor
networks and when using the data inside an Intranet.
Once associated with the Access Point, the nodes may ask for an IP address by using the DHCP protocol or use a preconfigured
static IP. The AP connection can be encrypted, in this case, you have to specify also the pass-phrase or key to the Wifi module.
The Wifi module supports these security modes: WEP-128, WPA2-PSK , WPA1-PSK, and WPA-PSK mixed mode.
-5- v0.3
Topologies
Nodes may also connect to a standard Wifi router with DSL or cable connectivity and send the data to a web server located on
the Internet. Then users are able to get this data from the Cloud. This is the typical scenario for companies which want to give
data accessibility services.
As pointed before the Wifi module can join any standard Wifi router, however the connection may also be performed using
Meshlium instead of a standard Wifi router. Meshlium is the multiprotocol router designed by Libelium which is specially
recommended for outdoor applications as it is designed to resist the hardest conditions in real field deployments.
-6- v0.3
Topologies
Meshlium is also ready to deal with hundreds of nodes at the same time, receiving sensor
data from all of them and storing it in its internal database or sending it to an Internet
server. As well as this, Meshlium may work as a Wifi to 3G/GPRS gateway, giving access
to the internet to all the nodes in the network using the mobile phones infrastructure.
It is also important to mention that the transmission power of the Wifi interface
integrated in Meshlium is many times higher than the ones available in “home oriented”
Wifi routers so the distance we can get increases dramatically from a few meters to
dozens or even hundreds depending on the location of the nodes.
Using Meshlium as Wifi Access Point allows to control and to store the messages received
from the Wifi module, or allows to combine Wifi technology with other protocols such as
ZigBee. Meshlium may work as:
-7- v0.3
Topologies
All module Wifi on the Adhoc network participate in keeping the network alive and each keeps track of the other active devices
on the network by sending and receiving beacon and probe packets. IP addresses are automatically assigned through the Auto
IP function.
Since Auto IP fixes the first two bytes of the IP address you want to use the net-mask of 255.255.0.0 so that other device
connecting to the module can be reached. Alternatively you can set the netmask to a smaller subnet if the other device’s IP
addresses are being statically to he same subnet as the Adhoc device.
-8- v0.3
Hardware
3. Hardware
3.1. Specifications
Main Features:
-9- v0.3
Hardware
3.3. Bandwidth
142 ms 8 ms
9600
5,63 kbps 100 kbps
87 ms 8 ms
19200
9,19 kbps 100 kbps
51 ms 8 ms
57600
15,68 kbps 100 kbps
The distance ranges changes in communication between Waspmote with Wifi module and connecting it to an Access Point like
Meshlium. They also are different for a perfect transmission (100 % UDP packets received) and for the visibility of the AP (longer
distance range).
Next table shows the distance ranges of the module depending of the antenna.
The final distance varies on parameters such as if the Fresnel Zone is respected or the humidity in the ambient.
-10- v0.3
General considerations
4. General considerations
This section will describe the API for the Wifi module. The functions that manage the Wifi module belong to the class WaspWIFI,
and the object used to use them is defined as Wifi. All of them are described below including some examples of use.
4.1.2. Constructor
To start using Waspmote WIFI library, an object from class ‘WaspWIFI’ must be created. This object, called ‘WIFI’, is created inside
Waspmote WIFI library and it is public to all libraries. It is used through this guide to show how Waspmote WIFI library works.
When creating this constructor, all the variables are defined with an initial value by default.
-11- v0.3
General considerations
open(); Opens TCP connection, and checks that everything are going good
openHTTP(); Opens HTTP connection, and checks that everything are going good.
Public functions:
Basic functions
begin(); Selects the pins and the baud rate of the Waspmote.
setAuthKey(mode, pass); Sets the security mode and the Key or the pass-phrase of the network
-12- v0.3
General considerations
Join timer is the time in milliseconds the join function will wait for an access
setJoinTime(value); point to complete the association process. This timer is also the timeout for
the WPA handshaking process.
Sets the Scan options (the time scanning, the channel mask, and if it is passive
setScanOptions(time, mask, mode);
or active scan), does not launch the scan.
Synchronizes the time from a server. And if interval > 0, the module will
setTimeFromServer(IP, port, interval);
synchronize every interval seconds.
setDNS(type, IP, name); Sets the IP address, host and backup host of the DNS main or auxiliary server.
-13- v0.3
General considerations
sendAutoBroadcast(IP, port, interval, id); Configures and sends auto UDP-broadcast messages each interval seconds.
setUDPserver(port local); Sets the configuration and prepares it to receive a UDP datagram.
setTCPserver(port local); Sets the configuration and waits a TCP server connection in the selected port.
openFTP(user, password); Opens the FTP connection using user and password given.
getFile(file, local folder, remote folder); Gets the file via FTP.
Device configuration
setSleep(sleep time, sleep cycle); Sets the sleep time and the wake time (in seconds).
-14- v0.3
General considerations
Status Information
getTime(); Displays the time-server UDP address and time-server port number.
-15- v0.3
General considerations
Baud Rates
Authentication Modes
Join Modes
MANUAL 0 Manual policy for joining/associating.
AUTO_STOR 1 Try to join the access point that matches the stored SSID, passkey and channel.
AUTO_BEST 2 Join ANY access point with security matching the stored authentication mode.
CREATE_ADHOC 4 Create an Ad-hoc network.
DHCP Options
Connection Options
-16- v0.3
General considerations
Flags Options
TCP_STATUS 1 00000001 – TCP connections are kept open when the connection to the access point is lost.
00000010 – Nagle's algorithm, it is a means of improving the efficiency of TCP/IP networks by reducing
TCP_NODELAY 2
the number of packets that need to be sent over the network.
TCP_RETRY 4 00000100 – Enables the TCP retry.
UDP_RETRY 8 00001000 – Retry if no ACK from UDP.
DNS_CACHE 16 00010000 – Enables DNS Cache.
ARP_CACHE 32 00100000 – Enables ARP Table Cache.
UDP_AUTO_PA 64 01000000 – UDP auto pairing enabled.
BYTE_STAMP 128 10000000 - 8 byte time-stamp to UDP or TCP packets.
DNS Options
HTTP Options
IP 1 HTTP connection is specified by an IP address.
DNS 2 HTTP connection is specified by a DNS address.
READ Options
BLO 0 Blocking read.
NOBLO 1 Read with a timeout of 2 seconds.
FTP Options
FTP_PASIVE 0 FTP Passive mode (default).
FTP_ACTIVE 1 FTP Active mode.
Debug Mode
Char answer[512]
It is a vector of 512 char bytes, so if the user is going to receive less or more data, can adjust this vector on the library, in order
to save memory.
-17- v0.3
Initialization
5. Initialization
Before starting to use a module, it needs to be initialized. During this process, configuration parameters are sent to the module.
USB and SD card are also initialized. USB UART connection is enabled for checking Debug messages, and SD card is used to store
files or upload files when using FTP connections.
•• ZigBee - Bluetooth
•• ZigBee - RFID
•• ZigBee - Wifi
•• ZigBee – 3G/GPRS
•• Wifi - RFID
•• Wifi - 3G/GPRS
•• Wifi – Bluetooth
•• Etc.
Next image shows the sockets available along with the UART assigned. On one hand, SOCKET0 allows to plug any kind of radio
module through the UART0. On the other hand, SOCKET1 permits to connect a radio module through the UART1.
This API provides a function in order to initialize the Wifi module module called WIFI.ON(socket). This function supports a new
parameter which permits to select the socket. It is possible to choose between socket0 or socket1.
-18- v0.3
Initialization
The rest of functions are used the same way as they are used with older API versions. In order to understand them we recommend
to read this guide.
WARNING:
•• Avoid to use DIGITAL7 pin when working with Expansion Board. This pin is used for setting the XBee into sleep.
•• Avoid to use DIGITAL6 pin when working with Expansion Board. This pin is used as power supply for the Expansion Board
•• Incompatibility with Sensor Boards:
•• Gases Board: Incompatible with SOCKET4 and NO2/O3 sensor.
•• Agriculture Board: Incompatible with Sensirion and the atmospheric pressure sensor.
•• Smart Metering Board: Incompatible with SOCKET11 and SOCKET13
•• Smart Cities Board: Incompatible with microphone and the CLK of the interruption shift register.
•• Events Board: Incompatible with interruption shift register.
5.2. Initialization
Using WIFI function begin, Waspmote is prepared to communicate with the Wifi module, setting up the Pin connections and
Baud-rates. This version of API configures the UART 1 as default output for using the radio Wifi.
This function also initializes the USB and SD card connections. USB is used for displaying the data received from the connections
(more information on section 8) or for showing debug messages (more on section 10).
5.3. Setting ON
With the function ON module is powered and the UART is opened to communicate with the module. It also enters in command
mode and sets some default configurations.
-19- v0.3
Access Point Configuration
It has to be remarked that those information is deleted every time Waspmote reboots unless the configuration is saved as
default in Wifi module using function storeData for using automatic associations for example.
Next codes are examples of Wifi Access Point configuration/ creation for each type of connection.
///////////MANUAL-MODE///////////////
With Auto-Best the Wifi module joins any access point with security matching the stored authentication mode. This ignores the
stored SSID and searches for the access point with the strongest signal.
/////////AUTO-BEST/////////////////
And with Auto-Store, try to join the access point that matches the stored SSID, passkey and channel. Channel can be set to 0
for scanning.
////////AUTO-STORE/////////////////
-20- v0.3
Access Point Configuration
////////CREATE ADHOC///////////////
The user can also configure the beacon milliseconds and the timeout probe of the Ad-hoc connection and create it by calling
instead setAdhocOptions (beacon,probe). The code will look like:
// 1.0 Configure the number of channel where we want to create the ADHOC
// network.
WIFI.setChannel(6);
// 1.1 Configure the authentication mode of the auto-join.
WIFI.setAutojoinAuth(ADHOC);
// 1.2 Sets the name of the ADHOC network we want to join.
WIFI.setESSID(“NEW_ADHOC”);
// 2. Configure Ad-hoc connection beacon time and timeout.
if (WIFI.setAdhocOptions(1000,5))
// 3. Whatever we want to do, for example, send something
while(1){ WIFI.send(“something”); delay(2000);}
-21- v0.3
Network Configuration
7. Network Configuration
This section describes how to configure the network, the Wifi module has to be prepared to join to an access point or to create
an Adhoc network.
The most important thing here is to configure the Wifi module address, needed for making connections on the Internet. The Wifi
module can set the IP address by calling WIFI.setDHCPoptions(type) and the types can be DHCP_OFF, DHCP_ON, AUTO_IP or
DHCP_CACHE. Next sections show how to use
To use this protocol, the module needs to call WIFI.setDHCPoptions(DHCP_ON) and then try to join the access point.
Another way to solve IP automatically, is calling WIFI.setDHCPoptions (DHCP_CACHE), in this case the Wifi module uses
previous IP address if lease is not expired (lease survives reboot).
7.2. IP manual
IP address can also be set manually [WIFI.setDHCPoptions(DHCP_OFF)]. In this case you can set as well the gateway address,
the net-mask, the DNS address and the local port.
There are functions to configure each address. Next code shows an example of manual network configuration.
7.3. Ad-hoc
Finally, calling WIFI.setDHCPoptions(AUTO_IP), the Wifi module is configured to solve the IP address in Adhoc networks.
A link-local address (Auto-IP) is an Internet Protocol address that is intended only for communications within the segment of
a local network (a link) or a point-to-point connection that a host is connected to. Routers do not forward packets with link-
local addresses. Link-local addresses are assigned using stateless address auto-configuration. Link-local addresses for IPv4 are
defined in the address block 169.254.0.0/16.
In the automatic address configuration process, network hosts select a random candidate address within the reserved range
and use Address Resolution Protocol (ARP) probes to ascertain that the address is not in use on the network. If a reply is received
to the ARP, it indicates the candidate IP address is already in use; a new random candidate IP address is then created and the
process repeated. The process ends when there is no reply to the ARP, indicating the candidate IP address is available.
Microsoft refers to this address auto configuration method as Automatic Private IP Addressing (APIPA). It is sometimes also
casually referred to as Auto-IP.
-22- v0.3
Connection Creation
8. Connection Creation
This section will describe how to create/open a connection. There are functions to open or prepare connections for each type
of protocol type. Possible connections are Broadcast-UDP, UDP client-only, UDP server, TCP client, TCP server, FTP and HTTP.
8.1. TCP
TCP is a connection-oriented protocol; the user has to open and close the connection when he finishes to work with it. This
protocol is the most reliable, and the user can even configure a password for the connection.
First, the Wifi module configures the protocol with WIFI.setConnectionOptions (type). The types in TCP can be CLIENT_SERVER
for TCP server & client, or CLIENT for TCP client only.
And then the user can set the remote parameters in the creation function or with the function WIFI.setRemoteHost(IP,PORT).
To create the connection, the Wifi module can act as a client, or as a server / client. As a client, the Wifi module specifies the remote
address and port and makes the connection. And as a server, the Wifi module specifies the port and waits for a connection on
that port.
In this case, the Wifi module opens a connection, sends a message, and waits a message from the server. If this message begins
with zero, then finishes the loop and closes the connection.
-23- v0.3
Connection Creation
In this case, the Wifi module opens a connection and waits a client. When the client is connected, the Wifi module reads a
message, if this message begins with zero, finishes the loop and closes the connection, and if not, sends a message to the client.
8.2. UDP
UDP is a connectionless protocol, for this reason there is not a close method to close the connection. However, with the function
close the user enters in command mode only and can continue doing other things with the radio Wifi.
There is no initial handshake between the hosts to set up the UDP connection. This makes UDP an unreliable protocol, as there
is no guarantee that the data will be correctly delivered. However, UDP is suited for applications that cannot tolerate too much
latency but can tolerate some errors in data. Transmission of video and audio would be a good example of UDP application.
In this case, Wifi module specifies UDP mode and server address, sends a datagram, waits 1 second and then sends another
datagram. Finally, it closes the UDP mode.
-24- v0.3
Connection Creation
In this case, Wifi module specifies UDP mode and port address to receive the datagram, and waits a datagram from the server. If
that datagram begins with zero, then finishes the loop and closes the UDP mode.
8.3. HTTP
The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information
systems. HTTP is the foundation of data communication for the World Wide Web.
The Waspmote Wifi module is capable of getting or posting data to a web server; the user has to specify the address indicating
the IP or DNS address, and the get query phrase.
The function WIFI.getURL(type,Address(IP or DNS),Query) sends the HTTP get query to the specified server. The server is
able to be specified by an IP address, or a DNS name.
-25- v0.3
Connection Creation
8.4. FTP
In the FTP protocol, connections are created when uploading of downloading a file from a connection previously configured,
and then closed.
•• First the Wifi module has to specify the IP address, the port, the mode of the FTP server, and the timeout of the connection
with the function WIFI.setFTP (IP,Port,passive/active,timeout).
•• Second, the Wifi module has to specify the user name and password of the FTP server with the function WIFI.
openFTP(USER,PASSWORD).
•• And then, the Wifi module can save a file from the server to the SD card, or upload a file to the server with the functions
WIFI.getFile(filename, folder_sd, folder_ftp) and WIFI.uploadFile(filename ,folder_sd, folder_ftp).
// 1. Configure the Remote parameters (Sets the FTP address, port, etc)
WIFI.setFTP(“62.75.203.94”,21,FTP_PASIVE,20);
// 2 Prepares the ftp account with the user name and password
WIFI.openFTP(“USERNAME”,”PASSWORD”);
8.5. Broadcast-UDP
The Wifi module can be setup to automatically generate UDP broadcast packets at certain intervals. This feature can work in
parallel with any other protocol before explained. Those packets are sent using UDP protocol.
•• Some AP will disconnect devices that have joined don’t send any packets after a time.
•• Application programs to auto-discover and auto-connect the Wifi module can use this feature.
The format of the Broadcast-UDP packet is 110 Bytes of data organized as next table shows.
Number of Bytes
0-5 6 7 8-9 10-13 18-31 32-59 60-91 92-93
Next code is an example of Broadcast-UDP connection creation; in the function the user need to specify the broadcast address,
the port, the seconds and the User device ID.
-26- v0.3
Device Options
9. Device Options
There are some device options when using Wifi module. These options are made to improve speed or to save energy.
// Sets the baud rate between the radio Wifi and Waspmote
WIFI.setBaudRate(115200);
// Sets the transmission rate with the network
WIFI.setTXRate(15);
// Sets the power of transmission
WIFI.setTXPower(0);
// Sets the timeout in milliseconds of joining to a network
WIFI.setJoinTime(100);
-27- v0.3
Device Options
The user can put radio Wifi in sleep mode and wake it up manually in the code with functions sleep and wake.
That feature may be done automatically with setSleep(30,90) function. When radio Wifi reboots, this configuration will wake
radio Wifi up during 30 seconds and then it will sleep the radio during 90 seconds. The user can configure default connections
when waking up.
Next code is an example of automatic use of the function setSleep(30,90) with some default connections.
The process in this code is the following: Waspmote wakes up, joins TEST_WIFI network, and send UDP Broadcast packets during
30 seconds. Then Waspmote sleeps the radio Wifi, and last 90 seconds it wakes up again.
-28- v0.3
Status Information
These options are: connection, network, signal quality, statistics, delays, ad-hoc, broadcast, communication, DNS, FTP, IP, MAC,
device ID, timers, UDP and UART settings.
-29- v0.3
Connecting to Meshlium
Focusing in Wifi technologies, there are two main ways of using Meshlium and Waspmote with module Wifi. First is using
Meshlium to create an Access Point and give the Wifi module access to the Internet. And the other is configuring Meshlium for
receiving, capturing and sending messages from and to Waspmote with Wifi module.
-30- v0.3
Connecting to Meshlium
Then access Meshlium through the Wifi connection. First of all search the available access points and connect to “Meshlium”.
No password is needed as the network is public (you can change it later in the Wifi AP Interface options). When you select it,
Meshlium will give an IP from the range 10.10.10.10 - 10.10.10.250.
Now you can open your browser and access to the Meshlium Manager System:
•• URL: http://10.10.10.1/ManagerSystem
•• user: root
•• password: libelium
-31- v0.3
Connecting to Meshlium
In the manager system, Meshlium AP can be configured by giving it a name and providing it the security desired. The user can
also configure a DHCP server to give IP addresses to the radio Wifi. To do this, click in Interfaces, and then in Wifi AP. Now you
will see next image:
If your network does not offer DHCP service Meshlium starts with a default IP (192.168.1.100). In this case you can connect
Meshlium through the Wifi connection (which is always available) or with the crossover cable provided with Meshlium. If you
want to access to the Manager System using the crossover ethernet cable go to:
•• URL: http://192.168.1.100/ManagerSystem
•• user: root
•• password: libelium
Then, when the Wifi radio connects this AP, the Meshlium can act like an Internet Router and provide Internet access to the
Waspmote network, by the Ethernet or the 3G configuration. Once connected, Waspmote can create any kind of connection to
the Internet (see section 8) through Meshlium using external IP addresses.
-32- v0.3
Connecting to Meshlium
As Meshlium is based on a Debian system you can use tools such as aptitude in order to download and install new libraries,
services, data bases and applications. See next chapter “Installing new packages and libraries” to see how to add them.
Meshlium counts with a file system up to 32GB which can be used by developers to store the data generated in their own
applications and with the sensor data coming from the ZigBee, Bluetooth and GPS capturer tools. You can access through SSH
using the same user and password of the Manager System.
The user partition is located in /mnt/user and it is read and write enabled by default so you don’t need to remount it. However,
if you want to add your own applications in the system path you must remount the system in read and write mode. To do so
execute as root: “remountrw”.
#remountrw
-33- v0.3
Connecting to Meshlium
You can use “scp” (available by default in most linux distributions) in order to copy your own files to Meshlium.
Alternatively you can use a sftp client program, like Filezilla, (available for windows and mac) and use it from you pc to upload
files to Meshlium using the following data:
•• Host: 10.10.10.1
•• Server Type: SFTP
•• Port: 22
•• User: root
•• Passw: libelium
Last you must remount the system in read only mode. To do so execute as root: “remountro”.
#remountro
IMPORTANT: Installing new software is a delicate task which has to be done for experts. If you are not sure about adding new
files to the system path do not perform any action including remounting the system as read and write. If you have any doubt
ask to our Development team at: http://www.libelium.com/forum
All the information coming from all the interfaces (ZigBee, Bluetooth, 3G/GPRS, Wifi and from the GPS module) can be stored
in the Local File System and Local Data Base as explained in the “Storage Options” section or even exported to an external Data
Base connected to the internet.
-34- v0.3
Connecting to Meshlium
We have created a template code files in order to help developers to create their own capture and storage applications.
main()
{
/* file descriptors */
int fd;
int i;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
int numbytes;
char buf[MAXDATASIZE];
/* create a socket() */
if ((fd=socket(AF_INET, SOCK_DGRAM, 0)) == -1 ) {
printf(“socket error\n”);
exit(-1);
}
/* create the struct sockaddr_in with the addresses/settings */
sin_size = sizeof(client);
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;
bzero(&(server.sin_zero),8);
/* bind() the socket to a port */
if(bind(fd,(structsockaddr*)&server, sizeof(structsockaddr))==-1) {
printf(“bind error \n”);
exit(-1);
}
This example is for UDP connection, if the user wants to make a TCP connection, there are some changes in the code, for
example, function socket changes and the functions listen() and accept() are needed.
The real files (wifi_UDP.c, wifi_TCP.c) can be downloaded from the Meshlium Development section:
http://www.libelium.com/development/meshlium
You can download these files and change them in order to make it compatible for your specific configuration.
-35- v0.3
Connecting to Meshlium
The compilation can be done in the same Meshlium. Just copy these files in a folder accessing by SSH and execute:
#./wifiUDP
#./wifiTCP
-36- v0.3
Connecting to a PC
12. Connecting to a PC
In this section we will see how to use the Waspmote Wifi PC App to communicate with the Wifi radio easily from any PC, and
test your Wifi Waspmote programs. There are three versions of the program: Windows (executable program), Mac (application
program) and Linux (Java executable program).
For the installation, you just need to download the latest version of the Radio Wifi PC-test from the support section:
http://www.libelium.com/development/waspmote
•• First part corresponds to the configuration and the creation/close of the connection.
•• In the second part there are two text areas, first one displays information and messages received, and the second text area
corresponds to the message that will be send when the user presses send button.
•• Finally, in the bottom of the screen there is a button to finish the program.
•• First select TCP protocol, introduce port number 3550 and click Run!
•• Once pressed run button, messages from the Wifi module will be received
•• When the first message is received, Send button is enabled and the user can send a message back to the radio Wifi
•• Fill the text area in the bottom of the window with the message to send, and press Send
•• Press Close to finish the connection securely
-37- v0.3
Connecting to a PC
Next code has been upload to a Waspmote with module Wifi to do the example:
void setup(){
// First, initialize the WIFI API and the connections with the Waspmote.
WIFI.begin();
// Then switch on the WIFI module.
WIFI.ON(socket0);
// 1. Configure the transport protocol (UDP, TCP, FTP, HTTP...)
WIFI.setConnectionOptions(CLIENT);
// 2. Configure the way the modules will resolve the IP address.
WIFI.setDHCPoptions(DHCP_ON);
}
void loop(){
// 3. Configure how to connect the AP.
WIFI.setJoinMode(MANUAL);
if (WIFI.join(“Libelium_AP”))
{ // 4. Call the function to create the TCP client.
if (WIFI.setTCPclient(PC_IP_Address, 3550,2000))
{ // 5. Now the connection is open, and we can use send and
// read functions to control the connection.
while(1){
WIFI.send(“TCP - Hi from WIFI module!”);
WIFI.read();
}
// Closes the TCP connection.
WIFI.close();
} } }
-38- v0.3
Connecting to a Smartphone directly
We have developed the application Waspmote Wifi, for both iPhone and another for Android platforms. The application may be
downloaded from the official app markets or from the Libelium website for free: http://www.libelium.com/apps
•• Iphone: http://itunes.apple.com/us/app/waspmote-wifi/id501974230
•• Android: https://market.android.com/details?id=com.libelium.WIFI_module
•• From the iPhone, go to App Store. Go to Search screen and search “Waspmote Wifi”.
•• Select Waspmote Wifi app. Press FREE button, and then INSTALL button.
•• Accept the rights and then the app will appear in your iPhone screen.
•• You can do the same from the Computer with iTunes. Open iTunes and search “Waspmote Wifi”:
http://itunes.apple.com/us/app/waspmote-wifi/id501974230
•• Save the app in iTunes and synchronize it with your iPhone or iPod.
-39- v0.3
Connecting to a Smartphone directly
•• Then double click on the icon, or right click and open with iTunes.
•• Inside iTunes, on the left panel, click on DEVICES->Your Device.
•• Select on the top “Apps”, and select Sync Apps. Drag into the desired screen Waspmote Wifi app.
-40- v0.3
Connecting to a Smartphone directly
To connect to the network created by the Wifi module of Waspmote: Go to Settings->Wi-Fi and select iPHONE_ADHOC.
Once connected, you will see a blue Wifi icon in the top of the screen:
-41- v0.3
Connecting to a Smartphone directly
Inside the app, the first tab “Network” shows the information of the connection:
The Monitoring tab shows the information the nodes are sending which contains the sensor data gathered. As an example
some parameters are shown: temperature, humidity, luminosity, O2, CO2, PIR and a 3D model of Waspmote that moves with the
accelerometer information in real time:
-42- v0.3
Connecting to a Smartphone directly
Finally, in the Actuator tab, there are three switches to send ON and OFF commands and a fader to control the exact value sent.
In the Libelium website there is a video which shows how the application works with a set of lights.
The Waspmote code used in this program can be downloaded from the Libelium website:
http://www.libelium.com/development/waspmote
If you are interested in the source code of the iPhone App in order to create your own software on top of it contact our
Commercial Department at: [email protected]
-43- v0.3
Connecting to a Smartphone directly
13.2.1. Installation
a) Download the application from Android Market:
-44- v0.3
Connecting to a Smartphone directly
Go to Settings->Tethering & Portable Hotspot or Settings->Wifi-> My Wifi Zone (depending of the version of mobile)
-45- v0.3
Connecting to a Smartphone directly
Then configure the WLAN hotspot (name= ANDROID, Security= Open). This settings you can change if you change as well the
Waspmote code.
Finally, enable Portable WLAN hotspot (or My Wifi Zone), and Waspmote will connect to the Android device. Once connected,
you can launch the Waspmote Wifi Demo app.
Inside the app, the first tab “Network” shows the information of the connection:
-46- v0.3
Connecting to a Smartphone directly
The Monitoring tab shows the information the nodes are sending which contains the sensor data gathered. As an example
some parameters are shown: temperature, humidity, luminosity, O2, CO2, PIR and a 3D model of Waspmote that moves with
the accelerometer information in real time.
Finally, in the Actuator tab, there are three switches to send ON and OFF commands and a fader to control the exact value
sent. In the Libelium website there is a video which shows how the application works with a set of lights.
-47- v0.3
Connecting to a Smartphone directly
The Waspmote code used in this program can be downloaded from the Libelium website:
http://www.libelium.com/development/waspmote
If you are interested in the source code of the Android App in order to create your own software on top of it contact
our Commercial Department at: [email protected]
-48- v0.3
Code examples and extended information
http://www.libelium.com/waspmote
http://www.libelium.com/support/waspmote
http://www.libelium.com/development/waspmote
-49- v0.3