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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL
  11. from plantdatabase import PlantDataBase
  12. from data_handling_functions import data_handler
  13. # inits
  14. mydatabase = PlantDataBase()
  15. mydatabase.create_table()
  16. def on_message(client: mqtt.Client, userdata, message):
  17. """
  18. This method gets called, if a subscribed channel gets a new message.
  19. Message gets forwarded to the data handler
  20. :param client: mqtt client object
  21. :param userdata:
  22. :param message: received message object
  23. :return: None
  24. """
  25. print(type(message))
  26. print(f'message received! {message.topic}')
  27. data_handler(client, message, mydatabase)
  28. def on_connect(client: mqtt.Client, userdata, flags, rc):
  29. """
  30. This method gets called, when it connects to a mqtt broker.
  31. It is used to subscribe to the specific topics
  32. :param client: mqtt client object
  33. :param userdata:
  34. :param flags:
  35. :param rc: connection flag
  36. :return:
  37. """
  38. if rc == 0:
  39. print("connected")
  40. # TOPIC SUBSCRIPTIONS
  41. # From Robot:
  42. client.subscribe('Robot/Data/SensorData')
  43. client.subscribe('Robot/Data/Position')
  44. client.subscribe('Robot/Data/Battery')
  45. # client.subscribe('Robot/Data/Picture')
  46. # From FrontEnd:
  47. client.subscribe('BackEnd/Action/Drive')
  48. client.subscribe('BackEnd/Action/GetPosition')
  49. client.subscribe('BackEnd/Action/GetBattery')
  50. client.subscribe('BackEnd/Action/GetAllData')
  51. # END TOPIC SUBSCRIPTIONS
  52. else:
  53. print("connection failed")
  54. def main():
  55. client = mqtt.Client()
  56. client.on_connect = on_connect
  57. client.on_message = on_message
  58. client.connect(MQTT_BROKER_GLOBAL)
  59. client.loop_forever()
  60. if __name__ == "__main__":
  61. main()