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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. created by waldlhauser
  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 sensors
  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, MQTT_BROKER_LOCAL
  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
  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 = mqtt.Client(RASPI_CLIENT_ID, transport="websockets") # Local Broker
  46. client.on_connect = on_connect
  47. # client.connect(MQTT_BROKER_GLOBAL)
  48. client.connect("192.168.137.197") # Local Broker
  49. logging.info("Robot initialised")
  50. client.loop_forever() # Loop_start not needed
  51. if __name__ == "__main__":
  52. main()