Raspberry pi automatische Bewässerung
Verfasst: Sonntag 3. Mai 2020, 10:18
Hallo liebe Leute
,
ich in recht neu in der Sprache und habe ein Kleine Projekt für meine Büropflanzen rausgesucht, um diese zu bewässern.
Leider erhalte ich derzeit folgende Fehlermeldung:
---------------------------------------------------------------------------------------------------------------------------------------
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "Neu.py", line 36, in run
humidity = self.get_sync_humidity(self.potconfig['sensorchannel'], self.potconfig['sensorgpio'])
TypeError: 'NoneType' object has no attribute '__getitem__'
-------------------------------------------------------------------------------------------------------------------------------------------
Mein Skript selber sieht wie folgt aus:
-------------------------------------------------------------------------------------------------------------------------------------------
import datetime
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import RPi.GPIO as GPIO
import threading
import yaml
import pprint
import smtplib
from influxdb import InfluxDBClient
class PotThread(threading.Thread):
#args ist ein pot-dict
def __init__(self, group=None, monitoronly=False, influxclient=None, target=None, threadname=None, debug=None, args=()):
threading.Thread.__init__(self, group=group, target=target, name=threadname)
if 'pot' in args:
self.potconfig=args['pot']
else:
self.potconfig={}
if threadname:
self.threadname=threadname
else:
if self.potconfig and "name" in self.potconfig:
self.threadname = self.potconfig['name']
else:
self.threadname = "Unknown"
self.debug = debug
self.active = True
self.influxclient = influxclient
self.monitoronly = monitoronly
def run(self):
measurements = []
while True:
if self.active:
humidity = self.get_sync_humidity(self.potconfig['sensorchannel'], self.potconfig['sensorgpio'])
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Humidity: "+str(humidity))
if self.influxclient:
measurement = {
'measurement': 'humidity',
'tags': {
'name': self.threadname
},
'time' : time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
'fields': {
'level':humidity
}
}
measurements.append(measurement)
try:
self.influxclient.write_points(measurements)
measurements=[]
except:
print ("Influx failed for "+self.threadname)
if humidity > int(self.potconfig['schwellwert']):
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump on ")
self.pump_on(self.potconfig['pumpsekunden'], self.potconfig['relaisgpio'])
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump off ")
time.sleep(self.potconfig['messpause2'])
humidity2 = self.get_sync_humidity(self.potconfig['sensorchannel'], self.potconfig['sensorgpio'])
if humidity2 > int(self.potconfig['schwellwert2']):
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump on ")
self.pump_on(self.potconfig['pumpsekunden2'], self.potconfig['relaisgpio'])
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump off ")
time.sleep(self.potconfig['messpause'])
def pump_on(self, seconds, gpio):
if not self.monitoronly:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(gpio, GPIO.OUT)
GPIO.output(gpio, 0)
time.sleep(int(seconds))
GPIO.output(gpio, 1)
def get_sync_humidity(self, sensorchannel, sensorgpio):
SPI_PORT = 0
SPI_DEVICE = 0
lock = threading.RLock()
lock.acquire()
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(sensorgpio, GPIO.OUT)
GPIO.output(sensorgpio, 1)
time.sleep(3)
hum = mcp.read_adc(sensorchannel)
GPIO.output(sensorgpio, 0)
mcp._spi.close()
lock.release()
return(hum)
def set_active(self, active):
if self.debug:
print (self.threadname+" Setting active to: "+str(active))
self.active = active
if __name__ == '__main__':
configfile = open("config.yml", "r")
configyml = configfile.read()
configfile.close()
config=yaml.load(configyml, Loader=yaml.Loader)
influxclient = None
if "influx" in config:
influxclient = InfluxDBClient(config['influx']['server'], 8086, config['influx']['user'], config['influx']['password'], config['influx']['database'])
debug = config['debug']
monitoronly = False
if "monitoronly" in config:
monitoronly = config["monitoronly"]
children = []
for pot in config['pots']:
pt = PotThread(debug=debug, influxclient=influxclient, monitoronly=monitoronly, args=(pot))
pt.start()
children.append(pt)
tankpot = {}
tankthread = PotThread(monitoronly=True, debug=debug, args=(tankpot))
while True:
tankhum = tankthread.get_sync_humidity(config['tank']['sensorchannel'], config['tank']['sensorgpio'])
if debug:
print ("Tank: "+str(tankhum))
if tankhum > config['tank']['schwellwert']:
mailserver = smtplib.SMTP(config['mail']['server'])
mailserver.sendmail(config['mail']['from'], config['mail']['to'], "Please fill water tank")
mailserver.quit()
print ("Please fill tank")
for pot in children:
pot.set_active(False)
else:
for pot in children:
pot.set_active(True)
time.sleep(config['tank']['messpause'])
-------------------------------------------------------------------------------------------------------------------------------------------
Ich hoffe mir kann da einer weiterhelfen
Vielen Dank

ich in recht neu in der Sprache und habe ein Kleine Projekt für meine Büropflanzen rausgesucht, um diese zu bewässern.
Leider erhalte ich derzeit folgende Fehlermeldung:
---------------------------------------------------------------------------------------------------------------------------------------
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "Neu.py", line 36, in run
humidity = self.get_sync_humidity(self.potconfig['sensorchannel'], self.potconfig['sensorgpio'])
TypeError: 'NoneType' object has no attribute '__getitem__'
-------------------------------------------------------------------------------------------------------------------------------------------
Mein Skript selber sieht wie folgt aus:
-------------------------------------------------------------------------------------------------------------------------------------------
import datetime
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import RPi.GPIO as GPIO
import threading
import yaml
import pprint
import smtplib
from influxdb import InfluxDBClient
class PotThread(threading.Thread):
#args ist ein pot-dict
def __init__(self, group=None, monitoronly=False, influxclient=None, target=None, threadname=None, debug=None, args=()):
threading.Thread.__init__(self, group=group, target=target, name=threadname)
if 'pot' in args:
self.potconfig=args['pot']
else:
self.potconfig={}
if threadname:
self.threadname=threadname
else:
if self.potconfig and "name" in self.potconfig:
self.threadname = self.potconfig['name']
else:
self.threadname = "Unknown"
self.debug = debug
self.active = True
self.influxclient = influxclient
self.monitoronly = monitoronly
def run(self):
measurements = []
while True:
if self.active:
humidity = self.get_sync_humidity(self.potconfig['sensorchannel'], self.potconfig['sensorgpio'])
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Humidity: "+str(humidity))
if self.influxclient:
measurement = {
'measurement': 'humidity',
'tags': {
'name': self.threadname
},
'time' : time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
'fields': {
'level':humidity
}
}
measurements.append(measurement)
try:
self.influxclient.write_points(measurements)
measurements=[]
except:
print ("Influx failed for "+self.threadname)
if humidity > int(self.potconfig['schwellwert']):
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump on ")
self.pump_on(self.potconfig['pumpsekunden'], self.potconfig['relaisgpio'])
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump off ")
time.sleep(self.potconfig['messpause2'])
humidity2 = self.get_sync_humidity(self.potconfig['sensorchannel'], self.potconfig['sensorgpio'])
if humidity2 > int(self.potconfig['schwellwert2']):
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump on ")
self.pump_on(self.potconfig['pumpsekunden2'], self.potconfig['relaisgpio'])
if self.debug:
print (str(datetime.datetime.now())+" "+self.threadname+" Pump off ")
time.sleep(self.potconfig['messpause'])
def pump_on(self, seconds, gpio):
if not self.monitoronly:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(gpio, GPIO.OUT)
GPIO.output(gpio, 0)
time.sleep(int(seconds))
GPIO.output(gpio, 1)
def get_sync_humidity(self, sensorchannel, sensorgpio):
SPI_PORT = 0
SPI_DEVICE = 0
lock = threading.RLock()
lock.acquire()
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(sensorgpio, GPIO.OUT)
GPIO.output(sensorgpio, 1)
time.sleep(3)
hum = mcp.read_adc(sensorchannel)
GPIO.output(sensorgpio, 0)
mcp._spi.close()
lock.release()
return(hum)
def set_active(self, active):
if self.debug:
print (self.threadname+" Setting active to: "+str(active))
self.active = active
if __name__ == '__main__':
configfile = open("config.yml", "r")
configyml = configfile.read()
configfile.close()
config=yaml.load(configyml, Loader=yaml.Loader)
influxclient = None
if "influx" in config:
influxclient = InfluxDBClient(config['influx']['server'], 8086, config['influx']['user'], config['influx']['password'], config['influx']['database'])
debug = config['debug']
monitoronly = False
if "monitoronly" in config:
monitoronly = config["monitoronly"]
children = []
for pot in config['pots']:
pt = PotThread(debug=debug, influxclient=influxclient, monitoronly=monitoronly, args=(pot))
pt.start()
children.append(pt)
tankpot = {}
tankthread = PotThread(monitoronly=True, debug=debug, args=(tankpot))
while True:
tankhum = tankthread.get_sync_humidity(config['tank']['sensorchannel'], config['tank']['sensorgpio'])
if debug:
print ("Tank: "+str(tankhum))
if tankhum > config['tank']['schwellwert']:
mailserver = smtplib.SMTP(config['mail']['server'])
mailserver.sendmail(config['mail']['from'], config['mail']['to'], "Please fill water tank")
mailserver.quit()
print ("Please fill tank")
for pot in children:
pot.set_active(False)
else:
for pot in children:
pot.set_active(True)
time.sleep(config['tank']['messpause'])
-------------------------------------------------------------------------------------------------------------------------------------------
Ich hoffe mir kann da einer weiterhelfen

Vielen Dank