树莓派_天气闹钟

1、播放mp3

omxplayer xxx.mp3
#测试一下

2、配置运行环境:使用的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就是正确的,
sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3

3、控制LED灯vim LEDShining.py

# -*- 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()

4、获取天气并用百度语音转mp3文件vim wulala.py

# -*- coding: utf-8 -*-
from aip import AipSpeech
import requests
import re
from bs4 import BeautifulSoup
import time

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
        
###百度语音合成-开发文档https://ai.baidu.com/tech/speech     
##用百度的AIP
##把文字变成mp3文件
def stringToMp3(strings_txt):
    strings_txt = '起床啦!今天是' + strings_txt
    APPID = '*******************'
    APIKey = '**************'
    SecretKey = '***********'
 
    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/weather1d/101180201.shtml'
    html=getHtmlText(url)
    stringToMp3(makeSoup(html))
 
if __name__ == '__main__':
    main()

最后设置定时运行
定时

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 * * * omxplayer /home/pi/naozhong/tmp.mp3`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值