Pages: [1]   Go Down
Author Topic: WiFly Webserver Problems - Help needed  (Read 1115 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
First i have to say the Arduino platform is very interesting, i stumbled on it accidentally.
Unfortunately I am not an experienced programmer and now i have i problem with my project....

I use an Arduino UNO and a Sparkfun WiFly-Shield (http://www.watterott.com/de/SparkFun-WiFly-Shield) with the Alpha 2 Release of the Experimental WiFly Arduino Library from Sparkfun.

I only need to switch one relay and measure the temperature. Right now eveything works, but not reliable!

My problems:

1.
After i power on the Arduino, i need to press reset switch once bevore the WiFly-Shield connects to my WLAN. Why?
Do i need to add some reset functions to my code?

2.
If i try to connect via web, it is not responding reliable. Sometimes it is not responding at all (and i have to press reset, maybe a loop?), pinging the WiFly-Shield is without problems at all. Ping works always. So i think my programming is not correct.

Here is the code based on some examples:

Code:

#include "WiFly.h"
#include "WString.h"

boolean LEDON = false;
String readString;

const int ledPin =  9; //digital pin for magnet
int pin = 5; // analog pin for temperature
int tempc = 0,tempf=0; // temperature variables
int samples[8]; // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;

Server server(80);

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

  //exit CMD mode if not already done
  SpiSerial.println("");
  SpiSerial.println("exit");
  delay(1000);

  //set into CMD mode
  SpiSerial.print("$$$");
  delay(1000);

  //set authorization level
  SpiSerial.println("set w a 4");
  delay(1000);

  //set passphrase
  SpiSerial.println("set w p MYPASSPHRASE");
  delay(1000);

  //set different stuff to be shure
  SpiSerial.println("set ip dhcp 0");
  delay(1000);

  SpiSerial.println("set ip address 192.168.0.222");
  delay(1000);

  SpiSerial.println("set ip gateway 192.168.0.1");
  delay(1000);

  SpiSerial.println("set ip netmask 255.255.255.0");
  delay(1000);

  //set localport
  SpiSerial.println("set i l 80");
  delay(1000);

  //disable *HELLO* default message on connect
  SpiSerial.println("set comm remote 0");
  delay(1000);

  //join wifi network <ssid>
  SpiSerial.println("join MYSID");
  delay(5000);

  //exit CMD mode
  SpiSerial.println("exit");
  delay(1000); 

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  LEDON = false;
}

void loop() {
  //digitalWrite(ledPin, HIGH);
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    //start Temperatur sensing
    for(i=0; i<=7; i++){ // gets 8 samples of temperature
      samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
      tempc = tempc + samples[i];
      delay(1000);
    }

    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100)
        {
          //store characters to string
          readString += c; //replaces readString.append(c);
        }
        if (c == '\n') {
          //if (c == '\n' && currentLineIsBlank) {
          if(readString.indexOf("gate=0")>-1){
            //led has to be turned OFF
            digitalWrite(ledPin, LOW);    // set the LED OFF
            LEDON = false;
          } ///else
          if(readString.indexOf("gate=1")>-1) {
            //led has to be turned ON
            digitalWrite(ledPin, HIGH);    // set on (5 Volts)
            delay(5000);
            digitalWrite(ledPin, LOW);    // set off
            //LEDON = true;
          }

          tempc = tempc/8.0; // better precision
          tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit

          if(tempc > maxi) {
            maxi = tempc;
          } // set max temperature
          if(tempc < mini) {
            mini = tempc;
          } // set min temperature

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          // print something, in HTML format:
          client.println("<h3><a href=\"http://192.168.0.222?gate=1\">Open Relay for 5 seconds</a></h3>");
          client.println("<br />");
          client.println("<br />");
          client.println("<h4>Temperature: ");
          client.println(tempc);
          client.println(" Grad Celsius</h4>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    //clearing string for next read
    readString="";
    tempc = 0;
    // give the web browser time to receive the data
    delay(100);
    // close the connection:
    client.stop();
  }
}


Regards
J.Fester
Logged

Pages: [1]   Go Up
Jump to: