2017-08-01 09:13:45 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
|
|
|
|
# Adapted script from Adafruit
|
|
|
|
# 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.
|
|
|
|
# Written by Adafruit Industries. MIT license.
|
|
|
|
# Adapted and enhanced for escpos library by MrWunderbar666
|
|
|
|
|
2020-05-31 14:21:42 +00:00
|
|
|
# Icons taken from https://adamwhitcroft.com/climacons/
|
2017-08-01 09:13:45 +00:00
|
|
|
# Check out his github: https://github.com/AdamWhitcroft/climacons
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import calendar
|
|
|
|
import urllib
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
|
|
|
|
from escpos.printer import Usb
|
|
|
|
|
|
|
|
""" Setting up the main pathing """
|
|
|
|
this_dir, this_filename = os.path.split(__file__)
|
|
|
|
GRAPHICS_PATH = os.path.join(this_dir, "graphics/climacons/")
|
|
|
|
|
|
|
|
# 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"
|
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
LAT = "22.345490" # Your Location
|
|
|
|
LONG = "114.189945" # Your Location
|
2017-08-01 09:13:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def forecast_icon(idx):
|
2021-10-30 16:15:22 +00:00
|
|
|
icon = data["daily"]["data"][idx]["icon"]
|
2017-08-01 09:13:45 +00:00
|
|
|
image = GRAPHICS_PATH + icon + ".png"
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
|
|
|
# Dumps one forecast line to the printer
|
|
|
|
def forecast(idx):
|
2021-10-30 16:15:22 +00:00
|
|
|
date = datetime.fromtimestamp(int(data["daily"]["data"][idx]["time"]))
|
2017-08-01 09:13:45 +00:00
|
|
|
day = calendar.day_name[date.weekday()]
|
2021-10-30 16:15:22 +00:00
|
|
|
lo = data["daily"]["data"][idx]["temperatureMin"]
|
|
|
|
hi = data["daily"]["data"][idx]["temperatureMax"]
|
|
|
|
cond = data["daily"]["data"][idx]["summary"]
|
2017-08-01 09:13:45 +00:00
|
|
|
print(date)
|
|
|
|
print(day)
|
|
|
|
print(lo)
|
|
|
|
print(hi)
|
|
|
|
print(cond)
|
|
|
|
time.sleep(1)
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.set(font="a", height=2, align="left", bold=False, double_height=False)
|
|
|
|
printer.text(day + " \n ")
|
|
|
|
time.sleep(5) # Sleep to prevent printer buffer overflow
|
|
|
|
printer.text("\n")
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.image(forecast_icon(idx))
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.text("low " + str(lo))
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.text(deg)
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.text("\n")
|
|
|
|
printer.text(" high " + str(hi))
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.text(deg)
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.text("\n")
|
2017-08-01 09:13:45 +00:00
|
|
|
# take care of pesky unicode dash
|
2023-04-19 20:11:09 +00:00
|
|
|
printer.text(cond.replace("\u2013", "-").encode("utf-8"))
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.text("\n \n")
|
2017-08-01 09:13:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def icon():
|
2021-10-30 16:15:22 +00:00
|
|
|
icon = data["currently"]["icon"]
|
2017-08-01 09:13:45 +00:00
|
|
|
image = GRAPHICS_PATH + icon + ".png"
|
|
|
|
return image
|
|
|
|
|
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
deg = " C" # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol
|
2017-08-01 09:13:45 +00:00
|
|
|
|
|
|
|
# if you want Fahrenheit change units= to 'us'
|
2021-10-30 16:15:22 +00:00
|
|
|
url = (
|
|
|
|
"https://api.darksky.net/forecast/"
|
|
|
|
+ API_KEY
|
|
|
|
+ "/"
|
|
|
|
+ LAT
|
|
|
|
+ ","
|
|
|
|
+ LONG
|
|
|
|
+ "?exclude=[alerts,minutely,hourly,flags]&units=si"
|
|
|
|
) # change last bit to 'us' for Fahrenheit
|
2017-08-01 09:13:45 +00:00
|
|
|
response = urllib.urlopen(url)
|
|
|
|
data = json.loads(response.read())
|
|
|
|
|
|
|
|
printer.print_and_feed(n=1)
|
|
|
|
printer.control("LF")
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.set(font="a", height=2, align="center", bold=True, double_height=True)
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.text("Weather Forecast")
|
|
|
|
printer.text("\n")
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.set(align="center")
|
2017-08-01 09:13:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Print current conditions
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.set(font="a", height=2, align="center", bold=True, double_height=False)
|
|
|
|
printer.text("Current conditions: \n")
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.image(icon())
|
|
|
|
printer.text("\n")
|
|
|
|
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.set(font="a", height=2, align="left", bold=False, double_height=False)
|
|
|
|
temp = data["currently"]["temperature"]
|
|
|
|
cond = data["currently"]["summary"]
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.text(temp)
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.text(" ")
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.text(deg)
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.text(" ")
|
|
|
|
printer.text("\n")
|
|
|
|
printer.text("Sky: " + cond)
|
|
|
|
printer.text("\n")
|
|
|
|
printer.text("\n")
|
2017-08-01 09:13:45 +00:00
|
|
|
|
|
|
|
# Print forecast
|
2021-10-30 16:15:22 +00:00
|
|
|
printer.set(font="a", height=2, align="center", bold=True, double_height=False)
|
|
|
|
printer.text("Forecast: \n")
|
2017-08-01 09:13:45 +00:00
|
|
|
forecast(0)
|
|
|
|
forecast(1)
|
2017-08-10 19:54:52 +00:00
|
|
|
printer.cut()
|
2017-08-01 09:13:45 +00:00
|
|
|
printer.control("LF")
|