I have a code for controlling the lights in my house via web, when i press On , the lights turn on without a problem, but when i press off, the lights turn off and a few seconds later they turn back on. Can anyone help me fix this problem on my code. And if any info on how i can convert my website to a mobile website so i can control them through my iphone would be great, and thanks for help.
Code below:
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
#include <x10.h>
#include <x10constants.h>
const int zeroCrossPin = 2;
const int dataPin = 3;
x10 myHouse = x10(zeroCrossPin, dataPin);
Servo myservo; // create servo object to control a servo
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;
//////////////////////
void setup(){
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
// myservo.write(90); //set initial servo position if desired
// myservo.attach(7); //the pin for the servo control
//enable serial data print
Serial.begin(9600);
// Serial.println("server servo/pin 5 test 1.0"); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
// Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("");
client.println("");
client.println("Arduino GET test page");
client.println("");
client.println("");
client.println("
Family Lights
");client.println("<a href="/?on"">ON");
client.println("<a href="/?off"">OFF");
client.println("");
client.println("");
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("off") >0)//checks for off
{
Serial.println("Lights off:");
myHouse.write(A, UNIT_1, 3);
myHouse.write(A, OFF, 3);
myHouse.write(B, UNIT_1, 3);
myHouse.write(B, OFF, 3);
myHouse.write(C, UNIT_1, 3);
myHouse.write(C, OFF, 3);
// myservo.write(140);
// digitalWrite(5, LOW); // set pin 4 low
// Serial.println("Led Off");
}
if(readString.indexOf("on") >0)//checks for on
{
Serial.println("Lights on:");
myHouse.write(A, UNIT_1, 3);
myHouse.write(A, ON, 3);
myHouse.write(B, UNIT_1, 3);
myHouse.write(B, ON, 3);
myHouse.write(C, UNIT_1, 3);
myHouse.write(C, ON, 3);
// myservo.write(40);
// digitalWrite(5, HIGH); // set pin 4 high
// Serial.println("Led On");
}
//clearing string for next read
readString="";
}
}
}
}
}