看我如何用树莓派做“魔镜”

2017-03-27 24,153

本文《看我如何用树莓派做“魔镜”》 由一叶知安团队原创投稿安全脉搏首发,作者Catsay,安全脉搏独家首发表本文,如需要转载,请先联系安全脉搏授权;未经授权请勿转载。

一、材料

树莓派、原子镜、显示器(显示屏+对应的驱动板)、HDMI线(HDMI转VGA)、8G及以上容量内存卡

安装系统不用多说了吧,百度都有,用的是树莓派官方的Raspbian系统

Dht11温度传感器

 

二、使树莓派显示中文

1.解锁root账户

sudo passwd root

执行此命令后系统会提示输入两遍的root密码,输入你想设的密码即可,然后再执行

sudo passwd –unlock root

这样就可以解锁root账户了

以下教程所提到的命令如遇到权限不足问题,请使用root账户执行,方法:

输入:su回车

再输入你设置的root密码回车即可(输入密码时密码为不可见)

2.更新软件包(过程会很慢 耐心等待):

sudo apt-get update&& apt-get upgrade -y

3.安装字体

sudo apt-get install ttf-wqy-zenhei

4.刷新字库缓存

sudo fc-cache

5.字体配置

sudo dpkg-reconfigure locales

6.用上下键和空格键选择下列内容然后回车

en-GB.UTF-8 UTF-8

zh_CN.GB2312

zh.CN.GB18030GB18030

zh_CN.GBK GBK

zh_CN.UTF-8 UTF-8

在出现的菜单里选择zh_CN.UTF-8 UTF-8,回车确定

7.安装中文拼音输入法

sudo apt-get install scim-pinyin

8.设置时间

sudo dpkg-reconfigure tzdata

先选择Asia,回车,再选择ShangHai,回车

9.重启

sudo reboot

三、屏幕设置

如果你想让魔镜为纵向模式,所以需要将屏幕顺时针旋转90度。

树莓派的BIOS设置储存在系统启动分区中。在这分区中,有一个config.txt文件,载有所有设置。要旋转显示器,在这文件内加上以下两行代码:

1.屏幕旋转90°

display_rotate=1

2.允许HDMI支持热插拔

hdmi_force_hotplug=1

四、配置服务器

1.安装Apachce

sudo apt-get install apache2

sudo apt-get install apache2-doc

sudo apt-get install apache2-utils

2.PHP

sudo apt-get install php5

sudo apt-get install libapache2-mod-php5 php5-xcache

3.建立一个PHP网页

sudo nano /var/www/html/index.php

输入以下内容

<html>

<?php

Phpinfo();

?>

</html>

保存后在浏览器中输入127.0.0.1并回车,出现以下界面即为成功

(如果是apache页面请用以下命令删除/var/www/html目录下的index.html文件)

rm -rf /var/www/html/index.html

r1

五、Chromium的开机自启及全屏运行并指向特定页面

1.在/home/pi/.config目录里创建一个autostart文件夹

mkdir /home/pi/.config/autostart/

2.在/home/pi/.config/autostart目录里创建并编辑my.desktop文件

nano /home/pi/.config/autostart/my.desktop

3.输入以下内容

[Desktop Entry]

Type=Application

Exec=chromium-browser –disable-popup-blocking –no-first-run –disable-desktop-notifications –kiosk

nano编辑器Ctrl+O键是写入文件(保存文件)会提到是否保存,请按回车键

Ctrl+X退出编辑器

4.在Chromium中设置开启浏览器是打开指定页为:http://localhost

5.鼠标不移动时自动隐藏光标

sudo apt-get install x11-xserver-utils unclutter

(本文《看我如何用树莓派做“魔镜”》由作者Catsay原创投稿安全脉搏首发,安全脉搏首发表本文,如需要转载,请先联系安全脉搏授权;未经授权请勿转载。https://www.secpulse.com/archives/57106.html)

六、网页安装:

这是我使用的源码

源码来自:

https://github.com/HelloWk/MagicMirror

七、使用FTP传输文件

安装FTP工具

sudo apt-get install vsftpd

安装完成后启动FTP服务:

sudo service vsftpd start

编辑vsftdp的配置文件

sudo nano /etc/vsftpd.conf

全部删除后输入以下配置:

 

listen=NO

listen_ipv6=YES

anonymous_enable=NO

local_enable=YES

write_enable=YES

local_umask=000

dirmessage_enable=YES

use_localtime=YES

connect_from_port_20=YES

secure_chroot_dir=/var/run/vsftpd/empty

pam_service_name=vsftpd

rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem

rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

ssl_enable=NO

local_root=/var/www/html

保存后执行以下命令(设置html文件夹权限为777不然FTP工具上传不了)

sudo chmod -R 777 /var/www/html

FTP地址就是你的树莓派ip地址

用户名是你树莓派登陆用户名

密码是你树莓派登陆密码

端口是默认端口:21

把下载好的源码解压后MagicMirror-master文件夹下所有文件用FTP工具上传

浏览器打开http://localhost看看效果

同局域网内的设备访问你的树莓派ip也可访问

八、魔镜配置文件:

设置天气://注意,注册时需要翻墙 登录不需要

首先我们需要到http://www.openweathermap.org去注册一个免费的账号,会得到一个API key,然后我们把这个key写在js/config.js这个文件的weather: {}的APPID中,同时把参数q:后面改成你自己的城市(提前在网页测试),lang的值改成zh_cn,

具体位置如下所示

 r2

九、新闻订阅

订阅只需取得订阅源的地址然后写在js/config.js这个文件的news: {}中即可。

r3

中文的问候语

 r4

 

十、温度传感器

————————————-使用dht11传感器———————————–

1.新增文件:
controllers/thsensor.php
内容:

<?php
$myfile = fopen(“../th/wth.th”, “r”) or die(“Unable to open file!”);
echo fread($myfile,filesize(“../th/wth.th”));
fclose($myfile);
?>

2.dht11传感器控制端接树莓派第11针:
01 —- 3.3v   | 5v   —- 02
03 —-           | 5v   —- 04
。。。。。。。。。。。。。。
11 —- GPIO0|GPIO1 —- 12

3.新增文件
py/dht11.py
内容:

#!/usr/bin/python

import RPi.GPIO as GPIO
import time

channel = 17

while True:
data = []
j = 0

GPIO.setmode(GPIO.BCM)

time.sleep(1)

GPIO.setup(channel, GPIO.OUT)

GPIO.output(channel, GPIO.LOW)
time.sleep(0.02)
GPIO.output(channel, GPIO.HIGH)

GPIO.setup(channel, GPIO.IN)

while GPIO.input(channel) == GPIO.LOW:
continue

while GPIO.input(channel) == GPIO.HIGH:
continue

while j < 40:
k = 0
while GPIO.input(channel) == GPIO.LOW:
continue

while GPIO.input(channel) == GPIO.HIGH:
k += 1
if k > 100:
break

if k < 8:
data.append(0)
else:
data.append(1)

j += 1

print “sensor is working.”
print data

humidity_bit = data[0:8]
humidity_point_bit = data[8:16]
temperature_bit = data[16:24]
temperature_point_bit = data[24:32]
check_bit = data[32:40]

humidity = 0
humidity_point = 0
temperature = 0
temperature_point = 0
check = 0

for i in range(8):
humidity += humidity_bit[i] * 2 ** (7 – i)
humidity_point += humidity_point_bit[i] * 2 ** (7 – i)
temperature += temperature_bit[i] * 2 ** (7 – i)
temperature_point += temperature_point_bit[i] * 2 ** (7 – i)
check += check_bit[i] * 2 ** (7 – i)

tmp = humidity + humidity_point + temperature + temperature_point

if check == tmp:
f = file(“/var/www/html/th/wth.th”,”w+”)
li = [‘{“temperature”:%s,”humidity”:%s}’ % (temperature, humidity) ]
f.writelines(li)
f.close()

time.sleep(10)

del data[:]
j = 0

#print “temperature : “, temperature, “, humidity : ” , humidity
#else:
#print “wrong”
#print “temperature : “, temperature, “, humidity : ” , humidity, ” check : “, check, ” tmp : “, tmp

GPIO.cleanup()

4.新增文件
js/weather/tem_hum.js 编辑后保存为UTF-8编码
内容:

var tem_hum = {
dl: config.displayLanguage || ‘en’,
temperatureLocation: ‘.temhum’,
updateInterval: 10000,

}

tem_hum.updateTemHum = function () {
$.ajax({
type: ‘GET’,
datatype:’jsonp’,
url: ‘controllers/thsensor.php’,
success: function (data) {
// console.info(data);
var ths = JSON.parse(data);

if (tem_hum.dl == ‘cn’) {
// var thtext = ‘室内温湿 ‘ + ths.temperature + ‘°, ‘ + ths.humidity + ‘%’;
var thtext1 = ‘<div>’ + ths.temperature + ‘°</div><div class=”xxsmall”>室温</div>’
var thtext2 = ‘<div>’ + ths.humidity    + ‘%</div><div class=”xxsmall”>湿度</div>’
var thtext  = ‘<div><span class=”th”>’+thtext1+'</span> <span class=”th” style=”width: 20;”></span> <span class=”th”>’+thtext2+'</span></div>’
} else {
var thtext = ‘温度:’ + ths.temperature + ‘℃, 湿度:’ + ths.humidity + ‘%’;
}

$(this.temperatureLocation).updateWithText(thtext, this.fadeInterval);
}.bind(this),
error: function () {
// non-specific error message that should be updated
console.error(‘No thsensor results’);
}
});

}

tem_hum.init = function () {

this.updateTemHum();

this.intervalId = setInterval(function () {
this.updateTemHum();
}.bind(this), this.updateInterval)

}

5.新增文件:
th/wth.th

{“temperature”:18,”humidity”:50}

6.修改:
index.php
最下方<script src=”” type=””></script>处,最后面添加:

<script src=”js/weather/tem_hum.js” type=”text/javascript”></script>

7.脚本开机自启

①在/home/pi/.config目录里创建一个autostart文件夹(如果提示文件夹已存在请直接下一步)
mkdir /home/pi/.config/autostart/

②在/home/pi/.config/autostart目录里创建并编辑dht11.desktop文件
nano /home/pi/.config/autostart/dht11.desktop

③输入以下内容

[Desktop Entry]
Name=dht11
Comment=My Python dht11
Exec=python /var/www/html/py/dht11.py
Terminal=false
MultipleArgs=false
Type=Application
Categories=Application;Development;
StartupNotify=true

  • 输入reboot重启

注意:

Gpio接口如图:

r5

r6

 

 

————————————最后————————————————-

组装....

 

木条+钉子+4个铁片

r7

r8

做好后如图:

 r9

 本文《看我如何用树莓派做“魔镜”》由作者Catsay原创投稿安全脉搏首发,安全脉搏首发表本文,如需要转载,请先联系安全脉搏授权;未经授权请勿转载。

本文作者:一叶知安

本文为安全脉搏专栏作者发布,转载请注明:https://www.secpulse.com/archives/57106.html

Tags:
评论  (1)
快来写下你的想法吧!

一叶知安

文章数:14 积分: 49

一叶知安致力于分享渗透测试中的各种实战技巧,以及其他好玩有意思的黑客技术。“自由”、“分享”是一叶从始至终的宗旨。

安全问答社区

安全问答社区

脉搏官方公众号

脉搏公众号