Pages: [1] 2 3   Go Down
Author Topic: First steps with WiFly and Arduino  (Read 5876 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Trying to get a WiFly (this one http://www.sparkfun.com/products/11047), xBee Pro Shield and Arduino Duemilanove to work together.  Ultimately I want the arduino to pull data from the web but at the moment getting anything working and understanding it would be a start.  I've started with the WiFly_Test sketch, entered my network details, uploaded it and watched the serial monitor but it gets no further than "Starting WiFly Tester. Free memory:1168".  The red leds never stop flashing (im guessing a bad thing). 

Can anyone recommend a tutorial that walks through the WiFly_Test sketch step by step or another very simple one?  All the tutorials I'm finding are broken links.  Or can you tell me if something needs to be done before I upload the sketch i.e. does the WiFly need to be pre-configured?  What about the xBee/USB switch on the shield do I need to toggle that between positions?  It seems you do if you have an xBee on the shield.

Thanks in advance.
Garrett
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You didn't provide a link to the code your using. There are several libraries to be used with the WiFly Bees. Provide links to the one you used and post the full code of the sketch you're using.

My RN-XV works without any problem but I configured it manually and patched the library to be more predictable.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Thanks for your reply.  Yes I didn't know there were several libraries for this when I posted and it's since added to my confusion even more.  I'll try to be as descriptive as possible.  I've attached photos of the arduino, wifly and xbee shield I'm using for reference. 

So far I've been through the WiFly library (this one: https://github.com/jmr13031/WiFly-Shield) - but I believe that's pre-version 1 of Arduino?, the WiFlySerial library (this one: http://sourceforge.net/projects/arduinowifly/files/) and WiFlyHQ (this one: https://github.com/harlequin-tech/WiFlyHQ).  The last two compile and upload their examples fine and each starts, puts output in the serial monitor and then seems to go no further.

This is the WiFly_Test sketch I've mentioned which is an example that comes with WiFlySerial.  I don't care which library or demo I get working, anything would be a start I could start working back from.

thanks
Garrett


WiFly_Test:

Code:
/*
 * WiFly_Device Test Platform
 * A simple tester for communicating with the WiFly GSx RN-131b/g series.
 * LGPL 2.0
 * Tom Waldock, 2011
 */

#include <Arduino.h>
#include <Streaming.h>
#include <SoftwareSerial.h>
#include "WiFlySerial.h"
#include "MemoryFree.h"
#include "Credentials.h"


// Pins are 3 for INCOMING TO Arduino, 5 for OUTGOING TO Wifly
// Arduino       WiFly
//  2 - receive  TX   (Send from Wifly, Receive to Arduino)
//  3 - send     RX   (Send from Arduino, Receive to WiFly)
WiFlySerial WiFly(2,3);

#define REQUEST_BUFFER_SIZE 120
#define HEADER_BUFFER_SIZE 150
#define BODY_BUFFER_SIZE 100

char bufRequest[REQUEST_BUFFER_SIZE];
char bufHeader[HEADER_BUFFER_SIZE];
char bufBody[BODY_BUFFER_SIZE];


void setup() {
 
  Serial.begin(9600);
  Serial.println(F("Starting WiFly Tester." ) );
  Serial << F("Free memory:") << freeMemory() << endl; 

  WiFly.begin();
  Serial << F("Starting WiFly...") <<  WiFly.getLibraryVersion(bufRequest, REQUEST_BUFFER_SIZE)
   << F(" Free memory:") << freeMemory() << endl;
 
  // get MAC
  Serial << F("MAC: ") << WiFly.getMAC(bufRequest, REQUEST_BUFFER_SIZE) << endl;
  // is connected ?
 
  // WiFly.setDebugChannel( (Print*) &Serial);
 
  WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK);
  WiFly.setJoinMode(  WIFLY_JOIN_AUTO );
  WiFly.setDHCPMode( WIFLY_DHCP_ON );

   
  // if not connected restart link
  WiFly.getDeviceStatus();
  if (! WiFly.isifUp() ) {
    Serial << "Leave:" <<  ssid << WiFly.leave() << endl;
    // join
    if (WiFly.setSSID(ssid) ) {   
      Serial << "SSID Set :" << ssid << endl;
    }
    if (WiFly.setPassphrase(passphrase)) {
      Serial << "Passphrase Set :" << endl;
    }
    Serial << "Joining... :"<< ssid << endl;

    if ( WiFly.join() ) {
      Serial << F("Joined ") << ssid << F(" successfully.") << endl;
      WiFly.setNTP( ntp_server ); // use your favorite NTP server
    } else {
      Serial << F("Join to ") << ssid << F(" failed.") << endl;
    }
  } // if not connected

  Serial << F("IP: ") << WiFly.getIP(bufRequest, REQUEST_BUFFER_SIZE) << endl <<
    F("Netmask: ") << WiFly.getNetMask(bufRequest, REQUEST_BUFFER_SIZE) << endl <<
    F("Gateway: ") << WiFly.getGateway(bufRequest, REQUEST_BUFFER_SIZE) << endl <<
    F("DNS: ") << WiFly.getDNS(bufRequest, REQUEST_BUFFER_SIZE) << endl
    << F("WiFly Sensors: ") << bufBody <<  WiFly.SendCommand("show q 0x177 ",">", bufBody, BODY_BUFFER_SIZE) << endl
    << F("WiFly Temp: ") <<  WiFly.SendCommand("show q t ",">", bufBody, BODY_BUFFER_SIZE)
    << F("WiFly battery: ") << WiFly.getBattery(bufBody, BODY_BUFFER_SIZE) << endl;

  WiFly.SendCommand("set comm remote 0",">", bufBody, BODY_BUFFER_SIZE);
  memset (bufBody,'\0',BODY_BUFFER_SIZE);

  WiFly.closeConnection();
  Serial << F("After Setup mem:") << freeMemory() << endl ;
 
  Serial << F("WiFly now listening for commands.  Type 'exit' to listen for wifi traffic.  $$$ (no CR) for command-mode.") << endl;


  // clear out prior requests.
  WiFly.flush();
  while (WiFly.available() )
    WiFly.read();
 
}

char chOut;
void loop() {
  // Terminal routine

  // Always display a response uninterrupted by typing
  // but note that this makes the terminal unresponsive
  // while a response is being received.
 
  while(WiFly.available() > 0) {
    Serial.write(WiFly.read());
  }
 
  if(Serial.available()) { // Outgoing data
    WiFly.write( (chOut = Serial.read()) );
    Serial.write (chOut);
  }

} //loop


In the Creditials.h below is SSID the network name and passphrase the WEP number?  I've read that this needs to be 128 bit on my network and have set it as that.

Credentials.h:

Code:
#ifndef __CREDENTIALS_H__
#define __CREDENTIALS_H__

// Wifi parameters
char passphrase[] = "PassPhrase"; //"YourSecretPassPhrase123"
char ssid[] = "SSID"; //"YourSSID"
char ntp_server[] = "nist1-la.ustiming.org"; // or your favorite ntp server.

#endif


MemoryFree.cpp:

Code:
extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;


#include "MemoryFree.h"


int freeMemory() {
  int free_memory;

  if((int)__brkval == 0)
     free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else
    free_memory = ((int)&free_memory) - ((int)__brkval);

  return free_memory;
}


MemoryFree.h:

Code:
// memoryFree header

#ifndef MEMORY_FREE_H
#define MEMORY_FREE_H

#ifdef __cplusplus
extern "C" {
#endif

int freeMemory();

#ifdef  __cplusplus
}
#endif

#endif


* photo.JPG (282.7 KB, 960x720 - viewed 43 times.)

* photo1.JPG (266.43 KB, 960x720 - viewed 35 times.)
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First you have to know your network parameters (the WiFi). From what you write it seems you're using WEP (and not WPA or WPA2). In the example code a WPA2 passphrase is chosen, so for WEP you have to change

Code:
WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK);

to

Code:
WiFly.setAuthMode(WIFLY_AUTH_WEP);

and set the WEP keys accordingly. Unfortunately the library doesn't support that (setting the WEP key), so you have to do that manually, switching the Bee to USB and sending commands from your serial terminal. The command for setting the WEP key is

Code:
set wlan key <key-value>

This and other available commands are in the WiFly User Manual and Command Reference (WiFly-RN-UM). You probably should read in that document and should it be only for reference.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Unfortunately the library doesn't support that (setting the WEP key), so you have to do that manually, switching the Bee to USB and sending commands from your serial terminal. The command for setting the WEP key is

Code:
set wlan key <key-value>

This and other available commands are in the WiFly User Manual and Command Reference (WiFly-RN-UM). You probably should read in that document and should it be only for reference.

Hi

Can't believe I didn't spot the WPA2 line.  Anyway no change, it's this last bit I'm sure I doing wrong, I understand how to send the command from the serial monitor but am not sure what you mean by switching the bee to usb.  Is the switch the part I've highlighted in the attached image?  If so I permanently have the switch on the USB side as sketches don't send when they are on the xbee side.

Is there a better library to use for setting the WEP key in the sketch?

Thanks in advance.

Garrett


* xbee-shield-for-arduino.jpg (67.61 KB, 600x600 - viewed 24 times.)
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This switch has to be on the USB side when you're transferring sketches to the Arduino but on the Bee side when this sketch runs as otherwise your Arduino has no connection to the RN-XV.

You also have to remove all Serial code from your sketch as the Serial is used for communication with the RN-XV, you cannot use the serial connection for the Bee and for debugging at the same time.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your patience on this, still no joy.

This switch has to be on the USB side when you're transferring sketches to the Arduino but on the Bee side when this sketch runs as otherwise your Arduino has no connection to the RN-XV.

right I understand.

You also have to remove all Serial code from your sketch as the Serial is used for communication with the RN-XV, you cannot use the serial connection for the Bee and for debugging at the same time.

So every line that has a call to serial needs to be commented out?  That's nearly every second line in the example I have, if all of that is gone how do I know if it has successfully joined a network or not?  At the moment my WiFly_Test sketch looks like this.

Code:
/*
 * WiFly_Device Test Platform
 * A simple tester for communicating with the WiFly GSx RN-131b/g series.
 * LGPL 2.0
 * Tom Waldock, 2011
 */

#include <Arduino.h>
#include <Streaming.h>
#include <SoftwareSerial.h>
#include "WiFlySerial.h"
#include "MemoryFree.h"
#include "Credentials.h"


// Pins are 3 for INCOMING TO Arduino, 5 for OUTGOING TO Wifly
// Arduino       WiFly
//  2 - receive  TX   (Send from Wifly, Receive to Arduino)
//  3 - send     RX   (Send from Arduino, Receive to WiFly)
WiFlySerial WiFly(2,3);

#define REQUEST_BUFFER_SIZE 120
#define HEADER_BUFFER_SIZE 150
#define BODY_BUFFER_SIZE 100

char bufRequest[REQUEST_BUFFER_SIZE];
char bufHeader[HEADER_BUFFER_SIZE];
char bufBody[BODY_BUFFER_SIZE];


void setup() {
 
  //Serial.begin(9600);
  //Serial.println(F("Starting WiFly Tester." ) );
  //Serial << F("Free memory:") << freeMemory() << endl; 

  WiFly.begin();
  //Serial << F("Starting WiFly...") <<  WiFly.getLibraryVersion(bufRequest, REQUEST_BUFFER_SIZE)
  // << F(" Free memory:") << freeMemory() << endl;
 
  // get MAC
  //Serial << F("MAC: ") << WiFly.getMAC(bufRequest, REQUEST_BUFFER_SIZE) << endl;
  // is connected ?
 
  WiFly.setDebugChannel( (Print*) &Serial);
 
  //WPA2 security
  //WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK);
 
  //WEP 128 bit security
  WiFly.setAuthMode(WIFLY_AUTH_WEP);
 
  WiFly.setJoinMode(  WIFLY_JOIN_AUTO );
  WiFly.setDHCPMode( WIFLY_DHCP_ON );

   
  // if not connected restart link
  WiFly.getDeviceStatus();
  if (! WiFly.isifUp() ) {
    //Serial << "Leave:" <<  ssid << WiFly.leave() << endl;
    // join
    if (WiFly.setSSID(ssid) ) {   
      //Serial << "SSID Set :" << ssid << endl;
    }
    if (WiFly.setPassphrase(passphrase)) {
      //Serial << "Passphrase Set :" << endl;
    }
    //Serial << "Joining... :"<< ssid << endl;

    if ( WiFly.join() ) {
      //Serial << F("Joined ") << ssid << F(" successfully.") << endl;
      WiFly.setNTP( ntp_server ); // use your favorite NTP server
    } else {
      //Serial << F("Join to ") << ssid << F(" failed.") << endl;
    }
  } // if not connected

  Serial << F("IP: ") << WiFly.getIP(bufRequest, REQUEST_BUFFER_SIZE) << endl <<
    F("Netmask: ") << WiFly.getNetMask(bufRequest, REQUEST_BUFFER_SIZE) << endl <<
    F("Gateway: ") << WiFly.getGateway(bufRequest, REQUEST_BUFFER_SIZE) << endl <<
    F("DNS: ") << WiFly.getDNS(bufRequest, REQUEST_BUFFER_SIZE) << endl
    << F("WiFly Sensors: ") << bufBody <<  WiFly.SendCommand("show q 0x177 ",">", bufBody, BODY_BUFFER_SIZE) << endl
    << F("WiFly Temp: ") <<  WiFly.SendCommand("show q t ",">", bufBody, BODY_BUFFER_SIZE)
    << F("WiFly battery: ") << WiFly.getBattery(bufBody, BODY_BUFFER_SIZE) << endl;

  WiFly.SendCommand("set comm remote 0",">", bufBody, BODY_BUFFER_SIZE);
  memset (bufBody,'\0',BODY_BUFFER_SIZE);

  WiFly.closeConnection();
  //Serial << F("After Setup mem:") << freeMemory() << endl ;
 
  //Serial << F("WiFly now listening for commands.  Type 'exit' to listen for wifi traffic.  $$$ (no CR) for command-mode.") << endl;


  // clear out prior requests.
  WiFly.flush();
  while (WiFly.available() )
    WiFly.read();
 
}

char chOut;
void loop() {
  // Terminal routine

  // Always display a response uninterrupted by typing
  // but note that this makes the terminal unresponsive
  // while a response is being received.
 
  while(WiFly.available() > 0) {
    //Serial.write(WiFly.read());
  }
 
  //if(Serial.available()) { // Outgoing data
  //  WiFly.write( (chOut = Serial.read()) );
  //  Serial.write (chOut);
  //}

} //loop
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are several possibilities to debug if the serial is used for device communication. The cheapest and fastest to realise is using one ore more LEDs to show some states. An easy one from the programmers view is using SoftwareSerial to establish a serial interface on two (one is sufficient for debugging) digital pins. You then need a USB2Serial adapter (available from Arduino or other suppliers) to connect that serial to your PC. The third option is an LCD display to show the messages. There different versions available, from 4-bit to 8-bit parallel and almost any serial connection (UART is not feasible in your situation, but I2C or SPI will do).

I think your stock of devices and/or budget will decide. :-)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are several possibilities to debug if the serial is used for device communication. The cheapest and fastest to realise is using one ore more LEDs to show some states. An easy one from the programmers view is using SoftwareSerial to establish a serial interface on two (one is sufficient for debugging) digital pins. You then need a USB2Serial adapter (available from Arduino or other suppliers) to connect that serial to your PC. The third option is an LCD display to show the messages. There different versions available, from 4-bit to 8-bit parallel and almost any serial connection (UART is not feasible in your situation, but I2C or SPI will do).

I think your stock of devices and/or budget will decide. :-)

Yes I've seen somebody testing with LEDs, different LEDs, flashing rates etc.  I'd much rather debug with messages back to Serial Monitor but I don't understand how a usb2serial is needed.  My arduino is USB and I have no serial ports on my mac, I thought the USB on the arduino doubled up as serial and power?  Also the three LEDs on the WiFly, aren't they LEDs showing the WiFly's state?

I've now tried turning off all security (wep) on my network and still no change, the red led on the WiFly never stops flashing rapidly.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, your Arduino has a USB connection that doubles as power source and serial connection. The problem is that the Arduino has only one serial port (in hardware) and that one is used for the USB connection when uploading sketches. Because XBees usually also communicate using the serial, there is a conflict which is usually solved by disconnecting (in your case toggling the switch) the XBee shield when uploading the sketches and using the serial connection the rest of the time.
It's a bit like your phone at home. You can phone different people without any problem but only one at a time. The phone company has some switching circuitry to switch you through to the different people. A standard Arduino (without additional electronics) does not have such a switching logic, so you have to do this manually.

You're right, that the LEDs on the shield change their state when you are successfully connected to the network.

Have you removed all the serial stuff from the sketch with the exception I mentioned in a earlier post? Because else you won't ever connect to your network because the RN-XV will be confused with the debugging stuff your sending to it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes I think I have removed all the serial code.  Below is my sketch.  I've also tried changing my network to WPA-PSK Encryption (WPA2) and used WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK); in the sketch but nothing changes.


Code:
/*
 * WiFly_Device Test Platform
 * A simple tester for communicating with the WiFly GSx RN-131b/g series.
 * LGPL 2.0
 * Tom Waldock, 2011
 */

#include <Arduino.h>
#include <Streaming.h>
#include <SoftwareSerial.h>
#include "WiFlySerial.h"
#include "MemoryFree.h"
#include "Credentials.h"


// Pins are 3 for INCOMING TO Arduino, 5 for OUTGOING TO Wifly
// Arduino       WiFly
//  2 - receive  TX   (Send from Wifly, Receive to Arduino)
//  3 - send     RX   (Send from Arduino, Receive to WiFly)
WiFlySerial WiFly(2,3);

#define REQUEST_BUFFER_SIZE 120
#define HEADER_BUFFER_SIZE 150
#define BODY_BUFFER_SIZE 100

char bufRequest[REQUEST_BUFFER_SIZE];
char bufHeader[HEADER_BUFFER_SIZE];
char bufBody[BODY_BUFFER_SIZE];


void setup() {
 
  WiFly.begin();
 
  WiFly.setDebugChannel( (Print*) &Serial);
 
  //WEP 128 bit security
  WiFly.setAuthMode(WIFLY_AUTH_WEP);
 
  //WPA2 security
  //WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK);
 
  WiFly.setJoinMode(  WIFLY_JOIN_AUTO );
  WiFly.setDHCPMode( WIFLY_DHCP_ON );

  // if not connected restart link
  WiFly.getDeviceStatus();
  if (! WiFly.isifUp() ) {

    // join
    if (WiFly.setSSID(ssid) ) {   
      //Serial << "SSID Set :" << ssid << endl;
    }
   
    if (WiFly.setPassphrase(passphrase)) {

    }

    if ( WiFly.join() ) {
      WiFly.setNTP( ntp_server ); // use your favorite NTP server
    } else {

    }
  } // if not connected

  WiFly.SendCommand("set comm remote 0",">", bufBody, BODY_BUFFER_SIZE);
  memset (bufBody,'\0',BODY_BUFFER_SIZE);

  WiFly.closeConnection();

  // clear out prior requests.
  WiFly.flush();
  while (WiFly.available() )
    WiFly.read();
 
}

char chOut;
void loop() {
  // Terminal routine

  // Always display a response uninterrupted by typing
  // but note that this makes the terminal unresponsive
  // while a response is being received.
 
  while(WiFly.available() > 0) {
 
  }

} //loop
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you remove your WEP authentication you don't have any encryption. Or was that a misunderstanding and you changed your encryption to WPA2 (PSK)?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried last night with out any encryption so my network was open and I removed the line WiFly.setAuthMode(WIFLY_AUTH_WEP);.  Today I tried WPA2 (just in case as every example I'm finding on the web is for WPA2) and used the line WiFly.setAuthMode( WIFLY_AUTH_WPA2_PSK);.  Nether of these tests have changed anything.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4745
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you disabled the encryption you cannot just remove a line and expect the configuration to be also removed. The library does configure the RN-XV by sending serial commands. Although the library looks like that these configuration has to be made at every startup, this is not true. The RN-XV stores it's configuration in EEPROM. So if the last configuration was WPA2 and you remove the configuration line, it stays WPA2 and is not changing to any default value.
To have no encryption the value has to be changed to WIFLY_AUTH_OPEN.

I configured my RN-XV by connecting it directly (better by the use of a Foca) to my notebook and sending the serial commands myself. So I have immediate feedback from the device and if I was successful, I simply disconnect the Bee from the notebook and connect it to the Arduino. After powerup it reconnects to the WiFi network and shows success by it's status LED.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 39
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you disabled the encryption you cannot just remove a line and expect the configuration to be also removed. The library does configure the RN-XV by sending serial commands. Although the library looks like that these configuration has to be made at every startup, this is not true. The RN-XV stores it's configuration in EEPROM. So if the last configuration was WPA2 and you remove the configuration line, it stays WPA2 and is not changing to any default value.
To have no encryption the value has to be changed to WIFLY_AUTH_OPEN.

I configured my RN-XV by connecting it directly (better by the use of a Foca) to my notebook and sending the serial commands myself. So I have immediate feedback from the device and if I was successful, I simply disconnect the Bee from the notebook and connect it to the Arduino. After powerup it reconnects to the WiFi network and shows success by it's status LED.

Hi

Ok I've just tied it with my network open and using the line WiFly.setAuthMode( WIFLY_AUTH_OPEN ); - still no change, doesn't connect.  Not sure what a Foca is?  I'm happy to try and configure it directly, I've seen this mentioned in the WiFly userguide, using $$$ and changing modes etc. but what do I do this through on osx?  Can it be done in osx terminal?
Logged

Pages: [1] 2 3   Go Up
Jump to: