Hi, da ist dann wohl mir ein Fehler beim Kopieren des Scriptes passiert, sorry.
Ja, ich verwende Python3 und hier kommt mein Code:
Python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
################################################################################
#
# Raspberry Pi TinyTX Weather - Touch-Display - v0.2
#
################################################################################
import pygame
import sys
import os
import time
import string
import sqlite3
from pygame.locals import *
from itertools import cycle
#-#-# CONFIG - START
DBfile = "/Data/SensorDB";
SECURITYKEY = "23338d373027ce83b1f81b9e9563b629";
os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ['SDL_VIDEODRIVER']="fbcon"
# Uncomment if you have a touch panel and find the X value for your device
os.environ["SDL_MOUSEDRV"] = "TSLIB"
os.environ["SDL_MOUSEDEV"] = "/dev/input/event0"
#-#-# CONFIG - END
####################################################################
# default values
defaultrows = ['id','time','nodeID','place']
wert={}
nodeIDs=[]
# set up the colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
CYAN = ( 0, 255, 255)
MAGENTA=(255, 0, 255)
YELLOW =(255, 255, 0)
####################################################################
def GETnodeIDs():
global nodeIDs
try:
con = sqlite3.connect(DBfile)
con.row_factory = sqlite3.Row
cur = con.cursor()
# first get all nodeIDs
cur.execute("SELECT nodeID FROM werte WHERE 1 GROUP BY nodeID ORDER BY nodeID ASC")
rows = cur.fetchall()
for row in rows:
nID = row["nodeID"]
nodeIDs.append(nID)
# except Exeption as e:
except sqlite3.Error as e:
print ("Error %s:", e.args[0])
# print ("Error ",e)
sys.exit(1)
finally:
if con:
con.close()
#---------------------------------------------------------------------
def GETvalues(nodeID):
try:
con = sqlite3.connect(DBfile)
con.row_factory = sqlite3.Row
cur = con.cursor()
# Get last Data from nodeID
cur.execute("SELECT * FROM werte WHERE nodeID=(?) ORDER BY time DESC LIMIT 1", (nodeID,))
rownames = list(map(lambda x: x[0], cur.description))
rows = cur.fetchall()
for row in rows:
for rn in rownames:
drowfound=0
for drow in defaultrows:
if (drow == rn):
drowfound=1
if (drowfound == 0):
if (row[rn] is None):
continue
if (row[rn] != ""):
wert[rn] = row[rn]
#print "%s %s: %s" % (row["place"], rn, row[rn])
DisplayWeather(row["place"], row["time"], wert)
except sqlite3.Error as e:
print ("Error %s:", e.args[0])
sys.exit(1)
finally:
if con:
con.close()
#---------------------------------------------------------------------
def screen_cap():
pygame.image.save( screen, "screenshot.jpeg" )
print ("Screen capture complete.")
#---------------------------------------------------------------------
def DisplayWeather(place, LastUpdate, wert):
global nextbox
# Fill the screen with black
screen.fill(BLACK)
xmax = screen.get_width() #widht
ymax = screen.get_height() #high
lines = 5
lc = (255,255,255) #white border
fn = "freesans"
# next box
nextbox = pygame.draw.rect(screen, RED, (xmax-65, 35, 55, 30))
# next text
font = pygame.font.SysFont( fn, int(ymax*0.1), bold=0 )
ntxt = font.render( "Next", True, BLACK )
screen.blit( ntxt, (screen.get_width()-62, 37) )
# Conditions
st = 0.16 # Yaxis Start Pos
gp = 0.065 # Line Spacing Gap
th = 0.10 # Text Height
dh = 0.05 # Degree Symbol Height
so = 0.01 # Degree Symbol Yaxis Offset
xp = 0.52 # Xaxis Start Pos
x2 = 0.78 # Second Column Xaxis Start Pos
# Time & Date
TDheight = int(ymax*0.125) # font groesse
font = pygame.font.SysFont( fn, TDheight, bold=1 ) # Regular Font
tm1 = time.strftime( "%a, %b %d %H:%M:%S", time.localtime(LastUpdate) ) # 1st part
rtm1 = font.render( tm1, True, lc )
(tx1,ty1) = rtm1.get_size()
tp = xmax / 2 - (tx1) / 2
screen.blit( rtm1, (tp,1) )
# Place
# int(ymax*0.1) -> font groesse
font = pygame.font.SysFont( fn, int(ymax*0.1), bold=0 )
ptxt = font.render( place, True, WHITE )
(ptx,pty) = ptxt.get_size()
x = xmax*0.05 # Abstand von links
screen.blit( ptxt, (x,ymax*0.15) ) # ymax*0.15 -> Abstand von oben
# Values
distance=0.30
for wname in wert:
if (wname == "temp"):
NAME = "Temperatur:"
COLOR = RED
value = float(wert[wname]) / 100
elif (wname == "hum"):
NAME = "Luftfeuchte:"
COLOR = BLUE
value = float(wert[wname]) / 100
elif (wname == "pressure"):
NAME = "Luftdruck:"
COLOR = WHITE
value = float(wert[wname]) / 100
elif (wname == "height"):
NAME = "Höhe:"
COLOR = WHITE
value = float(wert[wname]) / 100
elif (wname == "supplyV"):
NAME = "supplyV:"
COLOR = WHITE
value = float(wert[wname]) / 1000
else:
NAME = wname + ":"
COLOR = WHITE
value = float(wert[wname]) / 100
print ("%s %s: %s", (place, wname, wert[wname]))
font = pygame.font.SysFont( fn, int(ymax*0.15), bold=1 )
txt = font.render( NAME, True, WHITE )
(tx,ty) = txt.get_size()
NAMEwidth = xmax*0.02
screen.blit( txt, (NAMEwidth,ymax*distance) )
font = pygame.font.SysFont( fn, int(ymax*0.15), bold=1 )
vtxt = font.render( str(value), True, COLOR )
(vtx,vty) = vtxt.get_size()
VALUEwidth = xmax-90
screen.blit( vtxt, (VALUEwidth,ymax*distance) )
distance +=0.15
pygame.display.update()
#---------------------------------------------------------------------
def BackLight():
# 1 = off , 0 = on
f = open('/sys/class/backlight/fb_ili9325/bl_power','r+')
curstate = int(f.read());
if (curstate == 1):
newstate = 0
else:
newstate = 1
f.write(str(newstate))
f.close()
#---------------------------------------------------------------------
def main():
global screen
# Initialise pygame
pygame.init()
# Initialise font support
pygame.font.init()
# Render the screen
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
# Clear the screen to BLACK to start
#screen.fill(BLACK)
#pygame.mouse.set_visible(0)
# klick-box um backlight ein/aus zu schalten..
#http://www.python4all.de/PyGame.html
# Fill background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill(BLACK)
if (screen.get_height() == 240):
backlightboxHeight = screen.get_height() - 150
else:
backlightboxHeight = 80
backlightbox = pygame.draw.rect(background, BLACK, (0, backlightboxHeight, screen.get_width(), screen.get_height()))
# 1 2 3 4
# 1 = abstand von links
# 2 = abstand von oben
# 3 = fuellmenge nach rechts
# 4 = fuellmenge nach unten
screen.blit(background, (0, 0))
# Update the display
pygame.display.update()
# First, get all nodeIDs
GETnodeIDs()
# prepare cycle function
IDcycle = cycle(nodeIDs)
# cycle to first nodeID
nextID = IDcycle.next()
lastID = nextID
# Save displayed ID list
showedIDs=[]
showedIDs.append(nextID)
running = True
while running:
if (nextID != lastID):
showedIDs=[]
showedIDs.append(nextID)
if (showedIDs == nodeIDs):
print("re-getting nodeIDs")
GETnodeIDs()
IDcycle = cycle(nodeIDs)
nextID = IDcycle.next()
lastID = nextID
showedIDs=[]
showedIDs.append(nextID)
# Process keyboard events.
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
print("Pos: %sx%s" % pygame.mouse.get_pos())
if backlightbox.collidepoint(pygame.mouse.get_pos()):
BackLight()
GETvalues(nextID)
#nextID = IDcycle.next()
if nextbox.collidepoint(pygame.mouse.get_pos()):
print("next")
nextID = IDcycle.next()
lastID = nextID
showedIDs.append(nextID)
GETvalues(nextID)
pygame.display.update()
if __name__ == '__main__':
main()
Alles anzeigen
Danke für die Hilfe.