Arduino Mega 2560 - Arduino Xbee v1.1 + Roving Networks WiFly RN-171B

I'm using Arduino Mega 2560, Ardunio Xbee v1.1 and Sparkfun WiFly RN XV 171B to send sensors data to a webserver.
I first program RN XV 171B Module with commands for example $$$ to enter in command mode and set w s , set w p and first i upload on the microcontroller the following sketch and the jumper on Arduino Xbee in USB mode.
void setup(){}void loop(){}

After i marked the IP Address of the WiFly module.

Which could be the correct sketch to program the microcontroller to read sensor data and send this data to webserver that collects data?
I tried to upload the sketch 'WiFly_Test' of WiFly serial example but doesn't work.

Thank you in advance
Alessandro

I tried to upload the sketch 'WiFly_Test' of WiFly serial example but doesn't work.

What didn't work? It didn't compile? It didn't upload? It didn't connect to your network? It compiled, linked, and connected but then played "It's a small world after all" at top volume from your neighbors speakers at three in the morning?

Oh, and what code?

The compile and upload it's correct.
When i pluged the Arduino Xbee Shield with Module WiFi RN XV 171B, i started the monitor serial from Arduino Program i saw that the code arrive until
Serial.println(F("Starting WiFly Tester." ) );
Serial << F("Free memory:") << freeMemory() << endl;

This is the code of Wifly_test of the example WiFly Serial

#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(0,1); // modified by me
WiFlySerial WiFly(2,3); // code original

#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

i started the monitor serial from Arduino Program i saw that the code arrive until

You can not open the serial monitor while code is being uploaded, so you can't see "code arrive". Even if you could, it would look nothing like that.

WiFlySerial WiFly(0,1); // modified by me

You can NOT do software serial on the hardware serial pins while using them as hardware serial pins. Get over it.

Edit: Fixed mistake in last sentence. It should have, and now does, say "as hardware serial pins".

How could i resolve this problem?

How could i resolve this problem?

First, you define what the problem is that you are trying to resolve.

Second, you post links, not handwaving text that define the hardware you are using. There are a number of XBee shields. Some are suitable only as ice-scrapers. Others are pretty good. Which do you have?

My configuration is:

  • Arduino Mega 2560 rev. 3
  • Arduino XBee v1.1
  • Module WiFi Roving Networks WiFly RN-171B

My configuration is:

So, you don't have an XBee shield? Are you duct taping the XBee onto the Arduino? What is the WiFi device for? Is it duct-taped on, too?

Yes, i've Arduino Xbee v1.1 as Xbee Shield.
I plug WiFly RN-XV171 on Arduino Xbee and all (ARduino Xbee + WiFly Module) after on Arduino Mega 2560.