Thursday, July 14, 2011

Arduino - Web server controlled LED using serial connection between the Arduino and web server

I was chatting to a friend who was working on home automation.  Controlling lights, heating and other stuff around the house using a touch screen controller and I thought if you used a web site as the interface then connected to the Arduino over a serial link it would be possible to switch things on and off from any devlice that could display a web page and access the web server.

Personally, I would not recommend using a serial link from the server to the Arduino as the Arduino resets itself everytime something is sent over serial,  More correctly an ethernet shield would be great.  Giving more scalability as the web server can talk to mutliple Arduinos with no great extra effort.

I installed XAMPP on my netbook as the webserver and used PHP code on the server to present the web interface and send the command to the Arduino over serial.  The Arduino is waiting for a serial command and then depending on what it is will turn on or off the mounted LED.

So, not the most impressive looking demo, but as a proof of concept if I can turn on a LED I can control a relay to turn on an kind of device.


Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.


PHP on the server:

<?php

exec('mode com4: baud=9600 data=8 stop=1 parity=n xon=no');

$switch1 = "";

/* Serial script for pan/tilt Camera with servos */
/* Script by Aneal Khimani, 2-12-10 */

//check the GET action SuperGlobal var to see if an
//action is to be performed

if (isset($_GET['action'])) {

$switch1 = $_GET['action'];

//Action required

switch ($switch1) {
case "on":
$fp = fopen("com4", "w");
fwrite($fp, chr(97));
fclose($fp);
break;

case "off":
$fp = fopen("com4", "w");
fwrite($fp, chr(98));
fclose($fp);
break;


}
}

?>

<html>
<body>
<center>

<p>
<font size="4">Flick Switch</font><br />
<table width="30">
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=on" ?>">On</a></td><td><?php if($switch1 == 'on'){echo "<b>ON</b>";} ?></td></tr>
<tr><td><a href="<?=$_SERVER['PHP_SELF'] . "?action=off" ?>">Off</a></td><td><?php if($switch1 == 'off'){echo "<b>OFF</b>";} ?></td></tr>
</table>
</p>

</center>
</body>
</html>


Arduino Code:

/*

  Used with the PHP code
  http://localhost/arduino.php
 */

void setup() {               

  Serial.begin(9600); // initialize serial communication:

  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);    
}

void loop() {

// read the serial port:

if (Serial.available() > 0) {
int inByte = Serial.read();

switch (inByte) {
case 'a':
  digitalWrite(13, HIGH);   // set the LED on
  Serial.println("HIGH");
 
break;

case 'b':
  digitalWrite(13, LOW);   // set the LED off
  Serial.
  println("LOW");

break;
}

}

}

7 comments:

  1. Please can you give an example to control two or more leds in PHP code? Im attempting do to that for my home auto, but my understanding is very poor. I try to modify the above code, but i cant get there. Thanks very much

    ReplyDelete
  2. Hi m2snd,

    I'm just about to head off on holidays so I will not be able to do coding until nearly the end of August.

    I did prepare a reply but you cannot include code in comments, so it will not let me post it.

    Message me on my YouTube Channel and I will send you my reply.

    Gotta run now, I'm getting the stares for not helping with the packing for the holiday...

    Take care,
    Albert.

    ReplyDelete
  3. hello, is char 97 and 98 the code to turn your lights on and off. Just wondering as Im trying to set up a web remote using php, sending IR code down the line to my arduino.

    I have implemented some php code using WAMP server, I keep getting errors with ['php_self'] saving I do not have permission to access this file on the server.

    Anyone know if this is wamps error or mine.

    ReplyDelete
  4. Do I need the Arduino IDE on the WAMP?
    Is the sketch communicating with the PHP somehow?

    ReplyDelete
  5. Thank you!! thank you !!! thank you!!!
    You rule!!

    ReplyDelete
  6. Hi there ...
    I'm running on Windows 7 with Arduino MEGA 2560 and XAMPP ... I can't manage to make this work ... all is set but pin13 led doesn't react ... I only get a COM11 file (my arduino port is COM11 so i changed it from the php code to match) with 'a' written for 'ON' and 'b' for 'OFF' :/ any idea on how i can solve this and make it work? was anyone successful with this?

    ReplyDelete
  7. i connect my arduino on COM4, i dont get any errors on php or arduino code, but the serial port doesnt receive anything from

    ReplyDelete

Note: Only a member of this blog may post a comment.