First steps with WiFly and Arduino

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.

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.

pylon:
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?

You need a serial terminal or the serial monitor of the Arduino IDE, so no problem, it works with your OS X box. The Foca is one of many available USB2Serial adapters. USB2Serial Light is one from the original Arduino manufacturers which also is a possibility but more difficult to wire. Another possibility might be to use your XBee shield and switch the serial selector to the position where the sketch transfer does not work (I think you called it Bee position). Then you should be able to communicate to the Bee directly, at least on my shield this works.

ok some progress, I abandoned doing it in the sketch and searched around for a way to talk to the WiFly from osx, this pdf: ww1.microchip.com/downloads/en/.../rn-wifly-eval-um-ver1.0r.pdf

was a god send and recommended CoolTerm. Followed the steps in the pdf to configure it for a WEP secured network and it looks like it has connected. No flashing red light and now I have a slow pulsing green light so I think that means it has an ip and is connected. I can also see the device in my network now.

Since testing seems to be quite difficult I want to make a slow looping sketching which passes a number (a counter) to a php file online which in turn will write it to a text file. Php side is easy and I think I can conceptualise the loop in arduino. I've spotted this in an example:

  // Open connection, then sent GET Request, and display response.
  if (wifi.openConnection( MY_SERVER_POST ) ) {
    
    wifi <<  (const char*) strRequest ; 
    
    // Show server response

    unsigned long TimeOut = millis() + 3000;

    while ( millis()  < TimeOut && wifi.isConnectionOpen() ) {
      if (  wifi.available() > 0 ) {
        Serial <<  (char) wifi.read();
      }
    }
    
    // Force-close connection

    wifi.closeConnection();
 
  } else {
    // Failed to open
    Serial << GetBuffer_P(IDX_WT_MSG_FAIL_OPEN,bufTemp,TEMP_BUFFER_SIZE) << MY_SERVER_POST << endl;
  }

Which I think gives me the actual GET calls. If you have advice on this last part I'd appreciate it. Once I get this working I'm going to post it all to this thread because this was far more difficult than it should be.

Garrett

This will probably not work for you as it uses the Serial for debugging:

Serial <<  (char) wifi.read();

You have to redirect somewhere else like I wrote to a SoftwareSerial or ignore the read value completely (not recommended).

Hi

Yes I know I'm not retrieving the data arduino side I want to send it and I'll check it php/txt side. So essentially I want to send a GET to:

mypage.php?count=1

And the php will write it to a file. It's the openconnection, while loop and closeconnection in that code which I think will help me.

Hi

On the final bit now I think but I'm confused on what I hope is just one last detail. I'm unsure what line is the actual GET request being made in my arduino code. I copied mine from a demo that came with WiFlySerial library that does both GET and POST requests and I can see where the request line is built - just not where it's executed.

Anyway in the demo below the loop I've made should work every five second (it was working without all the WiFly code). I've highlighted where I'm stuck inside the "if (WiFly.openConnection(MY_SERVER_GET))" section which is not working (the counter.txt file online isn't updating). What should I be doing here?

Thanks in advance
Garrett

#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Streaming.h>
#include <PString.h>
#include "WiFlySerial.h"


//various buffer sizes
#define REQUEST_BUFFER_SIZE 180
#define TEMP_BUFFER_SIZE 60

//server hosting GET example php script
#define MY_SERVER_GET "http://www.mysite.com/"
#define MY_SERVER_GET_URL "pathway/to/index.php"


//WiFly pins
#define ARDUINO_RX_PIN  2
#define ARDUINO_TX_PIN  3

WiFlySerial WiFly(ARDUINO_RX_PIN ,ARDUINO_TX_PIN);
char bufRequest[REQUEST_BUFFER_SIZE];
char bufTemp[TEMP_BUFFER_SIZE];




//loop counter
int iLoopCounter = 0;

//start time
unsigned long startTime = 0;


void setup()
{
  //set up serial
  //Serial.begin(9600);
  
  //start up WiFly
  WiFly.begin();
}


void loop()
{
  //calculate the time since last time the cycle was completed
  unsigned long loopTime = millis() - startTime;

  //if the timer is greater than or equal to 5 seconds (5000 milliseconds)
  if (loopTime >= 5000)
  {
    //trigger
    //Serial.println("Loop");
    
    char bufRequest[REQUEST_BUFFER_SIZE];
    PString strRequest(bufRequest, REQUEST_BUFFER_SIZE);
      
    // Build GET expression
    strRequest << F("GET ") << MY_SERVER_GET_URL << F("?count=") << iLoopCounter 
    << F(" HTTP/1.1") << "\n"
    << F("Host: ") << MY_SERVER_GET << "\n"
    << F("Connection: close") << "\n"
    << "\n\n";
    // send data via request
    // close connection
  
    //open connection, then send GET Request, and display response.
    if (WiFly.openConnection(MY_SERVER_GET))
    {
      
      
      //***
      //I'm confused about this bit, what's actually doing the GET request here?
      //I don't think I need the wjile at the moment - that's testing the request has happened right?

      WiFly <<  (const char*) strRequest; 
    
      // Show server response

      //how long to wait before timing out on the GET request
      unsigned long TimeOut = millis() + 3000;

      while (millis()  < TimeOut && WiFly.isConnectionOpen())
      {
        if (WiFly.available() > 0)
        {
            WiFly.read();
        }
      }

      //I'm confused about this bit, what's actually doing the GET request here?
      //***
      
      

      //force-close connection
      WiFly.closeConnection();
    }
    else
    {
      //failed to open
    }
  
    //restart timer
    startTime = millis();
  }

  //increment the loop counter
  iLoopCounter++;

  //to prevent spamming serial monitor too much
  delay(250);
}

This is the php counter if you need to see it, this works fine.

<?php

//error_reporting(E_ALL ^ E_NOTICE);

//if their is a GET request
if ($_SERVER['REQUEST_METHOD'] == 'GET') 
{
	//for debugging
	//print_r($_GET);

	//if count number in url
	if ($_REQUEST['count'])
	{
		//get count number in url
		$count = $_REQUEST['count'];
	}
	else
	{
		//error no count number in url
		$error = "No count passed in url.";
	}

	//name of the counter database file
	$logfile = "counter.txt";

	//open the counter file
	$handle = fopen($logfile, "r+") or die("Can't write to log file! Please Change the file permissions (CHMOD to 666 on UNIX machines!)");

	//write the passed count to the file
	fwrite($handle, $count);

	//close file
	fclose($handle);
}

?>

I guess you know you have to change the MY_SERVER_GET and MY_SERVER_GET_URL defines to match your server's setup? This is what's being inserted into your request and if the values in it are not correct you won't reach your server.

Your request is executed (if this is possible to say, the request line is sent over the existing connection) in this line:

WiFly <<  (const char*) strRequest;

pylon:
I guess you know you have to change the MY_SERVER_GET and MY_SERVER_GET_URL defines to match your server's setup? This is what's being inserted into your request and if the values in it are not correct you won't reach your server.

Your request is executed (if this is possible to say, the request line is sent over the existing connection) in this line:

WiFly <<  (const char*) strRequest;

Hi

Yes, I just put false details for the forum. Ah ok that is that line that's sending the GET request. Mmm, not sure what's happening then as the php script doesn't execute. It works fine when I do it via a browser.

You mentioned about a serial adaptor for testing before. Is it this adaptor?

I think I need to get one of these as its impossible to test what's going on, its perhaps something small like a typo in the request URL.

Thanks.
Garrett

You mentioned about a serial adaptor for testing before. Is it this adaptor?
FTDI Serial TTL-232 USB Cable : ID 70 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Almost, but this is the 3V version. I'd go for the 5V version as the standard Arduino runs it's pins on 5V. Usually 60% of the max level should be enough to trigger as HIGH but 3V is exactly on the edge then which is something I would avoid.

I often use the original Arduino USB2Serial LIght (http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=11&products_id=143) or the Foca (http://shop.boxtec.ch/product_info.php/products_id/40332) which has the possibility to switch between 3V3 and 5V for the signal lines and includes a XBee socket to access any Bee directly from the PC.

pylon:
Almost, but this is the 3V version. I'd go for the 5V version as the standard Arduino runs it's pins on 5V. Usually 60% of the max level should be enough to trigger as HIGH but 3V is exactly on the edge then which is something I would avoid.

I often use the original Arduino USB2Serial LIght (http://store.arduino.cc/ww/index.php?main_page=product_info&cPath=11&products_id=143) or the Foca (http://shop.boxtec.ch/product_info.php/products_id/40332)

ah right I'll see if I can find a 5v version. I can't find an image of how the Arduino USB2Serial LIght is to be wired up to the Arduino so that's making me hesitant about that - I have found images of the USB FTDI TTL-232 cable - TTL-232R cable connected to Arduino. Is the FTDI friend (FTDI Friend + extras [v1.0] : ID 284 : $14.75 : Adafruit Industries, Unique & fun DIY electronics and kits) the same thing again?

Garrett

The FTDI friend is almost the same as the Foca but without the XBee interface. If you just need a USB2Serial adapter, it's a good way to go.

The connection usually is quite easy, in your case you just need GND, TX and RX.

Hi

Sorry to ask again but I want to be sure before I purchase and I've discovered there seem to be an enormous variety of usb/ttl adaptors.

I've found a 5V adaptor here:
http://robosavvy.com/store/product_info.php/manufacturers_id/33/products_id/986
which is cheaper than all the usb to ttl cables I've seen (I have a usb male to female extension already). It looks the same to me but the (CP210) number is not one I'm seeing anywhere else. Maybe its just the brand id number?

and I've found a tutorial here:

which is nice and clear - even though the adaptor is different, same principle right?

thanks in advance
Garrett

The adapter you linked to is using another chip doing the conversion between serial and USB. In the Arduino world the FTDI chip is very common for that and that's why almost everybody doing development with Arduinos has installed the necessary drivers already. This adapter uses the CP210 chip which is not that common and you probably have to install drivers (for Windows or Macintosh, current Linux distros include the driver already). Because it's not used as broad as the FTDI chip, future availability of drivers for Windows or Macintosh are not assured but apart from that it's a viable and good choice, I think.

Ah ok I should probably stick to one of the others then. Thanks again.

Garrett

Hi

My usbserial cable has arrived and I'm not having much luck getting software serial to work. At the moment I've disconnected the RN-XV and xBee Shield to keep things simple to just test how software serial works. I've installed the VCP driver (http://www.ftdichip.com/Drivers/VCP.htm), I guess this is the one I need as opposed to the D2XX driver? I've restarted and I'm using osx terminal to monitor the software serial port, so I ls /dev/tty.* like so:

garrett-macbook:~ garrett$ ls /dev/tty.*
/dev/tty.Bluetooth-File-Exchange	/dev/tty.Bluetooth-PDA-Sync
/dev/tty.Bluetooth-File-Transfer	/dev/tty.usbserial-A6008dRD
/dev/tty.Bluetooth-Modem		/dev/tty.usbserial-AH019JWT

I'm following the instructions here (arduino, terminal and two serial ports | Conversations with spaces) but I don't see anything like the "/dev/tty.SparkFun-BT-COM0" port he has. Anyway I connect to the last one:

screen /dev/tty.usbserial-AH019JWT 9600

This is the code I'm testing which I upload to the arduino via hardware serial:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

void setup()  
{
  Serial.begin(57600);
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}

In Arduinos Serial Monitor (set to 57600 baud), the serial lines execute but the software serial lines are never shown in terminal. I've attached a photo of the usbserial cable wired up just in case there is anything wrong there but I've used the same RX,TX in the script.

Any suggestions as to what I'm doing wrong, perhaps I'm missing another driver or something small like that, would be greatly appreciated.

Thanks in advance.
Garrett

I guess the connections of your USB2Serial are equivalent to my Arduino USB2Serial Light. If this is so, you have to swap the RX/TX lines, at least that's my interpretation of your picture. My wiring is:

Arduino -> USB2Serial
GND -> 1 (GND)
2 -> 4 (TX)
3 -> 5 (RX)

I don't have a Mac, so I don't know how the USB adapter show up there. I have /dev/ttyACM0 and /dev/ttyACM1. You probably can test which one it is by disconnecting the cable and see which device disappears (at least on my Linux box this is the easiest way).

I tried your test code and it works like a charm. Immediate response (when I don't use the Arduino Serial Monitor but a serial terminal program) in both ways.

Hi

Yes that worked perfectly, thanks very much, I read the diagram I was looking at wrong (2313ConnectSerial | Photos and illustrations for a tutorial … | Flickr).

So I'm testing with software serial in my WiFly code now and it seems that the begin() command being sent to the WiFly is where everything stops. I've stripped it down to keep it simple (i.e. just starting up the wifi) and in the code below it never gets as far as the loop(). Physically all the connections on the arduino to the shield and RN-XV look good, the switch is on xbee and I have no problems configuring the RN-XV from CoolTerm on my computer so I think this elimintates any issues with connections. Maybe I'm forgetting to include a key library that's needed or could there be something wrong wih the WiFlySerial.h library?

Thanks in advance
Garrett

#include <Arduino.h>
#include <Time.h>
#include <SoftwareSerial.h>
#include <Streaming.h>
#include <PString.h>
#include <WiFlySerial.h>


//various buffer sizes
#define REQUEST_BUFFER_SIZE 180
#define TEMP_BUFFER_SIZE 60

//server hosting GET example php script
#define MY_SERVER_GET "http://www.asquare.org/"
#define MY_SERVER_GET_URL "in-progress/arduino-test/counter/index.php"


//WiFly pins
#define ARDUINO_RX_PIN  2
#define ARDUINO_TX_PIN  3

WiFlySerial wifi(ARDUINO_RX_PIN, ARDUINO_TX_PIN);
char bufRequest[REQUEST_BUFFER_SIZE];
char bufTemp[TEMP_BUFFER_SIZE];


//sotware serial pins for testing
SoftwareSerial mySerial(4, 5);



//loop counter
int iLoopCounter = 0;

//start time
unsigned long startTime = 0;


void setup()
{
  //set up serial
  //Serial.begin(9600);
  
  //set the data rate for the SoftwareSerial port and send a message to test
  mySerial.begin(9600);
  mySerial.println("Software serial working");
  
  //start up WiFly
  wifi.begin()
  
  //I tried testing with this in the hope it would send back an error code of some sort but nothing
  //mySerial.write(wifi.begin());
  
}


void loop()
{
  //to test
  mySerial.println("test");

  //to prevent spamming serial monitor too much
  delay(250);
}