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. # Weather forecast for Raspberry Pi w/Adafruit Mini Thermal Printer.
# Retrieves data from DarkSky.net's API, prints current conditions and # Retrieves data from DarkSky.net's API, prints current conditions and
# forecasts for next two days. # forecasts for next two days.
# weather example using nice bitmaps. # Weather example using nice bitmaps.
# Written by Adafruit Industries. MIT license. # Written by Adafruit Industries. MIT license.
# # Adapted and enhanced for escpos library by MrWunderbar666
# 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
# Icons taken from http://adamwhitcroft.com/climacons/ # Icons taken from http://adamwhitcroft.com/climacons/
# Check out his github: https://github.com/AdamWhitcroft/climacons
from __future__ import print_function from __future__ import print_function
from datetime import date from datetime import date
@ -29,42 +25,22 @@ from escpos.printer import Usb
# Adapt to your needs # Adapt to your needs
printer = Usb(0x0416, 0x5011, profile="POS-5890") 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" API_KEY = "YOUR API KEY"
LAT = "22.345490" # Your Location LAT = "22.345490" # Your Location
LONG = "114.189945" # Your Location LONG = "114.189945" # Your Location
def forecast_icon(idx): def forecast_icon(idx):
icon = data['daily']['data'][idx]['icon'] icon = data['daily']['data'][idx]['icon']
if 'clear-day' in icon: image = "/graphics/" + icon + ".png"
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'
return image return image
# Dumps one forecast line to the printer # Dumps one forecast line to the printer
def forecast(idx): def forecast(idx):
date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time'])) date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time']))
day = calendar.day_name[date.weekday()] day = calendar.day_name[date.weekday()]
lo = data['daily']['data'][idx]['temperatureMin'] lo = data['daily']['data'][idx]['temperatureMin']
hi = data['daily']['data'][idx]['temperatureMax'] hi = data['daily']['data'][idx]['temperatureMax']
@ -77,7 +53,7 @@ def forecast(idx):
time.sleep(1) time.sleep(1)
printer.set(font='a', height=2, align='left', bold=False, double_height=False) printer.set(font='a', height=2, align='left', bold=False, double_height=False)
printer.text(day + ' \n ') printer.text(day + ' \n ')
time.sleep(5) # Sleep to prevent printer buffer overflow time.sleep(5) # Sleep to prevent printer buffer overflow
printer.text('\n') printer.text('\n')
printer.image(forecast_icon(idx), high_density_vertical=False, high_density_horizontal=False, impl=u'bitImageRaster', fragment_height=960) printer.image(forecast_icon(idx), high_density_vertical=False, high_density_horizontal=False, impl=u'bitImageRaster', fragment_height=960)
printer.text('low ' + str(lo) ) printer.text('low ' + str(lo) )
@ -89,36 +65,17 @@ def forecast(idx):
printer.text(cond.replace(u'\u2013', '-').encode('utf-8')) # take care of pesky unicode dash printer.text(cond.replace(u'\u2013', '-').encode('utf-8')) # take care of pesky unicode dash
printer.text('\n \n') printer.text('\n \n')
def icon(): def icon():
icon = data['currently']['icon'] icon = data['currently']['icon']
if 'clear-day' in icon: image = "/graphics/" + icon + ".png"
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'
return image 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) response = urllib.urlopen(url)
data = json.loads(response.read()) data = json.loads(response.read())
@ -128,17 +85,14 @@ printer.set(font='a', height=2, align='center', bold=True, double_height=True)
printer.text("Weather Forecast") printer.text("Weather Forecast")
printer.text("\n") printer.text("\n")
printer.set(align='center') printer.set(align='center')
# Print current conditions # Print current conditions
printer.set(font='a', height=2, align='center', bold=True, double_height=False) printer.set(font='a', height=2, align='center', bold=True, double_height=False)
printer.text('Current conditions: \n') printer.text('Current conditions: \n')
printer.image(icon()) printer.image(icon())
printer.text("\n") printer.text("\n")
printer.set(font='a', height=2, align='left', bold=False, double_height=False) printer.set(font='a', height=2, align='left', bold=False, double_height=False)
temp = data['currently']['temperature'] temp = data['currently']['temperature']
cond = data['currently']['summary'] cond = data['currently']['summary']
@ -149,13 +103,11 @@ printer.text(' ')
printer.text('\n') printer.text('\n')
printer.text('Sky: ' + cond) printer.text('Sky: ' + cond)
printer.text('\n') printer.text('\n')
printer.text('\n') printer.text('\n')
# Print forecast # Print forecast
printer.set(font='a', height=2, align='center', bold=True, double_height=False) printer.set(font='a', height=2, align='center', bold=True, double_height=False)
printer.text('Forecast: \n') printer.text('Forecast: \n')
#printer.boldOff()
forecast(0) forecast(0)
forecast(1) forecast(1)
printer.cut printer.cut