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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. created by waldluis
  3. This file contains the main script for the RaspberryPi of smart garden project
  4. It has the task to control the EV3 robot and take measurements with the mounted sensor
  5. The sensor data is published to MQTT topics to be available for the backend server
  6. Used protocol for interaction: mqtt (paho-mqtt module)
  7. Interaction with the EV3 via SSH
  8. """
  9. import paho.mqtt.client as mqtt
  10. import functions
  11. from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL
  12. def on_connect(client: mqtt.Client, userdata, flags, rc):
  13. """
  14. This method gets called, when it connects to a mqtt broker.
  15. It is used to subscribe to the specific topics
  16. Args:
  17. client (mqtt.Client): current mqtt client
  18. userdata (_type_): _description_
  19. flags (_type_): _description_
  20. rc (_type_): _description_
  21. """
  22. if rc == 0:
  23. #Add callbacks
  24. client.message_callback_add(Topics["ROBOT_ACTION_DRIVE"], functions.drive_plant)
  25. client.message_callback_add(Topics["ROBOT_ACTION_GETPOSITION"], functions.get_position)
  26. client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], functions.get_BatteryStatus)
  27. #Subscribe to topics
  28. client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
  29. client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
  30. client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
  31. print("MQTT initialized")
  32. def main():
  33. """
  34. Initialises MQTT and Sensors
  35. Runs forever and controlls all robot functions
  36. """
  37. client = mqtt.Client(RASPI_CLIENT_ID)
  38. client.on_connect = on_connect
  39. client.connect(MQTT_BROKER_GLOBAL)
  40. # CHECK forever or start
  41. client.loop_start()
  42. print("Starting Loop")
  43. while True:
  44. pass
  45. if __name__ == "__main__":
  46. main()