Moin
Man mag es kaum glauben - aber ich brauch auch mal ein bisschen Hilfe (hab ich schon erwähnt das ich Python hassssse? :()
Und zwar versuche ich krampfhaft seit ein paar Tagen die TinyTX Sensor Daten, die ich in einer SQLite Datenbank gespeichert hab, auf einem kleinen SPI Display über Framebuffer, mithilfe von pygame, darzustellen - das Display funktioniert also das ist hier nicht das Problem...
Orientieren tue ich mich an folgenden Scripts:
http://ph-elec.com/archives/rpi-small-lcd-display/
https://github.com/notro/fbtft/wiki/Pygame
http://www.gtkdb.de/index_36_2229.html
Dazu habe ich mir folgendes Script zusammengeschustert:
#!/usr/bin/python
# -*- coding: utf-8 -*-
################################################################################
#
# Raspberry Pi TinyTX Weather Display
#
################################################################################
import pygame, sys, os, time, string, sqlite3
from pygame.locals import *
#-#-# CONFIG - START
DBfile = "/tmp/.db.sensors"
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/eventX"
#-#-# CONFIG - END
#---------------------------------------------------------------------
####################################################################
def UpdateWeather():
global data, LastUpdate
"Get Data from SQLite Sensor-Database"
defaultcolumns = ['id','time','nodeID','place','supplyV']
try:
con = sqlite3.connect(DBfile)
con.row_factory = sqlite3.Row
cur = con.cursor()
# first get all nodeIDs
cur.execute("SELECT nodeID,place FROM werte WHERE 1 GROUP BY nodeID ORDER BY nodeID ASC")
rows = cur.fetchall()
for row in rows:
print "%s %s" % (row["nodeID"], row["place"])
# Get last Data from nodeID
cur.execute("SELECT time,nodeID,place,supplyV,temp,hum,date(time,'unixepoch') AS Date FROM werte WHERE nodeID?) ORDER BY Date DESC LIMIT 1", (row["nodeID"],))
rows = cur.fetchall()
for row in rows:
LastUpdate = row["Date"]
temp = float(row["temp"]) / 100
place = row["place"]
data.p = place
data.t = temp
print "%s %s" % (place, temp)
except sqlite3.Error, 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():
xmax = 656 - 35
ymax = 416 - 5
lines = 5
lc = (255,255,255)
fn = "freesans"
# Draw Screen Border
pygame.draw.line( screen, lc, (0,0),(xmax,0), lines )
pygame.draw.line( screen, lc, (0,0),(0,ymax), lines )
pygame.draw.line( screen, lc, (0,ymax),(xmax,ymax), lines ) # Bottom
pygame.draw.line( screen, lc, (xmax,0),(xmax,ymax+2), lines )
pygame.draw.line( screen, lc, (0,ymax*0.15),(xmax,ymax*0.15), lines )
pygame.draw.line( screen, lc, (0,ymax*0.5),(xmax,ymax*0.5), lines )
pygame.draw.line( screen, lc, (xmax*0.25,ymax*0.5),(xmax*0.25,ymax), lines )
pygame.draw.line( screen, lc, (xmax*0.5,ymax*0.15),(xmax*0.5,ymax), lines )
pygame.draw.line( screen, lc, (xmax*0.75,ymax*0.5),(xmax*0.75,ymax), lines )
# Time & Date
font = pygame.font.SysFont( fn, int(ymax*0.125), bold=1 ) # Regular Font
sfont = pygame.font.SysFont( fn, int(ymax*0.075), bold=1 ) # Small Font for Seconds
tm1 = time.strftime( "%a, %b %d %I:%M", time.localtime() ) # 1st part
tm2 = time.strftime( "%S", time.localtime() ) # 2nd
tm3 = time.strftime( " %P", time.localtime() ) #
rtm1 = font.render( tm1, True, lc )
(tx1,ty1) = rtm1.get_size()
rtm2 = sfont.render( tm2, True, lc )
(tx2,ty2) = rtm2.get_size()
rtm3 = font.render( tm3, True, lc )
(tx3,ty3) = rtm3.get_size()
tp = xmax / 2 - (tx1 + tx2 + tx3) / 2
screen.blit( rtm1, (tp,1) )
screen.blit( rtm2, (tp+tx1+3,8) )
screen.blit( rtm3, (tp+tx1+tx2,1) )
# Outside Temp
font = pygame.font.SysFont( fn, int(ymax*(0.5-0.15)*0.9), bold=1 )
txt = font.render( data.t, True, lc )
(tx,ty) = txt.get_size()
#---------------------------------------------------------------------
def main():
global screen,data
# set up the colors
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
mouseX, mouseY = 0, 0
mode = 'w'
s = ""
# Initialise pygame
pygame.init()
# Initialise font support
pygame.font.init()
# Render the screen
screen = pygame.display.set_mode((320, 240), 0, 32)
# Clear the screen to BLACK to start
screen.fill(BLACK)
pygame.mouse.set_visible(0)
# Update the display
pygame.display.update()
UpdateWeather()
while True:
# Process keyboard events.
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if ( event.key == K_KP_ENTER ):
running = False
elif ( event.key == K_w ):
mode = 'w'
elif ( event.key == K_s ):
screen_cap()
pygame.display.update()
if ( mode == 'w' ):
# Update / Refresh the display after each second.
if ( s != time.strftime("%S") ):
s = time.strftime("%S")
DisplayWeather()
# Once the screen is updated, we have a full second to get the weather.
# Once per minute, update the weather from the net.
if ( int(s) == 0 ):
UpdateWeather()
pygame.display.update()
pygame.time.wait( 100 )
if __name__ == '__main__':
main()
Alles anzeigen
Wenn ich das ausführe erhalte ich folgende Fehlermeldung:
root@raspberrypi:~# python Sensor.py
xcb_connection_has_error() returned true
19 Wohnzimmer
Traceback (most recent call last):
File "Sensor.py", line 169, in <module>
main()
File "Sensor.py", line 137, in main
UpdateWeather()
File "Sensor.py", line 45, in UpdateWeather
data.p = place
NameError: global name 'data' is not defined
root@raspberrypi:~#
Alles anzeigen
Ich versteh nicht was der von mir will.. Ich dachte 'data' ist sowas wie ein Array?
Wenn ich in das mir bekannte Format änder: data[p] = place ändert sich leider nichts...
(hab ich schon erwähnt das ich Python hassssse? :()
Was mach ich falsch?