Hier mein zusammengestückelter code, geht leider nicht zusammen
im letzten abschnitt soll er bei : [font="Monaco"] if (cur_temp > alarm_hi) or (cur_temp < alarm_lo):[/font]
[font="Monaco"]die mail abschicken, mit Textinhalt cur_temp[/font]
[font="Monaco"]klappt net und er meint ab: msg = .. --> IndentatioError: expected an indented block[/font]
[font="Monaco"]Wenn ich jedoch an dieser stelle nen extra python file aufrufe mit inhalt senden[/font]
[font="Monaco"]script2run = "/home/pi/temp-alarm/send.py"
execfile( script2run )
[/font]
[font="Monaco"]klappt es, aber nur statisch. ich möchte in der mail auch gerne die aktuelle Temperatur als text haben.[/font]
Python
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# read 1-wire sensor
# in case of value exceeding alarm limit
# send email via smtp
# 2013-06-06 V0.1 by Thomas Hoeser
#
import sys
import smtplib
# For guessing MIME type
import mimetypes
# Import the email modules we'll need
import email
import email.mime.application
verbose_level = 2
debug_level = 0
error_temp = -999
# dictionary with for 1-wire sensors: [sensor name] [1-Wire device]
sensor_dict = { "Wohnzimmer": "28-00000487bb70",
"Balkon" : "28-00000487bb70",
"Speicher" : "28-000004be39a5"
}
#---------------------------------------------------------------------------------------------
def read_sensor(Sensor):
if verbose_level > 2:
print "1++++ read_sensor()"
print "sensor:" , Sensor
if debug_level == 0:
# get 1-Wire id from dictionary
sensor_slave = Sensor
# Open 1-wire slave file
sensor_device = '/sys/bus/w1/devices/28-000004c4dfe0/w1_slave'
if verbose_level > 2:
print "open: ", sensor_device
try:
file = open(sensor_device)
filecontent = file.read() # Read content from 1-wire slave file
file.close() # Close 1-wire slave file
stringvalue = filecontent.split("\n")[1].split(" ")[9] # Extract temperature string
if stringvalue[0].find("YES") > 0:
temp = error_temp
else:
temp = float(stringvalue[2:]) / 1000 # Convert temperature value
# temp=str(temp)
except IOError:
print "PANIC read_sensor - Cannot find file >" + sensor_slave + "< in /sys/bus/w1/devices/"
print "No sensor attached"
print "check with > cat /sys/devices/w1_bus_master1/w1_master_slaves"
temp"Sensor not attached")
#else:
# this is dummy function generating a random number
# ony used for testing purposes
#temp = random.randrange(-10, 30, 2) + 0.3
# temp = Sensor + " " + str(temp)
return(temp) # exit function read_sensor
#---------------------------------------------------------------------------------------------
if __name__ == "__main__":
alarm_hi = 20.5 # upper alarm level
alarm_lo = -10.5 # lowe alarm level
cur_temp = read_sensor("Speicher")
print cur_temp, alarm_hi, alarm_lo
if cur_temp == error_temp:
print "read error - CRC = NO"
else:
if (cur_temp > alarm_hi) or (cur_temp < alarm_lo):
msg = email.mime.Multipart.MIMEMultipart()
msg['Subject'] = 'Temperatur Alarm'
msg['From'] = 'Absender'
msg['To'] = 'empfanger'
# The main body is just another attachment
body = email.mime.Text.MIMEText(cur_temp)
msg.attach(body)
# send via Gmail server
# NOTE: my ISP, Centurylink, seems to be automatically rewriting
# port 25 packets to be port 587 and it is trashing port 587 packets.
# So, I use the default port 25, but I authenticate.
s = smtplib.SMTP('smtp.gmail.com:587')
s.starttls()
s.login('Absender','Passwort')
s.sendmail('Empfänger',['empfanger'], msg.as_string())
s.quit()
else:
print "o.k."
sys.exit(0)
Alles anzeigen
ich hoffe ihr könnt mir weiterhelfen?
mfg Ghoethe