Added try/except in sensor adding part

Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>
master
Ebbe Baß 2022-08-03 17:18:13 +02:00
parent 3167fff6e6
commit dee007196b
1 changed files with 36 additions and 33 deletions

69
main.py
View File

@ -40,45 +40,48 @@ def relay_controller(pin: int, state: str):
error_print("Unknown state ("+state+") for relay channel "+str(pin)) error_print("Unknown state ("+state+") for relay channel "+str(pin))
#ultrasonic #ultrasonic
def get_water_level(): try:
GPIO.setmode(GPIO.BCM) def get_water_level():
GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM)
GPIO_TRIGGER = 24 GPIO.setwarnings(False)
GPIO_ECHO = 25 GPIO_TRIGGER = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO_ECHO = 25
GPIO.setup(GPIO_ECHO, GPIO.IN) GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.output(GPIO_TRIGGER, True) GPIO.setup(GPIO_ECHO, GPIO.IN)
time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, True)
GPIO.output(GPIO_TRIGGER, False) time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
startTime = time.time()
StopTime = time.time()
while GPIO.input(GPIO_ECHO) == 0:
startTime = time.time() startTime = time.time()
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time() StopTime = time.time()
TimeElapsed = StopTime - startTime while GPIO.input(GPIO_ECHO) == 0:
distance = (TimeElapsed * 34300) / 2 startTime = time.time()
rounded_distance = int(distance) while GPIO.input(GPIO_ECHO) == 1:
return str(rounded_distance) StopTime = time.time()
TimeElapsed = StopTime - startTime
distance = (TimeElapsed * 34300) / 2
rounded_distance = int(distance)
return str(rounded_distance)
debug_print("Added water level detector") print(get_water_level())
debug_print("Added water level detector")
except:
print("Adding water level sensor fail.")
#temperature #temperature
def get_temperature(): try:
i2c = busio.I2C(board.SCL, board.SDA) def get_temperature():
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76) i2c = busio.I2C(board.SCL, board.SDA)
rounded_temp = int(bmp280.temperature) bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c, address=0x76)
return str(rounded_temp) rounded_temp = int(bmp280.temperature)
return str(rounded_temp)
debug_print("Added temperature sensor") debug_print("Added temperature sensor")
print(get_temperature())
print(get_water_level()) except:
debug_print("got water level") print("Adding temperature sensor faild.")
print(get_temperature())
debug_print("got temperature")
def mqtt_on_connect(client, userdata, flags, rc): def mqtt_on_connect(client, userdata, flags, rc):
client.subscribe("strawberry-pi-greenhouse/relay/channel/1") client.subscribe("strawberry-pi-greenhouse/relay/channel/1")