Hallo,
ich hab versuch als totaler Python-Anfänger mir ein Script zu basteln, welches mir die Temperatur eines Sensors (DS18S20) auf meinem 16x2 Char LCD ausgibt. Die Ausgabe der Temperatur in der Konsole funktioniert, allerdings funktioniert das gebastelte Skript für das Display nicht. Hoffe ihr könnt mir da helfen...
Ziel der Sache ist das in der ersten Zeile Datum und Uhrzeit stehen und in der 2. Zeile die aktuelle Raumtemperatur angezeigt wird.
Script 1 (Uhr):
Python
[font="Monaco, Consolas, Courier, monospace"]#!/usr/bin/python
[/font]
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
#Added
from subprocess import *
from time import sleep, strftime
from datetime import datetime
# Initialize the LCD plate. Should auto-detect correct I2C bus. If not,
# pass '0' for early 256 MB Model B boards or '1' for all later versions
lcd = Adafruit_CharLCDPlate()
# Clear display and show greeting, pause 1 sec
lcd.clear()
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
sleep(1)
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
lcd.begin(16,1)
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
while 1:
lcd.clear()
ipaddr = run_cmd(cmd)
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
lcd.message('Datum und Uhr %s' % ( ipaddr ) )
sleep(1)
Alles anzeigen
Script 2 (Temperatur-Messung):
Code
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '10-0008028d988d')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
while True:
print(read_temp())
time.sleep(1)
Alles anzeigen
[font="Helvetica Neue, Helvetica, Arial, sans-serif"]"Fertiges"-Script:[/font]
Python
#!/usr/bin/python
from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
#Added 04 10 2013
from subprocess import *
from time import sleep, strftime
from datetime import datetime
#Added 05 10 2013
import os
import glob
import time
# Initialize the LCD plate. Should auto-detect correct I2C bus. If not,
# pass '0' for early 256 MB Model B boards or '1' for all later versions
lcd = Adafruit_CharLCDPlate()
#Added 05 10 2013
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
# Clear display and show greeting, pause 1 sec
lcd.clear()
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
sleep(1)
#Added 05 10 2013
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '10-0008028d988d')[0]
device_file = device_folder + '/w1_slave'
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
lcd.begin(16,1)
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
#Added 05 10 2013
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
while 1:
lcd.clear()
ipaddr = run_cmd(cmd)
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
lcd.message('Temp %read_temp ' % ( ipaddr ) )
sleep(1)
#Added 05 10 2013
while True:
print(read_temp())
time.sleep(1)
Alles anzeigen
[font="Helvetica Neue, Helvetica, Arial, sans-serif"]MfG c3ntry[/font]