repository to manage all files related to the makeathon farm bot project (Software + Documentation).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainProg.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import paho.mqtt.client as mqtt
  2. import json
  3. import threading
  4. import os
  5. import signal
  6. import time
  7. from raspy_sensors import RaspySensors
  8. #region global Varaibles
  9. #sensors:RaspySensors()
  10. leftRight = 50
  11. plantNumber = 0
  12. sensorData = {
  13. "Air Temperature [°C]" : 0,
  14. "Air Humidity [%]" : 0,
  15. "Earth Humidity [%]" : 0,
  16. "Brightness [Lux]" : 0,
  17. "Plant ID": 0,
  18. "Action ID": 0
  19. }
  20. gpsPosition = {
  21. "Position": 0,
  22. "Action ID": 0
  23. }
  24. batteryStatus = {
  25. "Battery": 0,
  26. "Action ID": 0
  27. }
  28. #endregion
  29. #region global functions
  30. def measure_send_data(plantID, actionID):
  31. '''Measure data for one plant via sensor class and send via MQTT'''
  32. sensorData = sensors.readSensors()
  33. sensorData["Plant_ID"] = plantID
  34. sensorData["Action_ID"] = actionID
  35. client.publish("Robot/Data/SensorData", json.dumps(sensorData, indent=4))
  36. def drive_home():
  37. '''Function to drive robot back to starting position in thread'''
  38. thread = threading.Thread(target= os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/drive_back.py {leftRight}'), daemon=True)
  39. thread.start()
  40. def drive_plant():
  41. '''Function to drive to plant according to number from MQTT message in thread'''
  42. print(f"Driving to Plant {plantNumber}")
  43. os.system(f'sshpass -p maker ssh robot@ev3dev.local python3 /home/robot/Programme/plant_{plantNumber}.py')
  44. print("Raising Signal")
  45. signal.raise_signal(signal.SIGUSR1)
  46. def init_mqtt():
  47. '''Initialise MQTT client'''
  48. mqttBroker = "mqtt.eclipseprojects.io"
  49. global client
  50. client = mqtt.Client("Robot")
  51. #Add callbacks
  52. client.message_callback_add("ROBOT/DATA", send_data_json) #Testing
  53. client.message_callback_add("ROBOT/ACTION/DRIVE", drive_plant)
  54. client.message_callback_add("ROBOT/ACTION/GETPOSITION", get_position)
  55. client.message_callback_add("ROBOT/ACTION/GETBATTERY", get_BatteryStatus)
  56. #Subscribe to topics
  57. client.subscribe("ROBOT/DATA") #Testing
  58. client.subscribe("ROBOT/ACTION/DRIVE")
  59. client.subscribe("ROBOT/Action/GETPOSITION")
  60. client.subscribe("ROBOT/ACTION/GETBATTERY")
  61. client.connect(mqttBroker)
  62. def signal_measure():
  63. '''Signal function to measure one plant and start driving home'''
  64. print("Measuring Sensors")
  65. #measure_send_data()
  66. print("Driving Home")
  67. drive_home()
  68. #endregion
  69. #region MQTT callbacks
  70. #Testing
  71. def send_data_json(client, userdata, message):
  72. strIn = str(message.payload.decode("UTF-8"))
  73. dataDict = json.loads(strIn)
  74. print("Received data: ", json.dumps(dataDict))
  75. def drive_plant(clients, userdata, message):
  76. '''Function to drive to plant according to request'''
  77. print("received drive to plant")
  78. plantNumber = int(message.payload.decode("UTF-8"))
  79. if plantNumber % 2 == 0:
  80. leftRight = -50 #rotating left
  81. else:
  82. leftRight = 50 #rotating right
  83. thread = threading.Thread(target= drive_plant, daemon=True)
  84. thread.start()
  85. def get_position(clients, userdata, message):
  86. '''Callback function for GPS position request
  87. Function to send actual GPS position via MQTT'''
  88. #[ ]TODO handle MQTT message
  89. gpsPosition["Position"] = sensors.readPosition()
  90. gpsPosition["Action_ID"] = message
  91. client.publish("Robot/Data/Position", json.dumps(gpsPosition, indent=4))
  92. def get_BatteryStatus(clients, userdata, message):
  93. '''Callback function for battery status request
  94. Function to read battery status from ev3 and send via MQTT'''
  95. #[ ]TODO handle MQTT message
  96. batteryStatus["Battery"] = sensors.readBattery()
  97. batteryStatus["Action ID"] = message
  98. client.publish("Robot/Data/Battery", json.dumps(batteryStatus, indent=4))
  99. batteryStatus()
  100. #endregion
  101. def main():
  102. init_mqtt()
  103. print("MQTT initialized")
  104. signal.signal(signal.SIGUSR1, signal_measure)
  105. # dataDict = {} #Testing
  106. client.loop_forever()
  107. print("Starting Loop")
  108. while True:
  109. pass
  110. if __name__ == "__main__":
  111. main()