Go Down

Topic: Problem with my internet code (Read 579 times) previous topic - next topic

luisga018

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("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Family Lights</H1>");
         
          client.println("<a href=\"/?on\"\">ON</a>");
          client.println("<a href=\"/?off\"\">OFF</a>");
       
          client.println("</BODY>");
          client.println("</HTML>");

          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="";

        }
      }
    }
  }
}

PaulS

What is in readString when the \n is detected? What does you serial output tell you?

PeterH

Code: [Select]

          client.println("<a href=\"/?on\"\">ON</a>");
          client.println("<a href=\"/?off\"\">OFF</a>");


What is the actual HTML source displayed by the browser? You seem to be printing too many quote characters, so it may not be what you expect.

You have debug statements in the code commented out. Why don't you uncomment them so that you can see what HTTP request you're receiving for the 'off' command, and whether the lights are being turned on by the same request or a different request.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up