Wifi Shield V2.0
This WiFi shield features the RN171 TCP/IP module to allow your Arduino/Seeeduino to
connect with up to 802.11b/g wireless networks.
The shield's default communication protocol with the Arduino is UART/Serial, and you
may select which digital pins (D0 to D7) to use for RX and TX with two jumper rows
we've incorporated. The shield also has two on-board Grove connectors for I2C and
Serial to allow the shield to be used with any of our Grove devices.
An on-board antenna allows the shield to cover a wider range and transmit stronger
signals. The RN171 module supports TCP, UDP, FTP, and HTTP communication
protocols to meet the needs of most wireless and Internet of Things (IoT) network
projects e.g. smart home networks, robots control, personal weather stations.
The shield is very well documented with our examples below and its user manual.
Version Tracker
Parameter
Wifi Shield V1.0
Wifi Shield V1.1(v1.2)
Wifi Shield V2.0
Voltage
+3.5V~+5V
+3.5V~+5V
+3.5V~+5V
Standard Shield
Yes
Yes
Yes
Communication Mode Serial port
Serial port
Serial port
Standard Shield
No
Yes
Yes
Antenna Type
mast antenna
PCB antenna
onboard antenna
Library File
Wifi Shield Library V1.0 New Wifi Shield Library New Wifi Shield Library the same as v1.2
Specifications
Parameter
Value
Operating voltage
3.3~5.5 V
Compatible board directly
Arduino Uno/Seeeduino
Current
25~400mA
Transmit power
0‐10 dBm
Frequency
2402~2480 MHz
Channel
0~13
Network rate
1‐11 Mbps for 802.11b/6‐54Mbps for 802.11g
Dimension
60X56X19 mm
Net Weight
24±1 g
Secure WiFi authentication
WEP‐128, WPA‐PSK (TKIP), WPA2‐PSK (AES)
Built‐in networking applications DHCP client, DNS client, ARP, ICMP ping, FTP, TELNET, HTTP, UDP, TCP
Certification
RN171: FCC, CE
Compatibility
We have produced a lot of extension board that can make your platform board more powerful,
however not every extension board is compatible with all the platform board, here we use a table
to illustrate how are those boards compatible with platform board.
Note
Please note that "Not recommended" means that it might have chance to work with the platform
board however requires extra work such as jump wires or rewriting the code. If you are
interested in digging more, welcome to contact with techsupport@seeed.cc.
Click to see full
picture
Hardware Overview
The WiFi shield is compatible with any Arduino/Seeeduino development board as it only
requires two digital pins of your choice between D0-D7 for UART/serial communication. To
install, simply stack the shield on the Arduino/Seeeduino board.
1. Serial Peripheral Interface (SPI) Connections (MOSI, SCK, MISO): These pins are not
connected to any of the Arduino's pins, they are independent and the logic level output/input
of them is 3.3V. They can be used to communicate with the Arduino via SPI but a 3.3V
logic converter between these pins and the Arduino's will be needed. The data rate in SPI
mode can reach up to 2Mbps.
RES_Wifi: The Wifi shield has an on-board "Rest Button" for the RN-171 module, you
may also reset the RN-171 via software by sending the reset command. Additionally, if you
would like to connect this pin to the Arduino's digital 6 pin, simply solder the pad labeled
"P5" on the shield.
2. RN171: A super low power consumption wireless module with TCP/IP stack built in.
3. Antenna: I.PEX connector.
4. RN171 breakout section: The RN171 module has its own analog input and GPIO pins, which the
shield provides access to via this breakout section. The GPIO pins (IO3, IO7, IO8, and IO9) are 3.3V
tolerant while the analog input pins (S_0 and S_1) can read 0‐400mV (Do not exceed 1.2V). The
RN171 can be configured to use these pins by software or they may connected to other pins to use
other RN171 functions such as adhoc mode. The voltage of VCC is dependent on the supply power
of the WiFi shield.
5. UART/Serial Select area: Two jumper rows to let you select which RX and TX pins you want to use
to communicate with the Arduino.
6. Grove connectors: Analog I2C Grove (if using Arduino UNO or Seeeduino) for pins A4&A5 and
Digital Serial Grove for D8&D9. The voltage VCC is dependent on the power supply of the board.
Pins Used / Shield Compatibility¶
The WiFi shield uses any two digital pins of your choice between D0 and D7 to communicate
with the RN171 WiFi module, however keep in mind that D0 and D1 are used by the Arduino
for programming and serial communication purposes and using them might interfere with these
two functions.
In the example codes in this page we use D2 and D3 as RX and TX for the shield. In this case,
the jumper hats should be connected as shown below:
D2 selected for WIFI_TX, D3 selected for WIFI_RX
RN171 WiFi Module
The RN-171 is a standalone complete TCP/IP wireless networking module. Due to its small form
factor and extremely low power consumption, the RN-171 is perfect for mobile wireless
applications. It incorporates a 2.4GHz radio, 32-bit SPARC processor, TCP/IP stack, real-time
clock, crypto accelerator, power management, and analog sensor interfaces.
In the simplest configuration the hardware only requires four connections (PWR, TX, RX and
GND) to create a wireless WiFi data connection. Additionally, the analog sensor inputs of the
RN171 can be used as analog input pins, their rating is 0-400 mV (Do not exceed 1.2V DC).
Power: The operating voltage of the RN-171 module is 3.3VDC typically, so a voltage regulator
and logic level translator are designed on the WiFi shield. The LD1117 regulator on the shield
converts to 3.3VDC, which supplies the RN171 module. However, due to the auto judgement
schematic of power supply, the RN-171 can be powered via both 3V3 pin and 5V pin. But the
supply power would be 5v if providing both 3.3v and 5v to the board. If using with an
Arduino/Seeeduino board simply stack the WiFi shield on the board.
Diagram of how the RN171 module is interfaced to the Arduino
GPIO_6 : The GPIO6 pin of the RN171 WiFi module is by default only connected to the LED
labeled D5 on the WiFi shield. This LED is used to display the status of the Access Point (AP)
connection. If however, you would like to connect GPIO6 to digital pin 5 of the Arduino, simply
solder the pad labeled "P6" on the WiFi shield.
LED Status Indicators
Label Description
Status
Hardware Connection
D5 Green LED. Indicates the
association status.
OFF: means the module is not associated with Connected to GPIO6 of the
a network.
RN171 module
Label Description
Status
Hardware Connection
Solid ON: indicates that it is associated and
Internet access is OK
D1 Red LED. Indicates the
TCP/IP connection status.
Solid ON: connected over TCP.
Connected to GPIO4 of the
RN171 module
Fast Toggle (2 times/second): No IP
address or module is in command mode.
Slow Toggle (once/second): IP address is
OK.
RST Red LED. WiFi module reset Solid ON: The reset button (WIFI_RST) is been Connected to Reset of the
status.
pressed.
RN171 module.
PWR Green LED. Indicates WiFi Solid ON:The module/shield is powered up.
module's power up status.
Connected to the 3.3V output of
the LD1117 voltage regulator.
WiFi Library
We have created a library to help you interface with the shield, in this section we'll show you
how to set up the library and introduce some of the functions.
Setup
1. Download the library code as a zip file from the Wifi Shield github page.
2. Unzip the downloaded file into your …/arduino/libraries/ folder .
3. Rename the unzipped folder "WifiShield"
4. Start the Arduino IDE (or restart if it is open).
Functions
These are the most important/useful function in the library, we invite you to look at the .h files
yourself to see all the functions available.
join()
Description:
Used to join a WiFi access point
Syntax:
join(const char *ssid, const char *phrase, int auth)
Parameters:
ssid: The name of the access point you want the shield to connect to
phrase: The password/phrase of the access point you want the shield to connect to
auth: The authentication type of the access point you want the shield to connect to. Can be
one of the following constants:
WIFLY_AUTH_OPEN
WIFLY_AUTH_WEP
WIFLY_AUTH_WPA1
WIFLY_AUTH_WPA1_2
WIFLY_AUTH_WPA2_PSK
WIFLY_AUTH_ADHOC
Returns:
boolean: true if the connection to the access point was successful, false otherwise.
Example:
1#include
2#include "WiFly.h"
3
4SoftwareSerial uart(2, 3); // create a serial connection to the WiFi shield TX and RX pins.
5WiFly wifly(&uart); // create a WiFly library object using the serial connection to the WiFi shield we
6created above.
7
8void setup()
9{
10 uart.begin(9600); // start the serial connection to the shield
11 Serial.begin(9600); // start the Arduino serial monitor window connection
12 wifly.reset(); // reset the shield
13 while(wifly.join("mySSID","mySSIDpassword",WIFLY_AUTH_WPA2_PSK) == false)
14 {
Serial.println("Failed to connect to accesspoint. Will try again.");
15
16 }
17 Serial.println("Connected to access point!");
18}
19
20void loop()
21{
22
}
Tip
The examples is based on Arduino UNO and we take D2/D3 as the SoftwareSerial pins. If you
are using an Arduino Mega, D2 is not available anymore. More details please refer to Arduino
Software Serial Here's an example.
As for the code, you need to do some change as well:
1SoftwareSerial uart(10, 3); // create a serial connection to the WiFi shield TX and RX pins.
receive()
Description:
Can be used to read data from the shield, an alternative for the Arduino's read() function.
Syntax:
receive(uint8_t *buf, int len, int timeout)
Parameters:
buf: A buffer array where the bytes read from the shield is stored.
len: The length/size of the buffer array
timeout: A timeout value to know when to stop trying to read.
Returns:
int: The number of bytes read from the shield.
Example:
1 char c;
2 while (wifly.receive((uint8_t *)&c, 1, 300) > 0) {
3 Serial.print((char)c);
4}
See File->Examples->WiFi_Shield->wifly_test sketch for a complete example.
sendCommand()
Description:
Some our functions (e.g. join(), reboot(), save()) act as wrappers for the text commands listed
in the user manual of the RN171 module. The function sendCommand() allows you to come up
with your own wrapper function if ours do not meet your needs.
Syntax:
sendCommand(const char *cmd, const char *ack, int timeout)
Parameters:
cmd: Any command from the RN‐171's user manual.
ack: The expected return string from the command
timeout: The time allowed before considering the output a bad request/response
Returns:
boolean: true if the WiFi shield responded with the ack string, false otherwise.
Example:
1// our join() function is wrapper for the join command, as seen below.
2//The string "Associated" is what the user manual says the RN171 will return on
3success.
4if(sendCommand("join\r", "Associated",DEFAULT_WAIT_RESPONSE_TIME*10))
5{
6 // joined
7}else{
8 // not able to join
}
See File->Examples->WiFi_Shield->wifly_test sketch for a complete example.
WiFi Shield Examples/Applications
Example 1: Send Commands to WiFi Shield and Receive Response Via The Arduino
Serial Monitor Window
The WiFi shield's RN-171 module is configured by sending it the commands found in its
datasheet. You may write a sketch to send the commands automatically, but this is a great
example that we recommend you go through because it will teach you exactly how the WiFi
shield and RN-171 works.
To proceed follow the steps below, we have also created a video if you prefer to watch that
Video - Getting Started With Seeeduino's WiFi Shield.
Step 1: WiFi Shield Jumpers Configuration
Position the jumpers in the WiFi shield such that digital pin 2 (D2) is selected for WIFI_TX, and
digital pin 3 (D3) is selected for WIFI_RX as shown in the photo below. These are the pins we
will use to send and receive information from the RN-171.
Pins D2 for TX, and D3 for RX
Step 2: Software/Code
In the sketch below we have created a UART object to allow us to send and receive data from
the RN-171/WiFi Shield. We then use this object in conjunction with the WiFly library to send
data to the shield. The Arduino's Serial object is used to print the data we receive from the shield,
and to receive the commands we want to send to the shield via the WiFly/UART object.
Upload the following code to your Arduino board:
1#include
2#include
3#include "WiFly.h"
4
5// set up a new serial port.
6SoftwareSerial uart(2, 3); // create a serial connection to the WiFi shield TX and RX pins.
7WiFly wifly(&uart); // create a WiFly library object using the serial connection to the WiFi shield we created above.
8
9void setup()
10{
11 uart.begin(9600); // start the serial connection to the shield
12 Serial.begin(9600); // start the Arduino serial monitor window connection
13 delay(3000); // wait 3 second to allow the serial/uart object to start
14}
15
16void loop()
17{
18
19
20
21
22
23
24
25
26}
while (wifly.available()) // if there is data available from the shield
{
Serial.write(wifly.read()); // display the data in the Serial monitor window.
}
while (Serial.available()) // if we typed a command
{
wifly.write(Serial.read()); // send the command to the WiFi shield.
}
Step 3: Entering Command Mode
The WiFly RN-171 module in the WiFi shield can operate in two modes: data, and command.
When in data mode, the shield is able to receive and initiate connections. When in command
mode, we are able to configure the module using the commands listed in its datasheet.
To enter command mode, follow these steps:
1. Open the Arduino Serial monitor.
2. Set the serial monitor to “No line ending”, baud rate to 9600.
3. Type "$$$" into the Arduino Serial Monitor and press enter.
4. The module will respond with the letters “CMD”, indicating that it has entered command mode.
Let's go ahead and test some commands, do the following:
1. In the Arduino Serial monitor window, select “Carriage return” and a baud rate of 9600.
2. Now type each of the commands in the table below into the Arduino Serial Monitor and press
enter.
3. The module will output a response, as described in the table, for each command.
Commands Description
scan
This command performs an active probe scan of access points on all 13 channels. When you use this
command, the module returns the MAC address, signal strength, SSID name, and security mode of the
access points it finds.
get ip
This command displays the IP address and port number settings
For a complete list of configuration commands, please see the RN-171 Reference Guide starting
on page 11.
Example 2: Connect to An Access Point / Internet Router
In this example we will show you how to connect the WiFi shield to an access point (your
internet router) with and without you typing the commands required:
Connecting By Typing Commands
This section will teach you how to connect the WiFi shield to an access point using commands
from the RN-171 datasheet, by going through this section you will then know exactly what is
happening in the background when you use our WiFi Arduino libraries.
Do the following:
1. Upload the code in Example One to your Arduino board
2. Enter command mode:
a. Set the serial monitor to “No line ending”, baud rate to 9600.
b. Type $$$ into the Arduino Serial Monitor and press enter.
3. Set the serial monitor to “Carriage return”.
4. Scan for available access points:
a. Type scan and press enter. The Arduino serial monitor window will output a list of comma
separated values for each access point the WiFi shield has found. From left to right the third
value is the security mode, the last value is the SSID. This example shows a security mode of 4
with an SSID name MySSID: 01,01,‐88,04,1104,1c,00,45:56:78
93:1f,MySSID
5. From the list of access points found, find the one which corresponds to your internet router
and note the security mode, and SSID as we will need these two values to connect to it.
6. Set the security mode in the shield:
a. Type set wlan auth m. Replace m with the security mode number (in this example that would
be 4) of the access point you wish to connect to.
b. The security modes supported by the WiFi shield are listed in Figure 1 below.
7. Set the access point phrase
a. Type set wlan phrase myPhrase. Replace myPhrase with your access point's
password/security key.
Note
If your access point's security type is WEP use key instead of phrase in the command above.
b. The access point's (internet router) phrase is the password you use to connect to it from
your PC. In Windows you can find it as shown in the animated image below:
How to find a networks' security key/password
8. Join the access point
a. Now that we have set the security type and phrase of the access point, we may connect to it.
b. Type join MySSID. Replace MySSID with your access point's broadcast name.
c. The word "Associated!" will be displayed in the Arduino serial monitor window if successful.
A description of the commands you entered in the steps above is available in the table below. A
more detailed description of each command can be found in the RN171's user manual.
Number Commands
Description
1
scan
This command performs an active probe scan of access points on all 13 channels. When
you use this command, the module returns the MAC address, signal strength, SSID name,
and security mode of the access points it finds.
2
set wlan auth 4 Find the value that corresponds to the security protocol on your access point. Then, tell
the WiFly what security protocol to use, it is the number shown in Figure 1 that
corresponds to the access point's security protocol. Here we choose “4”.
3
set wlan phrase Tell the WiFi shield your passphrase.
seeed‐mkt
Number Commands
Description
4
Tell the WiFi shield to join, “SEEED‐MKT“ is the name of the access point we choose to
connect. After sending the command the module should now connect and print out
information about the connection. (If the connection is failed, try to send the command
again until it works )
join SEEED‐
MKT
Value Authentication Mode
0
Open (Default)
1
WEP‐128
2
WPA1
3
Mixed WPA1 and WPA2‐PSK
4
WPA2‐PSK
5
Not used
6
AD hoc mode (join any ad hoc network)
8
WPE‐64
Figure 1
Connecting Using Our WiFi Libraries
Now that you know how to connect to an access point by typing each command it's time to use
the libraries and examples we provide.
To see code required to connect to an access point go to “File -> Examples -> Wifi_Shield ->
wifi_test”. Change the code to use your own SSID (access point name), and KEY (your access
point's password), then upload the sketch to your Arduino IDE.
#define SSID
#define KEY
" SEEED-MKT "
" seeed-mkt "
With the sketch uploaded to your Arduino board, open the serial monitor window. If the shield
was successful in joining the access point an "OK" message will be displayed along with the
connection information resulting from the "get everything" command. If the shield failed to join
the access point a "Failed" message will be displayed.
Configuring The Shield to Connect On Power‐Up
The shield can be configured to connect on power up, you only have to do this once:
1. Send the "set wlan ssid mySSID" command replacing mySSID with your SSID
2. Send the "set wlan join 1" command.
3. Send the "save" command.
Now the shield will connect to the access point automatically on power up.
A description of what each command does can be found in the RN-171 datasheet and in the table
below.
Number Commands
Description
1
set wlan ssid
"" is the name of the access point you'd like to connect to automatically
2
set wlan join 1 This tells the module to try and connect to the SSID stored in memory automatically.
3
save
Store/Save these settings in the Wifi's config file
Setting a Static IP Address
To have the shield obtain a static IP address from the access point, once connected to the access
point, send the following commands:
Number
Commands
Description
1
set ip dhcp 0
Turn of DHCP .
2
set ip address
Set the IP address you want .
Example 3: Communicating With the Network ()¶
This example will show you how a device such as your PC and/or phone may talk to the WiFi
shield.
Follow these steps:
1. Configure the module with step1‐7 in Example 2's section Connecting By Typing Commands
2. Set the listening IP port to "80" by sending the commands "set ip local 80"
3. Connect/Join your shield to an access point as shown in the step 8 in Example 2's
section Connecting By Typing Commands
4. Save these setting by sending the "save" command
5. Get the IP address of your shield with the command "get ip". The IP address and port will be
displayed to the right of "IP=" in the response (e.g. IP=192.168.0.10:80)
6. Open your web browser and type your shield's IP address in your web browser's URL bar and press
Enter to visit it.
7. Your Arduino's serial monitor window will display an HTTP response similar to the one below. This
is the information that your browser sent to the shield to request data.
1*OPEN*GET / HTTP/1.1
2Host: 192.168.0.10
3Connection: keep-alive
4Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
5User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
6Chrome/39.0.2171.95 Safari/537.36
7Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
The browser is now waiting for data, the Wifi module can send sensor values, serve web pages,
or any other data straight back to the browser! In this case, the browser is waiting for a web page.
If the Wifi module responds with an HTML-formatted page, the browser will display it. The next
examples will teach you how to do all this fun stuff.
Example 4: Using the WiFi Shield as Webserver (Serving Webpages From the Shield)¶
As you saw in Example 3, an internet/web browser is able to connect to the WiFi shield. Once a
connection has been established (when the browser sends its HTTP request), the WiFi shield
may then send back HTML code for the browser to display as a webpage. In this example you
will learn what is needed for the shield to reply to a web browser.
Step One: Arduino Code
Upload the following code to your Arduino board replacing "myssid" and "mypassword" with
your accesspoint's values respectively:
1#include
2#include "WiFly.h"
3
"myssid"
4#define SSID
"mypassword"
5#define KEY
6// check your access point's security mode, mine was WPA20-PSK
7// if yours is different you'll need to change the AUTH constant, see the file WiFly.h for avalable security
8codes
WIFLY_AUTH_WPA2_PSK
9#define AUTH
10
11int flag = 0;
12
13// Pins' connection
WiFly
14// Arduino
15// 2 TX
16// 3 RX
17
18SoftwareSerial wiflyUart(2, 3); // create a WiFi shield serial object
19WiFly wifly(&wiflyUart); // pass the wifi siheld serial object to the WiFly class
20
21void setup()
22{
23 wiflyUart.begin(9600); // start wifi shield uart port
24 Serial.begin(9600); // start the arduino serial port
25 Serial.println("--------- WIFLY Webserver --------");
26
27 // wait for initilization of wifly
28 delay(1000);
29
30 wifly.reset(); // reset the shield
31 delay(1000);
32 //set WiFly params
33
34 wifly.sendCommand("set ip local 80\r"); // set the local comm port to 80
35 delay(100);
36
37 wifly.sendCommand("set comm remote 0\r"); // do not send a default string when a connection
38opens
39 delay(100);
40
41 wifly.sendCommand("set comm open *OPEN*\r"); // set the string that the wifi shield will output when
42a connection is opened
43 delay(100);
44
45 Serial.println("Join " SSID );
46 if (wifly.join(SSID, KEY, AUTH)) {
Serial.println("OK");
47
48 } else {
Serial.println("Failed");
49
50 }
51
52 delay(5000);
53
54 wifly.sendCommand("get ip\r");
55 char c;
56
57 while (wifly.receive((uint8_t *)&c, 1, 300) > 0) { // print the response from the get ip command
Serial.print((char)c);
58
59 }
60
61 Serial.println("Web server ready");
62
63}
64
65void loop()
66{
67
68 if(wifly.available())
69 { // the wifi shield has data available
if(wiflyUart.find("*OPEN*")) // see if the data available is from an open connection by looking for
70
71the *OPEN* string
{
72
Serial.println("New Browser Request!");
73
delay(1000); // delay enough time for the browser to complete sending its HTTP request string
74
// send HTTP header
75
wiflyUart.println("HTTP/1.1 200 OK");
76
wiflyUart.println("Content-Type: text/html; charset=UTF-8");
77
wiflyUart.println("Content-Length: 244"); // length of HTML code
78
wiflyUart.println("Connection: close");
79
wiflyUart.println();
80
81
// send webpage's HTML code
82
wiflyUart.print("");
83
wiflyUart.print("");
84
wiflyUart.print("My WiFI Shield Webpage");
85
wiflyUart.print("");
86
wiflyUart.print("");
87
wiflyUart.print("Hello World!");
88
wiflyUart.print("This website is served from my WiFi module");
89
wiflyUart.print("Yahoo! Google");
wiflyUart.print("My Button");
92
wiflyUart.print("");
wiflyUart.print("");
}
}
}
Step Two: Get the Shield's IP Address
Open the serial monitor window and wait for the "Web server ready" message to display. The
serial monitor will also display the WiFi shield's IP address:
Arduino program serial comm output. The IP address of the shield is highlighted.
Step Three: Visiting the webpage
Now visit that IP address in your web browser. The webpage below should be displayed, it
contains a link to Yahoo! and Google and a button that doesn't do anything (yet):
A simple webpage with two links and one button served from the WiFi shield.
When the webpage is visited the serial monitor window will also display a "New Browser
Request!" string as shown below:
The Arduino serial comm window showing that it detected a new browser connection/request.
Note
In case of some browsers, like Google Chrome, even typing the URL in the bar sends a webpage request,
this is because these browsers try to get the webpage's title for the user's convenience even before
he/she visits the webpage.
Example 5: Controlling The Arduino Digital Pins From a Webpage (Toggling LEDs
From an Webpage)
In this example we will create a webpage with three buttons to control three different digital pins
in the Arduino.
For this tutorial follow the steps below. We have also created a video where we explain the code
in more detail.
Video - WiFi Shield Arduino Digital Pin Control From Webpage
Step 1: Hardware
Connect three LEDs and resistor to digital pins 11, 12, and 13 as shown in the schematic below:
Three LEDs and 1k resistors connected to pins 11, 12, and 13.
Step 2: Arduino Sketch
Upload the following code to your Arduino board but replace "mySSID" and "myPassword" with
your access point's SSID name and password:
1#include
2#include "WiFly.h"
3
"mySSID"
4#define SSID
"myPassword"
5#define KEY
6// check your access point's security mode, mine was WPA20-PSK
7// if yours is different you'll need to change the AUTH constant, see the file WiFly.h for avalable
8security codes
WIFLY_AUTH_WPA2_PSK
9#define AUTH
10
11int flag = 0;
12
13// Pins' connection
WiFly
14// Arduino
15// 2 TX
16// 3 RX
17
18SoftwareSerial wiflyUart(2, 3); // create a WiFi shield serial object
19WiFly wifly(&wiflyUart); // pass the wifi siheld serial object to the WiFly class
20char ip[16];
21
22void setup()
23{
24 pinMode(11,OUTPUT);
25 digitalWrite(11,LOW);
26
27 pinMode(12,OUTPUT);
28 digitalWrite(12,LOW);
29
30 pinMode(13,OUTPUT);
31 digitalWrite(13,LOW);
32
33 wiflyUart.begin(9600); // start wifi shield uart port
34
35 Serial.begin(9600); // start the arduino serial port
36 Serial.println("--------- WIFLY Webserver --------");
37
38 // wait for initilization of wifly
39 delay(1000);
40
41 wifly.reset(); // reset the shield
42 delay(1000);
43 //set WiFly params
44
45 wifly.sendCommand("set ip local 80\r"); // set the local comm port to 80
46 delay(100);
47
48 wifly.sendCommand("set comm remote 0\r"); // do not send a default string when a connection
49opens
50 delay(100);
51
52 wifly.sendCommand("set comm open *OPEN*\r"); // set the string that the wifi shield will output
53when a connection is opened
54 delay(100);
55
56 Serial.println("Join " SSID );
57 if (wifly.join(SSID, KEY, AUTH)) {
Serial.println("OK");
58
59 } else {
Serial.println("Failed");
60
61 }
62
63 delay(5000);
64
65 wifly.sendCommand("get ip\r");
66
67 wiflyUart.setTimeout(500);
68 if(!wiflyUart.find("IP="))
69 {
Serial.println("can not get ip");
70
while(1);;
71
72 }else
73 {
Serial.print("IP:");
74
75 }
76
77 char c;
78 int index = 0;
79 while (wifly.receive((uint8_t *)&c, 1, 300) > 0) { // print the response from the get ip command
if(c == ':')
80
{
81
ip[index] = 0;
82
break;
83
}
84
ip[index++] = c;
85
Serial.print((char)c);
86
?
87
88 }
89 Serial.println();
90 while (wifly.receive((uint8_t *)&c, 1, 300) > 0);;
91 Serial.println("Web server ready");
92}
93
94void loop()
95{
// the wifi shield has data available
96 if(wifly.available())
97 {
98
if(wiflyUart.find("*OPEN*")) // see if the data available is from an open connection by looking for
99
100the *OPEN* string
{
101
Serial.println("New Browser Request!");
102
delay(1000); // delay enough time for the browser to complete sending its HTTP request string
103
104
if(wiflyUart.find("pin=")) // look for the string "pin=" in the http request, if it's there then we want
105
106to control the LED
{
107
Serial.println("LED Control");
108
// the user wants to toggle the LEDs
109
int pinNumber = (wiflyUart.read()-48); // get first number i.e. if the pin 13 then the 1st
110
111number is 1
int secondNumber = (wiflyUart.read()-48);
112
if(secondNumber>=0 && secondNumber