Für das Messen der Lichtstärke benutzen wir den BHT1750 mit Breakoutboard.
Das Script zum messen und speichern:
# -*- coding: UTF-8 -*- import MySQLdb import smbus import time from time import * # aktuelle, lokale Zeit als Tupel lt = localtime() # Entpacken des Tupels, Datum jahr, monat, tag, stunde, minute, sekunde = lt[0:6] tag = "%04i-%02i-%02i" % (jahr,monat,tag) zeit = "%02i:%02i:%02i" % (stunde,minute,sekunde) db=MySQLdb.connect("127.0.0.1","pi","f6g7h8","grow") cursor = db.cursor() print tag, zeit #lichtsensor bh1750 bus = smbus.SMBus(1) data = bus.read_i2c_block_data(0x23,0x21) # feld1: default I2C adresse - feld2: methode 0x13=wiederholt 4LX 16ms - 0x10=wiederholt 0.5LX 120ms - 0x11=1LX 120ms // 0x20=einmalig 0.5LX 120ms - 0x21=einmalig 1LX 120ms - 0x23=einmalig 4LX R1L1 = ((data[1] + (256 * data[0])) / 1.2) R1L1 = round(R1L1/100,0) *100 print "Raum1 Lichtsensor1: ",R1L1 if R1L1 == 0: print"0 ist doof für die Darstellung und wird deshalb zur 1" R1L1 = 1 sensorname = "R1L1" cursor.execute("select wert from log where datum='%s' and sensor='%s' order by zeit desc" % (tag, sensorname) ) wert=cursor.fetchone() if not wert or wert[0] != R1L1: cursor.execute("insert into log(datum,zeit,sensor,wert) values('%s','%s','%s','%s')" % (tag, zeit, sensorname, R1L1) ) cursor.execute("SELECT COUNT(*) FROM log") anzahl = cursor.fetchone() print "%s Zeilen in Tabelle log" % anzahl[0] db.commit() db.close()