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

Wifi shield for Raspberry Pi tutorial

Introduction

In this tutorial we are going to introduce the use of Raspberry Pi with a WiFi module. We will use the WiFi Module Roving RN-171.

Ingredients:

Difficulty: Medium -medium

Preparation Time: 20 minutes

 

NOTE: All the code examples in this tutorial use the arduPi library. You can see the documentation and download the library here.

Step 1: Connecting the WIFI module:


Connect the Wifi module to Raspberry Pi to Arduino Shield as shown in the picture.

Step 2 : Enter in command mode:

Communication with the UART can be done using cutecom (just type cutecom in a terminal on the Raspberry).

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("$$$"); 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 protocolspan>
 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 s
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:

/*
*  Copyright (C) 2012 Libelium Comunicaciones Distribuidas S.L.
*  http://www.libelium.com
*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see .
*
*  Version 0.1
*  Author: Joaquín Ruiz
*/

//Include SerialPi library
#include "arduPi.h"

SerialPi Serial;

void check();

char received[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 libelium.2012_\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.libelium.com\r"); check();

//Configures HTTP connection
Serial.print("set i r 80\r"); check();
Serial.print("set c r GET$/radioWifitest.php?name=Anartz\r"); check();
Serial.print("set o f 1\r"); check();

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

void check(){
cont=0; delay(1500);
// Receive the answer from the WIFI module.
while (Serial.available()>0)
{
received[cont]=Serial.read(); delay(100);
cont++;
}
received[cont]='\0';
// Print the answer.
//Serial.println(received);
printf("%s",received);

// Clean the buffer to be able to send a command.
Serial.flush(); delay(1500);

pinMode(13,OUTPUT);
digitalWrite(13,HIGH);

}


int main (){
	setup();
	while(1){
		loop();
	}
	return (0);
}

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.

 

Links and documentation

Datasheet
User Manual

If you are searching for Wireless Sensor Networks devices (motes) you may be interested in our ready to market sensor platform: Waspmote. You can discover the differences between both platforms in the document: Waspmote vs SquidBee.