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

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.
Logged

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

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:

Code:
  // 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

Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

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

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.
Logged

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

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

Code:
#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.

Code:
<?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);
}

?>
« Last Edit: June 16, 2012, 11:48:17 am by garrettlynch » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
WiFly <<  (const char*) strRequest;

Logged

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

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:

Code:
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?
http://www.adafruit.com/products/70

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
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You mentioned about a serial adaptor for testing before.  Is it this adaptor?
http://www.adafruit.com/products/70

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.
Logged

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

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 (http://www.adafruit.com/products/284) the same thing again?

Garrett
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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:
http://hcgilje.wordpress.com/resources/arduino-terminal-and-two-serial-ports/

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

thanks in advance
Garrett
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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

Garrett
Logged

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

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:

Code:
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 (http://hcgilje.wordpress.com/resources/arduino-terminal-and-two-serial-ports/) but I don't see anything like the "/dev/tty.SparkFun-BT-COM0" port he has.  Anyway I connect to the last one:

Code:
screen /dev/tty.usbserial-AH019JWT 9600

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

Code:
#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


* photo.JPG (394.37 KB, 960x720 - viewed 18 times.)
« Last Edit: June 23, 2012, 08:50:09 am by garrettlynch » Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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