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 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. def on_message(client, userdata, message):
  14. print(f'message received! {message.topic}')
  15. data_handler(client, message)
  16. def on_connect(client, userdata, flags, rc):
  17. if rc == 0:
  18. print("connected")
  19. # TOPIC SUBSCRIPTIONS
  20. client.subscribe("planttest")
  21. # Robot topics
  22. client.subscribe("Robot/Data/SensorData")
  23. client.subscribe("Robot/Data/Position")
  24. client.subscribe("Robot/Data/Battery")
  25. client.subscribe("Robot/Data/Picture")
  26. # FrontEnd topics
  27. client.subscribe("BackEnd/Action/All")
  28. client.subscribe("BackEnd/Action/GetPosition")
  29. client.subscribe("BackEnd/Action/GetBattery")
  30. client.subscribe("BackEnd/Action/GetAllData")
  31. # END TOPIC SUBSCRIPTIONS
  32. else:
  33. print("connection failed")
  34. def main():
  35. mydatabase = PlantDataBase()
  36. mydatabase.create_table()
  37. client = mqtt.Client()
  38. client.on_connect = on_connect
  39. client.on_message = on_message
  40. client.connect(MQTT_BROKER_GLOBAL)
  41. client.loop_forever()
  42. if __name__ == "__main__":
  43. main()