import time import RPi.GPIO as GPIO import urllib2 import simplejson # Zuordnung der GPIO Pins (ggf. anpassen) LCD_RS = 4 LCD_E = 17 LCD_DATA4 = 18 LCD_DATA5 = 22 LCD_DATA6 = 23 LCD_DATA7 = 24 LCD_WIDTH = 16 # Zeichen je Zeile LCD_LINE_1 = 0x80 # Adresse der ersten Display Zeile LCD_LINE_2 = 0xC0 # Adresse der zweiten Display Zeile LCD_CHR = GPIO.HIGH LCD_CMD = GPIO.LOW E_PULSE = 0.0005 E_DELAY = 0.0005 # URL, ggf. Parameter anpassen # Link zu den Parameter Beschreibungen: # https://developers.google.com/news-search/v1/jsondevguide#json_urlbase URL = ('https://ajax.googleapis.com/ajax/services/search/news?' + 'v=1.0&q=%20&topic=h&hl=de') # Alle wie viele Minuten auf neue Nachrichten gecheckt werden soll REFRESH_TIME = 5 # (Minuten) # HDD44780 Funktionen def lcd_send_byte(bits, mode): # Pins auf LOW setzen GPIO.output(LCD_RS, mode) GPIO.output(LCD_DATA4, GPIO.LOW) GPIO.output(LCD_DATA5, GPIO.LOW) GPIO.output(LCD_DATA6, GPIO.LOW) GPIO.output(LCD_DATA7, GPIO.LOW) if bits & 0x10 == 0x10: GPIO.output(LCD_DATA4, GPIO.HIGH) if bits & 0x20 == 0x20: GPIO.output(LCD_DATA5, GPIO.HIGH) if bits & 0x40 == 0x40: GPIO.output(LCD_DATA6, GPIO.HIGH) if bits & 0x80 == 0x80: GPIO.output(LCD_DATA7, GPIO.HIGH) time.sleep(E_DELAY) GPIO.output(LCD_E, GPIO.HIGH) time.sleep(E_PULSE) GPIO.output(LCD_E, GPIO.LOW) time.sleep(E_DELAY) GPIO.output(LCD_DATA4, GPIO.LOW) GPIO.output(LCD_DATA5, GPIO.LOW) GPIO.output(LCD_DATA6, GPIO.LOW) GPIO.output(LCD_DATA7, GPIO.LOW) if bits&0x01==0x01: GPIO.output(LCD_DATA4, GPIO.HIGH) if bits&0x02==0x02: GPIO.output(LCD_DATA5, GPIO.HIGH) if bits&0x04==0x04: GPIO.output(LCD_DATA6, GPIO.HIGH) if bits&0x08==0x08: GPIO.output(LCD_DATA7, GPIO.HIGH) time.sleep(E_DELAY) GPIO.output(LCD_E, GPIO.HIGH) time.sleep(E_PULSE) GPIO.output(LCD_E, GPIO.LOW) time.sleep(E_DELAY) def display_init(): lcd_send_byte(0x33, LCD_CMD) lcd_send_byte(0x32, LCD_CMD) lcd_send_byte(0x28, LCD_CMD) lcd_send_byte(0x0C, LCD_CMD) lcd_send_byte(0x06, LCD_CMD) lcd_send_byte(0x01, LCD_CMD) def lcd_message(message): message = message.ljust(LCD_WIDTH," ") for i in range(LCD_WIDTH): lcd_send_byte(ord(message[i]),LCD_CHR) # Funktionen def loadURL(): request = urllib2.Request(URL, None, {}) response = urllib2.urlopen(request) return simplejson.load(response) # Um die def formatString(string): new = u'' for i in range(len(string)): if ord(string[i]) > 127: new = new + unichr(ord(string[i]) - 7) else: new = new + string[i] return new def printTopics(results): for obj in results["responseData"]["results"]: topic = formatString(obj['titleNoFormatting']+" ") while len(topic) > 0: idx1 = len(topic) if len(topic) <= LCD_WIDTH else (LCD_WIDTH if topic[LCD_WIDTH-1] == " " or topic[LCD_WIDTH] == " " or topic[:LCD_WIDTH].rfind(" ") == -1 else topic[:LCD_WIDTH].rfind(" ")) idx2 = LCD_WIDTH if idx1 == len(topic) or idx1-LCD_WIDTH < 0 or topic[min(idx1+1, LCD_WIDTH):][LCD_WIDTH-1] == " " or topic[min(idx1+1, LCD_WIDTH):][LCD_WIDTH] == " " or topic[min(idx1+1, LCD_WIDTH):][:LCD_WIDTH].rfind(" ") == -1 else topic[min(idx1+1, LCD_WIDTH):][:LCD_WIDTH].rfind(" ") topic1 = topic[:idx1].lstrip() topic2 = topic[min(idx1+1, LCD_WIDTH):][:idx2].lstrip() lcd_send_byte(LCD_LINE_1, LCD_CMD) lcd_message(topic1) lcd_send_byte(LCD_LINE_2, LCD_CMD) lcd_message(topic2) print topic1 print topic2 time.sleep(1.5) topic = topic[min(idx1+1, LCD_WIDTH):][idx2:].lstrip() lcd_send_byte(LCD_LINE_1, LCD_CMD) lcd_message("") lcd_send_byte(LCD_LINE_2, LCD_CMD) lcd_message("") time.sleep(3) if __name__ == '__main__': # initialisieren GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(LCD_E, GPIO.OUT) GPIO.setup(LCD_RS, GPIO.OUT) GPIO.setup(LCD_DATA4, GPIO.OUT) GPIO.setup(LCD_DATA5, GPIO.OUT) GPIO.setup(LCD_DATA6, GPIO.OUT) GPIO.setup(LCD_DATA7, GPIO.OUT) display_init() try: results = loadURL() counter = time.time() while True: if counter + REFRESH_TIME*60 <= time.time(): results = loadURL() printTopics(results) except KeyboardInterrupt: print "Abbruch" except: print "Hoppla. Es ist etwas schief gelaufen" finally: GPIO.cleanup()