Go Down

Topic: Home controller system on web page (Read 792 times) previous topic - next topic

Mickey90

hye guys, i dont event know what am i typing here but seems like i got prob with my arduino its either the board itself or the programming.. ok im now trying to develop the home controller which can control the light the fan and adding the additional feature for security which is PIR sensor and buzzer as alarm controll directly from web page using the html code. im using the Arduino uno r3 + Ethernet shield w5200 v1.0 . when ever i tried to compiled i got the error such as 'Ethernet' was not declared in this scope is it because i change the libraries from arduino ide w5100 to 5200.blur

#include <SPI.h>
#include <Ethernet.h>


   //ethernet configuration
byte ip[] = { 192, 168, 0, 13};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

EthernetServer server(27);

//initial
 
  int LED=8; //led is connected to digital pin 4
  int PIR=3; //PIR sensor is connected to digital pin 3
  int LDR=5; //LDR sensor is connected to analog in 5
  int LAMP=4; //LAMP is conected to digital pin 2
  int FAN=9; //FAN is connected to digital pin 9
  int BUZZER=2; //BUZZER is connected to digital pin 8
  int PIRstate=0; //ariable for PIR sensor status
  int lampstate=0; //variable for LAMP status
  int fanstate=0; //ariable for FAN status
  float photocell=0; //variable for photocell (LDR) analog value
  char c=0; //received data
  char command[2]= "\0"; //command
 
   void setup()
   {       
      Ethernet.begin(mac, ip);
      server.begin();
      pinMode(LED,OUTPUT);
      pinMode(LAMP,OUTPUT);
      pinMode(PIR,INPUT);
      pinMode(FAN,OUTPUT);
      pinMode(BUZZER,OUTPUT);
   }
   void loop()
   {
      EthernetClient client=server.available();
      //detect if current is the first line
      boolean current_line_is_first=true;
      
      if(client)
      {
         //an http request ends with a blank line
         boolean current_line_is_blank= true;
         while (client.connected())
         {
            if (client.available())
            {
               char c=client.read();
               //if we've gotten to the end of the line(receive a newline character)
               //and the line is blank, the http request has ended,
               //so we can send a reply
               if(c=='\n'&&current_line_is_blank)
               {
                  //send a standard http response header
                  client.println("HTTP/1.1 200 OK");
                  client.println("Content-Type: text/html");
                  client.println();
                  client.println("<html>");
                  //auto reload webpage every 5 second
                  client.println("<META HTTP-EQUIV=REFRESH CONTENT=5 URL=>");
                  //webpage title
                  client.println("<body style='width:60%;margin:auto'>");
                  client.println("<div style='border:1px solid white;border-radius:10px;background-color.#0099ff;'>");
                  client.println("<centre><p><h1><center>WELCOME HOME!!</h1></p><center>

");
                  //read analog pin 1 for the alue of photocell
                  photocell=analogRead(LDR);
                  if(photocell<40)
                  {
                     digitalWrite(LED,HIGH);
                  }
                  else
                  {
                     digitalWrite (LED,LOW);
                  }
                  client.print("<p2>LDR SENSOR: <h2>Light Intensity= <font color=white>");
                  client.println(photocell,2);
                  client.println("</font></h2></p2>

");
                  //read digital pin 13 for the state of PIR sensor
                  client.println("<p3>SECURITY SYSTEM:</p3>");
                  client.println("<form method=get name=form>");
                  client.println("<button name=b value=1 type=submit style=height:60px;width:120px;border-radius:20px;background-color:green;>PIR On</button>");
                  client.println("<button name=b value=2 type=submit style=height:60px;width:120px;border-radius:20px;background-color:red;>PIR Off</button>");
                  client.println("</form>
");
                  
                  client.println("intruder detection=");
                  if(PIRstate==HIGH)//PIR sensor detected movement
                  {
                     client.println("<font color=red>Motion Detected!</font>");
                  }
                  else //No movement is detected
                  {
                     client.println("<font coor=green>No Movement</font>");
                  }
                  
                  if(PIRstate==HIGH)
                  {
                     //PIR sensor detected movement
                     unsigned char i;
                     for(i=0;i<25;i++)
                     {
                        digitalWrite(BUZZER,HIGH);
                        delay(100); //Delay 1ms
                        digitalWrite(BUZZER,LOW);
                        delay(100); //Delay 1ms
                     }
                  }
                  else // NO MOVEMENT IS DETECTED
                  {
                     digitalWrite(BUZZER,LOW);
                  }
                  client.println("

");
                  //button functions
                  client.println("<p4>APPLIANCES:<h4>");
                  client.println("<form method=get name=form>");
                  client.println("<button name=b value=3 type=submit style=height:60px;width:120px;border-radius:20px;background-color:green;>LAMP On</button>");
                  client.println("<button name=b value=4 type=submit stlye=height:60px;width:120px;border-radius:20px;background-color:red;>LAMP Off</button>");
                  client.println("<form method=get name=form>");
                  client.println("<button name=b value=5 type=submit style=height:60px;width:120px;border-radius:20px;background-color:green;>FAN high</button>");
                  client.println("<button name=b value=6 type=submit style=height:60px;width:120px;border-radius:20px;background-color:green;>FAN low</button>");
                  client.println("<button name=b value=7 type=submit style=height:60px;width:120px;border-radius:20px;background-color:red;>FAN off</button>");
                  client.println("</h4></p4></form>

");
                  client.println("<p6>APPLIANCES CONDITION:");
                  lampstate=digitalRead(2);
                  client.print("<h6 style=color:white>LAMP:");
                  if(lampstate==HIGH)
                  {
                     client.println("<font color=green>ON</font>
");
                  }
                  else
                  {
                     client.println("<font color=red>OFF</font>
");
                  }
                  client.print("FAN :");
                  if (!strcmp(command, "5"))
                  {
                     analogWrite(FAN,244);
                     client.println("<font color=green>ON</font>
</h6></p6>");
                  }
                  else if(!strcmp(command, "6"))
                  {
                     analogWrite(FAN,57);
                     client.println("<font color=green>ON</font>
</h6></p6>");
                  }
                  else if (!strcmp(command, "7"))
                  {
                     analogWrite(FAN,0);
                     client.println("<font color=red>OFF</font>
</h6></p6>");
                     
                  }
                  else if(fanstate==0)
                  {
                     client.println("<font color=red>OFF</font>
</h6></p6>");
                  }
                  client.println("</div></body>");
                  client.println("</html>");
                  
                  break;
                  
               }
               if(c== '\n')
               {
                  //we're starting a new line
                  current_line_is_first= false;
                  current_line_is_blank= true;
               }
               else if(c != '\r')
               {
                  //we'e gotten a character on the current line
                  current_line_is_blank = false;
               }
               //get the first http request
               if(current_line_is_first && c == '=')
               {
                  for(int i=0;i<1;i++)
                  {
                     c=client.read();
                     command=c;
                  }
                  //LED control
                  if(!strcmp(command,"3"))
                  {
                     digitalWrite(LAMP,HIGH);
                  }
                  else if(!strcmp(command,"4"))
                  {
                     digitalWrite(LAMP,LOW);
                  }
                  if(!strcmp(command,"1"))
                  {
                     PIRstate=digitalRead(PIR);
                  }
                  else if(!strcmp(command,"2"))
                  {
                     PIRstate=0;
                  }
                  
               }
            }
         
         }
            //give the web browser time to receive the data
            delay(1);
            client.stop();
      }
   
   }

spycatcher2k

OK - So what is your question? If it's 'when ever i tried to compiled i got the error such as 'Ethernet' was not declared in this scope is it because i change the libraries from arduino ide w5100 to 5200' Then post a link to the library (correctly), your code (Correctly) and wha you have changed to modify the code to work with a totally different library . . . . please.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

JimboZA

Good example of why to use code tags.

Did you see it turned italic in the middle there? There's an array called command with elements called "i" and guess what?.... open square bracket followed by an i followed by a close square bracket is what turns italic on. So that few characters of your code has been interpreted rather than appearing on the screen.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

Mickey90


OK - So what is your question? If it's 'when ever i tried to compiled i got the error such as 'Ethernet' was not declared in this scope is it because i change the libraries from arduino ide w5100 to 5200' Then post a link to the library (correctly), your code (Correctly) and wha you have changed to modify the code to work with a totally different library . . . . please.

ok I'm so nervous n I don't event understand my own word! Haha I mean does different ethernet shield such as w5100 and w5200 version. Had different declaration such when I used w1500 on libraries it sadi inital was #include<ethernet.h> but for the w5200 it used #include<EthernetServerV2_0.h> does it play the importan role for the whole programming structure?

Mickey90


Good example of why to use code tags.

Did you see it turned italic in the middle there? There's an array called command with elements called "i" and guess what?.... open square bracket followed by an i followed by a close square bracket is what turns italic on. So that few characters of your code has been interpreted rather than appearing on the screen.

Which mean? So overall my html is okay?

JimboZA



Which mean? So overall my html is okay?


No it means your code is impossible to read... Use the code tag # above the  :smiley-sweat: so that

Code: [Select]
it
{
looks like
}
this
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

zoomkat

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

ehsanabdullah

i got error in programming that on  serial port when i give command it shows G
                                                                                                                                                           G
her is the code of  my project please help me out of this problem
Code: [Select]

/*

Arduino with Ethernet Shield
*/

#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>
int aled = 4;
int bled = 5;
int cled = 8;
int dled = 9;
const byte ledPin =  3;    // LED pin
const byte motionPin = 2;   // motion detector input pin
byte senseMotion = 0 ;
Servo microservo;
int pos = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   //physical mac address
byte ip[] = { 169, 254, 109, 80 };                      // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 169, 254, 109, 254 };                   // internet access via router
byte subnet[] = { 255, 255, 0, 0 };                  //subnet mask
EthernetServer server(80);                             //server port     
String readString;

void setup() {
// Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  pinMode(ledPin, OUTPUT);     
  pinMode(motionPin, INPUT);
  pinMode(aled, OUTPUT);
  pinMode(bled, OUTPUT);
  pinMode(cled, OUTPUT);
  pinMode(dled, OUTPUT);
  microservo.attach(7);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


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);
         }
         // Now watch for burglers
    senseMotion = digitalRead(motionPin);
    if (senseMotion == HIGH) {    // burgler found!
      digitalWrite(ledPin, HIGH);
    } else {                      // no burgler, yet...
      digitalWrite(ledPin, LOW);
    }
         //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("<body bgcolor=\"#22e3c3\">");
           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
           client.println("<link rel='stylesheet' type='text/css' href='http://randomnerdtutorials.com/ethernetcss.css' />");
           client.println("<TITLE>Random Nerd Tutorials Project</TITLE>");
           client.println("</HEAD>");
           client.println("<BODY>");
           client.println("<H1>Home Automation by Technology Department</H1>");
           client.println("<hr />");
           client.println("<br />"); 
           client.println("<H2>Arduino with Ethernet Shield</H2>");
           client.println("<br />"); 
           client.println("<a href=\"/?button1on\"\">Turn On 1LED</a>");
           client.println("<a href=\"/?button1off\"\">Turn Off 1LED</a><br />");   
           client.println("<br />");     
           client.println("<br />");
           client.println("<a href=\"/?button2on\"\">Turn On 2LED</a>");
           client.println("<a href=\"/?button2off\"\">Turn Off 2LED</a><br />");
           client.println("<br />");     
           client.println("<br />");
           client.println("<a href=\"/?button3on\"\">Turn On 3LED</a>");
           client.println("<a href=\"/?button3off\"\">Turn Off 3LED</a><br />");
           client.println("<br />");     
           client.println("<br />");
           client.println("<a href=\"/?button4on\"\">Turn On 4LED</a>");
           client.println("<a href=\"/?button4off\"\">Turn Off 4LED</a><br />");
           client.println("<br />");     
           client.println("<br />");
           client.println("<a href=\"/?button5on\"\">Rotate Left Direction</a>");
           client.println("<a href=\"/?button5off\"\">Rotate Right Direction</a><br />");
           client.println("<br />");
           client.println("<br />");     
           client.println("<br />");
           client.println("<a href=\"/?button6off\"\">Turn off alarm</a>");
           client.println("<br />");     
           client.println("<br />");
            client.println("<big>Engg Technologist;</big>");
            client.println("                 <small>JALAL KHAN;</small>");
            client.println("            <small>M.AQEEL;</small>");
            client.println("         <small>M.MEHBOOB ANJUM;</small>");
            client.println("      <small>EHSAN-E-ABDULLAH;</small>");
           
           
           client.println("<br />");
           client.println("</BODY>");
           client.println("</HTML>");
     
           delay(1);
           //stopping client
           client.stop();
           //controls the Arduino if you press the buttons
           if (readString.indexOf("?button1on") >0)
           {
               digitalWrite(aled, HIGH);
           }
           if (readString.indexOf("?button1off") >0)
           {
               digitalWrite(aled, LOW);
           }
            if (readString.indexOf("?button2on") >0)
            {
               digitalWrite(bled, HIGH);
           }
           if (readString.indexOf("?button2off") >0)
           {
               digitalWrite(bled, LOW);
            }
             if (readString.indexOf("?button3on") >0)
             {
               digitalWrite(cled, HIGH);
           }
           if (readString.indexOf("?button3off") >0)
           {
               digitalWrite(cled, LOW);
           }
            if (readString.indexOf("?button4on") >0)
            {
               digitalWrite(dled, HIGH);
           }
           if (readString.indexOf("?button4off") >0)
           {
               digitalWrite(dled, LOW);
           }
           
           if (readString.indexOf("?button5on") >0)
           {
                for(pos = 0; pos < 180; pos += 3)  // goes from 0 degrees to 180 degrees

                {                                  // in steps of 1 degree
                  microservo.write(pos);              // tell servo to go to position in variable 'pos'
                  delay(15);                       // waits 15ms for the servo to reach the position
                }
           }
           if (readString.indexOf("?button5off") >0){
                for(pos = 180; pos>=1; pos-=3)     // goes from 180 degrees to 0 degrees
                {                               
                  microservo.write(pos);              // tell servo to go to position in variable 'pos'
                  delay(15);                       // waits 15ms for the servo to reach the position
                }
           }
             if (readString.indexOf("?button6off") >0)
           {
             digitalWrite(senseMotion, LOW);
           }
            //clearing string for next read
           
             readString=""; 
           
           
         }
      }

    }
  }
}


   


Go Up