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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #Subscribe to topics
  30. client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
  31. client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
  32. client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
  33. logging.info("MQTT initialized")
  34. def main():
  35. """
  36. Initialises MQTT and Sensors
  37. Runs forever and controlls all robot functions
  38. """
  39. logging.basicConfig(filename="robot.log", filemode="a", encoding="utf-8", level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s %(message)s',
  40. datefmt="%d-%m-%Y %H:%M:%S")
  41. logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
  42. client = mqtt.Client(RASPI_CLIENT_ID)
  43. client.on_connect = on_connect
  44. client.connect(MQTT_BROKER_GLOBAL)
  45. # CHECK forever or start
  46. client.loop_start()
  47. logging.info("Robot initialised")
  48. while True:
  49. pass
  50. if __name__ == "__main__":
  51. main()