Minor improvements

This commit is contained in:
mrwunderbar666 2017-07-27 00:01:11 +08:00
parent b607285dd7
commit 17831593cf
10 changed files with 17 additions and 65 deletions

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 21 KiB

After

Width:  |  Height:  |  Size: 21 KiB

View File

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 20 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -5,17 +5,13 @@
# Weather forecast for Raspberry Pi w/Adafruit Mini Thermal Printer.
# Retrieves data from DarkSky.net's API, prints current conditions and
# forecasts for next two days.
# weather example using nice bitmaps.
# Weather example using nice bitmaps.
# Written by Adafruit Industries. MIT license.
#
# Required software includes Adafruit_Thermal and PySerial libraries.
# Other libraries used are part of stock Python install.
#
# Resources:
# http://www.adafruit.com/products/597 Mini Thermal Receipt Printer
# http://www.adafruit.com/products/600 Printer starter pack
# Adapted and enhanced for escpos library by MrWunderbar666
# Icons taken from http://adamwhitcroft.com/climacons/
# Check out his github: https://github.com/AdamWhitcroft/climacons
from __future__ import print_function
from datetime import date
@ -29,6 +25,8 @@ from escpos.printer import Usb
# Adapt to your needs
printer = Usb(0x0416, 0x5011, profile="POS-5890")
# You can get your API Key on www.darksky.net and register a dev account.
# Technically you can use any other weather service, of course :)
API_KEY = "YOUR API KEY"
LAT = "22.345490" # Your Location
@ -36,35 +34,13 @@ LONG = "114.189945" # Your Location
def forecast_icon(idx):
icon = data['daily']['data'][idx]['icon']
if 'clear-day' in icon:
image = 'clear-day.png'
elif 'clear-night' in icon:
image = 'clear-night.png'
elif 'partly-cloudy-day' in icon:
image = 'partly-cloudy-day.png'
elif 'partly-cloudy-night' in icon:
image = 'partly-cloudy-night.png'
elif 'rain' in icon:
image = 'rain.png'
elif 'snow' in icon:
image = 'snow.png'
elif 'sleet' in icon:
image = 'sleet.png'
elif 'wind' in icon:
image = 'wind.png'
elif 'fog' in icon:
image = 'fog.png'
elif 'cloudy' in icon:
image = 'cloudy.png'
else:
image = 'clear-day.png'
image = "/graphics/" + icon + ".png"
return image
# Dumps one forecast line to the printer
def forecast(idx):
date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time']))
day = calendar.day_name[date.weekday()]
lo = data['daily']['data'][idx]['temperatureMin']
hi = data['daily']['data'][idx]['temperatureMax']
@ -89,36 +65,17 @@ def forecast(idx):
printer.text(cond.replace(u'\u2013', '-').encode('utf-8')) # take care of pesky unicode dash
printer.text('\n \n')
def icon():
icon = data['currently']['icon']
if 'clear-day' in icon:
image = 'clear-day.png'
elif 'clear-night' in icon:
image = 'clear-night.png'
elif 'partly-cloudy-day' in icon:
image = 'partly-cloudy-day.png'
elif 'partly-cloudy-night' in icon:
image = 'partly-cloudy-night.png'
elif 'rain' in icon:
image = 'rain.png'
elif 'snow' in icon:
image = 'snow.png'
elif 'sleet' in icon:
image = 'sleet.png'
elif 'wind' in icon:
image = 'wind.png'
elif 'fog' in icon:
image = 'fog.png'
elif 'cloudy' in icon:
image = 'cloudy.png'
else:
image = 'clear-day.png'
image = "/graphics/" + icon + ".png"
return image
deg = ' C' # Degree symbol on thermal printer
deg = ' C' # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol
url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=si" # if you want Fahrenheit change units= to 'us'
# if you want Fahrenheit change units= to 'us'
url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=si"
response = urllib.urlopen(url)
data = json.loads(response.read())
@ -132,13 +89,10 @@ printer.set(align='center')
# Print current conditions
printer.set(font='a', height=2, align='center', bold=True, double_height=False)
printer.text('Current conditions: \n')
printer.image(icon())
printer.text("\n")
printer.set(font='a', height=2, align='left', bold=False, double_height=False)
temp = data['currently']['temperature']
cond = data['currently']['summary']
@ -149,13 +103,11 @@ printer.text(' ')
printer.text('\n')
printer.text('Sky: ' + cond)
printer.text('\n')
printer.text('\n')
# Print forecast
printer.set(font='a', height=2, align='center', bold=True, double_height=False)
printer.text('Forecast: \n')
#printer.boldOff()
forecast(0)
forecast(1)
printer.cut