
是时候关掉你的手机闹铃了,用树莓派外接一个音箱就可以 Make 一款科技感和实用性兼备的“AI 闹钟”。这里用到了 Linux 的计划任务、百度语音 API、天气 API。
不看天气的死宅(不爱带包),出门遇到下雨天就尴尬了,在学校还好可以蹭伞,到外面就不行了。
一开始的解决办法就是将爬到的天气信息发到微信上GETWeaInfo
结果有个很大的问题就是,网络要是断了,或者程序不运行了,就要重新载终端上扫二维码登录(太麻烦了)。
于是乎~就有了下面的想法——用树莓派做个天气闹钟。下面开始了!
播放mp3
前提:树莓派安装系统(我装的是最小化的Raspbian jessie)),ssh连接,wifi连接How to set up WiFi..网上都有…音响(基本都可以,没有就用耳机先代替),LED。
sudo apt-get update sudo apt-get install mplayer #更新源 #安装mplayer(用他来播放MP3) mplayer xxx.mp3 #测试一下
如果有声ok,没有参考林佳楠的博客
配置运行环境
前提:用的python3
安装python3
sudo apt-get install build-essential libsqlite3-dev sqlite3 bzip2 libbz2-dev libssl-dev openssl libgdbm-dev liblzma-dev libreadline-dev libncursesw5-dev #安装依赖包 wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz #下载源码包,你也可以换其他的 tar zxf Python-3.6.1.tgz #解压 cd Python-3.6.1/ ./configure --prefix=/usr/loacl/python3 make sudo make install echo $? #如果是0就是正确的,非零仔细看报错信息去百度google(我没有遇到,不好解答) sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3
安装virtualenv
sudo pip install virtualenv mkdir naozhong cd naozhong virtualenv - p /usr/bin/python3 naoz #/usr/bin/python3 是你安装python3可执行路径(不知道就运行$ which pytho3), source naoz/bin/activate #前面会出现(dirname) #deactivate (退出)
virtulaenv的作用&&详细使用
安装需要用到的库
pip install requests
pip install beautifulsoup4
pip install baidu-aip
#安装失败,因为依赖的库pillow安装失败
#那就装一些pillow的依赖包
sudo apt-get install libtiff5-dev
libjpeg8-dev
zlib1g-dev
libfreetype6-dev
liblcms2-dev
libwebp-dev
tcl8.6-dev
tk8.6-dev
python-tk
#我试下来需要一个一个安装,
#能安装就安装,不能的就跳过。
#再次 pip install pillow&&pip install baidu-aip
pip install rpi.gpio
#遇到问题error:command 'arm-linux-gnueabihf-gcc' failed with exit status 1
sudo apt-get install python3-dev
sudo apt-get install libevent-dev
#再次pip install rpi.gpio
代码
灯闪亮
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
#init GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
def LED_shining():
try:
GPIO.output(11, 1)
time.sleep(0.5)
GPIO.output(11, 0)
time.sleep(0.5)
except:
print('')
def main():
for tmp_a in range(60):
LED_shining()
GPIO.cleanup()
if __name__ == '__main__':
main()
led正极接在11引脚上,负极我放在GND
获取天气并用百度语音转mp3文件
# -*- coding: utf-8 -*-
from aip import AipSpeech
import requests
import re
from bs4 import BeautifulSoup
import time
'''
爬取天气网-无锡
http://www.weather.com.cn/weather/101190201.shtml
'''
def getHtmlText(url,code='utf-8'):
try:
r = requests.get(url)
r.raise_for_status()
r.encoding = code
return r.text
except:
return ''
def makeSoup(html):
wstr = ''
if html == '':
return '哎呀~今天我也不知道无锡天气了'
else:
soup = BeautifulSoup(html,'html.parser')
soup1 = soup.find_all('li',attrs = {'class':'on'})[1]
str1 = re.findall(r'>(.*)</',str(soup1))
b = ''
try:
slist = re.findall(r'^(.*)</span>(.*)<i>(.*)$',str1[4])
for x in range(len(slist[0])):
b += slist[0][x]
except:
b = str1[4]
if '/' in b:
b = b.replace('/','-')
str1[4] = '无锡的温度是'+b
#print(str1[4])
str1[6] = '小风风是'+str1[6]
for i in str1:
if i != '':
wstr = wstr +i
if '雨' in wstr:
wstr += '今天别忘记带雨伞哦!'
#print(wstr)
return wstr
'''
用百度的AIP
把文字变成mp3文件
'''
def stringToMp3(strings_txt):
strings_txt = '起床呀~懒虫~起床啊~死肥宅~起床啦~要上班啦!今天是' + strings_txt
APPID = '9***3**8'
APIKey = 'QC*****UK*****nP***b'
SecretKey = 'e8***6******25*****56'
aipSpeech = AipSpeech(APPID,APIKey,SecretKey)
result = aipSpeech.synthesis(strings_txt,'zh','1',\
{'vol':8,
'per':4,
'spd':5})
if not isinstance(result,dict):
with open('test_tmp.mp3','wb') as f:
f.write(result)
'''
执行的主函数
'''
def main():
url = 'http://www.weather.com.cn/weather/101190201.shtml'
html=getHtmlText(url)
stringToMp3(makeSoup(html))
if __name__ == '__main__':
main()
去创建新应用,看开发文档,查看key,复制进去,就可以使用百度语音api了。
百度语音合成-开发文档
天气网选择你的城市,把main函数下的url改了,如果html结构都是一样的,那就都可以执行,如果不一样需要更改makeSoup函数了。
最后设置定时运行
首先看时区(中国的是CST)、时间对不对
时区不对:
sudo dpkg-reconfigure tzdata #选择亚洲-上海就可以了
时间不对:
sudo ntpd -s -d
定时
crontab -e
第一次运行需要指定您的编辑器(随意选)
在最后添加
50,53,55 7 * * * /home/pi/naozhong/naoz/bin/python3 /home/pi/naozhong/LEDShining.py 45 7 * * * /home/pi/naozhong/naoz/bin/python3 /home/pi/naozhong/wulala.py > /home/pi/naozhong/wulala.log 2>&1 50,53,55 7 * * * `mplayer /home/pi/naozhong/tmp.mp3`
前面的python3,就是virtualenv下的python
后面的文件也需要使用绝对路径
mpalyer命令有’`’这个符合不要忘记加上
本文来自:
http://shumeipai.nxez.com/2017/10/27/crontab-and-raspberry-pi-voice-alarm-clock.html
