警告沛厨,在看這篇文章之前,你得先去找一個女朋友摔认,哈哈哈逆皮,當然沒有的話,也是可以的参袱,而且你也得會操作系統(tǒng)的安裝电谣,懂些Linux秽梅,等你嘗試做出來了,說不定............(期待神評)剿牺,也需要一些樹莓派設備
軟硬件清單
讀卡器以及 SD 卡(裝系統(tǒng)用)
音箱一枚企垦,最好支持 3.5mm
SSH連接工具(SecureCRT,Xshell)
寬帶晒来、路由器(這應該是家中常備了)
裝好系統(tǒng)的樹莓派 3B+ 一只(充電器钞诡、CPU散熱風扇等)
樹莓派 3B+ 的系統(tǒng)默認預裝了 Python3 ,我們只需要安裝一些第三方依賴就可以潜索,以下便是主要代碼:
import time
import random
import os
import pygame
import urllib.request
import json
from aip import AipSpeech
"""
樹莓派打造智能鬧鐘
pip3 install pygame
pip3 install baidu-aip
"""
# 獲取天氣
def get_weather():
# 天氣臭增,編碼,
url = 'http://www.weather.com.cn/data/cityinfo/101120201.html'
obj = urllib.request.urlopen(url)
data_b = obj.read()
data_s = data_b.decode('utf-8')
data_dict = json.loads(data_s)
rt = data_dict['weatherinfo']
weather = '親愛的:該起床了竹习,別睡了誊抛,快變小豬了,哈哈哈哈哈整陌,我想你了拗窃,你想我嗎?青島的溫度是 {} 到 {}泌辫,天氣 {}'
weather = weather.format(rt['temp1'], rt['temp2'], rt['weather'])
if '雨' in weather:
weather += '今天別忘記帶雨傘哦随夸!'
du_say(weather)
# 文字轉(zhuǎn)語音
def du_say(weather):
app_id = '****'
api_key = '****'
secret_key = '****'
client = AipSpeech(app_id, api_key, secret_key)
# per 3是漢子 4是妹子,spd 是語速震放,vol 是音量
result = client.synthesis(weather, 'zh', 1, {
'vol': 5, 'per': 3, 'spd': 4
})
# 識別正確返回語音二進制 錯誤則返回dict 參照下面錯誤碼
if not isinstance(result, dict):
with open('weather.mp3', 'wb') as f:
f.write(result)
py_game_player('weather.mp3')
# 播放天氣和音樂
def py_game_player(file):
pygame.mixer.init()
print("播報天氣")
pygame.mixer.music.load(file)
pygame.mixer.music.play(loops=1, start=0.0)
print("播放音樂")
while True:
if pygame.mixer.music.get_busy() == 0:
# Linux 配置定時任務要設置絕對路徑
mp3 = "/home/pi/alarmClock/"+str(random.randint(1, 6)) + ".mp3"
# mp3 = str(random.randint(1, 6)) + ".mp3"
pygame.mixer.music.load(mp3)
pygame.mixer.music.play(loops=1, start=0.0)
break
while True:
if pygame.mixer.music.get_busy() == 0:
print("播報完畢宾毒,起床啦")
break
if __name__ == '__main__':
get_weather()
#小編整理一套Python資料和PDF,有需要Python學習資料可以加學習群:766545907殿遂,反正閑著也是閑著呢诈铛,不如學點東西啦~~
其實,這款鬧鐘并不智能墨礁,并且還有一些昂貴幢竹,幸好身邊有兩個平時不怎么用的音箱,就拿來廢物利用了恩静。好處是可以隨心所欲的DIY焕毫,比如做一款APP,或者后臺管理驶乾,進行遠程控制邑飒,給予女朋友無時無刻的關懷。