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

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. from raspy_sensors import RaspySensors
  12. from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL
  13. def on_connect(client: mqtt.Client, userdata, flags, rc):
  14. """
  15. This method gets called, when it connects to a mqtt broker.
  16. It is used to subscribe to the specific topics
  17. Args:
  18. client (mqtt.Client): current mqtt client
  19. userdata (_type_): _description_
  20. flags (_type_): _description_
  21. rc (_type_): _description_
  22. """
  23. if rc == 0:
  24. #Add callbacks
  25. client.message_callback_add("ROBOT/DATA", functions.send_data_json) #Testing
  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("ROBOT/DATA") #Testing
  31. client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
  32. client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
  33. client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
  34. print("MQTT initialized")
  35. def main():
  36. """
  37. Initialises MQTT and Sensors
  38. Runs forever and controlls all robot functions
  39. """
  40. client = mqtt.Client(RASPI_CLIENT_ID)
  41. client.on_connect = on_connect
  42. client.connect(MQTT_BROKER_GLOBAL)
  43. # dataDict = {} #Testing
  44. # CHECK forever or start
  45. client.loop_start()
  46. print("Starting Loop")
  47. while True:
  48. # print("Looping")
  49. # time.sleep(1)
  50. pass
  51. if __name__ == "__main__":
  52. main()