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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import paho.mqtt.client as mqtt
  2. import json
  3. from raspySensors import RaspySensors
  4. #region global Varaibles
  5. sensors = RaspySensors()
  6. client = mqtt.Client()
  7. sensorData = {
  8. "Air Temperature [°C]" : 0,
  9. "Air Humidity [%]" : 0,
  10. "Earth Humidity [%]" : 0,
  11. "Brightness [Lux]" : 0,
  12. "Plant ID": 0,
  13. "Action ID": 0
  14. }
  15. gpsPosition = {
  16. "Position": 0,
  17. "Action ID": 0
  18. }
  19. batteryStatus = {
  20. "Battery": 0,
  21. "Action ID": 0
  22. }
  23. #endregion
  24. #region global functions
  25. def measure_send_data(plantID, actionID):
  26. '''Measure data for one plant via sensor class and send via MQTT'''
  27. sensorData = sensors.readSensors()
  28. sensorData["Plant_ID"] = plantID
  29. sensorData["Action_ID"] = actionID
  30. client.publish("Robot/Data/SensorData", json.dumps(sensorData, indent=4))
  31. def init_mqtt():
  32. '''Initialise MQTT client'''
  33. mqttBroker = "mqtt.eclipseprojects.io"
  34. client = mqtt.Client("Robot")
  35. #Add callbacks
  36. client.message_callback_add("Robot/Data", send_data_json) #Testing
  37. client.message_callback_add("Robot/Action/Drive", drive_plant)
  38. client.message_callback_add("Robot/Action/GetPosition", get_position)
  39. client.message_callback_add("Robot/Action/GetBattery", get_BatteryStatus)
  40. #Subscribe to topics
  41. client.subscribe("Robot/Data") #Testing
  42. client.subscribe("Robot/Action/Drive")
  43. client.subscribe("Robot/Action/GetPosition")
  44. client.subscribe("Robot/Action/GetBattery")
  45. client.connect(mqttBroker)
  46. #endregion
  47. #region MQTT callbacks
  48. #Testing
  49. def send_data_json(client, userdata, message):
  50. strIn = str(message.payload.decode("UTF-8"))
  51. dataDict = json.loads(strIn)
  52. print("Received data: ", json.dumps(dataDict))
  53. def drive_plant(clients, userdata, message):
  54. '''Function to drive to plant according to request'''
  55. #[ ]TODO handle MQTT message
  56. print(f"Driving to plant {message}")
  57. #[ ]TODO Start drive forward -> Thread
  58. print(f"Measuring data at Plant {message}")
  59. measure_send_data() # With Signal not here
  60. print("Driving back to start position")
  61. #[ ]TODO Start Drive Back Function in Thread
  62. print("Back at starting Position") #Signal
  63. def get_position(clients, userdata, message):
  64. '''Callback function for GPS position request
  65. Function to send actual GPS position via MQTT'''
  66. #[ ]TODO handle MQTT message
  67. gpsPosition["Position"] = sensors.readPosition()
  68. gpsPosition["Action_ID"] = message
  69. client.publish("Robot/Data/Position", json.dumps(gpsPosition, indent=4))
  70. def get_BatteryStatus(clients, userdata, message):
  71. '''Callback function for battery status request
  72. Function to read battery status from ev3 and send via MQTT'''
  73. #[ ]TODO handle MQTT message
  74. batteryStatus["Battery"] = sensors.readBattery()
  75. batteryStatus["Action ID"] = message
  76. client.publish("Robot/Data/Battery", json.dumps(batteryStatus, indent=4))
  77. batteryStatus()
  78. #endregion
  79. def main():
  80. init_mqtt()
  81. dataDict = {} #Testing
  82. client.loop_start()
  83. while True:
  84. pass
  85. if __name__ == "__main__":
  86. main()