SHH error handling, sensors class changed to functions
This commit is contained in:
parent
20bad535eb
commit
a6259e5392
@ -14,8 +14,14 @@ def measure_send_data(plantID, actionID, client: mqtt.Client):
|
||||
actionID (_type_): current ID of driving action
|
||||
client (mqtt.Client): current mqtt client for publishing
|
||||
"""
|
||||
sensor = Sensors.RaspySensors()
|
||||
sensorData = sensor.readSensors()
|
||||
try:
|
||||
sensorData = Sensors.readSensors()
|
||||
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
|
||||
|
||||
|
||||
sensorData["PlantID"] = plantID
|
||||
sensorData["ActionID"] = actionID
|
||||
client.publish(Topics["ROBOT_DATA_SENSORDATA"], json.dumps(sensorData, indent=4))
|
||||
@ -33,16 +39,30 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client):
|
||||
client (mqtt.Client): current mqtt client for publishing
|
||||
"""
|
||||
# FIXME Change to color code driving
|
||||
os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantID}.py')
|
||||
# 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/plant_{plantID}.py')
|
||||
# errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_plant.py {plantID}')
|
||||
|
||||
# # TODO Error Message
|
||||
if errorCode != 0:
|
||||
print(f"Robot Error {errorCode} occurred")
|
||||
print("Drive Plant aborted, Robot at starting position")
|
||||
return
|
||||
|
||||
print("Measuring Sensors")
|
||||
|
||||
#FIXME Sensor not working
|
||||
# measure_send_data(plantID, actionID, client)
|
||||
measure_send_data(plantID, actionID, client)
|
||||
|
||||
|
||||
print("Robot driving home")
|
||||
os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}')
|
||||
errorCode = os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {plantID}')
|
||||
|
||||
# TODO Error Message
|
||||
if errorCode != 0:
|
||||
print(f"Robot Error {errorCode} occurred")
|
||||
print(f"Drive Home aborted, Robot at plant {plantID}")
|
||||
return
|
||||
|
||||
print("Robot home")
|
||||
|
||||
#TODO decide about robot occupied message
|
||||
@ -51,13 +71,6 @@ def drive_plant_thread(plantID, actionID, client: mqtt.Client):
|
||||
|
||||
#region MQTT callbacks
|
||||
|
||||
#Testing
|
||||
def send_data_json(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
|
||||
strIn = str(message.payload.decode("UTF-8"))
|
||||
dataDict = json.loads(strIn)
|
||||
print("Received data: ", json.dumps(dataDict))
|
||||
|
||||
|
||||
def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage):
|
||||
"""
|
||||
Function to drive to plant according to request
|
||||
|
@ -10,7 +10,7 @@ Interaction with the EV3 via SSH
|
||||
|
||||
import paho.mqtt.client as mqtt
|
||||
import functions
|
||||
from raspy_sensors import RaspySensors
|
||||
|
||||
from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL
|
||||
|
||||
|
||||
@ -27,13 +27,11 @@ def on_connect(client: mqtt.Client, userdata, flags, rc):
|
||||
"""
|
||||
if rc == 0:
|
||||
#Add callbacks
|
||||
client.message_callback_add("ROBOT/DATA", functions.send_data_json) #Testing
|
||||
client.message_callback_add(Topics["ROBOT_ACTION_DRIVE"], functions.drive_plant)
|
||||
client.message_callback_add(Topics["ROBOT_ACTION_GETPOSITION"], functions.get_position)
|
||||
client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], functions.get_BatteryStatus)
|
||||
|
||||
#Subscribe to topics
|
||||
client.subscribe("ROBOT/DATA") #Testing
|
||||
client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
|
||||
client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
|
||||
client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
|
||||
@ -50,7 +48,6 @@ def main():
|
||||
client = mqtt.Client(RASPI_CLIENT_ID)
|
||||
client.on_connect = on_connect
|
||||
client.connect(MQTT_BROKER_GLOBAL)
|
||||
# dataDict = {} #Testing
|
||||
|
||||
# CHECK forever or start
|
||||
client.loop_start()
|
||||
|
@ -1,94 +1,103 @@
|
||||
#from picamera import PiCamera
|
||||
# from picamera import PiCamera
|
||||
import adafruit_dht
|
||||
import adafruit_tsl2561
|
||||
import board
|
||||
import json
|
||||
|
||||
class RaspySensors:
|
||||
"""
|
||||
Class to handle all sensors
|
||||
|
||||
# TODO
|
||||
def readDHT22():
|
||||
# Air Temperature & Humidity
|
||||
try:
|
||||
dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
|
||||
except:
|
||||
raise Exception("DHT22 not connected")
|
||||
|
||||
# read DHT22
|
||||
# if Error reading Data try again
|
||||
while True:
|
||||
try:
|
||||
sensorData["AirTemperature"] = dht22.temperature
|
||||
sensorData["AirHumidity"] = dht22.humidity
|
||||
except:
|
||||
continue
|
||||
|
||||
break
|
||||
|
||||
# TODO
|
||||
def readTSL2561():
|
||||
# Brightness
|
||||
try:
|
||||
tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
|
||||
except:
|
||||
raise Exception("TSL2561 not connected")
|
||||
|
||||
# read TSL2561
|
||||
if type(tsl2561.lux) == type(None): # Max Value 40.000 -> above error
|
||||
sensorData["Brightness"] = 40000
|
||||
else:
|
||||
sensorData["Brightness"] = int(tsl2561.lux)
|
||||
|
||||
|
||||
# TODO SoilMoisture Sensor Function
|
||||
|
||||
|
||||
# TODO Function for all sensors
|
||||
def readSensors():
|
||||
"""
|
||||
Read all Sensors and return Dictionary with data
|
||||
|
||||
Returns:
|
||||
sensordata (dict): all data of sensors
|
||||
"""
|
||||
|
||||
|
||||
# global Variables
|
||||
sensorData = {
|
||||
"AirTemperature": 0.0,
|
||||
"AirHumidity": 0.0,
|
||||
"SoilMoisture": 0.0,
|
||||
"Brightness": 0,
|
||||
"PlantID": 0,
|
||||
"ActionID": 0,
|
||||
}
|
||||
|
||||
print("TEST")
|
||||
|
||||
|
||||
return sensorData
|
||||
|
||||
|
||||
# TODO - take picture function
|
||||
def takePicture():
|
||||
"""
|
||||
Take picture and return picture
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
picture = ""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Init all Sensors
|
||||
"""
|
||||
#TODO Message if Error
|
||||
return picture
|
||||
|
||||
|
||||
#Air Temperature & Humidity
|
||||
self.dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False)
|
||||
# TODO - read position with sensor
|
||||
def readPosition():
|
||||
"""
|
||||
Read and return Position
|
||||
|
||||
#Brightness
|
||||
self.tsl2561 = adafruit_tsl2561.TSL2561(board.I2C())
|
||||
|
||||
#global Variables
|
||||
self.sensorData ={
|
||||
"AirTemperature": 0.0,
|
||||
"AirHumidity" : 0.0,
|
||||
"SoilMoisture" : 0.0,
|
||||
"Brightness" : 0,
|
||||
"PlantID": 0,
|
||||
"ActionID": 0
|
||||
}
|
||||
|
||||
def readSensors(self):
|
||||
"""
|
||||
Read all Sensors and return Dictionary with data
|
||||
|
||||
Returns:
|
||||
sensordata (dict): all data of sensors
|
||||
"""
|
||||
|
||||
#read DHT22
|
||||
#if Error reading Data try again
|
||||
while True:
|
||||
try:
|
||||
self.sensorData["AirTemperature"] = self.dht22.temperature
|
||||
self.sensorData["AirHumidity"] = self.dht22.humidity
|
||||
except:
|
||||
continue
|
||||
|
||||
break
|
||||
|
||||
#read TSL2561
|
||||
if type(self.tsl2561.lux) == type(None): #Max Value 40.000 -> above error
|
||||
self.sensorData["Brightness"] = 40000
|
||||
else:
|
||||
self.sensorData["Brightness"] = int(self.tsl2561.lux)
|
||||
|
||||
#TODO SoilMoisture Sensor
|
||||
|
||||
return self.sensorData
|
||||
|
||||
#TODO - take picture function
|
||||
def takePicture(self):
|
||||
"""
|
||||
Take picture and return picture
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
return self.picture
|
||||
|
||||
#TODO - read position with sensor
|
||||
def readPosition(self):
|
||||
"""
|
||||
Read and return Position
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
return self.position
|
||||
Returns:
|
||||
_type_: _description_
|
||||
"""
|
||||
position = ""
|
||||
return position
|
||||
|
||||
|
||||
#for Testing only
|
||||
# for Testing only
|
||||
def main():
|
||||
sensors = RaspySensors()
|
||||
test = sensors.readSensors()
|
||||
print("Data:" + json.dumps(test))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
x
Reference in New Issue
Block a user