WiFly + Arduino + PHP = Light control

Hi all,

I using tutorial from

http://www.arduino.cc/cgi-bin/yabb2/YaB … 78347111/0

Got nice UI (page 7 to 9) website lods from my server nicely show current led statuses but unfotianatly Trying to turn led on or off not working corectly.

If i press led 3 it will hang,then i press led 8 it will turn it on,and if i will press led 8 again its hang again, but after led 3 pressed it turning it on.And so on and on each time tu turn le on or off need press other led first than you led which you want to press.

Hope its understandibe my writing and problem :slight_smile:

I using WiFly Shield code library alpha 1 release and arduino 2009

my Arduino code is

/*
 * Web Server
 *
 * (Based on Ethernet's WebServer Example)
 *
 * A simple web server that shows the value of the analog input pins.
 */

#include "WiFly.h"
#include "Credentials.h"
#include <WString.h>
String readString = String(100);



Server server(80);

void setup() {
  WiFly.begin();
  if (!WiFly.join(ssid, passphrase)) {
    while (1) {
      // Hang on failure.
    }
  }
  //pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
  pinMode(8, OUTPUT); digitalWrite(8, LOW); // led 8
    pinMode(3, OUTPUT); digitalWrite(3, LOW); // led 3



  server.begin();
}

void loop(){
  Client client = server.available();
  if (client) {
    while (client.connected()) {
	if (client.available()) {
	  char c = client.read();
	  if (readString.length() < 100){
	    readString.append(c);
	  }
	  if (c == '\n') {
	  
		 if(readString.contains("led8")){
		   toggle(8);
		 } ///else
		 if(readString.contains("led3")) {
		   toggle(3);
		 }
		 client.println("<html><center>");
		 client.println("Light 3
");
		 if (digitalRead(3) == LOW){
		   client.println("<img src='http://192.168.1.9/images/lightoff.png'>");
		 } else if (digitalRead(3) == HIGH){
		   client.println("<img src='http://192.168.1.9/images/lighton.png'>");
		 }
		 client.println("<form method='get'><input type=submit name=led3 value=toggle></form>");
readString="";
		 client.println("Light 8
");
		 if (digitalRead(8) == LOW){
		   client.println("<img src='http://192.168.1.9/images/lightoff.png'>");
		 } else if (digitalRead(8) == HIGH){
		   client.println("<img src='http://192.168.1.9/images/lighton.png'>");
		 }
		 client.println("<form method='get'><input type=submit name=led8 value=toggle></form>");
		 client.println("</center></html>");
	    readString="";
delay(10);
	    client.stop();
	  }
	}
    }
  }
}

int toggle(int pin){
  if(digitalRead(pin) == LOW){
    digitalWrite(pin, HIGH);
  } else {
    digitalWrite(pin, LOW);
  }
}

And PHP code is

<html>
<head>
<title>Motor Control</title>
<script type="text/JavaScript" src="scripts/jquery.js"></script>
<script type="text/JavaScript" src="scripts/js/jquery-ui-1.8.2.custom.min.js"></script>
<link type="text/css" href="scripts/css/start/jquery-ui-1.8.2.custom.css" rel="stylesheet" />
<script type="text/javascript">
	//accordion code set to mouseover and no autoheight
	$(function() {
		$("#accordion").accordion({
			event: "mouseover",
			autoHeight: false
		});
	});
</script>
</head>
<body BGcolor='grey'>
<div id=accordion style="width:250px;">
<h3><a href="#">Control</a></h3>
<div bgcolor='grey'>
<iframe width='150' height='350' allowtransparency=true frameborder=0 scrolling=none  src="http://192.168.1.14"></iframe>
</div>
</div>
</body>
</html>

all setup is on my network with ubuntu 10.04 server .

You probably want to set a variable in your get request. Most browsers will not change get variables between requests unless the form changes their value. You may want to change the way you pass the variable. Use a syntax like “?led1=1&led2=1”, and parse the URL for the actual value, instead of just looking that the value is there. That way when the browser carries over variables from one GET to another GET, it won’t necessarily screw up your variables.

Also, your code will send a page while c == ‘\n’. You may want to rethink the processing you do. (ie- Process the entire get request, then pass the request to the code that serves the page. As it is you’re ignoring the possibility that the browser may be sending additional information in the request. (The true end of a get request is ‘\n\n’, aka a blank line.) If you start sending the page before the browser has finished it’s request it may not be very happy with you, and may cause strange things to happen.

I also notice that your server code doesn’t send any response header. It may be sent by one of your included libraries, but I don’t see one that seems like it would do it. At a minimum you should transmit

HTTP1.1 200 OK
CONTENT-TYPE: text/html; charset=utf8
[BLANK LINE]

to tell the browser its request was accepted, and what kind of data it should be expecting. Otherwise it may keep repeating the request until it times out.

You may want to read up on HTTP, or get any of the firefox extentions that let you look at the response header, to make sure the data is actually coming back to you, and that the requests are correct.