WiFly "GET" using the WiFly library

Hi -

I am using the WiFly-Arduino shield, with code linked to the new WiFly library (http://sparkfun.com/Code/wifly/WiFly-20 … 023939.zip).

Things work well - I can enter command mode, and configure the device. I can telnet into the device remotely. But, I am having trouble processing GET requests. I made a page that provides the epoch time within square brackets, with the goal of being able to timestamp data collected by the arduino. In setup(), I would want to

a) open a connection,

b) receive the serial data from the remote server,

c) process the serial character stream to extract the time string,

d) then drop the connection.

I can do a and b, but am not getting the expected results from parsing the string, and then the program hangs at the end of the GET request, without dropping back into command mode (as I have it coded).

Program looks like this:

#include "WiFly.h"

int epochTimeAtInit = 0;

void setup() {

  Serial.begin(9600);
  Serial.println("WiFly Shield Terminal Routine");
  WiFly.begin();
  SpiSerial.print("join Stanford\r\n");
  delay(3000);

  SpiSerial.print("open econet.stanford.edu 80\r\n");
  delay(100);  
  SpiSerial.print("GET http://econet.stanford.edu/getEpochTime.php HTTP/1.1\r\n");
  delay(100);
  SpiSerial.print("HOST: econet.stanford.edu\r\n\r\n");
  delay(100);
  SpiSerial.print("close\r\n");
  delay(100);
  SpiSerial.print("exit\r\n");
  delay(100);
  //SpiSerial.print("$$\r\n");
  
  char * epochTimeString;
  char testChar;
  boolean junk = true;
  int iTimeString = 0;
  while(SpiSerial.available()) {
    testChar = SpiSerial.read();
    if (testChar == '[') { junk = false;}
    if (testChar == ']') { junk = true;}
    if (junk == false) { epochTimeString += testChar; }
  }
  
  epochTimeAtInit = (int)epochTimeString;
  Serial.print("Epoch Time = "); Serial.println(epochTimeString);

}

Serial output (from screen command) looks like this. The value in the brackets is the epoch time, so that’s good. Notice that the “Epoch Time =” part prints to the Serial port before the WiFly connection stuff, even though it appears afterwards in code. The cursor hangs after that last character and no typing prints to the screen. WiFly seems not to respond to command mode requests. I’m not sure how to proceed. Ideas? Anyone else trying to deal with GET requests? - Adam

WiFly Shield Terminal Routine

Epoch Time = `g

join Stanford

Auto-Assoc Stanford chan=11 mode=OPEN SCAN OK

Joining Stanford now…

<2.18>

Associated!

DHCP: Start

DHCP in 659ms, lease=2520s

IF=UP

DHCP=ON

IP=10.33.155.106:80

NM=255.255.248.0

GW=10.33.152.1

Listen on 80

open econet.stanford.edu 80

Connect to 171.66.69.32:80

<2.18>

HTTP/1.1 200 OK

Date: Wed, 21 Jul 2010 20:13:21 GMT

Server: Apache/2.2.3 (Red Hat)

X-Powered-By: PHP/5.1.6

Content-Length: 12

Connection: close

Content-Type: text/html; charset=UTF-8

[1279743201]

Some improvement - I no longer hang after the last character, and I seem to be able to enter and exit command mode normally, showing loop() is operating just fine. I also tried initializing the time at the start of loop(). Nevertheless, processing the webpage for the epoch time is still an obstacle.

Hi,

A couple of things:

Is there a specific reason why you’re using the lower level SpiSerial object rather than the WiFly object to join the network and make requests? (While it’s the only way to do some things at the moment the only reason SpiSerial is currently accessible is because my C++ fu isn’t good enough to hide it where I wanted to. :slight_smile: )

From a quick reading of your code I can see at least a couple of other probable issues:

First, Arduino “strings” don’t work like this, you need to look into character arrays:

epochTimeString += testChar;

And if you want an integer at the end of the process then you need to look at converting a number from an ASCII representation to an integer.

Also, you probably need to add a “continue” in this check, otherwise you’re including the “[” character:

if (testChar == ‘[’) { junk = false;}

I suggest you write a simple sketch to test your parsing/conversion code first before adding the web retrieval part.

–Philip;