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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import logging
  12. import sys
  13. from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL
  14. def on_connect(client: mqtt.Client, userdata, flags, rc):
  15. """
  16. This method gets called, when it connects to a mqtt broker.
  17. It is used to subscribe to the specific topics
  18. Args:
  19. client (mqtt.Client): current mqtt client
  20. userdata (_type_): _description_
  21. flags (_type_): _description_
  22. rc (_type_): _description_
  23. """
  24. if rc == 0:
  25. #Add callbacks
  26. client.message_callback_add(Topics["ROBOT_ACTION_DRIVE"], functions.drive_plant)
  27. client.message_callback_add(Topics["ROBOT_ACTION_GETPOSITION"], functions.get_position)
  28. client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], functions.get_BatteryStatus)
  29. client.message_callback_add(Topics["ROBOT_ACTION_DRIVEALL"], functions.drive_plant_all)
  30. #Subscribe to topics
  31. client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
  32. client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
  33. client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
  34. client.subscribe(Topics["ROBOT_ACTION_DRIVEALL"])
  35. logging.info("MQTT initialized")
  36. def main():
  37. """
  38. Initialises MQTT and Sensors
  39. Runs forever and controlls all robot functions
  40. """
  41. logging.basicConfig(filename="robot.log", filemode="a", encoding="utf-8", level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s %(message)s',
  42. datefmt="%d-%m-%Y %H:%M:%S")
  43. logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
  44. client = mqtt.Client(RASPI_CLIENT_ID)
  45. client.on_connect = on_connect
  46. client.connect(MQTT_BROKER_GLOBAL)
  47. # CHECK forever or start
  48. client.loop_start()
  49. logging.info("Robot initialised")
  50. while True:
  51. pass
  52. if __name__ == "__main__":
  53. main()