Sensor error handling added

This commit is contained in:
Luis Waldhauser 2023-05-07 12:32:50 +02:00
parent 56f9065fc7
commit 9f12f21ac4
2 changed files with 67 additions and 39 deletions

View File

@ -3,7 +3,7 @@ import json
import threading import threading
import os import os
import raspy_sensors as Sensors import raspy_sensors as Sensors
from defines import Topics from defines import Topics, SENSORDATA
def measure_send_data(plantID, actionID, client: mqtt.Client): def measure_send_data(plantID, actionID, client: mqtt.Client):
""" """
@ -14,13 +14,13 @@ def measure_send_data(plantID, actionID, client: mqtt.Client):
actionID (_type_): current ID of driving action actionID (_type_): current ID of driving action
client (mqtt.Client): current mqtt client for publishing client (mqtt.Client): current mqtt client for publishing
""" """
sensorData = SENSORDATA
try: try:
sensorData = Sensors.readSensors() sensorData = Sensors.readSensors()
except Exception as e: except Exception as e:
print(str(e)) print(str(e))
# TODO Error message MQTT
sensorData["PlantID"] = plantID sensorData["PlantID"] = plantID
sensorData["ActionID"] = actionID sensorData["ActionID"] = actionID
@ -39,14 +39,14 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client):
client (mqtt.Client): current mqtt client for publishing client (mqtt.Client): current mqtt client for publishing
""" """
# FIXME Change to color code driving # FIXME Change to color code driving
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py') # errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py')
# errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}') # # errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}')
# # TODO Error Message # TODO Error Message
if errorCode != 0: # if errorCode != 0:
print(f"Robot Error {errorCode} occurred") # print(f"Robot Error {errorCode} occurred")
print("Drive Plant aborted, Robot at starting position") # print("Drive Plant aborted, Robot at starting position")
return # return
print("Measuring Sensors") print("Measuring Sensors")

View File

@ -3,40 +3,59 @@ import adafruit_dht
import adafruit_tsl2561 import adafruit_tsl2561
import board import board
import json import json
from defines import SENSORDATA
# TODO
def readDHT22(): def readDHT22():
# Air Temperature & Humidity """
Reads DHT22 air temperature and air humidity and return values as float
Raises:
Exception: If DHT22 not connected properly
Returns:
float: air temperature in °C
float: air humidity %
"""
try: try:
dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False) dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
except: except:
raise Exception("DHT22 not connected") raise Exception("DHT22 not connected")
# read DHT22 # read DHT22
# if Error reading Data try again try:
while True: temperature = dht22.temperature
try: humidity = dht22.humidity
sensorData["AirTemperature"] = dht22.temperature except:
sensorData["AirHumidity"] = dht22.humidity raise Exception("DHT22 not connected")
except:
continue return temperature, humidity
break
# TODO
def readTSL2561(): def readTSL2561():
# Brightness """
Reads TSL2561 brightness in Lux
Raises:
Exception: If TSL2561 not connected properly
Returns:
int: brightness in Lux
"""
try: try:
tsl2561 = adafruit_tsl2561.TSL2561(board.I2C()) tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
except: except:
raise Exception("TSL2561 not connected") raise Exception("TSL2561 not connected")
# read TSL2561 # read TSL2561
brightness = 0
if type(tsl2561.lux) == type(None): # Max Value 40.000 -> above error if type(tsl2561.lux) == type(None): # Max Value 40.000 -> above error
sensorData["Brightness"] = 40000 brightness = 40000
else: else:
sensorData["Brightness"] = int(tsl2561.lux) brightness = int(tsl2561.lux)
return brightness
# TODO SoilMoisture Sensor Function # TODO SoilMoisture Sensor Function
@ -45,25 +64,34 @@ def readTSL2561():
# TODO Function for all sensors # TODO Function for all sensors
def readSensors(): def readSensors():
""" """
Read all Sensors and return Dictionary with data Read DHT22, TSL2561 and Humidity Sensor
Raises:
Exception: DHT22 not connected
Exception: TSL2561 not connected
Returns: Returns:
sensordata (dict): all data of sensors dict: Sensordata
""" """
sensorData = SENSORDATA
errorMessage = ""
try:
sensorData["AirTemperature"], sensorData["AirHumidity"] = readDHT22()
except Exception as e:
sensorData["AirHumidity"] = 0 # otherwise old value
sensorData["AirTemperature"] = 0
errorMessage = str(e) + "\n"
# global Variables try:
sensorData = { sensorData["Brightness"] = readTSL2561()
"AirTemperature": 0.0, except Exception as e:
"AirHumidity": 0.0, sensorData["Brightness"] = 0 # otherwise old value
"SoilMoisture": 0.0, errorMessage = errorMessage + str(e) + "\n"
"Brightness": 0,
"PlantID": 0,
"ActionID": 0,
}
print("TEST")
# combined error message
if errorMessage != "":
raise Exception(errorMessage)
return sensorData return sensorData