Final Project_ web controlled leds and desk Lamp

Created by Dewu Han and Tony Chen

This is our project management and Gantt char of our porject

Our original project is to control a lamp over the internet with an Arduino over web-interface, but we have upgraded our Project to work as multifunction Christmas decorating light

The Hardware

  • A Computer
  • A External Hard Drive
  • A Linux Web-Server with PHP
  • An Arduino Uno
  • Breadboard & Jumpers
  • LEDs & Bulb

The program and codes

  • Server terminal configuration and commands

sean127@sean127:~$ sudo su
[sudo] password for sean127:
root@sean127:/home/sean127# vi /var/www/example.php
root@sean127:/home/sean127# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1079 errors:0 dropped:0 overruns:0 frame:0
TX packets:1079 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:91239 (91.2 KB) TX bytes:91239 (91.2 KB)
root@sean127:/home/sean127# sudo usermod -a -G dialout sean127
root@sean127:/home/sean127# sudo chmod a+rw /dev/ttyACM0
root@sean127:/home/sean127# /etc/init.d/apache2 restart
* Restarting web server apache2 ... waiting [ OK ]
root@sean127:/home/sean127# chmod 777 -R /var/www/
root@sean127:/home/sean127# /etc/init.d/apache2 restart
* Restarting web server apache2 ... waiting [ OK ]

The server is called LAMP server, it stands for Linux, Apache, MySQL, and Php. Although MySQL is installed on the server, but this project works without one. we ran several commands on the server, and the following are the most important ones.

check which port arduino is connected:
dmesg

to authorize data transmission for the usb port “ttyACM0”
sudo usermod -a -G dialout sean127
sudo chmod a+rw /dev/ttyACM0

restart apache
/etc/init.d/apache2 restart

the required web files are stored in the directory /var/www/
to create permission for php file to read and write to the text file we use this:
chmod 777 -R /var/www/

check ip of the server
ifconfig

  • Arduino Code

int timer = 200; // The higher the number, the slower the timing.
char inByte = 0;
char code = '0';
int led = 13;
int thisPin2 =2;
int thisPin3 =3;
int thisPin4 =4;
int thisPin5 =5;
int thisPin6 =6;
int thisPin7 =7;
int thisPin8 =8;

void setup(){
Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(thisPin2, OUTPUT);
pinMode(thisPin3, OUTPUT);
pinMode(thisPin4, OUTPUT);
pinMode(thisPin5, OUTPUT);
pinMode(thisPin6, OUTPUT);
pinMode(thisPin7, OUTPUT);
pinMode(thisPin8, OUTPUT);
}

void loop()
{
if (Serial.available() > 0) {
inByte = Serial.read();
}
else {
inByte = code;
}
Serial.println(inByte);
if(inByte == ‘1’){
leds();
code = ‘1’;
}
else if(inByte == ‘0’){
code = ‘0’;

}
else if(inByte == ‘2’){
digitalWrite(led,HIGH);
}
else if(inByte == ‘3’){
digitalWrite(led,LOW);
}
}

void leds()
{
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 9; thisPin++) { // turn the pin on: digitalWrite(thisPin, HIGH); delay(timer); // turn the pin off: digitalWrite(thisPin, LOW); } // loop from the highest pin to the lowest: for (int thisPin = 8; thisPin >= 2; thisPin–) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);

}
}

  • PHP Code for web interface

http://pastebin.com/5r4RvYFz

  • PHP serial class

http://code.google.com/p/php-serial/

Sample Code

deviceOpen(); // To write into
$serial->sendMessage("Hello !"); // Or to read from
$read = $serial->readPort(); // If you want to change the configuration, the device must be closed $serial->deviceClose(); // We can change the baud rate $serial->confBaudRate(2400); // etc... ?>

PHP_serial.class.php can be used to communicate with a serial port under Linux, OSX, or Windows. It takes the path of serial device and checks whether it is valid before opening a connection to it. It will read and send data to the serial port once the connection is opened.

 

Arduino and Relay connection

The circuit:

•Connect the base of the transistor to the pin used in the code I used pin 13
•Connect the LED in series with switching contacts of the relay.
•And connect your arduino to your server.

 

 

The proper way to drive Relays with the Arduino

•When Arduino sends 5v to the relay, the relay is closed.
•The 1N4004 diode connected to the power rail to protect Arduino from getting damaged if there is a surge
•Schematic is available on the Arduino playground.
The Web Interface
Php_serial.Class.php(serial class) and example.php(main web interface) were uploaded to the server in the same directory. The $serial->deviceSet(“/dev/ttyACM0”);  was edited in the example.php, pointing it to the right serial path.
Setting up the permission for serial port for data transmission
Setting up Static IP or a Dynamic DNS
The purpose is so the web pages are published to the public for easier access.
Setting up static IP
Setting up dynamic DNS, we used no-ip. There are other companies provide similar services such as http://dyn.com/dns/ ; we used no-ip instead, because it’s easier to setup. Other than that, we still have to port forward our server through the router.
Video Demo
https://www.youtube.com/embed/Zy58aJVl-ZA