#Project 4 - Wetterdaten #import libraries import time import Adafruit_SSD1306 import requests from PIL import Image from PIL import ImageDraw from PIL import ImageFont #Raspberry Pi pin config RST = 24 #128x32 display I2C #disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) #128x64 display I2C disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) #OpenWeatherMap.org URL open_weather_map_url = 'http://api.openweathermap.org/data/2.5/weather?q=Tegernsee,DE&APPID=c2c58a80ce6377889800c284c540c505' #init display disp.begin() while True: #clear display disp.clear() disp.display() #Erstellen eines leeren Bildes zum Zeichnen width = disp.width height = disp.height image = Image.new('1', (width, height)) #Bild aufrufen draw = ImageDraw.Draw(image) #schwarze gefüllte Box zeichnen draw.rectangle((0,0,width,height), outline=0, fill=0) #Definieren von Konstanten für den Zeichenbereich padding = 2 top = padding x = padding #Standardschriftart laden font = ImageFont.load_default() #OpenWeatherMap.org Wetterdatenanforderung weather_data = requests.get(open_weather_map_url) #display Ort location = weather_data.json().get('name') + ' - ' + weather_data.json().get('sys').get('country') draw.text((x, top), location, font=font, fill=255) #display Beschreibung description = 'Desc ' + weather_data.json().get('weather')[0].get('main') draw.text((x, top+10), description, font=font, fill=255) #Temperatur raw_temperature = weather_data.json().get('main').get('temp')-273.15 #Temperatur in Celsius temperature = 'Temperatur ' + str(raw_temperature) + '*C' #Temperature in Fahrenheit #temperature = 'Temp ' + str(raw_temperature*(9/5.0)+32) + '*F' draw.text((x, top+20), temperature, font=font, fill=255) #Luftdruck pressure = 'L-Druck ' + str(weather_data.json().get('main').get('pressure')) + 'hPa' draw.text((x, top+30), pressure, font=font, fill=255) #Luftfeuchte humidity = 'L-Feuchte ' + str(weather_data.json().get('main').get('humidity')) + '%' draw.text((x, top+40), humidity, font=font, fill=255) #Wind wind = 'Wind ' + str(weather_data.json().get('wind').get('speed')) + 'mps ' + str(weather_data.json().get('wind').get('deg')) + '*' draw.text((x, top+50), wind, font=font, fill=255) #display image disp.image(image) disp.display() time.sleep(10)