68 lines
2.0 KiB
Python
Raw Normal View History

"""
created by waldluis
This file contains the main script for the RaspberryPi of smart garden project
It has the task to control the EV3 robot and take measurements with the mounted sensor
The sensor data is published to MQTT topics to be available for the backend server
Used protocol for interaction: mqtt (paho-mqtt module)
Interaction with the EV3 via SSH
"""
import paho.mqtt.client as mqtt
import functions
2023-04-22 12:32:27 +02:00
from raspy_sensors import RaspySensors
from defines import Topics, RASPI_CLIENT_ID, MQTT_BROKER_GLOBAL
2023-04-27 17:33:24 +02:00
2023-04-22 12:32:27 +02:00
def on_connect(client: mqtt.Client, userdata, flags, rc):
"""
This method gets called, when it connects to a mqtt broker.
It is used to subscribe to the specific topics
2023-04-22 12:32:27 +02:00
Args:
client (mqtt.Client): current mqtt client
userdata (_type_): _description_
flags (_type_): _description_
rc (_type_): _description_
"""
if rc == 0:
#Add callbacks
client.message_callback_add("ROBOT/DATA", functions.send_data_json) #Testing
client.message_callback_add(Topics["ROBOT_ACTION_DRIVE"], functions.drive_plant)
client.message_callback_add(Topics["ROBOT_ACTION_GETPOSITION"], functions.get_position)
client.message_callback_add(Topics["ROBOT_ACTION_GETBATTERY"], functions.get_BatteryStatus)
#Subscribe to topics
client.subscribe("ROBOT/DATA") #Testing
client.subscribe(Topics["ROBOT_ACTION_DRIVE"])
client.subscribe(Topics["ROBOT_ACTION_GETPOSITION"])
client.subscribe(Topics["ROBOT_ACTION_GETBATTERY"])
print("MQTT initialized")
2023-04-22 14:34:46 +02:00
def main():
2023-04-24 13:04:03 +02:00
"""
Initialises MQTT and Sensors
Runs forever and controlls all robot functions
"""
client = mqtt.Client(RASPI_CLIENT_ID)
client.on_connect = on_connect
client.connect(MQTT_BROKER_GLOBAL)
2023-04-22 12:32:27 +02:00
# dataDict = {} #Testing
# CHECK forever or start
2023-04-22 14:34:46 +02:00
client.loop_start()
2023-04-22 12:32:27 +02:00
print("Starting Loop")
while True:
2023-04-22 14:34:46 +02:00
# print("Looping")
# time.sleep(1)
pass
if __name__ == "__main__":
main()