续前面魔镜项目,地址:http://www.jianshu.com/p/696cb979b62a
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":26,"humidity":70}
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
8.输入reboot重启
网友评论