Go Down

Topic: WiFlyHQ: A new library for the WiFly RN-XV (Read 43530 times) previous topic - next topic


I have a question - do you have plans to support flow control? I'm losing bytes while ftp-ing large amounts of data at high speed from Arduino.

Do you have a suggestion for how to implement hardware flow control on an atmega328p?

You may have a poorly matched baud rate selected based on the crystal speed on your arduino. Here's a nice break-down of the rate errors for different crystal speeds: http://www.wormfood.net/avrbaudcalc.php. You might get better results if you switch to a 14.7456 Mhz crystal.

The WiFly datasheet claims that you can use rates above 115200 if your packes are uniform and you ensure the last packet has been sent before sending the next. So you may have some options there.

Another problem may be that the RX serial buffer is overflowing before yor sketch empties it. Optimizing the sketch and/or increasing the size of the serial buffer may help.


May 10, 2012, 09:41 pm Last Edit: May 10, 2012, 10:53 pm by felis Reason: 1
I'm sending data, not receiving. That's how I do it. I pick a digital pin, make it an input and connect CTS to it. I configure flow control on WiFly. in write() I read the pin and don't send until it's low. It can easily be done by modifying write(); I just though it would be nice to have it configurable instead.

[EDIT] I meant RTS, not CTS, sorry. Also, when you have flow control enabled, tie CTS to ground, otherwise you won't be receiving anything from the module's UART. You can also drive CTS if you need flow control in this direction. [/EDIT]


I have implemented my own "flow control" at 460k baud.  It is also required when using FTP at 115k.  You must know the default buffer size for each baud rate.  You send that much at a time, then wait x milliseconds.  Find x by trial and error.  This is exactly the problem I'm referring to above.  It appears to depend on the internet connection speed, ping time, and ISP.  But not the Wifi maximum bitrate as much.  My solution works great.  Is there any way to decrease x?  I'm only able to get 5K/sec at 460k.


400 kbaud should be 50kB/sec.  I would have so many applications for this if I could decrease x to get the average rate to 10k bytes/sec.  Any ideas how?  Is it waiting for a response from the router after each packet sent?  Or from the FTP server?  I would instead by happy if I could get to 10k even with a 1% data loss.  5% loss?  Will experiment and post my results here.


The official arduino wireless proto shield with sd slot has a switch so you can talk directly to the RN-XV via usb...  Might be a good idea to include it.


Thanks I've added it to the wiki. I assume you have one and its working with your WiFly?

Yup it's the model I included in the title of my threads... :) the one you helped me with my Internet dog treat dispenser... :)
cheers and thanks again for your help with that :)


Has anyone figured this out?  Someone must want to send FTP data fast.  Please post working code with speed test results.


I'm having trouble with your httpserver example on github.  Should I download the newest library again?  Mine download is weeks old.

Got GET request
Sent index page
Unexpected: GET /favicon.ico HTTP/1.1
Sending 404

It displays the index page just fine in my browser, then nothing.
Please help!


May 20, 2012, 04:00 pm Last Edit: May 20, 2012, 04:13 pm by ndanl Reason: 1
ok guys so I've got this http://www.sparkfun.com/products/11047 module and this http://www.sparkfun.com/products/10586 board and I use this http://pastebin.com/iZhr87Lc sketch, can anybody help me implement the httpserver based on the sketch I have ?

my wireless router use wpa2



Jun 01, 2012, 04:11 pm Last Edit: Jun 01, 2012, 07:33 pm by supermaggel Reason: 1

I'm trying to get any of the example sketches working from the library, but i seem to get the same error
that ndanl got:

Code: [Select]
Free memory: 1283
setPrompt failed
Failed to enter command mode
Failed to start wifly
Terminal ready

I use this board and this module with the WiFlyHQ library.
I think it's the baudrate of the xbee that's not right, but how do i enter command mode? i have 2 tiny microswitches on the breakout board,
one for switching between XBEE/USB and one for RUN/PROG.

It seems to me that if i should set it to XBEE and PROG i should be able to enter command mode with '$$$' right?
and then issue the command set baud rate 115200 for example?

The sketch i'm using is the example HTTPSERVER sketch, so i've not included it.

Cany anyone help me? Thanks a bunch! i can make a photo of my setup if that's what's needed. but i just connected the module to the shield,
and the shield to the arduino. i've decoupled all of my other electronic stuff...

[EDIT] I've tried different baudrates, from 9600 up to 230400, doesn't make any difference.
Also, switching between XBEE/USB and RUN/PROG doesn't seem to make any difference either.. i'm at a loss what i can do to analyse / debug this :/

[edit2] i've also tried to just enter command mode, but thats not working either.. how can i know for sure that i'm connecting to the wifly module? or is this why you guys all use the softserial thing? because switching just doesnt work well?
if i want to use softserial, how should i be able to read the output on my mac? is that done with these FTDI - USB Boards? help!


Jun 05, 2012, 11:51 am Last Edit: Jun 05, 2012, 12:00 pm by supermaggel Reason: 1
I now finally understand how it all works, i did not before.
The library uses the hardware serial to send debug information, and uses a 'soft' serial to communicate
with the RN-XV module (you cannot see this communication, only the debug messages in the sketch).

Another problem i ran into, is that i could not enter command mode using this blasted shield. The shield is from DFrobot and is mentioned in compatible shields earlier in this thread i think.

See these comments (scroll down to ahmad's comments on Sparkfun.
So, i connected the RN-XV directly (because without cutting pins i can still return the shield, as i don't need it apparently) to 3.3V, ground, Din to arduino pin 8, Dout to arduino pin 9 and used the softserial. I might mix this up, maybe its Din to pin 9 and Dout to pin 8.

Anyway, this time it worked (after resetting the ad-hoc mode) However, i can only enter command mode by using Telnet on port 2000.
Kinda sucks. in Ad-hoc mode this is no problem, as the RN-XV will have it's own chosen IP, and so that IP + port 2000 is it's telnet port.
But in client mode, as in joining an existing WiFi network, you'd have to find out what it's IP is and then be able to connect to it, a bit annoying.

I hope this helps anyone who experiences the same problem with the mentioned shield!


Jun 05, 2012, 12:02 pm Last Edit: Jun 05, 2012, 02:26 pm by supermaggel Reason: 1
Also, i am now with my limited C++ knowledge going to find out if i can make a WebSocketServer
using the WebSocketClient example, and Per Ejeklint's WebSocketServer code.

If there's anyone with a lot of C++ experience who'd like to help me get this working, YES PLEASE!
drop me a line: maggelo@gmail.com.

I've also started a new topic in programming questions
in which i will run into problems concerning getting the 2 libraries to play nice together :)


Jul 04, 2012, 07:36 pm Last Edit: Jul 04, 2012, 08:09 pm by eszespeter Reason: 1

  • Starting
    Free memory: 1065
    Joining network
    Failed to join wifi network

I always get this using the rn-xv (rev3) with Arduino and trying to start the "httpclient".
I'm using the software serial. Reseted the board. Tried storing the auth data on the chip itself. Nothing works.

Any idea what's going wrong?


how to send a few GET request with this device??

I proof this

Code: [Select]
#include <SPI.h>
#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
#include "DHT.h"

#define Light_PIN A1 //imposto il pin a cui è connesso il sensore
#define Smoke_PIN A2 //imposto il pin a cui è connesso il sensore
#define DHT22_PIN 2 //imposto il pin a cui è connesso il sensore

#define DHTTYPE DHT22   // DHT 22  (AM2302)

#define ledLight_PIN 6
#define ledSmoke_PIN 7
#define ledTemp_PIN 8
#define ledHum_PIN 9

#define Periodo_Invio_Dati 10000 //10s = tempo minimo tra un'invio sul web e l'altro.(ms)
#define Periodo_Lettura_Sensore 2000 //2s = tempo minimo tra una lettura del sensore e l'altra (ms)

#define id_Light 2000
#define id_Smoke 3000
#define id_Temp 4000
#define id_Hum 5000

char username[] = "root";  //username per login sito
char password[] = "";      //password per login sito

WiFly wifly;

/* Change these to match your WiFi network */
const char mySSID[] = "DSG";
const char myPassword[] = "";

//const char site[] = "";

void terminal();
void print_P(const prog_char *str);
void println_P(const prog_char *str);

char serverName[] = "";//URL del server a cui connettersi
#define serverPort 80 //porta di connessione
char pageName[] = "pagina_server.php";//nome pagina php per la ricezione dei dati

SoftwareSerial wifiSerial(8,9);

int light_accum = 0;
float temp_accum = 0.0;
float smoke_accum = 0.0;
float hum_accum = 0.0;

long n_camp = 0;

float avg_light = 0.0;
float avg_smoke = 0.0;
float avg_temp = 0.0;
float avg_hum = 0.0;

unsigned long time = 0;
unsigned long SendTime = 0;
unsigned long ReadTime = 0;

void setup()
  // start the serial library:
  pinMode(ledLight_PIN, OUTPUT);
  pinMode(ledSmoke_PIN, OUTPUT);
  pinMode(ledTemp_PIN, OUTPUT);
  Serial.println("Connessione Configurata.");
  delay(1000);//aspetto un secondo per far avviare lo shield ethernet
  Serial.println("Programma Avviato, Setup Terminato!");


void loop()
    time = millis();
    if(time > SendTime + Periodo_Invio_Dati){
       SendTime = millis();
       avg_light = float(light_accum / double(n_camp));//calcolo la media delle lettura
       avg_smoke = float(smoke_accum / double(n_camp));//calcolo la media delle lettura
       avg_temp = float(temp_accum / double(n_camp));//calcolo la media delle lettura
       avg_hum = float(hum_accum / double(n_camp));
       if(n_camp > 0)
             digitalWrite(ledLight_PIN, HIGH);
             Serial.println("invio light sensor");
             Serial.println("fine invio");
             digitalWrite(ledLight_PIN, LOW);
             digitalWrite(ledSmoke_PIN, HIGH);
             Serial.println("invio smoke sensor");
             Serial.println("fine invio");
             digitalWrite(ledSmoke_PIN, LOW);
             digitalWrite(ledTemp_PIN, HIGH);
             Serial.println("invio temp sensor");
             Serial.println("fine invio");
             digitalWrite(ledTemp_PIN, LOW);
             digitalWrite(ledHum_PIN, HIGH);
             Serial.println("invio temp sensor");
             Serial.println("fine invio");
             digitalWrite(ledHum_PIN, LOW);       
          Serial.println("Nessuna Campionatura, controllare sensore");
       n_camp = 0; //azzero le variabili per iniziare nuovamente il calcolo della media
       light_accum = 0;
       smoke_accum = 0.0;
       temp_accum = 0.0;
       hum_accum = 0.0; 

    if(time > ReadTime + Periodo_Lettura_Sensore){
        ReadTime = millis();
        light_accum += readLightSensor();
        smoke_accum += readSmokeSensor();
        temp_accum += readTempSensor();       
        hum_accum += readHumiditySensor();
        Serial.print("Campione : ");
        Serial.print(" - ");

float readSmokeSensor(){
    int val = analogRead(Smoke_PIN);     // read the value from the analog sensor
    float Ro = 12000.0;    // this has to be tuned 12K Ohm
    float Vrl = val * ( 5.00 / 1024.0  );      // V
    float Rs = 20000 * ( 5.00 - Vrl) / Vrl ;   // Ohm
    float ratio =  Rs/Ro;   
    float ppm = 0.0;
    ppm = 37143 * pow (ratio, -3.178);
    return ppm;   

int readLightSensor(){
   int luce = analogRead(Light_PIN); // Inserisco il valore della lettura dell'input analogico sull'intero ValoreSensore
   luce = (5.0* luce * 200.0) / 1024.0;   
   return luce;

float readTempSensor(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float t = dht.readTemperature();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t)) {
    Serial.println("Failed to read from DHT");
  } else {
    return t;

float readHumiditySensor(){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();

  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(h)) {
    Serial.println("Failed to read from DHT");
  } else {
    return h;

void configWIFI(){
    print_P(PSTR("Free memory: "));

    if (!wifly.begin(&wifiSerial, &Serial)) {
        println_P(PSTR("Failed to start wifly"));

    char buf[32];
    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
/* Setup the WiFly to connect to a wifi network */
println_P(PSTR("Joining network"));

if (wifly.join()) {
    println_P(PSTR("Joined wifi network"));
} else {
    println_P(PSTR("Failed to join wifi network"));
    } else {
        println_P(PSTR("Already joined network"));

    print_P(PSTR("MAC: "));
    Serial.println(wifly.getMAC(buf, sizeof(buf)));
    print_P(PSTR("IP: "));
    Serial.println(wifly.getIP(buf, sizeof(buf)));
    print_P(PSTR("Netmask: "));
    Serial.println(wifly.getNetmask(buf, sizeof(buf)));
    print_P(PSTR("Gateway: "));
    Serial.println(wifly.getGateway(buf, sizeof(buf)));
    print_P(PSTR("SSID: "));
    Serial.println(wifly.getSSID(buf, sizeof(buf)));

    print_P(PSTR("DeviceID: "));
    Serial.println(wifly.getDeviceID(buf, sizeof(buf)));

    if (wifly.isConnected()) {
        println_P(PSTR("Old connection active. Closing"));
    if (wifly.open(serverName, 80)) {
        print_P(PSTR("Connected to "));

Serial.println("WIFI ALREADY");
    } else {
        println_P(PSTR("Failed to connect"));

/* Print a string from program memory */
void print_P(const prog_char *str)
    char ch;
    while ((ch=pgm_read_byte(str++)) != 0) {

void println_P(const prog_char *str)

/* Connect the WiFly serial to the serial monitor. */
void terminal()
    while (1) {
if (wifly.available() > 0) {

if (Serial.available() > 0) {

void InvioWIFIHttp(char server[], int porta, char pagina[], char username[], char password[], int idSensore, float dato)
  /* Send the request */
  //wifly.println("GET /arduino/pagina_server.php?username=&password=&sensore=5&dato=5 HTTP/1.0");
  wifly.print("GET /arduino/");
  wifly.println(" HTTP/1.1");
  wifly.print("Host: ");

the first data is sent ...

After the device does not send anything more ..

seems that the connection is closed .. how can I do?


Jul 14, 2012, 08:34 am Last Edit: Jul 14, 2012, 08:39 am by gilboash Reason: 1

  • Starting
    Free memory: 1065
    Joining network
    Failed to join wifi network

I always get this using the rn-xv (rev3) with Arduino and trying to start the "httpclient".
I'm using the software serial. Reseted the board. Tried storing the auth data on the chip itself. Nothing works.

Any idea what's going wrong?

eszespeter - did u try first connecting to your wifi network manually (writing to wifly directly using Ad-Hoc or serial)?
If it is connected this way, try to avoid the setup re-connection and see if it's working


dhunt - Thanks alot for this library, I was looking for a reliable TCP client solution to communicate with python TCP server app on my home PC and it works great also with low memory consumption.
From some reason i had to switch the #include <XX>, with #include "XX"


Go Up