Hi. My name is Peter, i am from Poland, i'm new here so i want to introduce myself. I have i problem with controlling leds over wifi shield using iphone 5. Here is my html code:
<html>
<head>
<script>
strLED1 = "";
strLED2 = "";
var LED1_state = 0;
var LED2_state = 0;
function GetButton1()
{
if (LED1_state === 1) {
LED1_state = 0;
strLED1 = "&LED1=0";
}
else {
LED1_state = 1;
strLED1 = "&LED1=1";
}
}
function GetButton2()
{
if (LED2_state === 1) {
LED2_state = 0;
strLED2 = "&LED2=0";
}
else {
LED2_state = 1;
strLED2 = "&LED2=1";
}
}
function GetArduinoIO()
{
nocache = "&nocache=" + Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function()
{
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseXML != null) {
var count;
var num_an = this.responseXML.getElementsByTagName('switch').length;
for (count = 0; count < num_an; count++) {
document.getElementsByClassName("switches")[count].innerHTML =
this.responseXML.getElementsByTagName('switch')[count].childNodes[0].nodeValue;
}
if (this.responseXML.getElementsByTagName('LED')[0].childNodes[0].nodeValue === "on") {
document.getElementById("LED1").innerHTML = "LED 1 is ON (D8)";
LED1_state = 1;
}
else {
document.getElementById("LED1").innerHTML = "LED 1 is OFF (D8)";
LED1_state = 0;
}
if (this.responseXML.getElementsByTagName('LED')[1].childNodes[0].nodeValue === "on") {
document.getElementById("LED2").innerHTML = "LED 2 is ON (D9)";
LED2_state = 1;
}
else {
document.getElementById("LED2").innerHTML = "LED 2 is OFF (D9)";
LED2_state = 0;
}
}
}
}
}
request.open("GET", "ajax_inputs" + strLED1 + strLED2 + nocache, true);
request.send(null);
setTimeout('GetArduinoIO()',8250);
strLED1 = "";
strLED2 = "";
}
</script>
</head>
<body onLoad="GetArduinoIO()">
<table width="100%" height="96%" border="0">
<tr>
<tk><td height="17%" colspan="2" align="center" valign="middle" bgcolor="#53A9FF"><font size="7" class="ipilot">iPilot</font></td></tk>
</tr>
<tr>
<td width="50%" align="center" valign="middle" class="myButton" onClick="GetButton1()" button>
Brama</button>
</td>
<td width="50%" align="center" valign="middle" class="myButton" onClick="GetButton2()" button>
Garaz</button>
</td>
</tr>
<tr>
<td height="16%" colspan="2"> </td>
</tr>
<tr>
<td height="7%" align="center" valign="middle"><font color="black"><span class="switches">...............</span></font></td>
<td align="center" valign="middle"><font color="black"><span class="switches">...............</span></font></td>
</tr>
</table>
</body>
</html>
and here is arduino code:
#include <WiFi.h>
#include <SPI.h>
#include <SD.h>
#define REQ_BUF_SZ 31
File webFile;
char ssid[] = "DOM";
char pass[] = "domdomdom";
int status = WL_IDLE_STATUS;
WiFiServer server(80);
char HTTP_req[REQ_BUF_SZ] = {0};
char req_index = 0;
boolean LED_state[2] = {0};
void setup()
{
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
Serial.begin(9600);
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true);
}
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(9000);
}
server.begin();
printWifiStatus();
Serial.println("Initi SD");
if (!SD.begin(4)) {
Serial.println("ERROR");
return;
}
Serial.println("SUCCESS - SD");
if (!SD.exists("index.htm")) {
Serial.println("ERROR");
return; // can't find index file
}
Serial.println("SUCCESS");
}
void loop()
{
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c;
req_index++;
}
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
if (StrContains(HTTP_req, "ajax_inputs")) {
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
SetLEDs();
XML_response(client);
}
else {
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
webFile = SD.open("index.htm");
if (webFile) {
byte clientBuf[64];
int clientCount = 0;
while(webFile.available())
{
clientBuf[clientCount] = webFile.read();
clientCount++;
if(clientCount > 63)
{
client.write(clientBuf,64);
clientCount = 0;
}
}
if(clientCount > 0) client.write(clientBuf,clientCount);
webFile.close();
}
}
Serial.print(HTTP_req);
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disonnected");
}
}
void SetLEDs(void)
{
if (StrContains(HTTP_req, "LED1=1")) {
LED_state[0] = 1;
digitalWrite(8, HIGH);
}
else if (StrContains(HTTP_req, "LED1=0")) {
LED_state[1] = 0;
digitalWrite(8, LOW);
}
if (StrContains(HTTP_req, "LED2=1")) {
LED_state[1] = 1;
digitalWrite(9, HIGH);
}
else if (StrContains(HTTP_req, "LED2=0")) {
LED_state[1] = 0;
digitalWrite(9, LOW);
}
}
void XML_response(WiFiClient cl)
{
int count;
int sw_arr[] = {2, 3};
cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
for (count = 0; count < 2; count++) {
cl.print("<switch>");
if (digitalRead(sw_arr[count])) {
cl.print("OPEN");
}
else {
cl.print("CLOSE");
}
cl.println("</switch>");
}
cl.print("<LED>");
if (LED_state[0]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
cl.print("<LED>");
if (LED_state[1]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
cl.print("</inputs>");
}
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;
len = strlen(str);
if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}
return 0;
}
}
The problem is that the codes run perfectly on ethernet shield not on wifi shield. The codes are from :
My problem is that when setTimeOut method is set to 1000 miliseconds, the request do not contain strLED1/2, i push the button and the request only contains GET /ajax_inputs&nocache=28047 without strLED. When i change the time to about 8000 miliseconds everything works fine, but it takes about 5-7 seconds after pushing the button to light the led.
Second problem is about iphone apple-touch-icon request, instead of GET /ajax_inputs&.... requests i get a lot of apple-touch-icon request which slows down the program for extra 5-7 seconds.
Can anybody help me?
Sorry for language mistakes.
Thanks