SmartHome Control with PHP and Arduino

Hello guys.. I recently bought an Arduino Uno and Ethernet shield to make a project for graduation project..
We will control a Smart Home with an arduino using an ip adress (and later with smartphone app)..
I tried many many ways to programm it but every way has it's problems...
Anyway i want to write the page with html/css/php but i can't figure out how to make arduino understand the php....For example my control panel's code is something like :

<!DOCTYPE html>

<!-- PHP Way -->

<html>

<head>
 <title>Smart Home</title>
 <style>
  body {background-color: #000000; color: green;}
  h1   {text-align: center; font-style: italic; font-size: 26pt; margin: 50px 0px;}
      table {width:620px; height:240px; font-size: 16pt; position:relative; margin:auto;}
      table th {font-size: 20pt; text-decoration: underline;}
      </style>
</head>

<body>
 
 <?php 

        $roomStatus = array("RoomOneStatus" => FALSE, "RoomTwoStatus" => FALSE, 
 "bathroomStatus" => FALSE, "kitchenStatus" => FALSE,
     "livingRoomStatus" => FALSE, "hallwayStatus" => FALSE, 
 "gardenStatus" => FALSE,);
 

    if (!$roomStatus["RoomOneStatus"]) {
           $roomOne = "Disabled";
   $style1 = "color:red;";
    }
    else {
   $roomOne = "Enabled";
   $style1 = "color:green;";
 }
   
    if (!$roomStatus["RoomTwoStatus"]) {
           $roomTwo = "Disabled";
   $style2 = "color:red;";
    }
    else {
 $roomTwo = "Enabled";
 $style2 = "color:green;";
 }
 
    if (!$roomStatus["bathroomStatus"]) {
           $bathroom = "Disabled";
   $style3 = "color:red;";
    }
    else {
   $bathroom = "Enabled";
   $style3 = "color:green;";
 }
 
 if (!$roomStatus["kitchenStatus"]) {
           $kitchen = "Disabled";
   $style4 = "color:red;";
    }
    else {
   $kitchen = "Enabled";
   $style4 = "color:green;";
 }
 
 if (!$roomStatus["livingRoomStatus"]) {
           $livingRoom = "Disabled";
   $style5 = "color:red;";
    }
    else {
   $livingRoom = "Enabled";
   $style5 = "color:green;";
 }
 
 if (!$roomStatus["hallwayStatus"]) {
           $hallway = "Disabled";
   $style6 = "color:red;";
    }
    else {
   $hallway = "Enabled";
   $style6 = "color:green;";
 }
 
 if (!$roomStatus["gardenStatus"]) {
           $garden = "Disabled";
   $style7 = "color:red;";
    }
    else {
   $garden = "Enabled";
   $style7 = "color:green;";
 }

 ?>

 <h1> Smart Home Control Panel </h1>

 <table border = "7" cellspacing = "10">
 <th>Rooms</th>
 <th>Device</th>
 <th>Status</th>
 <tr>
 <td>Room One</td>
 <td>Lights</td>
 <td style = "<?php echo $style1?>"><?php echo $roomOne ?></td>
 </tr>
 <tr>
 <td>Room Two</td>
 <td>Lights</td>
 <td style = "<?php echo $style2?>"><?php echo $roomTwo ?></td>
 </tr>
 <tr>
 <td>Bathroom</td>
 <td>Lights</td>
 <td style = "<?php echo $style3?>"><?php echo $bathroom ?></td>
 </tr>
 <tr>
 <td>Kitchen</td>
 <td>Lights</td>
 <td style = "<?php echo $style4?>"><?php echo $kitchen ?></td>
 </tr>
 <tr>
 <td>Living Room</td>
 <td>Lights</td>
 <td style = "<?php echo $style5?>"><?php echo $livingRoom ?></td>
 </tr>
 <tr>
 <td>Hallway</td>
 <td>Lights</td>
 <td style = "<?php echo $style6?>"><?php echo $hallway ?></td>
 </tr>
 <tr>
 <td>Garden</td>
 <td>Lights</td>
 <td style = "<?php echo $style7?>"><?php echo $garden ?></td>
 </tr>
 </table>

    <button style=\"width:120px;height:60px\"> <font size=\"3\"; color:red ;>Room One ON </font> </button> </a>
 <button style=\"width:120px;height:60px\"> <font size=\"3\"; color:red ;>Room One OFF </font> </button> </a>


</body>

</html>

and my arduino sketch is :

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


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,5); 
EthernetServer server(80);  

File webFile;


void setup()
{
    Ethernet.begin(mac, ip);  
    server.begin();           
    Serial.begin(9600);       
    
    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    
    if (!SD.exists("arduino.php")) {
        Serial.println("ERROR - Can't find arduino.php file!");
        return;  
    }
    Serial.println("SUCCESS - Found arduino.php file.");

    
}

void loop()
{
    EthernetClient client = server.available();  // try to get client

    if (client) {  // got client?
        boolean currentLineIsBlank = true;
        while (client.connected()) {
            if (client.available()) {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                if (c == '\n' && currentLineIsBlank) {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: close");
                    client.println();
                    // send web page
                    webFile = SD.open("arduino.php");        // open web page php file
                    
                    
                    if (webFile) {
                        while(webFile.available()) {
                            client.write(webFile.read()); // send web page to client
                        }
                        webFile.close();
                        
                    }
                    break;
                }
                
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      
        client.stop(); 
    } // end if (client)
}

But as you can imagine doesnt work...When i go to 192.168.1.5 the browser reads php as text...

Is there anyone with some free time and experiance to help me cause as you may realised i am hardly stuck :stuck_out_tongue:
Thanks in advance...

EDIT: But the way i am still in designing the html table...After figuring out how arduino can read the php ill move on writing the statements to turn on and off the leds.(Dunno how yet but lets get some things first) :stuck_out_tongue:

I have a small test link in my footer that goes over my small PHP, FLASH and Arduino test. (not really the same scenario as you have here though to be fair)..

Is the .php file just in some diectory/folder that you are pointing to? Or do you have some sort of the local webserver installed, so it can parse the php? (like WAMP or something?)

What do you mean by HTML table? (HTML tables should only beused for tabular data, not page or site layouts. :slight_smile:

Use CSS and table-less designs (DIVS..etc)

xl97:
Is the .php file just in some diectory/folder that you are pointing to? Or do you have some sort of the local webserver installed, so it can parse the php? (like WAMP or something?)

My arduino.php is inside www folder at wamp server...
Somehow i want to just communicate with arduino (without usb) within network...

If you want the arduino to do something based on PHP code, you will probably need to write some code for that.

can you successfully get this 'PHP' page in your WAMP install from another computer/laptop on your network?

xl97:
can you successfully get this 'PHP' page in your WAMP install from another computer/laptop on your network?

No i tried from another pc in the house and i can't see localhost/arduino.php

Well i am very bad as it comes to servers and all these things...

To be more clear...

When using Arduino as server i manage to control the led through the IP of the board on my computer...
But arduino's capacity is really really low for the features i want to add...So what i want to do is:
Having a page running on localhost(in my case) localhost/arduino.php that every time i press a button to light up a led the url will change...For example localhost/arduino.php?led1on
Then arduino as client will read the url and when he reads led1on for example will trigger the led...
Is that possible?

IMHO..

your first step is to get to the WAMP install on another computer on your network (NOT the PC that has WAMP installed/running on it)

How are you trying to access the page from other PC's?

\localhost wont work

do you have it open to the network?

for now remove Arduino from the equation.. put up a simple HTML page that says 'hello' (or whatever). and try access it from another machine on the network. google how to configure WAMP to do this..

then go back to your Arduino based project.

Ok i found the solution...I will use Arduino as Server so no need to bother with wamp etc...I done it and i can control from every device (phone,tablet, pcs) in network...Job done :slight_smile: