|
1 | | -#!/usr/bin/python |
2 | | - |
3 | | -# Weather forecast for Raspberry Pi w/Adafruit Mini Thermal Printer. |
4 | | -# Retrieves data from DarkSky.net's API, prints current conditions and |
5 | | -# forecasts for next two days. See timetemp.py for a different |
6 | | -# weather example using nice bitmaps. |
7 | | -# Written by Adafruit Industries. MIT license. |
8 | | -# |
9 | | -# Required software includes Adafruit_Thermal and PySerial libraries. |
10 | | -# Other libraries used are part of stock Python install. |
11 | | -# |
12 | | -# Resources: |
13 | | -# http://www.adafruit.com/products/597 Mini Thermal Receipt Printer |
14 | | -# http://www.adafruit.com/products/600 Printer starter pack |
15 | | - |
16 | 1 | from __future__ import print_function |
17 | 2 | from Adafruit_Thermal import * |
18 | 3 | from datetime import date |
19 | | -from datetime import datetime |
20 | 4 | import calendar |
21 | | -import urllib, json |
| 5 | +import urllib.request |
| 6 | +import json |
22 | 7 |
|
23 | | -API_KEY = "YOUR_API_KEY" |
| 8 | +printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5) |
| 9 | +def getLink(dailyOrHourly): |
| 10 | + latitude = "38.8894" #limit to four decimal digits |
| 11 | + longitude = "-77.0352" #limit to four decimal digits |
| 12 | + mainLink = "https://api.weather.gov/points/" + latitude + "," + longitude |
| 13 | + response_main = urllib.request.urlopen(mainLink) |
| 14 | + raw_data_main = response_main.read().decode() |
| 15 | + data_main = json.loads(raw_data_main) |
| 16 | + properties_main = data_main['properties'] |
| 17 | + dailyLink = properties_main["forecast"] |
| 18 | + hourlyLink = properties_main["forecastHourly"] |
| 19 | + if dailyOrHourly == "daily": |
| 20 | + return dailyLink |
| 21 | + elif dailyOrHourly == "hourly": |
| 22 | + return hourlyLink |
24 | 23 |
|
25 | | -LAT = "40.726019" |
26 | | -LONG = "-74.00536" |
| 24 | +url_daily = getLink("daily") |
| 25 | +response_daily = urllib.request.urlopen(url_daily) |
| 26 | +# status & reason |
| 27 | +# print(response_daily.status, response_daily.reason) |
27 | 28 |
|
28 | | -# Dumps one forecast line to the printer |
29 | | -def forecast(idx): |
| 29 | +raw_data_daily = response_daily.read().decode() |
| 30 | +data_daily = json.loads(raw_data_daily) |
| 31 | +forecast_periods_daily = data_daily['properties']['periods'] |
30 | 32 |
|
31 | | - date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time'])) |
32 | 33 |
|
33 | | - day = calendar.day_name[date.weekday()] |
34 | | - lo = data['daily']['data'][idx]['temperatureMin'] |
35 | | - hi = data['daily']['data'][idx]['temperatureMax'] |
36 | | - cond = data['daily']['data'][idx]['summary'] |
37 | | - printer.print(day + ': low ' + str(lo) ) |
38 | | - printer.print(deg) |
39 | | - printer.print(' high ' + str(hi)) |
40 | | - printer.print(deg) |
41 | | - printer.println(' ' + cond.replace(u'\u2013', '-').encode('utf-8')) # take care of pesky unicode dash |
| 34 | +current_period_isDayTime = forecast_periods_daily[0]['isDaytime'] |
42 | 35 |
|
43 | | -printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5) |
44 | | -deg = chr(0xf8) # Degree symbol on thermal printer |
| 36 | +if current_period_isDayTime: |
| 37 | + day_index = 0 |
| 38 | + night_index = 1 |
| 39 | +else: |
| 40 | + day_index = 1 |
| 41 | + night_index = 0 |
45 | 42 |
|
46 | | -url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=us" |
47 | | -response = urllib.urlopen(url) |
48 | | -data = json.loads(response.read()) |
| 43 | +day_name = forecast_periods_daily[day_index]['name'] |
| 44 | +hi_temp = forecast_periods_daily[day_index]['temperature'] |
| 45 | +night_name = forecast_periods_daily[night_index]['name'] |
| 46 | +lo_temp = forecast_periods_daily[night_index]['temperature'] |
| 47 | +current_detailed_forecast = forecast_periods_daily[0]['detailedForecast'] |
49 | 48 |
|
50 | | -# Print heading |
51 | | -printer.inverseOn() |
52 | | -printer.print('{:^32}'.format("DarkSky.Net Forecast")) |
53 | | -printer.inverseOff() |
54 | | - |
55 | | -# Print current conditions |
56 | | -printer.boldOn() |
57 | | -printer.print('{:^32}'.format('Current conditions:')) |
58 | | -printer.boldOff() |
| 49 | +url_hourly = getLink("hourly") |
| 50 | +response_hourly = urllib.request.urlopen(url_hourly) |
| 51 | +# status & reason |
| 52 | +#print(response_hourly.status, response_hourly.reason) |
59 | 53 |
|
| 54 | +raw_data_hourly = response_hourly.read().decode() |
| 55 | +data_hourly = json.loads(raw_data_hourly) |
| 56 | +forecast_periods_hourly = data_hourly['properties']['periods'] |
| 57 | +temperature = forecast_periods_hourly[0]['temperature'] |
60 | 58 |
|
61 | | -temp = data['currently']['temperature'] |
62 | | -cond = data['currently']['summary'] |
63 | | -printer.print(temp) |
64 | | -printer.print(deg) |
65 | | -printer.println(' ' + cond) |
| 59 | +d = date.today() |
| 60 | +week_day = calendar.day_name[date(d.year,d.month,d.day).weekday()] |
| 61 | +month_text = calendar.month_name[d.month] |
| 62 | +printer.underlineOn() |
| 63 | +printer.print("It's " + week_day + ", " + month_text + " " + str(d.day) + "\n") |
| 64 | +printer.underlineOff() |
66 | 65 | printer.boldOn() |
67 | | - |
68 | | -# Print forecast |
69 | | -printer.print('{:^32}'.format('Forecast:')) |
| 66 | +printer.print(day_name + "'s Forecast \n") |
70 | 67 | printer.boldOff() |
71 | | -forecast(0) |
72 | | -forecast(1) |
73 | | - |
| 68 | +printer.print("Current temperature: " + str(temperature) + " F \n") |
| 69 | +printer.print("High temperature: " + str(hi_temp) + " F \n") |
| 70 | +printer.print("Low temperature: " + str(lo_temp) + " F \n") |
| 71 | +printer.print(current_detailed_forecast + "\n") |
74 | 72 | printer.feed(3) |
0 commit comments