WiFlyHQ: A new library for the WiFly RN-XV

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 :slight_smile:

  • 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

#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)
DHT dht(DHT22_PIN, DHTTYPE);

#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

/*WIFI SETTING*/
WiFly wifly;

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

//const char site[] = "192.168.1.2";

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

char serverName[] = "192.168.1.200";//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:
  Serial.begin(9600);
  
  configWIFI();
  
  pinMode(ledLight_PIN, OUTPUT);
  pinMode(ledSmoke_PIN, OUTPUT);
  pinMode(ledTemp_PIN, OUTPUT);
  pinMode(ledHum_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)
       {
         Serial.println("connessione...");
                 
             digitalWrite(ledLight_PIN, HIGH);
             Serial.println("invio light sensor");
             InvioWIFIHttp(serverName,serverPort,pageName,username,password,id_Light,avg_light);
             Serial.println("fine invio");
             delay(100);
             digitalWrite(ledLight_PIN, LOW);
                         
             digitalWrite(ledSmoke_PIN, HIGH);
             Serial.println("invio smoke sensor");
             InvioWIFIHttp(serverName,serverPort,pageName,username,password,id_Smoke,avg_smoke);
             Serial.println("fine invio");
             delay(100);
             digitalWrite(ledSmoke_PIN, LOW);
             
             digitalWrite(ledTemp_PIN, HIGH);
             Serial.println("invio temp sensor");
             InvioWIFIHttp(serverName,serverPort,pageName,username,password,id_Temp,avg_temp);             
             Serial.println("fine invio");
             delay(100);
             digitalWrite(ledTemp_PIN, LOW); 
     
             digitalWrite(ledHum_PIN, HIGH);
             Serial.println("invio temp sensor");
             InvioWIFIHttp(serverName,serverPort,pageName,username,password,id_Hum,avg_hum);             
             Serial.println("fine invio");
             delay(100);
             digitalWrite(ledHum_PIN, LOW);        
         
        }else
          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;  
       Serial.flush();    
       wifly.flushRx(1);       
    }


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

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(){
    println_P(PSTR("Starting"));
    print_P(PSTR("Free memory: "));
    Serial.println(wifly.getFreeMemory(),DEC);

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

    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"));
	wifly.setSSID(mySSID);
	//wifly.setPassphrase(myPassword);
	wifly.enableDHCP();

	if (wifly.join()) {
	    println_P(PSTR("Joined wifi network"));
	} else {
	    println_P(PSTR("Failed to join wifi network"));
	    terminal();
	}
    } 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)));

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

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

	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) {
	Serial.write(ch);
    }
}

void println_P(const prog_char *str)
{
    print_P(str);
    Serial.println();
}

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


	if (Serial.available() > 0) {
	    wifly.write(Serial.read());
	}
    }
}

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.println();
  
  wifly.print("GET /arduino/");
  wifly.print(pagina);
  wifly.print("?username=");
  wifly.print(username);
  wifly.print("&password=");
  wifly.print(password);
  wifly.print("&sensore=");
  wifly.print("5");
  //wifly.print(idSensore);
  wifly.print("&dato=");
  wifly.print("12");
  //wifly.print(dato);
  wifly.println(" HTTP/1.1");
  wifly.print("Host: ");
  wifly.println(server);
  wifly.println();  
}

the first data is sent ...

After the device does not send anything more ..

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

eszespeter:

  • 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 , with #include "XX"

TX!!!

Hello everyone,

I'm a big fan of the WiflyHQ library and I use it with softwareserial in order to communicate with the RN-XV and the hardware serial is used for debuging purpose. However, when I want to use the hardware serial to communicate with the RN-XV (the DFRobot XBee shield only allows serial pins to be 0 and 1 on the arduino), I have the feeling that the µC can't get to talk with the RN-XV, as the sketch is unable to pass the enterCommandMode() function.

Here is my sketch used with softwareserial and hardware serial (this one is working) :

#include <Arduino.h>
#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(11,12);

#include <WiFlyHQ.h>

WiFly wifly;

void setup()
{
    Serial.begin(9600);
    while (!Serial) ;

    wifiSerial.begin(9600);   
    while(!wifly.begin(&wifiSerial, &Serial)){
      tone(8,880, 1000);
      delay(1000);
    }
    Serial.println("Done");
    tone(8,440, 200);
}
void loop()
{
    
}

and the sketch using only hardware serial :

#include <Arduino.h>
#include <WiFlyHQ.h>

WiFly wifly;

void setup()
{
    Serial.begin(9600);

    while(!wifly.begin(&Serial, NULL)){
      tone(8,880, 1000);
      delay(1000);
    }
    tone(8,440, 200);
}
void loop()
{
    
}

As you may have noticed, this is a very simple sketch, but even it doesn't work. Of course, debugging it is much more difficult as I don't have any serial to USB converter in order to use a softwareserial as debugging channel and my only debugging helps are a buzzer or LEDs. I don't think that the baudrate could be a cause as the software serial uses the same as the hardware one. So, if anyone has had the same issue, could you tell me what have you done to make it work or what is wrong in my code?

agoo:
Hello everyone,

I'm a big fan of the WiflyHQ library and I use it with softwareserial in order to communicate with the RN-XV and the hardware serial is used for debuging purpose. However, when I want to use the hardware serial to communicate with the RN-XV (the DFRobot XBee shield only allows serial pins to be 0 and 1 on the arduino), I have the feeling that the µC can't get to talk with the RN-XV, as the sketch is unable to pass the enterCommandMode() function.

Here is my sketch used with softwareserial and hardware serial (this one is working) :

#include <Arduino.h>

#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(11,12);

#include <WiFlyHQ.h>

WiFly wifly;

void setup()
{
    Serial.begin(9600);
    while (!Serial) ;

wifiSerial.begin(9600);   
    while(!wifly.begin(&wifiSerial, &Serial)){
      tone(8,880, 1000);
      delay(1000);
    }
    Serial.println("Done");
    tone(8,440, 200);
}
void loop()
{
   
}




and the sketch using only hardware serial :



#include <Arduino.h>
#include <WiFlyHQ.h>

WiFly wifly;

void setup()
{
    Serial.begin(9600);

while(!wifly.begin(&Serial, NULL)){
      tone(8,880, 1000);
      delay(1000);
    }
    tone(8,440, 200);
}
void loop()
{
   
}




As you may have noticed, this is a very simple sketch, but even it doesn't work. Of course, debugging it is much more difficult as I don't have any serial to USB converter in order to use a softwareserial as debugging channel and my only debugging helps are a buzzer or LEDs. I don't think that the baudrate could be a cause as the software serial uses the same as the hardware one. So, if anyone has had the same issue, could you tell me what have you done to make it work or what is wrong in my code?

My bad, I did not specified that I am using the Leonardo board. And this is the solution to my problem, because according to the ArduinoLeonardoHardware page :

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Leonardo, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

This is awesome, first because it works now and that means that I can still have a debugging terminal while the RN-XV is connected to the 0 and 1 pins of the arduino board!

The updated sketch for reference :

#include <Arduino.h>
#include <WiFlyHQ.h>

WiFly wifly;

void setup()
{
    Serial.begin(9600);
    Serial1.begin(9600);
    while (!Serial) ;

    while(!wifly.begin(&Serial1, &Serial)){
      tone(8,880, 1000);
      delay(1000);
    }
    Serial.println("Done");
    tone(8,440, 200);
}
void loop()
{
    
}

I have set up an RN-GSX (RN-131G) with WiflyHQ and it seems to run quite nicely, thanks for making this library available!

Two quick questions, and a sorry if i missed this in the discussions:

  • How can i set up a fixed ip address and net mask? DHCP works nicely but i need to make sure the board won't change it's address and it would be nice if it does not need to query a DHCP server in my environment.
  • Is it possible to raise the baud rate to say 19200 or 38400 to speed up things a bit? Board is an Arduino Pro Mini 3.3V/8MHz

Thanks,
Adrian

Hi,

I have an arduino uno board and a wifly shield RN-131G. Can I also use this library?
Everytime I want to launch the project i got the following output in the serial monitor:

Starting
Free memory: 1019
setPrompt failed
Failed to enter command mode
Failed to start wifly

Can somebody help me?

Cheers

gerby:
Hi,

I have an arduino uno board and a wifly shield RN-131G. Can I also use this library?
Everytime I want to launch the project i got the following output in the serial monitor:

Starting

Free memory: 1019
setPrompt failed
Failed to enter command mode
Failed to start wifly




Can somebody help me?

Cheers

I would really like an answer to this question as well - I have the exact same issue when I try to run this board.

I had this issue and it was because I had the wires going to the wrong pins.

I was using the wireless sd shield and I had to clip the pins on the shield for 1 and 2 and then run jumpers from pins 6 and 7 which I was using for software serial to pins 1 and 2 which the shield had wired to the wifly

Wonderful library! Can you add RTS hardware flow control? Has anyone sent data fast?

epinc:
I had this issue and it was because I had the wires going to the wrong pins.

I was using the wireless sd shield and I had to clip the pins on the shield for 1 and 2 and then run jumpers from pins 6 and 7 which I was using for software serial to pins 1 and 2 which the shield had wired to the wifly

I don't understand your answer.

First ,thanks dhunt's library

I am using wireless SD Shield + RN-XV and the Arduino Uno
and try any example in library or the Ad-hoc

but there are the same problem in serial monitor
""
Starting
Free memory: 1337
setPrompt failed
Failed to enter command mode
Failed to start wifly
Terminal ready
""
There is a switch on wireless SD shield (USB or MICRO)
and I try the two modes but the same problem will happen
Did anyone use the same wireless SD shield has this problem???

You need to bend pins 1 and 2 on the shield out of the way so they don't connect to the arduino. Then you need to run jumpers from pins 1 and 2 on the shield to the pins you are using for the software serial connection. Make sure you get tx and rx in the proper order.

The reason is that the shield is wiring the wifly to the hardware serial pins on the arduino, but you are already using hardware serial for debugging.

I forget but I think the position of the switch on the shield also matters.

JaSoNKiNg:
First ,thanks dhunt's library

I am using wireless SD Shield + RN-XV and the Arduino Uno
and try any example in library or the Ad-hoc

but there are the same problem in serial monitor
""
Starting
Free memory: 1337
setPrompt failed
Failed to enter command mode
Failed to start wifly
Terminal ready
""
There is a switch on wireless SD shield (USB or MICRO)
and I try the two modes but the same problem will happen
Did anyone use the same wireless SD shield has this problem???

I will try for this
Thanks,epinc

Does anyone know how to use this library with TELNET smtp?

I am trying to send emails to servers such as gmail.

Hi,
I have already tried several of WiFi modules as WebServer. Look at thread "WEB-Server with new ARDUINO wifi shield losing connection".
I have the problem that the connection to the router is lost at some point and thus the web server is no longer accessible. TThis also the case with the reboot of the router. Is there a possibility in your library for a connection test? Which xbee works with WiFly RN-XV. I have a SD card shield can also connect with.

Thanks for your help :wink:

There's another post on here with my pseudocode. You can use Gmail anonymously to send to other Gmail accounts, but it will only work depending on your ISP. I have tried T-Mobile, and my IP is blocked. So I used www.smtpcorp.com instead.

@pl3 - It's easy to test for a wifi connection. First initialize with:
set wlan linkmon 5
then
show net

If you want to be sure you are connected to the internet use:
ping i