contains all constants for the backend architecture of the smart garden project | contains all constants for the backend architecture of the smart garden project | ||||
""" | """ | ||||
MQTT_BROKER_LOCAL = "lorem ipsum" | |||||
MQTT_BROKER_LOCAL = "192.168.0.199" | |||||
MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" | MQTT_BROKER_GLOBAL = "mqtt.eclipseprojects.io" | ||||
RASPI_CLIENT_ID = "smart_farming_raspi" | RASPI_CLIENT_ID = "smart_farming_raspi" | ||||
BACKEND_CLIENT_ID = "smart_farming_server" | BACKEND_CLIENT_ID = "smart_farming_server" | ||||
"ROBOT_DATA_PICTURE": "ROBOT/DATA/PICTURE", | "ROBOT_DATA_PICTURE": "ROBOT/DATA/PICTURE", | ||||
"BACKEND_ACTION_DRIVE": "BACKEND/ACTION/DRIVE", | "BACKEND_ACTION_DRIVE": "BACKEND/ACTION/DRIVE", | ||||
"BACKEND_ACTION_DRIVEPALL": "BACKEND/ACTION/DRIVEALL", | |||||
"BACKEND_ACTION_GETPOSITION": "BACKEND/ACTION/GETPOSITION", | "BACKEND_ACTION_GETPOSITION": "BACKEND/ACTION/GETPOSITION", | ||||
"BACKEND_ACTION_GETBATTERY": "BACKEND/ACTION/GETBATTERY", | "BACKEND_ACTION_GETBATTERY": "BACKEND/ACTION/GETBATTERY", | ||||
"BACKEND_ACTION_GETALLDATA": "BACKEND/ACTION/GETALLDATA", | "BACKEND_ACTION_GETALLDATA": "BACKEND/ACTION/GETALLDATA", | ||||
"BACKEND_ACTION_NEWPLANT": "BACKEND/ACTION/NEWPLANT", | |||||
"BACKEND_ACTION_CONFIGUREPLANT": "BACKEND/ACTION/CONFIGUREPLANT", | |||||
"BACKEND_ACTION_DELETEPLANT": "BACKEND/ACTION/DELETEPLANT", | |||||
"BACKEND_ACTION_PLANTCOUNT": "BACKEND/ACTION/PLANTCOUNT", | |||||
"BACKEND_DATA_SENSORDATA": "BACKEND/DATA/SENSORDATA", | "BACKEND_DATA_SENSORDATA": "BACKEND/DATA/SENSORDATA", | ||||
"BACKEND_DATA_SENSORDATAALL": "BACKEND/DATA/SENSORDATA_ALL", | "BACKEND_DATA_SENSORDATAALL": "BACKEND/DATA/SENSORDATA_ALL", | ||||
"BACKEND_DATA_POSITION": "BACKEND/DATA/POSITION", | "BACKEND_DATA_POSITION": "BACKEND/DATA/POSITION", | ||||
"BACKEND_DATA_BATTERY": "BACKEND/DATA/BATTERY", | "BACKEND_DATA_BATTERY": "BACKEND/DATA/BATTERY", | ||||
"BACKEND_DATA_PICTURE": "BACKEND/DATA/PICTURE" | |||||
"BACKEND_DATA_PICTURE": "BACKEND/DATA/PICTURE", | |||||
"BACKEND_DATA_PLANTCOUNT": "BACKEND/DATA/PLANTCOUNT", | |||||
} | |||||
} | |||||
# MQTT Messages: | # MQTT Messages: | ||||
# region Robot -> Backend | # region Robot -> Backend | ||||
SENSORDATA = { | SENSORDATA = { | ||||
"AirTemperature": 0.0, | "AirTemperature": 0.0, | ||||
"AirHumidity" : 0.0, | |||||
"SoilMoisture" : 0.0, | |||||
"Brightness" : 0, | |||||
"AirHumidity": 0.0, | |||||
"SoilMoisture": 0.0, | |||||
"Brightness": 0, | |||||
"PlantID": 0, | "PlantID": 0, | ||||
"ActionID": 0 | "ActionID": 0 | ||||
} | } | ||||
PLANTDATA = { | PLANTDATA = { | ||||
"AirTemperature": 0.0, | "AirTemperature": 0.0, | ||||
"AirHumidity" : 0.0, | |||||
"SoilMoisture" : 0.0, | |||||
"Brightness" : 0, | |||||
"AirHumidity": 0.0, | |||||
"SoilMoisture": 0.0, | |||||
"Brightness": 0, | |||||
"PlantID": 0, | "PlantID": 0, | ||||
"Timestamp": "", | "Timestamp": "", | ||||
"MeasurementID": 0 | |||||
"MeasurementID": 0, | |||||
"PlantName": "" | |||||
} | } | ||||
ALLPLANTDATA = [ | ALLPLANTDATA = [ | ||||
"Timestamp": "" | "Timestamp": "" | ||||
} | } | ||||
PLANTCOUNT = { | |||||
"CurrenCount": 0, | |||||
"maxCount": 0 | |||||
} | |||||
# endregion | # endregion | ||||
# region Frontend -> Backend | # region Frontend -> Backend | ||||
DRIVE = { | DRIVE = { | ||||
"PlantID": 0 | |||||
"PlantName": "" | |||||
} | |||||
NEWPLANT = PLANTDATA | |||||
CONFIGUREPLANT = PLANTDATA | |||||
DELETEPLANT = { | |||||
"PlantID": "" | |||||
} | } | ||||
# DRIVEALL -> no message needed | |||||
# GETPOSITION -> no message needed | # GETPOSITION -> no message needed | ||||
# GETBATTERY -> no message needed | # GETBATTERY -> no message needed | ||||
# GETALLDATA -> no message needed | # GETALLDATA -> no message needed | ||||
# endregion | |||||
# endregion |
def measure_send_data(plantID, actionID, client: mqtt.Client): | def measure_send_data(plantID, actionID, client: mqtt.Client): | ||||
""" | """ | ||||
Reads all sensors and publishes data via MQTT | |||||
Reads all sensors and publishes data via MQTT in form of SENSORDATA | |||||
Args: | Args: | ||||
plantID (_type_): plant to measure | plantID (_type_): plant to measure | ||||
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/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 | # TODO Error Message | ||||
# if errorCode != 0: | |||||
# print(f"Robot Error {errorCode} occurred") | |||||
# print("Drive Plant aborted, Robot at starting position") | |||||
# return | |||||
if errorCode != 0: | |||||
print(f"Robot Error {errorCode} occurred") | |||||
print("Drive Plant aborted, Robot at starting position") | |||||
return | |||||
print("Measuring Sensors") | print("Measuring Sensors") | ||||
#FIXME Sensor not working | |||||
measure_send_data(plantID, actionID, client) | measure_send_data(plantID, actionID, client) | ||||
print("Robot driving home") | print("Robot driving home") | ||||
errorCode = 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}') | ||||
def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): | def drive_plant(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): | ||||
""" | """ | ||||
Function to drive to plant according to request | Function to drive to plant according to request | ||||
Starting Drive in Thread | |||||
Starting Drive in Thread to not block main programm | |||||
Args: | Args: | ||||
clients (mqtt.Client): current mqtt client | clients (mqtt.Client): current mqtt client | ||||
userdata (_type_): _description_ | userdata (_type_): _description_ | ||||
message (mqtt.MQTTMessage): received message | |||||
message (mqtt.MQTTMessage): received DRIVE message with PlantID and ActionID | |||||
""" | """ | ||||
dictMessage = json.loads(str(message.payload.decode("UTF-8"))) | dictMessage = json.loads(str(message.payload.decode("UTF-8"))) | ||||
plantID = dictMessage["PlantID"] | plantID = dictMessage["PlantID"] | ||||
def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): | def get_position(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): | ||||
""" | """ | ||||
Callback function for GPS position request | Callback function for GPS position request | ||||
Function to send actual GPS position via MQTT | |||||
Function to send actual GPS position via MQTT in form of POSITION | |||||
Args: | Args: | ||||
clients (mqtt.Client): current mqtt client | clients (mqtt.Client): current mqtt client | ||||
def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): | def get_BatteryStatus(clients: mqtt.Client, userdata, message: mqtt.MQTTMessage): | ||||
""" | """ | ||||
Callback function for battery status request | Callback function for battery status request | ||||
Function to read battery status from ev3 and send via MQTT | |||||
Function to read battery status from ev3 and send via MQTT in form of BATTERY | |||||
8,5V -> 100% | 8,5V -> 100% | ||||
5V -> 0% | 5V -> 0% | ||||
batteryLevel = round(batteryLevel / 1000000, 2) # Voltage | batteryLevel = round(batteryLevel / 1000000, 2) # Voltage | ||||
batteryLevel = batteryLevel - 5 | batteryLevel = batteryLevel - 5 | ||||
batteryLevel = round(batteryLevel / 3.5, 3) *100 # Percentage | |||||
batteryLevel = round(batteryLevel / 3.5, 3) *100 # Percentage | |||||
battery = { | battery = { | ||||
"Battery": batteryLevel | "Battery": batteryLevel |
import paho.mqtt.client as mqtt | import paho.mqtt.client as mqtt | ||||
import functions | import functions | ||||
from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL | from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL | ||||
rc (_type_): _description_ | rc (_type_): _description_ | ||||
""" | """ | ||||
if rc == 0: | if rc == 0: | ||||
#Add callbacks | |||||
#Add callbacks | |||||
client.message_callback_add(Topics["ROBOT_ACTION_DRIVE"], functions.drive_plant) | 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_GETPOSITION"], functions.get_position) | ||||
client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], functions.get_BatteryStatus) | client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], functions.get_BatteryStatus) | ||||
print("Starting Loop") | print("Starting Loop") | ||||
while True: | while True: | ||||
# print("Looping") | |||||
# time.sleep(1) | |||||
pass | pass | ||||
if __name__ == "__main__": | if __name__ == "__main__": | ||||
main() | main() |
Exception: If DHT22 not connected properly | Exception: If DHT22 not connected properly | ||||
Returns: | Returns: | ||||
float: air temperature in °C | |||||
float: air humidity % | |||||
float: temperature [°C] | |||||
float: humidity [%] | |||||
""" | """ | ||||
try: | try: | ||||
dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False) | dht22 = adafruit_dht.DHT22(board.D4, use_pulseio=False) | ||||
Exception: If TSL2561 not connected properly | Exception: If TSL2561 not connected properly | ||||
Returns: | Returns: | ||||
int: brightness in Lux | |||||
int: brightness [Lux] | |||||
""" | """ | ||||
try: | try: | ||||
tsl2561 = adafruit_tsl2561.TSL2561(board.I2C()) | tsl2561 = adafruit_tsl2561.TSL2561(board.I2C()) | ||||
""" | """ | ||||
position = "" | position = "" | ||||
return position | return position | ||||
# for Testing only | |||||
def main(): | |||||
sensors = RaspySensors() | |||||
test = sensors.readSensors() | |||||
print("Data:" + json.dumps(test)) | |||||
if __name__ == "__main__": | |||||
main() |