Remote controlling arduino with Wifly shield over browser

Hi everyone,
I recently got my arduino and a wifi shield based on the Wifly RN-171 wifi module. Now for testing purpose i was trying to turn a led on and off by typing ipadress/on and ipadress/off in my browser. However i wasnt able to succeed in actually controlling that led. There is a WiflySerial Library i was using for programming. Also my sketch is based on the example sketch that comes with this library aswell as a code from the sparkfun forums: RN-XV + Arduino Uno = slow closing connection? - SparkFun Electronics.

#include "DHT.h"
#include <SoftwareSerial.h>
#include <Streaming.h>
#include <WiFlySerial.h>

#define RX 2
#define TX 3
#define BUFFER_SIZE 80
#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);

WiFlySerial wifi(RX, TX);
char buffer[BUFFER_SIZE];
unsigned long t1;
unsigned long t2;
char error1[40]  = "Failed to read from DHT"; 
int e1 = 0;
int led = 13;
boolean Reconnect() {
  wifi.setDHCPMode(WIFLY_DHCP_CACHE );
  wifi.SendCommand("set u m 0x1", ">",buffer, BUFFER_SIZE);
  wifi.SendCommand("set comm remote 0", ">",buffer, BUFFER_SIZE);
  wifi.SendCommand("set comm match 0x9", ">",buffer, BUFFER_SIZE);
  wifi.leave() << endl;
  // join
  wifi.setPassphrase("-myPassphrase-");    
  wifi.join("--mySSID");
wifi.exitCommandMode();
}

void setup()
{
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
  Serial.begin(9600);
  wifi.begin();
  
 Serial << F("Starting...")<< endl;
   wifi.setAuthMode(WIFLY_AUTH_WPA2_PSK);
  wifi.setJoinMode(WIFLY_JOIN_AUTO);
  wifi.setDHCPMode(WIFLY_DHCP_ON);
  Reconnect();
  delay(1000);
   wifi.getDeviceStatus();
    while (! wifi.isAssociated() ) 
  {
    Reconnect();
  } 
  
  Serial << F("NETWORK") << endl
    << F("---MAC: ") << wifi.getMAC(buffer, BUFFER_SIZE) << endl
    << F("---IP: ") << wifi.getIP(buffer, BUFFER_SIZE) << endl
    << F("---Netmask: ") << wifi.getNetMask(buffer, BUFFER_SIZE) << endl
    << F("---Gateway: ") << wifi.getGateway(buffer, BUFFER_SIZE) << endl
    << F("---DNS: ") << wifi.getDNS(buffer, BUFFER_SIZE) << endl
    << F("---RSSI: ") << wifi.getRSSI(buffer, BUFFER_SIZE) << endl
    << F("---battery: ") <<  wifi.getBattery(buffer, BUFFER_SIZE) << endl;
  Serial << "Ready." << endl << endl;
}

void loop()
{
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    
    if (isnan(t) || isnan(h)) 
  {
    Serial.println(error1); endl;
    e1=0;
  }
  
  else
  {
    Serial << "Temperature: "<<t<<" "<<"Grad C " <<"Humidity: "<<h<<"%";
    e1=1;
  }
    wifi.getDeviceStatus();
    while (! wifi.isAssociated() ) 
  {
    Reconnect();
  } 
 
  if(wifi.serveConnection())
  {
    t1 = millis();
     Serial << t1 << ":" << "Connected" << endl;
  
    wifi.ScanForPattern(buffer, BUFFER_SIZE, " HTTP/1.1", 250);
    delay(100);
  if (strstr(buffer,"/on"))
   {
     digitalWrite(led,HIGH); 
   }
     else if (strstr(buffer,"/off"))
   {
     digitalWrite(led,LOW);  
   }
 
 }
    Serial << "GET request,  bytes: " << strlen(buffer) << " request: " << buffer << endl;
    if (e1=1)
    {
      wifi << "HTTP/1.1 200 OK \r Content-Type: text/html;charset=UTF-8\r Connection: close \r\n\r\n \r" <<"Server running for:" <<millis()/1000<< " seconds" <<"\r"<< "Humidity: "<< h <<" "<< "%"<< "\r"<<"Temperature: "<< t<<" "<<"Grad Celsius"<< "\r\n\r\n"<<"\t";
    }
    else 
    {
    wifi << "HTTP/1.1 200 OK \r Content-Type: text/html;charset=UTF-8\r Connection: close \r\n\r\n \r" << millis() <<"\r"<< error1<< "\r\n\r\n" << "\t";
    }
    wifi.closeConnection();
    t2 = millis();
  Serial << t2 << ":" << "Connection Closed, Duration: " << (t2 -t1) / 1000 << " seconds" << endl;

}

Library : http://sourceforge.net/projects/arduinowifly/files/
So basically how i thought it would work is , the wifi shield would listen for incomming Client requests then store that request in the buffer array as a string and if the buffer array contains a certain string for example “/on” it would turn on my led. However that dont seem to be the case so if anyone could tell me what are my mistakes i would be really thankful.

There is a WiflySerial Library i was using for programming.

Which one?

However that dont seem to be the case so if anyone could tell me what are my mistakes i would be really thankful.

Well, your biggest one was not posting all of your code. Without it, we can only guess at what things like wifi.ScanForPattern() are doing.

Your second one was not posting any output. It's hard to know where the code or logic is faulty without seeing what you are seeing.

I don't have experience with your exact WiFi module but my RN-XV does not work with the SoftwareSerial but works flawlessly with the hardware serial. I switched and now I'm doing the debugging over SoftwareSerial and draw the WiFi module from the hardware serial, just needed a USB2Serial adapter to connect to the computer. My RN-XV seemed to be very picky about the timing on the serial interface so the little time drifts inherent to the SoftwareSerial connections resulted in almost every fifth character transmitted faulty. Maybe you give that a try?

i ve edited my first post showing my complete sketch now and put a link for the library. pylon i indeed have been encounting wrong characters being transmitted pretty often. so you think strstr function doesnt find my my "/on"/ "/off string" due to bad character transmition? thx for replies

What output do you get on the debugging serial? Is your sketch failing always at the same line?

To ensure the interrupt for the hardware serial output isn't disrupting the SoftwareSerial while sending or receiving characters, put a Serial.flush() after every output to the Serial object.

hey,
after trying several times i can say that characters are being transmitted faulty pretty much randomly.
I tried using using Serial.flush() to fix this issue and i think it does help a bit though sometimes i still get wierd chars. Here is the output of the serial monitor on a try where i actually managed to turn the led on. This was however just an exception normally it doesnt work.

Starting…
NETWORK
—MAC: 00:06:66:72:29:92
—IP: 192.168.178.23:80
—Netmask: 255.255.255.0
—Gateway: 192.168.178?$(È?ÚÚ5Õ?0.0.0.0:2000
—DNS: 19"$?æ,³3g?1
—RSSI: (-73) dBm
—battery: 3072
Ready.

Temperature: 26.00 Grad C Humidity: 46.00%49552:Connected
GET request, bytes: 1 request: þ
60098:Connection Closed, Duration: 10 seconds
LED ON.
Read failFailed to read from DHT

which is from this code:

#include "DHT.h"
#include <SoftwareSerial.h>
#include <Streaming.h>
#include <WiFlySerial.h>

#define RX 2
#define TX 3
#define BUFFER_SIZE 80
#define DHTTYPE DHT11
#define DHTPIN 2
DHT dht(DHTPIN, DHTTYPE);

WiFlySerial wifi(RX, TX);
char buffer[BUFFER_SIZE];
unsigned long t1;
unsigned long t2;
char error1[40]  = "Failed to read from DHT"; 
int e1 = 0;
int led = 13;
boolean Reconnect() {
  wifi.setDHCPMode(WIFLY_DHCP_CACHE );
  wifi.SendCommand("set u m 0x1", ">",buffer, BUFFER_SIZE);
  wifi.SendCommand("set comm remote 0", ">",buffer, BUFFER_SIZE);
  wifi.SendCommand("set comm match 0x9", ">",buffer, BUFFER_SIZE);
  wifi.leave() << endl;
  // join
  wifi.setPassphrase("-myPassphrase-");    
  wifi.join("-mySSID-");
wifi.exitCommandMode();
}

void setup()
{
  pinMode(led,OUTPUT);
  digitalWrite(led,LOW);
  Serial.begin(9600);
  wifi.begin();
  
 Serial << F("Starting...")<< endl;
 Serial.flush();
   wifi.setAuthMode(WIFLY_AUTH_WPA2_PSK);
  wifi.setJoinMode(WIFLY_JOIN_AUTO);
  wifi.setDHCPMode(WIFLY_DHCP_ON);
  Reconnect();
  delay(1000);
   wifi.getDeviceStatus();
    while (! wifi.isAssociated() ) 
  {
    Reconnect();
  } 
  
  Serial << F("NETWORK") << endl;
  Serial  << F("---MAC: ") << wifi.getMAC(buffer, BUFFER_SIZE) << endl;
     Serial.flush(); 
  Serial  << F("---IP: ") << wifi.getIP(buffer, BUFFER_SIZE) << endl;
     Serial.flush(); 
  Serial  << F("---Netmask: ") << wifi.getNetMask(buffer, BUFFER_SIZE) << endl;
     Serial.flush(); 
  Serial  << F("---Gateway: ") << wifi.getGateway(buffer, BUFFER_SIZE) << endl;
     Serial.flush(); 
  Serial  << F("---DNS: ") << wifi.getDNS(buffer, BUFFER_SIZE) << endl;
     Serial.flush(); 
  Serial  << F("---RSSI: ") << wifi.getRSSI(buffer, BUFFER_SIZE) << endl;
     Serial.flush(); 
 Serial   << F("---battery: ") <<  wifi.getBattery(buffer, BUFFER_SIZE) << endl;
 Serial.flush();
  Serial << "Ready." << endl << endl;
  Serial.flush();
}

void loop()
{
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    
    if (isnan(t) || isnan(h)) 
  {
    Serial.println(error1); endl;
    Serial.flush();
    e1=0;
  }
  
  else
  {
    Serial << "Temperature: "<<t<<" "<<"Grad C " <<"Humidity: "<<h<<"%"<<endl;
    Serial.flush();
    e1=1;
  }
    wifi.getDeviceStatus();
    while (! wifi.isAssociated() ) 
  {
    Reconnect();
  } 
 
  if(wifi.serveConnection())
  {
    t1 = millis();
     Serial << t1 << ":" << "Connected" << endl;
     Serial.flush();
  
    wifi.ScanForPattern(buffer, BUFFER_SIZE, " HTTP/1.1", 250);
    delay(100);
    Serial.flush();

 }
    Serial << "GET request,  bytes: " << strlen(buffer) << " request: " << buffer << endl;
    Serial.flush();
    if (e1=1)
    {
      wifi << "HTTP/1.1 200 OK \r Content-Type: text/html;charset=UTF-8\r Connection: close \r\n\r\n \r" ;
      Serial.flush(); 
    wifi  <<"Server running for:" <<millis()/1000<< " seconds" <<"\r";
     Serial.flush(); 
    wifi  << "Humidity: "<< h <<" "<< "%"<< "\r";
      Serial.flush(); 
    wifi  <<"Temperature: "<< t<<" "<<"degrees Celsius"<< "\r\n\r\n"<<"\t";
   Serial.flush(); 
  }
    else 
    {
    wifi << "HTTP/1.1 200 OK \r Content-Type: text/html;charset=UTF-8\r Connection: close \r\n\r\n \r" << millis() <<"\r"
      << error1<< "\r\n\r\n" << "\t";
    Serial.flush();
  }
    wifi.closeConnection();
    t2 = millis();
  Serial << t2 << ":" << "Connection Closed, Duration: " << (t2 -t1) / 1000 << " seconds" << endl;
Serial.flush();
if (wifi.serveConnection())
{
wifi.ScanForPattern(buffer,BUFFER_SIZE,"t");
Serial.flush();

  if (strstr(buffer,"/on"))
   {
     digitalWrite(led,HIGH); 
     Serial << "LED ON."<<endl;
     Serial.flush();
   }
     else if (strstr(buffer,"/off"))
   {
     digitalWrite(led,LOW);  
     Serial << "LED OFF"<<endl;
     Serial.flush();
   }
}
}

(i used the flush() fuction a lot but i thought better too much than too less so i basically put one after every serial entry^^)
Also i want to say that the example sketch that comes with the wifly library works pretty good so maybe its not the serial connection but the actual code.

Looks very much like the problems I had. They went away when I changed to the hardware serial for the WiFi connection and SoftwareSerial for the debugging. The Roving Networks hardware seems to be quite picky about the serial timing and the timing of the SoftwareSerial changes when interrupts occur. If you look at the output with the scope you can see it, it's not much but clearly detectable.

I tried using using Serial.flush() to fix this issue and i think it does help a bit

How can throwing away random amounts of unread data possibly help?

So you basically just connect the wifi shield directly over a usb to serial adapter to the pc right? But i dont quiete understand yet how it works over hardware serial. From my understanding with the usb to serial adapter you just create another hardware serial port since the arduino uno has only one. But if the wifi data is transmitted directly to the pc how does the arduino communicate with the wifi shield? Also could i use an arduino mega instead of the usb to serial adapter since it got more hardware serial ports? Maybe you could give me some example sketch of yours for wifly + hardware serial so i could understand how it works. PaulS i looked up the function and the reference says:
flush()
Description

Waits for the transmission of outgoing serial data to complete.
So i didnt know that data is actually being removed i thought it would just wait but anyway isnt the data already read before i use the flush() function?