Raspberry pi 定时天气播报

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Raspberry pi 定时天气播报相关的知识,希望对你有一定的参考价值。

Raspberry pi 定时天气播报

    操作步骤:   

    1. 简单的写了python脚本

    2. 添加定时计划里执行

    3.使用百度的天气接口:http://api.map.baidu.com/telematics/v3/weather?location=city&output=json&ak=api_key

     百度的语音合成接口:http://tsn.baidu.com/text2audio?tex=text&lan=zh&cuid=cuid&ctp=1&tok=token

 

  python脚本内容

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import os
import urllib.request
import sys
import json

#get token
def get_token():
    api_key="your api key"
    sec_key="your secret key"
    auth_url="https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id="+api_key+"&client_secret="+sec_key
    
    res = urllib.request.urlopen(auth_url)
    #print(res.read())
    json_data = res.read().decode(utf-8)
    print(json_data)

    result = json.loads(json_data)[access_token]
    return result


def get_weather(city):
    api_key="your api key"
    weather_url="http://api.map.baidu.com/telematics/v3/weather?location="+city+"&output=json&ak="+api_key
    
    res = urllib.request.urlopen(weather_url)
    json_data = res.read().decode(utf-8)
    #print(json_data)
    json_data = json.loads(json_data)
    #get json data
    #check result status
    status = json_data[status]
    if status==success:
        results=json_data[results][0]
        pm25=results[pm25]
        weather_data=results[weather_data]
        date=weather_data[0][date]
        weather=weather_data[0][weather]
        wind=weather_data[0][wind]
        temperature=weather_data[0][temperature].replace(~,)
        result=今天是{} PM2.5是{} 天气{} {} 温度为{}.format(date,pm25,weather,wind,temperature)
        return result
    else:
        return ‘‘


token=get_token()
weather=get_weather(beijing)

if os.path.exists(/home/pi/weather/weather.txt):
    with open(/home/pi/weather/weather.txt,wt) as f:
        f.write(weather)
        f.close()
        print(weather)
else:
    print(weather.txt not exists)
        

#tts
url=http://tsn.baidu.com/text2audio?tex=+weather+&lan=zh&cuid=raspberrypi2&ctp=1&tok=+token

print(url)
try:
    os.system(/usr/bin/mplayer "%s" %(url))
except Exception as e:
    print(Exception,e)

 

    添加到crontab 定时计划里,每天早上7:10执行

10 7 * * * /usr/local/bin/python3.5 /home/pi/weather/weather.py

 

    v附录:百度天气API返回的JSON数据

{
  "error": 0,
  "status": "success",
  "date": "2016-02-25",
  "results": [
    {
      "currentCity": "beijing",
      "pm25": "33",
      "index": [
        {
          "title": "穿衣",
          "zs": "较冷",
          "tipt": "穿衣指数",
          "des": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"
        },
        {
          "title": "洗车",
          "zs": "较适宜",
          "tipt": "洗车指数",
          "des": "较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"
        },
        {
          "title": "旅游",
          "zs": "适宜",
          "tipt": "旅游指数",
          "des": "天气较好,同时又有微风伴您一路同行。虽会让人感觉有点凉,但仍适宜旅游,可不要错过机会呦!"
        },
        {
          "title": "感冒",
          "zs": "易发",
          "tipt": "感冒指数",
          "des": "昼夜温差大,且空气湿度较大,易发生感冒,请注意适当增减衣服,加强自我防护避免感冒。"
        },
        {
          "title": "运动",
          "zs": "较适宜",
          "tipt": "运动指数",
          "des": "天气较好,无雨水困扰,较适宜进行各种运动,但因天气凉,在户外运动请注意增减衣物。"
        },
        {
          "title": "紫外线强度",
          "zs": "最弱",
          "tipt": "紫外线强度指数",
          "des": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
        }
      ],
      "weather_data": [
        {
          "date": "周四 02月25日 (实时:4℃)",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/qing.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
          "weather": "",
          "wind": "微风",
          "temperature": "7 ~ -5℃"
        },
        {
          "date": "周五",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
          "weather": "多云转晴",
          "wind": "微风",
          "temperature": "8 ~ -3℃"
        },
        {
          "date": "周六",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/yin.png",
          "weather": "多云转阴",
          "wind": "微风",
          "temperature": "6 ~ -3℃"
        },
        {
          "date": "周日",
          "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png",
          "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/qing.png",
          "weather": "多云转晴",
          "wind": "北风3-4级",
          "temperature": "6 ~ -4℃"
        }
      ]
    }
  ]
}

 

以上是关于Raspberry pi 定时天气播报的主要内容,如果未能解决你的问题,请参考以下文章

案例篇:Python天气播报系统,这才是未雨绸缪

我如何让Raspberry Pi用python发送带有图片的电子邮件

慢响应Firebase + Python + Raspberry pi

如何为 Raspberry pi 3 编译 ARMv8 代码

在raspberry pi中安装缺少的python包

Raspberry Pi Photobooth 打印