Điều khiển đèn bằng router Dlink DIR615C2 thông qua web.
Thành phần cần thiết gồm:
- Relay module 4 chanel.
- Một ít dây điện
I. Mod phần cứng
Dlink DIR615C2 có 6 GPIO dư nên có thể tận dụng nó để điều khiển relay.
Mình chỉ dùng GPIO 0, 2 nối vào relay. Sơ đồ đấu dây như sau:
GPIO | RELAY MODULE |
---|---|
0 | IN1 |
2 | IN2 |
Chân VCC của relay nối vào 5V trên router, GND của relay nối vào GND trên router.
II. Phần mềm
1. Cài php và lighttpd
Mở file /etc/lighttpd/lighttd.conf sửa như sau:opkg updateopkg install libsqlite3opkg install php5-fastcgiopkg install php5-mod-sessionopkg install lighttpdopkg install lighttpd-mod-fastcgiopkg install lighttpd-mod-simple-vhost
- dòng thứ 7 #server.modules = ( thành server.modules = (
-dòng thứ 14 #"mod_fastcgi", thành "mod_fastcgi",
-dòng thứ 16 #"mod_simple_vhost", thành "mod_simple_vhost",
-dòng thứ 35 thêm "index.php" vào thành index-file.names = ("index.php", "index.html", "default.html", "index.htm", "default.htm" )
-dòng thứ 78 #server.port = 81 thành server.port = 81
-dòng 144 đến 153 sửa thành như thế này
#### fastcgi module
## read fastcgi.txt for more info
fastcgi.server = (
".php" => ("localhost" => (
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/bin/php-fcgi"
)
)
Kích hoạt lighttpd lúc khởi động:
Tạo file index.php như sau:
/etc/init.d/lighttpd enable
2. Code điều khiển.Tạo file index.php như sau:
<?php session_start(); $up = array( 'u' => "132679261da1fe24fd11aeb4230abd99762daf6b", 'p' => "132679261da1fe24fd11aeb4230abd99762daf6b" ); if(isset($_POST['login']) && $up['u']===sha1($_POST['username']) && $up['p']===sha1($_POST['password'])){ $_SESSION['username'] = $_POST['username']; $_SESSION['password'] = $_POST['password']; }; if(isset($_POST['logout'])){ unset($_SESSION['username']); unset($_SESSION['password']); } if(isset($_SESSION['username']) && isset($_SESSION['password'])){ if($up['u']===sha1($_SESSION['username']) && $up['p']===sha1($_SESSION['password'])){ if(isset($_POST['router'])) { $contents = file_get_contents("{$_SERVER['DOCUMENT_ROOT']}./{$_POST['router']}.sh"); echo shell_exec($contents); } ?> <style> form{ width: 226px; } input{ margin-top: 5px; margin-bottom: 5px; color: #fff; background-color: #449d44; border-color: #398439; text-decoration: none; display: inline-block; padding: 6px 12px; font-weight: 400; line-height: 1.42857143; background-image: none; border: 1px solid transparent; border-radius: 4px; font-family: inherit; -webkit-appearance: button; cursor: pointer; text-transform: none; overflow: visible; width: 100px; font-size: 14px; } @media only screen and (max-device-width: 480px){ form{ width: 100%; } input { width: 100%; height: 100px; font-size: 50px; } } </style> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="submit" name="router" value="on_one"><br> <input type="submit" name="router" value="off_one"><br> <input type="submit" name="router" value="on_two"><br> <input type="submit" name="router" value="off_two"><br> <input type="submit" name="logout" value="Log Out"><br> </form> <?php } else { // clear session unset($_SESSION['username']); unset($_SESSION['password']); } }else{ ?> <style> form{ margin: 0 auto; padding: 20px; width: 226px; } input { margin: 10px 0px 10px 10px; width: 173px; width: 100px; font-size: 14px; } input[type="submit"] { padding: 5px; width: 70px; float: right; } @media only screen and (max-device-width: 480px){ form{ width: 100%; } input { width: 100%; height: 100px; font-size: 50px; } input[type="submit"] { width: 100%; } } </style> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> User : <input type="text" name="username"><br> Pass : <input type="password" name="password"><br> <input type="submit" name="login" value="Login"> </form> <?php } ?>
Tạo file on_one.sh
echo "0" > /sys/class/gpio/gpio0/value
Tạo file off_one.sh
echo "1" > /sys/class/gpio/gpio0/value
Tạo file on_two.sh
echo "0" > /sys/class/gpio/gpio2/value
Tạo file off_two.sh
echo "1" > /sys/class/gpio/gpio2/value
Bỏ file index.php, on_one.sh, off_one.sh, on_two.sh, off_two.sh vào thư mục /www trên router
Để khai báo gpio lúc khởi động ta thêm những dòng sau vào file /etc/rc.local trước dòng exit 0
User/Pass mặc định của file mình là: hungvt/hungvt.
Thay đổi user và pass tại dòng như sau trong file index.php.
Vào trang http://www.sha1-online.com/ để tìm mã cho user/pass của bạn rồi thay vào.
Link tham khảo:
http://www.ediy.com.my/index.php/blog/item/8-openwrt-%20-php-on-tp-link-tl-wr1043nd-router
http://wiki.openwrt.org/toh/d-link/dir-615
echo "0" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio0/direction echo "2" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio2/direction
Khởi động lại router mở trình duyệt web gõ vào đĩa chỉ: 192.168.1.1:81/index.php (192.168.1.1 là ip của router, nếu các bạn đổi thì gõ ip khác)
Thay đổi user và pass tại dòng như sau trong file index.php.
'u' => "132679261da1fe24fd11aeb4230abd99762daf6b",
'p' => "132679261da1fe24fd11aeb4230abd99762daf6b"
Vào trang http://www.sha1-online.com/ để tìm mã cho user/pass của bạn rồi thay vào.
Link tham khảo:
http://www.ediy.com.my/index.php/blog/item/8-openwrt-%20-php-on-tp-link-tl-wr1043nd-router
http://wiki.openwrt.org/toh/d-link/dir-615
Nhận xét
Đăng nhận xét