# 通过手机来控制家电

- URL: https://wuming.si/blog/194/
- Author: Aaron
- Published: 2013-02-02
- Language: zh-Hans
- Categories: Progaming, Raspberry Pi, Technology
- Originally published at: https://blog.wo.ai/archives/194

心血来潮想通过树莓派对家里的电器进行控制，网上查了不少资料，决定动手一试。首先决定对电暖器开刀，这个电脑有一个红外的遥控器，可以开关、定时、设定高低档功能，我要做的就是模拟遥控器对其发出指令。初级篇，通过电脑命令行进行控制，高级篇使用手机进行控制。

参照baoxiaozhong的红外接收http://www.shumeipai.net/thread-12265-1-1.html 设置好了红外接收器，然后尝试用相似的方法实现了红外发射器，成功控制电暖器，后续将考虑控制电视，空调等 设备。

初级篇：用电脑来控制电暖器

原理：

先通过红外接收模块，采集遥控器的指令，然后通过红外发射器模块模拟遥控器发送红外指令控制。

环境：xbian 1.0 alpha4

硬件：taobao上淘的10多块一个的红外接收模块及红外接收模块，此处不发地址，免得广告嫌疑。

连接方式：

红外接收器  
vcc 连 pin1 (3.3v)  
gnd 连 pin6(ground)  
data连 pin12(gpio18)  
红外发射器  
vcc 连pin2(5v)  
gnd连pin6(ground)  
data连pin11(gpio17)

为什么这么连，我查了很久资料，后来在lirc_rpi原作者网站看到了，大概固定在程序里面了。  
地址：http://aron.ws/projects/lirc_rpi/

步骤：  
先通过红外接收器接收遥控器的编码，请参照http://www.shumeipai.net/thread-12265-1-1.html.  
在用户目录生成了lircd.conf文件

我生成了KEY_UP,KEY_DOWN,KEY_0,KEY_1等分别代替开启、定时、低、高四个按钮

然后将它拷贝到/etc/lirc/remotes下，  
sudo cp /home/xbian/lircd.conf /etc/lirc/remotes/xxx.conf （xxx随便你起名字，但是要和后面统一）  
sudo vi xxx.conf  
修改 name /home/xbian/lircd.conf 为 name xxx

sudo /etc/lirc/lircd.conf  
增加一行

include “/etc/lirc/remotes/xxx.conf”

保存退出  
现在可以测试了  
sudo irsend SEND_ONCE xxx KEY_ON  
sudo irsend SEND_ONCE xxx KEY_TIMER  
sudo irsend SEND_ONCE xxx KEY_LOW  
sudo irsend SEND_ONCE xxx KEY_HIGH

开始享受电暖器滴滴接收命令的的声音吧。

## 高级篇：通过手机来控制电暖器

安装LNMP  
参照http://geekpi.cn/blog/2012/10/1908

apt-get install mysql-server mysql-client nginx php5 php5-fpm php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps  
php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

mysql我没有做优化

配置nginx  
先配置/etc/nginx/nginx.conf，需要做的就是限定线程数和开启gzip支持，gzip开启后将大幅降低传输html的流量占用。

worker_processes 1; #只有一个CPU，单进程即可

worker_connections 256; #一般支持100在线连接就达到raspberry pi的极限了

gzip on; #gzip开启取消前面的#让默认设置生效即可  
gzip_disable “msie6”;

gzip_vary on;  
gzip_proxied any;  
gzip_comp_level 6;  
gzip_buffers 16 8k;  
gzip_http_version 1.1;  
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

接下来设置/etc/nginx/sites-avaliable/default ，配置站点属性

root /var/www ; #站点存储位置

index index.php index.html index.htm; #加入index.php为默认页面

location ~ \\.php$ {  
fastcgi_split_path_info ^(.+\\.php)(/.+)$;

fastcgi_pass unix:/var/run/php5-fpm.sock;  
fastcgi_index index.php;  
include fastcgi_params;  
}

#开启php解析，使用php-fpm作为Fastcgi通信模块

然后配置php sudo权限，这样php执行命令的时候不需要输入自己的密码  
为 nginx，而想透过 PHP 发送irsend，用 root 输入指令 vi /etc/sudoers，  
在档案最底加上以下一行:

www-data ALL=NOPASSWD: /usr/local/bin/irsend

在/usr/share/nginx/www/下 编写cmd.php

<?php  
$cmd = $_POST[‘cmd’];  
if(!$cmd){  
$cmd = $_GET[‘cmd’];  
}  
$device = $_POST[‘device’];  
if(!$device){  
$device = $_GET[‘device’];  
}  
if($cmd&$device){  
$cmdline = “/usr/bin/sudo /usr/local/bin/irsend SEND_ONCE “.$device.” “.$cmd ;  
echo($cmdline);  
$output = shell_exec($cmdline);  
echo($output);  
}  
?>

然后就可以通过http://ip地址/cmd.php?cmd=KEY_UP&device=xxx控制你的设备了。  
这里的xxx是可以不同设备的哦。看你前面irrecord里面配置的

当然这种方式操作不方便，和shell操作差不多，所以可以自己做一个html页面，放上若干按钮，每个按钮分别调用cmd.php上面的指令。

这是个简单的例子

<html xmlns=”http://www.w3.org/1999/xhtml”>  
<head>  
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />  
<meta name=”apple-mobile-web-app-capable” content=”yes”>  
<title>智能家庭控制台</title>  
<script type=”text/javascript” src=”js/jquary-1.7.1.js”></script>  
<script type=”text/javascript”>  
var xmlHttp;  
//创建XMLHttpRequest对象  
function createXMLHttpRequest(){  
if (window.ActiveXObject){  
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);  
}else if (window.XMLHttpRequest){  
xmlHttp=new XMLHttpRequest();  
}  
}  
function checkMe(url){  
createXMLHttpRequest();  
xmlHttp.open(“post”,url,true);  
xmlHttp.onreadystatechange=send; //用send函数进行发数  
xmlHttp.setRequestHeader(“Content-Type”,”application/x-www-form-urlencoded;”);  
xmlHttp.send(null);  
}  
function send(){  
//alert(“指令成功发送！”);  
}  
</script>  
</head>

<body>

<div id=”content”>  
<div id=”menu_mode”>  
<ul>  
<li><input type=”button” onclick=”checkMe(‘cmd.php?cmd=KEY_ON&device=heater’)” value=”开/关”/></li>  
<li><input type=”button” onclick=”checkMe(‘cmd.php?cmd=KEY_LOW&device=heater’)” value=”低档”/></li>  
<li><input type=”button” onclick=”checkMe(‘cmd.php?cmd=KEY_HIGH&device=heater’)” value=”高档”/></li>  
<li><input type=”button” onclick=”checkMe(‘cmd.php?cmd=KEY_HIGH&device=heater’)” value=”定时”/></li>  
</ul>  
</div>  
</div>  
</body>  
</html>

然后通过http://ip地址/index.html访问

对于使用iphone或者ipad的用户，还可以把网页快捷方式放到主屏幕，打开就是一个操作界面，是不是很来福啊？
