项目目的:监测香菇大棚内温湿度变化,请求近3天的天气数据,并将数据反馈给用户,以增强用户对香菇生长的管理
项目思路:用树莓派ZERO+DHT22收集温湿度数据,并用NGINX搭建WEB服务,让用户使用手机浏览器了解温湿度信息.期间考虑过使用LED点阵显示,但考虑到LED点阵安装位置固定,不方便查看,手机随身携带比较方便,WEB服务无须安装其他应用.
项目硬件:树莓派ZERO,DHT22,乐光CPE,斐讯K3,小米3A,监控录像机,监控摄像头,网线若干
硬件连接:
项目软件:python,nginx,html,和风天气API
软件思路:使用python加载DHT库读取DHT22温湿度数据,使用python的openpyxl库将温湿度数据以天为单位保存成.xlsx文件.使用和风天气API,获取近3天的天气数据,最后将温湿度数据及天气数据保存到index.html.编写python自运行shell脚本,防止python意外停止,并将shell设置开机自启.调整nginx配置文件,并加入开机自启.将树莓派ZERO设成静态IP地址,防止访问出现IO变动的问题.
项目代码:
greenhouse.sh
#!/bin/bash
while [ 1 ];do
python3 /home/pi/greenhouse/greenhousedetection.py
done
greenhousedetection.py
# -*- coding: utf-8 -*-
import Adafruit_DHT as dht
import datetime
import requests
import time
import os
from openpyxl import Workbook
# 传感器型号
sensor = dht.DHT22
# 树莓派GPIO BCM引脚号
pin1 = 5
pin2 = 6
pin3 = 13
pin4 = 19
# xlsx文件初始化
wb = Workbook() #创建文件对象
ws = wb.active #获取第一个sheet
# xlsx文件标头
ws['A1'] = "时间"
ws['B1'] = "室外标准温度"
ws['C1'] = "室外标准湿度"
ws['D1'] = "大棚北部温度"
ws['E1'] = "大棚北部湿度"
ws['F1'] = "大棚中部温度"
ws['G1'] = "大棚中部湿度"
ws['H1'] = "大棚南部温度"
ws['I1'] = "大棚南部湿度"
# xlsx偏移参数
i = 1
# 标准时间获取
time1 = time.strftime("%Y%m%d",time.localtime())
# 网页内容写入
def xieru():
f=open('/home/pi/greenhouse/html/index.html','w')
f.write('<!DOCTYPE html>'+'\n'
+'<html><head><meta charset="utf-8"><title></title></head>'+'\n'
+'<body>'+'\n'
+'<p style="text-align:center">'+'\n'
+'<font size="26" face="Time" color="orange">庆宝香菇大棚温湿度显示</font>'+'\n'
+'</p>'+'\n'
+'<p><font size="26" face="Time">室外标准温湿度</font></p>'+'\n'
+'<p><font size="26" face="Time">' + '温度:' + str(wendu1) + ' ' + '湿度:' + str(shidu1) + '</font></p>' + '\n'
+'<p><font size="26" face="Time">大棚北部温湿度</font></p>' + '\n'
+'<p><font size="26" face="Time">' + '温度:' + str(wendu2) + ' ' + '湿度:' + str(shidu2) + '</font></p>' + '\n'
+'<p><font size="26" face="Time">大棚中部温湿度</font></p>' + '\n'
+'<p><font size="26" face="Time">' + '温度:' + str(wendu3) + ' ' + '湿度:' + str(shidu3) + '</font></p>' + '\n'
+'<p><font size="26" face="Time">大棚南部温湿度</font></p>' + '\n'
+'<p><font size="26" face="Time">' + '温度:' + str(wendu4) + ' ' + '湿度:' + str(shidu4) + '</font></p>' + '\n'
+ '<p><font size="26" face="Time">今天天气' + str(date1) + '</font></p>' + '\n'
+ '<p><font size="8" face="Time">白天天气:' + str(cond_d1) + ' ' + '夜晚天气:' + str(
cond_n1) + ' ' + '最高温度:' + str(tmp_max1) + ' ' + '最低温度:' + str(
tmp_min1) + ' ' + '相对湿度:' + str(hum1) + ' ' + '能见度:' + str(
vis1) + '公里' + ' ' + '风向:' + str(wind_dir1) + ' ' + '风力:' + str(
wind_sc1) + '</font></p>' + '\n'
+ '<p><font size="26" face="Time">明天天气' + str(date2) + '</font></p>' + '\n'
+ '<p><font size="8" face="Time">白天天气:' + str(cond_d2) + ' ' + '夜晚天气:' + str(
cond_n2) + ' ' + '最高温度:' + str(tmp_max2) + ' ' + '最低温度:' + str(
tmp_min2) + ' ' + '相对湿度:' + str(hum2) + ' ' + '能见度:' + str(
vis2) + '公里' + ' ' + '风向:' + str(wind_dir2) + ' ' + '风力:' + str(
wind_sc2) + '</font></p>' + '\n'
+ '<p><font size="26" face="Time">后天天气' + str(date3) + '</font></p>' + '\n'
+ '<p><font size="8" face="Time">白天天气:' + str(cond_d3) + ' ' + '夜晚天气:' + str(
cond_n3) + ' ' + '最高温度:' + str(tmp_max3) + ' ' + '最低温度:' + str(
tmp_min3) + ' ' + '相对湿度:' + str(hum3) + ' ' + '能见度:' + str(
vis3) + '公里' + ' ' + '风向:' + str(wind_dir3) + ' ' + '风力:' + str(
wind_sc3) + '</font></p>' + '\n'
+'<meta http-equiv="Refresh" content="90";/>'+'\n'
+'</body>'+'\n'
+'</html>')
f.close()
# 主进程
while True:
i = i+1
r = requests.get(
'https://free-api.heweather.net/s6/weather/forecast?location=biyang&key=')
r.encoding = 'utf-8'
date1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['date']
cond_d1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['cond_txt_d']
cond_n1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['cond_txt_n']
tmp_max1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['tmp_max']
tmp_min1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['tmp_min']
hum1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['hum']
vis1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['vis']
wind_dir1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['wind_dir']
wind_sc1 = r.json()['HeWeather6'][0]['daily_forecast'][0]['wind_sc']
# print(cond_d1, cond_n1, tmp_max1, tmp_min1, hum1, vis1, wind_dir1, wind_sc1)
date2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['date']
cond_d2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['cond_txt_d']
cond_n2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['cond_txt_n']
tmp_max2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['tmp_max']
tmp_min2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['tmp_min']
hum2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['hum']
vis2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['vis']
wind_dir2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['wind_dir']
wind_sc2 = r.json()['HeWeather6'][0]['daily_forecast'][1]['wind_sc']
# print(cond_d2, cond_n2, tmp_max2, tmp_min2, hum2, vis2, wind_dir2, wind_sc2)
date3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['date']
cond_d3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['cond_txt_d']
cond_n3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['cond_txt_n']
tmp_max3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['tmp_max']
tmp_min3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['tmp_min']
hum3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['hum']
vis3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['vis']
wind_dir3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['wind_dir']
wind_sc3 = r.json()['HeWeather6'][0]['daily_forecast'][2]['wind_sc']
# print(cond_d3, cond_n3, tmp_max3, tmp_min3, hum3, vis3, wind_dir3, wind_sc3)
time2 = time.strftime("%Y%m%d",time.localtime())
shidu1,wendu1 = dht.read_retry(sensor,pin1)
shidu2,wendu2 = dht.read_retry(sensor,pin3)
shidu3,wendu3 = dht.read_retry(sensor,pin3)
shidu4,wendu4 = dht.read_retry(sensor,pin4)
if time2 > time1:
name = time2
time1 = time2
i = 2
# 写入一个自定义的时间格式
ws['A' + str(i)] = time.strftime("%Y%m%d%H%M", time.localtime())
#print("日期更新")
if shidu1 is not None and wendu1 is not None:
wendu1 = round(wendu1,3)
shidu1 = round(shidu1,3)
wendu2 = round(wendu2,3)
shidu2 = round(shidu2,3)
wendu3 = round(wendu3,3)
shidu3 = round(shidu3,3)
wendu4 = round(wendu4,3)
shidu4 = round(shidu4,3)
ws['B' + str(i)] = wendu1
ws['C' + str(i)] = shidu1
ws['D' + str(i)] = wendu2
ws['E' + str(i)] = shidu2
ws['F' + str(i)] = wendu3
ws['G' + str(i)] = shidu3
ws['H' + str(i)] = wendu4
ws['I' + str(i)] = shidu4
xieru()
else:
ws['J' + str(i)] = "传感器异常"
else:
name = time1
ws['A' + str(i)] = time.strftime("%Y%m%d%H%M", time.localtime())
#print("日期更新")
if shidu1 is not None and wendu1 is not None:
wendu1 = round(wendu1, 3)
shidu1 = round(shidu1, 3)
wendu2 = round(wendu2, 3)
shidu2 = round(shidu2, 3)
wendu3 = round(wendu3, 3)
shidu3 = round(shidu3, 3)
wendu4 = round(wendu4, 3)
shidu4 = round(shidu4, 3)
ws['B' + str(i)] = wendu1
ws['C' + str(i)] = shidu1
ws['D' + str(i)] = wendu2
ws['E' + str(i)] = shidu2
ws['F' + str(i)] = wendu3
ws['G' + str(i)] = shidu3
ws['H' + str(i)] = wendu4
ws['I' + str(i)] = shidu4
xieru()
else:
ws['D' + str(i)] = "传感器异常"
# Save the file
wb.save("/home/pi/greenhouse/datexlsx/"+str(name)+".xlsx")
time.sleep(90)
最终效果:
来源:51CTO
作者:xhlieren
链接:https://blog.51cto.com/14689689/2470583