● DHT11温湿度传感器
● 显示器
七英寸显示器

如果没有上述显示器也可以使用电视等显示器。
● 树莓派
1.2 本文所需的软件
●Python3环境
●RPi.GPIO库
●Adafruit DHT11库
二、树莓派实现方法2.1 硬件接线方法
DHT11传感器接线方法
我们采用Board的树莓派引脚编号方法,分别将DHT11传感器的正极接到1号引脚,负极接到9号引脚,输出接到7号引脚。
2.2 程序代码说明
#!/usr/bin/env python# encoding: utf-8
说明:#!/usr/bin/envpython表示调用系统环境变量中Python解释器,# encoding: utf-8表示采用utf-8的格式对代码进行编码。
import turtlefrom datetime import import stringimport socketfrom xml.dom.minidom import import timeimport Adafruit_DHTimport RPi.GPIO as gpioimport fcntlimport structfrom urllib.parse import quoteimport urllib.request
说明:加入程序所需的python包
def get_weather(): page_url =\"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName=北京\" url_quote =quote(page_url, safe=string.printable) page = urllib.request.urlopen(url_quote) lines = page.readlines() page.close() document = \"\" for line in lines : document = document +line.decode('utf-8') dom = parseString(document) strings = dom.getElementsByTagName(\"string\") weather_info = strings[10].childNodes[0].data return weather_info[0:33]
说明:get_weather()函数功能是从互联网获取北京当天的天气信息,并保存在weather_info字符串中,由于所获取的天气信息过多,这里仅仅选取部分信息作为函数的返回值。
def print_weather_info(): printer = turtle.Turtle() printer.penup() printer.pencolor('white') weather_info = get_weather() printer.backward(90) printer.write(weather_info, align=\"center\", font=(\"Courier\", 14,\"bold\")) printer.forward(90)return None
说明:print_weather_info()函数功能是单独显示天气信息。由于我们获取天气信息的网站规定每天免费查询的次数是有限的,同时后续时间信息的显示需要不断的更新数据,因此本程序采用单独显示天气信息的方法。
def get_ipaddr(name): s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)return socket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915, struct.pack('256s', name[:15].encode('utf-8')))[20:24])
说明:get_ipaddr()函数功能是获取指定网卡的ip地址,即函数的参数可以是wlan0、eth0或lo等。
def get_time(t): second = t.second + t.microsecond 0.000001 minute = t.minute +second / 60.0 hour = t.hour +minute / 60.0return \"%d点%d分%d秒\" % (hour, minute,second)
说明:get_time()函数功能是获取当前时刻,并以几点几分几秒的字符串形式进行返回。函数的参数是函数datetime.today()函数的返回值。
def get_week(t): week = [\"星期一\", \"星期二\", \"星期三\",\"星期四\", \"星期五\", \"星期六\", \"星期日\"]return week[t.weekday()]
说明:get_week()函数功能是获取当前时间的星期信息。函数的参数是函数datetime.today()函数的返回值。
def get_date(t): y = t.year m = t.month d = t.dayreturn \"%s年%d月%d日\" % (y, m, d)
说明:get_date()函数功能是获取当前时间的年月日信息。函数的参数是函数datetime.today()函数的返回值。
def sensor(): gpio.setmode(gpio.BCM) sensor = Adafruit_DHT.DHT11 pin_sensor = 4 gpio.setup(pin_sensor,gpio.IN) humidity, temperature =Adafruit_DHT.read_retry(sensor, pin_sensor)return \"室内温度:{0:0.1f}C 室内湿度:{1:0.1f}%\".format(temperature,humidity)
说明:sensor()函数功能是获取DHT11温湿度传感器测量信息,并以字符串的形式返回测量结果。
def screen_printer(): printer = turtle.Turtle() printer.hideturtle() printer.pencolor('white') printer.penup() t = datetime.today() printer.forward(60) printer.write(get_date(t), align=\"center\", font=(\"Courier\", 14,\"bold\")) printer.backward(60) printer.forward(30) printer.write(get_week(t), align=\"center\", font=(\"Courier\", 14,\"bold\")) printer.backward(30) printer.write(get_time(t), align=\"center\", font=(\"Courier\", 14,\"bold\")) printer.backward(30) printer.write(get_ipaddr('wlan0'), align=\"center\", font=(\"Courier\",14, \"bold\")) printer.backward(30) printer.write(sensor(), align=\"center\", font=(\"Courier\", 14, \"bold\")) printer.clear()return None
说明:screen_printer()函数功能是显示时间与温湿度传感器信息。
def perform(): while(1): screen_printer() time.sleep(1)return None
说明:perform()函数的功能是更新当前屏幕所显示的信息。
def setup(): turtle.mode(\"logo\") turtle.tracer(False) turtle.hideturtle() turtle.bgcolor('black')return None
说明:setup()函数的功能是设置屏幕显示配置,包括屏幕朝向与屏幕背景颜色等。
def main(): setup() print_weather_info() perform() turtle.mainloop()return None
说明:main()函数功能是规定了上述函数的执行流程。
if __name__ == \"__main__\": main()
2.3 实现效果
将上述代码保存为smart_clock.py文件,并在终端输入sudo python3 smart_clock.py命令运行上述代码,可看到如图6所示界面。注意,这里需要使用python3命令来运行,否则会出现python2与python3不兼容的问题。