My Cart

Wifi Module for Arduino and Raspberry Pi "Roving RN-XVee"

Difficulty Level: Expert -


Index

  1. Introduction
  2. Connecting the Wifi module
  3. Using Wifi module in gateway mode
  4. HTTP complete code
  5. Links and documentation

Introduction

In this tutorial we are going to introduce the use of Arduino and Raspberry Pi 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 and also fits in the Raspberry Pi to Arduino shields Connection Bridge.

Ingredients

Preparation Time: 20 minutes

1. Connecting the Wifi module

Connection in Arduino

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.

Connection in Raspberry Pi

Connect the Wifi module to Raspberry Pi connection bridge as shown in the picture.

2. Using Wifi module in gateway mode

The first thing we are going to do with the module is to check the basic commands in gateway mode.

You can check all the commands in the user manual .

Entering command mode in Arduino

To enter in command mode, open Serial Monitor in Arduino IDE and type “$$$”, (IMPORTANT: There is no carriage return after $$$, so select "No line ending" for this command, later change it to "Both NL&CR" for the next commands). You should see CMD returned to you. This will verify that your cable and comm. settings are correct.

Entering command mode in Raspberry Pi

To stablish UART connection with the module you can use several programs as cutecom in Raspbian graphical environment or minicom in a terminal window in order to use it via SSH. For using minicom follow these steps:

  1. Open a Terminal window on Raspberry Pi, or connect to Raspberry Pi through SSH. In order to enter via SSH change here the IP from your Raspberry Pi. The default user is pi.
    ssh -X pi@192.168.1.X

    Now you will have to type your password. The default password is “raspberry”.

  2. Install minicom (only if you haven't done it before):
    sudo apt-get install minicom

  3. Open comunication with the module UART, the baudrate may be 9600 or 115200:
    minicom -b 9600 -o -D /dev/ttyAMA0

  4. Then if you type $$$ you'll get CMD, this means that the communication with the module is working fine. (IMPORTANT: There is no carriage return after $$$, so don't press Enter button, just type and wait a second, later for the next commands you will need to press Enter button) Now, with the module working you can check some commands to control the module.

  5. To exit minicom press CTRL+A, then press X and Yes.

Let's check some commands

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.

To enable HTML and TCP protocols, enter command:

set ip protocol 18

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 YOUR_PASSWORD

If the security is WEP, enter command:

set wlan key YOUR_PASSWORD

To join the network ssid manually, enter command:

join YOUR_SSID

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

3. HTTP complete code

Here is a complete code to send a HTML GET request and check the response to our test server in Libelium.

Arduino code

Code:
/*  
 *  Wifi Module
 *  
 *  Copyright (C) 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. 
 *  a
 *  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 http://www.gnu.org/licenses/. 
 *  
 *  Version:           2.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos Martinez & Victor Boria
 */


//Enter here your data
const char wifi_ssid[] = "*********";
const char wifi_password[] = "*********";

const char server[] = "test.libelium.com";
const char server_port[] = "80";
const char GET[] = "test-get-post.php?a=1&b=2";

int8_t answer;
char response[300];
char response2[100];
int cont;
char aux_str[50];


void setup()
{
  //Write here you correct baud rate
  Serial.begin(9600);
  //Serial.begin(115200);
  
  wificonfig(); 
}

void loop()
{
  
  Serial.println(F("Sending HTTP GET"));
  sendGET();
  delay(5000);
}


void wificonfig() {
  
  Serial.println(F("Setting Wifi parameters"));
  sendCommand("exit\r","EXIT",2000);
  delay(2000);
  
  enterConfig(2000);
  sendCommand("leave\r","DeAuth",2000);
  delay(1000);
  // Sets DHCP and TCP protocol
  sendCommand("set ip dhcp 1\r","AOK",2000);
  delay(1000);   
  sendCommand("set ip protocol 18\r","AOK",2000);
  delay(1000);  

  // Configures the way to join the network AP, sets the encryption of the
  // network and joins it
  sendCommand("set wlan join 0\r","AOK",2000); //The auto-join feature is disabled
  delay(1000);

  snprintf(aux_str, sizeof(aux_str), "set wlan phrase %s\r", wifi_password);
  sendCommand(aux_str,"AOK",2000);
  delay(1000);   
  snprintf(aux_str, sizeof(aux_str), "join %s\r", wifi_ssid);
  answer = sendCommand(aux_str,"Associated",10000);


  if (answer == 1){

    snprintf(aux_str, sizeof(aux_str), "Connected to \"%s\"", wifi_ssid);
    Serial.println(aux_str);
    delay(5000);
  }

  else {
    snprintf(aux_str, sizeof(aux_str), "Error connecting to: \"%s\"", wifi_ssid);
    Serial.println(aux_str);
    delay(1000);
  }


  Serial.println(F("Wifi succesfully configured"));
  delay(1000);
}



void sendGET() {
  
  enterConfig(2000);
  sendCommand("set i h 0\r","AOK",2000);
  delay(1000);
  snprintf(aux_str, sizeof(aux_str), "set d n %s\r", server);
  sendCommand(aux_str,"AOK",2000);

  delay(1000);
  //Configures HTTP connection
  snprintf(aux_str, sizeof(aux_str), "set i r %s\r", server_port);
  sendCommand(aux_str,"AOK",2000);
  delay(1000);  

  sendCommand("set o f 1\r","AOK",2000);
  delay(1000); 

  snprintf(aux_str, sizeof(aux_str), "set c r GET$/%s\r", GET);
  sendCommand(aux_str,"AOK",10000);
  delay(1000); 

  // Calls open to launch the configured connection.
  sendCommand("open\r","*CLOS*",10000);
  delay(1000);

  findResponse();

}


void findResponse(){
  boolean go_On = true;
  uint16_t counter = 0;

  while (go_On){

    if (response[counter] == '\r'){

      if (response[counter+1] == '\n'){

        if (response[counter+2] == '\r'){

          if (response[counter+3] == '\n'){

            go_On = false;
          }

        }	

      }		

    }

    counter++;
  }
  counter = counter + 3;

  for (int i=0;response[i+counter]!='*'; i++){
    response2[i] = response[i+counter];
    delay(100);


  }

  snprintf(aux_str, sizeof(aux_str), "GET response:\"%s\"", response2);
  Serial.println(aux_str);


}


int8_t sendCommand(const char* Command, const char* expected_answer, unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 300);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.println(Command);    // Send Command 

    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){    
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, expected_answer) != NULL)    
      {
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));    

  return answer;
}


int8_t enterConfig(unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 300);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.print("$$$");    // Send Command 


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){    
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, "CMD") != NULL)    
      {
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));    

  return answer;
}
        

Open Serial Monitor to check the module responses:

Raspberry Pi code

Code:
/*  
 *  Wifi Module
 *  
 *  Copyright (C) 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. 
 *  a
 *  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 http://www.gnu.org/licenses/. 
 *  
 *  Version:           2.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos Martinez & Victor Boria
 */

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


//Enter here your data
const char wifi_ssid[] = "libelium_wsn2";
const char wifi_password[] = "libelium.2012_";

const char server[] = "test.libelium.com";
const char server_port[] = "80";
const char GET[] = "test-get-post.php?a=1&b=2";

int8_t sendCommand(const char* Command, const char* expected_answer, unsigned int timeout);
int8_t enterConfig(unsigned int timeout);
void wificonfig();
void sendGET();
void findResponse();
int8_t answer;
char response[1000];
char response2[300];
int cont;
char aux_str[50];


void setup()
{
  //Write here you correct baud rate
  Serial.begin(9600);
  //Serial.begin(115200);

  wificonfig(); 
}

void loop()
{
  printf("Sending HTTP GET: \"%s\" to server \"%s\"\n",GET,server);
  sendGET();
  delay(5000);


}


void wificonfig() {


  printf("Setting Wifi parameters\n");
  sendCommand("exit\r","EXIT",2000);
  delay(2000);


  enterConfig(2000);

  sendCommand("leave\r","DeAuth",2000);
  delay(1000);
  // Sets DHCP and TCP protocol
  sendCommand("set ip dhcp 1\r","AOK",2000);
  delay(1000);   
  sendCommand("set ip protocol 18\r","AOK",2000);
  delay(1000);  

  // Configures the way to join the network AP, sets the encryption of the
  // network and joins it
  sendCommand("set wlan join 0\r","AOK",2000); //The auto-join feature is disabled
  delay(1000);

  snprintf(aux_str, sizeof(aux_str), "set wlan phrase %s\r", wifi_password);
  sendCommand(aux_str,"AOK",2000);
  delay(1000);   
  snprintf(aux_str, sizeof(aux_str), "join %s\r", wifi_ssid);
  answer = sendCommand(aux_str,"Associated",10000);


  if (answer == 1){

    printf("Connected to \"%s\"\n",wifi_ssid);
    delay(5000);
  }

  else {

    printf("Error connecting to: \"%s\"\n",wifi_ssid);
    delay(1000);
  }


  printf("Wifi succesfully configured\n");
  delay(1000);
}



void sendGET() {

  enterConfig(2000);		
  sendCommand("set i h 0\r","AOK",2000);
  delay(1000);
  snprintf(aux_str, sizeof(aux_str), "set d n %s\r", server);
  sendCommand(aux_str,"AOK",2000);

  delay(1000);
  //Configures HTTP connection
  snprintf(aux_str, sizeof(aux_str), "set i r %s\r", server_port);
  sendCommand(aux_str,"AOK",2000);
  delay(1000);  

  sendCommand("set o f 1\r","AOK",2000);
  delay(1000); 

  snprintf(aux_str, sizeof(aux_str), "set c r GET$/%s\r", GET);
  sendCommand(aux_str,"AOK",10000);
  delay(1000); 

  // Calls open to launch the configured connection.
  sendCommand("open\r","*CLOS*",10000);
  delay(1000);

  findResponse();

}


int8_t sendCommand(const char* Command, const char* expected_answer, unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 1000);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.println(Command);    // Send Command 

    x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){    
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      //printf("%c",response[x]);
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, expected_answer) != NULL)    
      {
        //printf("\n");
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));    

  return answer;
}

void findResponse(){
  boolean go_On = true;
  uint16_t counter = 0;

  while (go_On){

    if (response[counter] == '\r'){

      if (response[counter+1] == '\n'){

        if (response[counter+2] == '\r'){

          if (response[counter+3] == '\n'){

            go_On = false;
          }

        }	

      }		

    }

    counter++;
  }
  counter = counter + 3;

  for (int i=0;response[i+counter]!='*'; i++){
    response2[i] = response[i+counter];
    delay(100);


  }


  printf("This is the GET response: \"%s\"\n",response2);


}

int8_t enterConfig(unsigned int timeout){

  uint8_t x=0,  answer=0;
  unsigned long previous;

  memset(response, 0, 1000);    // Initialize the string

  delay(100);

  while( Serial.available() > 0) Serial.read();    // Clean the input buffer

  Serial.print("$$$");    // Send Command 


  x = 0;
  previous = millis();

  // this loop waits for the answer
  do{
    if(Serial.available() != 0){    
      // if there are data in the UART input buffer, reads it and checks for the asnwer
      response[x] = Serial.read();
      //printf("%c",response[x]);
      x++;
      // check if the desired answer  is in the response of the module
      if (strstr(response, "CMD") != NULL)    
      {
        //printf("\n");
        answer = 1;
      }
    }
  }
  // Waits for the asnwer with time out
  while((answer == 0) && ((millis() - previous) < timeout));    

  return answer;
}

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



        

Check the module responses in your terminal window:

4. Links and Documentation





If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: