Just one week after the fire: e-Health and Distributor Orders already Shipping!! Read more.
libelium
  • rss
  • flickr
  • twitter
  • facebook
  • youtube

Wifi Module for Arduino "Roving RN-XVee"

Article Index

Introduction

In this tutorial we are going to introduce the use of ARDUINO with WiFi. We will use the WiFi Module Roving RN-171 . This module fits in the XBee socket of our Communication Shield and allows to connect your Arduino shield to a WiFi network.

Ingredients:

Difficulty: Medium - medium

Preparation Time: 20 minutes

Steps Index

Step 1: Connecting the WIFI module

Connect the Wifi module to XBEE shield module as shown in the picture.

Then connect the XBEE shield to Arduino as shown in the picture.

Step 2 : Enter in command mode

To enter in command mode, write “$$$” over the UART, (IMPORTANT: There is no carriage return after $$$). You should see CMD returned to you. This will verify that your cable and comm. Settings are correct.

// Enters in command mode
Serial.print("set ip dhcp 1\r"); check();
Serial.print("set ip protocol 2\r"); check();

Step 3 : DHCP protocol

To solve the IP address with DHCP protocol, enter command

set ip dhcp 1

Most valid commands will return an AOK response, and invalid ones will return an ERR decription.

// Sets DHCP and TCP protocol
Serial.print("set ip dhcp 1\r"); check();
Serial.print("set ip protocol 2\r"); check();

Step 4 : Connecting to a router AP

To set the policy for manually joining/associating with network access points, enter command

set wlan join 0

To set the passphrase for WPA and WPA2 security mode, enter command

set wlan phrase XXX

If the security is WEP, enter command

set wlan key XXX

To join the network ssid manually, enter command

join SSID

If everything is correct, you should see something like next image once called to join.

// Configures the way to join the network AP, sets the encryption of the
// network and joins it
Serial.print("set wlan join 0\r"); check();
Serial.print("set wlan phrase PASSWORD\r"); check();
Serial.print("join libelium_wsn2\r"); check();

Step 5 : HTTP protocol

To set the server address with a DNS address, enter commands

set ip host 0
set dns name WWW.URL.COM

To set the HTTP port, enter command

set ip remote 80

To set the GET query that is sent when the por is opened, enter command

set comm remote GET$/....

To set the correct format that is sent to the web server, enter command

set opt format 1 
//HTML header based broadcast interval in this case.

And finally, to open the connection, enter command

open

If everything is correct, you should see something like next image.

// Sets the DNS address
Serial.print("set i h 0\r"); check();
Serial.print("set d n www.URL.com\r"); check();

//Configures HTTP connection
Serial.print("set i r 80\r"); check();
Serial.print("set c r GET$/...\r"); check();
Serial.print("set o f 1\r"); check();

// Calls open to launch the configured connection.
Serial.print("open\r"); check();

Step 6. Complete Code

char recv[512];
int cont;

void setup()
{

Serial.begin(9600);
}

void loop()
{
while (Serial.available()>0) {}
// Enters in command mode
Serial.print("$$$"); check();

// Sets DHCP and TCP protocol
Serial.print("set ip dhcp 1\r"); check();
Serial.print("set ip protocol 2\r"); check();

// Configures the way to join the network AP, sets the encryption of the
// network and joins it
Serial.print("set wlan join 0\r"); check();
Serial.print("set wlan phrase PASSWORD\r"); check();
Serial.print("join libelium_wsn2\r"); check();

// Sets the DNS address
Serial.print("set i h 0\r"); check();
Serial.print("set d n www.URL.com\r"); check();

//Configures HTTP connection
Serial.print("set i r 80\r"); check();
Serial.print("set c r GET$/...\r"); check();
Serial.print("set o f 1\r"); check();

// Calls open to launch the configured connection.
Serial.print("open\r"); check();
}

void check(){
cont=0; delay(1000);
// Receive the answer from the WIFI module.
while (Serial.available()>0)
{
recv[cont]=Serial.read(); delay(100);
cont++;
}
recv[cont]='\0';
// Print the answer.
Serial.println(recv);
// Clean the buffer to be able to send a command.
Serial.flush(); delay(1000);
}

Important Issues

  • There is no carriage return () after the $$$ to enter command mode.
  • While in command mode, the device will accept ASCII bytes as commands. Each command ends with a carriage return.
  • The messages that come from the WIFI module are quite big, for this reason, if you want to debug your program and check those messages, go to
    /.../arduino-0022/hardware/arduino/cores/arduino and modify HardwareSerial.cpp then make RX_BUFFER_SIZE value set to 512.

Links and documentation