55 lines
1.5 KiB
Python
Raw Normal View History

"""
created by caliskan at 19.04.2023
This file contains the main script for the backend server of smart garden project
It has the task to be a bridge between the frontend and robot.
It also contains a database with the current plant data
Used protocol for interaction: mqtt (paho-mqtt module)
"""
# imports
import paho.mqtt.client as mqtt
from defines import MQTT_BROKER_LOCAL, MQTT_BROKER_GLOBAL
from plantdatabase import PlantDataBase
def on_message(client, userdata, message):
print(f'message received! {message.topic}')
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("connected")
# TOPIC SUBSCRIPTIONS
client.subscribe("planttest")
# Robot topics
client.subscribe("Robot/Data/SensorData")
client.subscribe("Robot/Data/Position")
client.subscribe("Robot/Data/Battery")
client.subscribe("Robot/Data/Picture")
# FrontEnd topics
client.subscribe("BackEnd/Data/SensorData")
client.subscribe("BackEnd/Data/SensorDataAll")
client.subscribe("BackEnd/Data/Position")
client.subscribe("BackEnd/Data/Battery")
client.subscribe("BackEnd/Data/Picture")
# END TOPIC SUBSCRIPTIONS
else:
print("connection failed")
def main():
mydatabase = PlantDataBase()
mydatabase.create_table()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER_GLOBAL)
client.loop_forever()
if __name__ == "__main__":
main()