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.

main.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. created by caliskan at 19.04.2023
  3. This file contains the main script for the backend server of smart garden project
  4. It has the task to be a bridge between the frontend and robot.
  5. It also contains a database with the current plant data
  6. Used protocol for interaction: mqtt (paho-mqtt module)
  7. """
  8. # imports
  9. import paho.mqtt.client as mqtt
  10. from software.defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL, Topics
  11. from plantdatabase import PlantDataBase
  12. import data_functions
  13. # inits
  14. mydatabase = PlantDataBase()
  15. mydatabase.create_table()
  16. def on_connect(client: mqtt.Client, userdata, flags, rc):
  17. """
  18. This method gets called, when it connects to a mqtt broker.
  19. It is used to subscribe to the specific topics
  20. :param client: mqtt client object
  21. :param userdata:
  22. :param flags:
  23. :param rc: connection flag
  24. :return:
  25. """
  26. if rc == 0:
  27. print("connected")
  28. # TOPIC SUBSCRIPTIONS
  29. # From Robot:
  30. client.subscribe(Topics['ROBOT_DATA_SENSORDATA'])
  31. client.message_callback_add(Topics['ROBOT_DATA_SENSORDATA'], data_functions.data_sensordata)
  32. client.subscribe(Topics['ROBOT_DATA_POSITION'])
  33. client.message_callback_add(Topics['ROBOT_DATA_POSITION'], data_functions.data_position)
  34. client.subscribe(Topics['ROBOT_DATA_BATTERY'])
  35. client.message_callback_add(Topics['ROBOT_DATA_BATTERY'], data_functions.data_sensordata)
  36. # client.subscribe('Robot/Data/Picture')
  37. # From FrontEnd:
  38. client.subscribe(Topics['BACKEND_ACTION_DRIVE'])
  39. client.message_callback_add(Topics['BACKEND_ACTION_DRIVE'], data_functions.action_drive)
  40. client.subscribe(Topics['BACKEND_ACTION_GETPOSITION'])
  41. client.message_callback_add(Topics['BACKEND_ACTION_GETPOSITION'], data_functions.action_getposition)
  42. client.subscribe(Topics['BACKEND_ACTION_GETBATTERY'])
  43. client.message_callback_add(Topics['BACKEND_ACTION_GETBATTERY'], data_functions.action_getbattery)
  44. client.subscribe(Topics['BACKEND_ACTION_GETALLDATA'])
  45. client.message_callback_add(Topics['BACKEND_ACTION_GETALLDATA'], data_functions.action_getalldata)
  46. # END TOPIC SUBSCRIPTIONS
  47. else:
  48. print("connection failed")
  49. def main():
  50. client = mqtt.Client()
  51. client.on_connect = on_connect
  52. client.connect(MQTT_BROKER_GLOBAL)
  53. client.loop_forever()
  54. if __name__ == "__main__":
  55. main()